类似于颜色缓冲区采样纹理,深度缓冲区也可以通过采样摄像机进行
一,设置深度图纹理
osg::ref_ptrosg::Texture2D makeDepthTexture(int width, int height)
{
osg::ref_ptrosg::Texture2D depthTex = new osg::Texture2D;
depthTex->setTextureSize(width, height);
depthTex->setSourceFormat(GL_DEPTH_COMPONENT);
depthTex->setSourceType(GL_FLOAT);
depthTex->setInternalFormat(GL_DEPTH_COMPONENT24);
depthTex->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST);
depthTex->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);
depthTex->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
depthTex->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
return depthTex;
}
//采样深度
osg::ref_ptrosg::Texture2D texDepth = makeDepthTexture(texWidth, texHeight);
二,将深度图纹理关联到深度缓冲区
sampleCamera0->attach(osg::Camera::DEPTH_BUFFER, texDepth); //关联深度贴图
三,将深度图纹理传递到pass1Camera
osg::ref_ptr<osg::StateSet> stateset = pass1Camera->getOrCreateStateSet();
{
stateset->setTextureAttributeAndModes(0, texDepth);
}
四,从shader中采样深度图
osg::ref_ptr<osg::Uniform> texDepthUniform = new osg::Uniform("texDepth", 0);
stateset->addUniform(texDepthUniform);
static const char* psShader =
{
“varying vec2 outTexCoord;”
“uniform sampler2D texDepth;”
“void main(void)\n”
“{\n”
“gl_FragColor = texture2D(texDepth,outTexCoord);”
“}\n”
};
运行结果

完整代码如下
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>

该代码示例展示了如何在OpenGL中使用OpenSceneGraph(osg)库创建深度纹理,将其关联到深度缓冲区,并在后续渲染阶段进行采样。通过设置纹理参数、创建相机并指定渲染顺序,以及在着色器中应用深度纹理,实现了对场景深度信息的捕获和处理。
最低0.47元/天 解锁文章
823

被折叠的 条评论
为什么被折叠?



