前面涉及到了深度纹理和颜色纹理,由于qedl中,也要两者混合。

所以,要考虑到两者混合,这里只是简单将其相加。
一,设置纹理
//采样颜色纹理
osg::ref_ptrosg::Texture2D texColor = createFloatRectangleTexture(texWidth, texHeight);
//采样深度
osg::ref_ptrosg::Texture2D texDepth = makeDepthTexture(texWidth, texHeight);
二,关联采样摄像机颜色缓冲区和深度缓冲区
sampleCamera0->attach(osg::Camera::COLOR_BUFFER0, texColor); //关联采样贴图
sampleCamera0->attach(osg::Camera::DEPTH_BUFFER, texDepth); //关联深度贴图
三,Pass1摄像机输入颜色纹理和深度纹理
osg::ref_ptr<osg::StateSet> stateset = pass1Camera->getOrCreateStateSet();
{
stateset->setTextureAttributeAndModes(0, texColor);
stateset->setTextureAttributeAndModes(1, texDepth);
}
四,shader处理这两个纹理
osg::ref_ptr<osg::Uniform> texColorUniform = new osg::Uniform("texColor", 0);
osg::ref_ptr<osg::Uniform> texDepthUniform = new osg::Uniform("texDepth", 1);
stateset->addUniform(texColorUniform);
stateset->addUniform(texDepthUniform);
static const char* psShader =
{
“varying vec2 outTexCoord;”
“uniform sampler2D texColor;”
“uniform sampler2D texDepth;”
“void main(void)\n”
“{\n”
“gl_FragColor = texture2D(texColor,outTexCoord) + texture2D(texDepth,outTexCoord);”
“}\n”
};
运行如下:

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

该代码示例展示了在OpenGL中如何创建并混合颜色纹理和深度纹理。首先,创建两个纹理对象,一个用于颜色,一个用于深度。然后,将它们分别关联到采样摄像机的颜色缓冲区和深度缓冲区。接着,在Pass1摄像机中设置这两个纹理,并在着色器中通过相加操作将它们混合。最终,这个混合处理被应用到一个几何体上,并在渲染过程中使用。
最低0.47元/天 解锁文章
1928

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



