查看: 904|回复: 4

奖励100元--求助关于shadowmap的问题

[复制链接]

该用户从未签到

发表于 2016-9-13 14:34:02 | 显示全部楼层 |阅读模式
最近学习一篇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);
}

ShadowMap.rar

113.54 KB, 下载次数: 30, 下载积分: 威望 1

该用户从未签到

 楼主| 发表于 2016-9-13 14:35:55 | 显示全部楼层
如果有哪位勇士能够找出其中的问题可以给我留言,也可以直接加我的微信,微信就是我的手机号18910285383,钱随不多,只是个人一点心意,请大家帮帮忙

该用户从未签到

发表于 2016-9-13 15:34:34 | 显示全部楼层
我把我以前写的shadowmap提供给你,你要吗

该用户从未签到

 楼主| 发表于 2016-9-13 17:39:58 | 显示全部楼层
风云轩 发表于 2016-9-13 15:34
我把我以前写的shadowmap提供给你,你要吗

要要要,dqpilb_2009@163.com

该用户从未签到

 楼主| 发表于 2016-9-29 16:26:36 | 显示全部楼层
结贴,上面的代码都没有问题,光源相机缺少了一个设置camera->setComputeNearFarMode(osg::Camera:O_NOT_COMPUTE_NEAR_FAR);

谢谢阿威的热情帮助
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表