osgFBO(十一):深度图

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

类似于颜色缓冲区采样纹理,深度缓冲区也可以通过采样摄像机进行
一,设置深度图纹理

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>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值