gpt4 book ai didi

opengl - 在片段着色器中使用textureCube访问环境贴图失败

转载 作者:行者123 更新时间:2023-12-02 09:45:17 25 4
gpt4 key购买 nike

我正在编写一个考虑两个表面的折射着色器。因此,我使用 FBO 来渲染纹理的深度和法线,并使用立方体贴图来表示环境。我需要使用存储在纹理中的法线值从立方体贴图中获取值,以获得背面的折射法线。

只要我不尝试从已从纹理检索值的向量访问立方体贴图,它就可以完美地工作。

这是一个失败的最小片段着色器。颜色仍然是极度的黑色。我确信对纹理 2D 的调用会返回非零值:如果我尝试显示方向中包含的纹理颜色(表示法线),我会得到一个完美的彩色模型。无论我对“方向”向量进行何种操作,它都会失败。

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;
}

这里是以颜色显示的向量“方向”的值,只是证明它们不为空! Values of "direction"这是上述着色器的结果(只是茶壶)。 enter image description here

虽然这段代码可以完美运行:

uniform samplerCube cubemap;
uniform vec2 viewportSize;
varying vec3 T1;

void main()
{
vec4 color = textureCube(cubemap, T1);
gl_FragColor = color;
}

enter image description here

我想不出任何原因为什么每当我访问采样器立方体值时我的颜色都会保持黑色!

为了完整起见,尽管我的立方体贴图可以工作,但以下是用于设置它的参数: 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);

除非我在某个地方错过了一些重要的东西,否则我认为这可能是一个驱动程序错误。我没有任何显卡,我使用的是 Intel Core i5 处理器芯片组。

00:02.0 VGA compatible controller: Intel Corporation 2nd Generation Core Processor Family Integrated Graphics Controller (rev 09)

知道为什么会发生这种情况,或者您有解决方法吗?

编辑:这是我的着色器类绑定(bind)纹理的方式

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 是立方体贴图。

以及执行绑定(bind)的代码:

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 是纹理 id 到统一 id 的映射。

最佳答案

您似乎没有为法线贴图和立方体贴图使用单独的纹理单元。一切都默认为纹理单元 0。您需要类似:

uniform sampler2D norm_tex;

uniform samplerCube cube_tex;

在着色器中。使用 (3.2+) 核心配置文件时,纹理查找应仅使用“重载”texture 函数。使用(3.3+)您还可以使用 sampler objects .

生成纹理并将其绑定(bind)到单独的纹理单元:

... 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);

关于opengl - 在片段着色器中使用textureCube访问环境贴图失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15775948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com