|
目的:做一个通过glsl实现的光照阴影效果
我的实现步骤:
第一步:建立rttcamera设置- osg::ref_ptr<osg::Node> node = osgDB::readNodeFile( "some model file" );
- osg::ref_ptr<osg::Texture2D> tex2D = new osg::Texture2D;
- tex2D->setTextureSize( 1024, 1024 );
- tex2D->setInternalFormat( GL_DEPTH_COMPONENT24 );
- tex2D->setSourceFormat( GL_DEPTH_COMPONENT );
- tex2D->setShadowComparison(true);
- tex2D->setShadowTextureMode(osg::Texture2D::LUMINANCE);
- tex2D->setSourceType( GL_FLOAT );
- osg::ref_ptr<osg::Camera> camera = new osg::Camera;
- camera->setClearColor( osg::Vec4() );
- camera->setClearMask( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
- camera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT );
- camera->setRenderOrder( osg::Camera::PRE_RENDER );
- tex2D->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR );
- tex2D->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR );
- tex->setBorderColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
- osg::BoundingSphere& bs = scene->getBound();
- float znear = 1.0f * bs.radius();
- float zfar = 3.0f * bs.radius();
- float proj_top = 0.5f * zfar;
- float proj_right = 0.75f * zfar;
- znear *= 0.9f;
- zfar *= 1.1f;
- camera->setProjectionMatrixAsFrustum(-proj_right, proj_right, -proj_top, proj_top,znear, zfar);
- camera->setViewport( 0, 0, tex2D->getTextureWidth(), tex2D->getTextureHeight() );
- camera->attach( osg::Camera::DEPTH_BUFFER, tex2D );
- camera->addChild( root.get() );
- camera->setViewMatrix(osg::Matrix::lookAt(...));
复制代码 然后在camera中加入自己的glsl代码,能够正常输出(通过相机attach一张imange输出到本地磁盘检测);
第二步:将tex2D作为texture传入
继上面的代码- osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
- mt->addChild(node.get());
- mt->getOrCreateStateSet()->setTextureAttributeAndModes(7, tex2D.get(), osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE);
- mt->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
- //最加入自己的glsl代码,并设置相应的uniform
复制代码 第三步:将mt和camera加入一个group节点,传入viewer。
在上面第二步中,我要将每个顶点进行矩阵转换来对应到rtt渲染后的深度图中对应的值。下面是部分glsl 的代码- // vertSource
- varying vec4 PreVertex;
- varying vec4 PreNormal;
- varying vec4 DepthCoord; // 深度纹理坐标
- uniform mat4 MatToShadow; // 转换矩阵
- uniform mat4 osg_ViewMatrixInverse;
- void main(void)
- {
- // 求出世界坐标系下的值
- PreVertex = osg_ViewMatrixInverse *gl_ModelViewMatrix* gl_Vertex;
-
- // 求出阴影纹理坐标的值
- DepthCoord =PreVertex * MatToShadow; or DepthCoord = MatToShadow * PreVertex;
- ...
-
- }
复制代码 问题:
我自己的一些思考,根据projCoord = VM * PM; 我把MatToShadow设置为 camera->getViewMatrix() * camera->getProjectionMatrix(); 然后用DepthCoord =PreVertex * MatToShadow;计算,但是不对。
问题来了,请问需要传入什么样的矩阵才能获取我想要的结果????? 或者说在以上的步骤当中我缺少了哪些环节,哪些环节是错误的。请大家帮我指点一下。 |
|