|
最近学习一篇OpenGL编写的关于shadowmap的文章,文章链接如下http://www.opengl-tutorial.org/i ... -16-shadow-mapping/。
在讲该方法转换成OSG中的过程中未能看到阴影效果。
其中的代码实现为:
viewer = new osgViewer::Viewer;
root = new osg::Group;
/************************************************************************/
/* scene init */
/************************************************************************/
osg::ref_ptr<osg::Node> lz = osgDB::readNodeFile("lz.osg");
//lz->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
root->addChild(lz);
osg::Vec3 center = lz->getBound().center();
osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("cow.osg");
//cow->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
osg::ref_ptr<osg::MatrixTransform> cowMat = new osg::MatrixTransform;
cowMat->setMatrix(osg::Matrix::translate(0, 0, 100));
cowMat->addChild(cow);
root->addChild(cowMat);
viewer->setSceneData(root);
/************************************************************************/
/* shadow map init */
/************************************************************************/
_depthMap = new osg::Texture2D;
_depthMap->setTextureSize(1024, 1024);
_depthMap->setSourceFormat(GL_DEPTH_COMPONENT);
_depthMap->setSourceType(GL_FLOAT);
_depthMap->setInternalFormat(GL_DEPTH_COMPONENT);
_depthMap->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_BORDER);
_depthMap->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_BORDER);
_depthMap->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
_depthMap->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
_depthMap->setBorderColor(osg::Vec4(1.0, 0.0, 0.0, 0.0));
_depthCamera = createRTTCamera(osg::Camera:EPTH_BUFFER, _depthMap, true);
_depthCamera->addChild(cowMat);
_depthCamera->addChild(lz);
root->addChild(_depthCamera);
osg::Vec3 heightMapCameraEye = osg::Vec3(0.0, 0.0, 200.0);
osg::Vec3 heightMapCameraUp = osg::Vec3(0,1,0);
osg::Vec3 heightMapCameraCenter = osg::Vec3(0.0, 0.0, 0.0);
_depthCamera->setViewMatrixAsLookAt(heightMapCameraEye,
heightMapCameraCenter,
heightMapCameraUp);
root->addChild(createHUDForHeightMapDisplay(_depthMap));
/************************************************************************/
/* shadow */
/************************************************************************/
osg::StateSet* state = viewer->getCamera()->getOrCreateStateSet();
osg::ref_ptr<osg:rogram> airportProgram = new osg::Program;
std::string shaderSource = readFileToString("shadowmap_vs.glsl");
airportProgram->addShader(new osg::Shader(osg::Shader::VERTEX, shaderSource));
shaderSource = readFileToString("shadowmap_fs.glsl");
airportProgram->addShader(new osg::Shader(osg::Shader::FRAGMENT, shaderSource));
state->setAttributeAndModes(airportProgram.get(), osg::StateAttribute::ON);
osg::Matrix viewMatrix = _depthCamera->getViewMatrix();
osg::Matrix prjMatrix = _depthCamera->getProjectionMatrix();
osg::Matrix biasMatrix(0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
osg::Matrix shadowMatrix = biasMatrix*prjMatrix*viewMatrix;
state->addUniform( new osg::Uniform("shadowMatrix", shadowMatrix) );
state->setTextureAttributeAndModes( 4, _depthMap);
state->addUniform( new osg::Uniform("depTex", 4) );
state->addUniform( new osg::Uniform("sceneTex", 0) );
osg::Uniform* u = new osg::Uniform(osg::Uniform::FLOAT_MAT4,"ViewMatrixInverse");
u->setUpdateCallback( new UpdateInverseViewMatrixUniformCallback(viewer->getCamera()) );
state->addUniform( u );
着色器代码如下:
顶点着色器:
uniform mat4 shadowMatrix;
uniform mat4 ViewMatrixInverse;
uniform mat4 osg_ViewMatrixInverse;
uniform mat4 osg_ModelViewMatrix;
varying vec4 shadowCoord;
varying vec2 uv;
void main()
{
mat4 modelMatrix = ViewMatrixInverse*gl_ModelViewMatrix;
//mat4 modelMatrix = osg_ViewMatrixInverse*osg_ModelViewMatrix;
vec4 posInWorld = modelMatrix * gl_Vertex;
shadowCoord = shadowMatrix * posInWorld;
uv = gl_MultiTexCoord0.xy;
gl_Position = ftransform();
}
片元着色器代码如下:
uniform sampler2D depTex;
uniform sampler2D sceneTex;
varying vec4 shadowCoord;
varying vec2 uv;
void main()
{
vec4 shadowColor = texture2D(depTex, shadowCoord.xy);
float depth = shadowColor.z/shadowColor.w;
if(depth < shadowCoord.z )
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
else
gl_FragColor = texture2D(sceneTex, uv);
}
|
|