我正在编写一个考虑到两个表面的折射着色器。因此,我使用FBO来渲染深度和法线到纹理,并使用立方体映射来表示环境。我需要使用存储在纹理中的法线的值来从立方体贴图中获取值,以便获得背面的折射法线。
只要我不试图从一个从纹理中检索其值的向量中访问它,这个立方体映射就能完美地工作。
这是一个失败的最小片段着色器。颜色一直是黑色的。我确信对texture 2D的调用会返回非零值:如果我尝试显示direction中包含的纹理颜色(表示法线),我会得到一个完美的彩色模型。不管我对“方向”向量做什么操作,它总是失败。
uniform samplerCube cubemap;
uniform sampler2D normalTexture;
uniform vec2 viewportSize;
void main()
{
vec3 direction = texture2D(normalTexture, gl_FragCoord.xy/viewportSize).xyz;
// direction = vec3(1., 0., 0) + direction; // fails as well!!
vec4 color = textureCube(cubemap, direction);
gl_FragColor = color;
}下面是显示为颜色的向量“方向”的值,只是证明它们不为空!

这是上面着色器的结果(只是茶壶)。

虽然这段代码可以完美地工作:
uniform samplerCube cubemap;
uniform vec2 viewportSize;
varying vec3 T1;
void main()
{
vec4 color = textureCube(cubemap, T1);
gl_FragColor = color;
}

我想不出任何原因,当我访问采样立方体的值时,我的颜色仍然是黑色的!
为了完整起见,即使我的立方图工作正常,以下是用于设置它的参数: glGenTextures(1,&mTextureId);
glEnable(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureId);
// Set parameters
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);除非我遗漏了什么重要的东西,否则我想这可能是驱动程序的bug。我没有显卡,我使用的是英特尔酷睿i5处理器芯片组。
00:02.0 VGA compatible controller: Intel Corporation 2nd Generation Core Processor Family Integrated Graphics Controller (rev 09)你知道为什么会发生这种情况吗,或者你有解决办法吗?
编辑:这是我的着色器类绑定纹理的方式
4 textures to bind
Bind texture 3 on texture unit unit 0
Bind to shader uniform: 327680
Bind texture 4 on texture unit unit 1
Bind to shader uniform: 262144
Bind texture 5 on texture unit unit 2
Bind to shader uniform: 393216
Bind texture 9 on texture unit unit 3
Bind to shader uniform: 196608纹理3和4是深度,5是法线贴图,9是立方体贴图。
以及执行绑定的代码:
void Shader::bindTextures() {
dinf << m_textures.size() << " textures to bind" << endl;
int texture_slot_index = 0;
for (auto it = m_textures.begin(); it != m_textures.end(); it++) {
dinf << "Bind texture " << it->first<< " on texture unit unit "
<< texture_slot_index << std::endl;
glActiveTexture(GL_TEXTURE0 + texture_slot_index);
glBindTexture(GL_TEXTURE_2D, it->first);
// Binds to the shader
dinf << "Bind to shader uniform: " << it->second << endl;
glUniform1i(it->second, texture_slot_index);
texture_slot_index++;
}
// Make sure that the texture unit which is left active is the number 0
glActiveTexture(GL_TEXTURE0);
}m_textures是纹理ids到统一ids的映射。
发布于 2013-04-03 23:50:05
我想通了,真的是一件很愚蠢的事情。我忘了更改我的着色器函数,将立方图绑定为GL_TEXTURE_CUBE_MAP,所有内容都绑定为GL_TEXTURE_2D!
不管怎样,谢谢你!
发布于 2013-04-03 14:09:32
您似乎没有为法线贴图和立方体贴图使用单独的纹理单位。一切都默认为纹理单位0。你需要这样的东西:
uniform sampler2D norm_tex;
uniform samplerCube cube_tex;在着色器中。当使用(3.2+)核心配置文件时,纹理查找应该只使用‘重载’texture函数。对于(3.3+),您也可以使用sampler objects。
生成纹理并将其绑定到单独的纹理单元:
... generate 'norm_tex' and 'cube_tex' ...
glActiveTexture(GL_TEXTURE0);
... bind 'norm_tex' and set parameters ...
glActiveTexture(GL_TEXTURE1);
... bind 'cube_tex' and set parameters ...
... glUseProgram(prog); ...
glUniform1i(glGetUniformLocation(prog, "norm_map"), 0);
glUniform1i(glGetUniformLocation(prog, "cube_map"), 1);https://stackoverflow.com/questions/15775948
复制相似问题