首页 文章

为什么OpenSceneGraph将所有Sampler2D映射到第一个纹理

提问于
浏览
0

我目前正在用OpenSceneGraph(3.4.0)和我自己的glsl(330)着色器编写程序 . 它使用多个纹理进行输入,然后使用预渲染相机进行多个渲染目标渲染,并使用第二个相机读取这些多个渲染目标纹理以进行延迟着色 . 因此两个相机都有自己的着色器(这里称为geometry_pass和lighting_pass) . 我的问题:两个着色器在阅读时都在所有 sampler2D 制服中使用相同的纹理 .

//in geometry_pass.frag
uniform sampler2D uAlbedoMap;
uniform sampler2D uHeightMap;
uniform sampler2D uNormalMap;
uniform sampler2D uRoughnessMap;
uniform sampler2D uSpecularMap;
[...]
layout (location = 0) out vec4 albedo;
layout (location = 1) out vec4 height;
layout (location = 2) out vec4 normal;
layout (location = 3) out vec4 position;
layout (location = 4) out vec4 roughness;
layout (location = 5) out vec4 specular;
[...]
albedo = vec4(texture(uAlbedoMap, vTexCoords).rgb, 1.0);
height = vec4(texture(uHeightMap, vTexCoords).rgb, 1.0);
normal = vec4(texture(uNormalMap, vTexCoords).rgb, 1.0);
position = vec4(vPosition_WorldSpace, 1.0);
roughness = vec4(texture(uRoughnessMap, vTexCoords).rgb, 1.0);
specular = vec4(texture(uSpecularMap, vTexCoords).rgb, 1.0);

这里的输出始终是 uAlbedoMap 的颜色,除了正确导出的位置 .

在光照过程中,当我读入几何体的纹理时,所有输入纹理都是相同的

//in lighting_pass.frag
uniform sampler2D uAlbedoMap;
uniform sampler2D uHeightMap;
uniform sampler2D uNormalMap;
uniform sampler2D uPositionMap;
uniform sampler2D uRoughnessMap;
uniform sampler2D uSpecularMap;
[...]
vec3 albedo = texture(uAlbedoMap, vTexCoord).rgb;
vec3 height = texture(uHeightMap, vTexCoord).rgb;
vec3 normal_TangentSpace = texture(uNormalMap, vTexCoord).rgb;
vec3 position_WorldSpace = texture(uPositionMap, vTexCoord).rgb;
vec3 roughness = texture(uRoughnessMap, vTexCoord).rgb;
vec3 specular = texture(uSpecularMap, vTexCoord).rgb;

即,正确导出的位置图也具有照明过程中反照率的颜色 .

因此,似乎正常工作的是纹理输出,但显然不起作用的是输入 . 我试图用CodeXL调试它,在那里我可以看到geometry_pass的所有图像(至少在某些点上)已经被正确绑定,它们都是可见的 . framebuffer对象的输出纹理确认geometry_pass的位置纹理是正确的 . 据我所知,当逐步执行此操作时,纹理被正确绑定(即均匀位置是正确的) .

现在显而易见的问题是:如何在着色器中正确使用这些纹理?


Construction of the program

查看器是 osgViewer::Viewer ,因此只有一个视图 . 场景图如下: displayCamera 是来自观察者的摄像机 . 由于我正在使用Qt(5.9.1),我在使用场景图做任何其他事情之前重置了GraphicsContext .

osg::ref_ptr<osg::Camera> camera = viewer.getCamera();

osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->windowDecoration = false;
traits->x = 0;
traits->y = 0;
traits->width = 640;
traits->height = 480;
traits->doubleBuffer = true;

camera->setGraphicsContext(new osgQt::GraphicsWindowQt(traits.get()));
camera->getGraphicsContext()->getState()->setUseModelViewAndProjectionUniforms(true);
camera->getGraphicsContext()->getState()->setUseVertexAttributeAliasing(true);
camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera->setClearColor(osg::Vec4(0.2f, 0.2f, 0.6f, 1.0f));
camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
camera->setViewMatrix(osg::Matrix::identity());

然后我将 displayCamera 设置为此查看器相机,创建第二个用于渲染到纹理的相机(因此称为 rttCamera )并将其作为子项添加到 displayCamera . 我将场景(由包含一个包含硬编码几何体的geode的agroup节点组成)添加到rttCamera,最后创建一个屏幕四边形几何体(在geode下面,而geode又是矩阵变换的子节点;这个矩阵变换是什么作为一个孩子添加到 displayCamera ) .

因此 displayCamera 有两个孩子 rttCamera 和matrixtransform-> screenQuad . rttCamera 有子场景 - > geode . 两个相机都有自己的渲染掩码,屏幕四边形使用 displayCamera 的渲染掩码,场景是 rttCamera 的渲染掩码 .

使用场景节点,我从文件(所有位图)中读取5个纹理,然后将 rttCamera 渲染到具有多个渲染目标的Framebuffer对象中(用于延迟着色) .

//model is the geode in the scene group node
osg::ref_ptr<osg::StateSet> ss = model->getOrCreateStateSet();
ss->addUniform(new osg::Uniform(name.toStdString().c_str(), counter));
ss->setTextureAttributeAndModes(counter, pairNameTexture.second, osg::StateAttribute::ON | osg::StateAttribute::PROTECTED);

.

//camera is the rttCamera
//bufferComponent is constructed by osg::Camera::COLOR_BUFFER0+counter
//(where counter is just an integer that gets incremented)
//texture is an osg::Texture2D that is newly created
camera->attach(bufferComponent, texture);
//the textures get stored to assign them later on
gBufferTextures[name] = texture;

这些mrt纹理作为纹理绑定到screenquad

//ssQuad is the stateset of the screen quad geode
QString uniformName = "u" + name + "Map";
uniformName[1] = uniformName[1].toUpper();

ssQuad->addUniform(new osg::Uniform(uniformName.toStdString().c_str(), counter));
osg::ref_ptr<osg::Texture2D> tex = gBufferTextures[name];
ssQuad->setTextureAttributeAndModes(counter, gBufferTextures[name], osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);

其他设置是rendertarget(rttCamera的FBO,displayCamera的Framebuffer),照明(两个摄像头都关闭) . rttCamera获得与为displaycamera创建的图形上下文相同的图形上下文(即图形上下文对象被传递给rttCamera并设置为其自己的图形上下文) .

纹理附件按如下方式创建(使用宽度和高度或大小的2的幂值没有区别)

osg::ref_ptr<osg::Texture2D> Utils::createTextureAttachment(int width, int height)
{
    osg::Texture2D* texture = new osg::Texture2D();
    //texture->setTextureSize(width, height);
    texture->setTextureSize(512, 512);
    texture->setInternalFormat(GL_RGBA);
    texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR);
    texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR);

    return texture;
}

如果有更多关键的解决方案或信息丢失,请告诉我 .

1 回答

  • 0

    所以我终于找到了错误 . 我的柜台一直是 unsigned int ,显然是不允许的 . 由于osg隐瞒了我的大部分错误,我没有看到这是一个问题......

    将它改为普通的 int 之后,我现在将不同的纹理添加到我的着色器中 .

相关问题