一个纹理数组的多个采样器

时间:2019-11-23 19:38:34

标签: opengl textures fragment-shader

  • 如何为一个纹理数组创建多个采样器?

到目前为止,我依靠OpenGL来确定声明的uniform sampler2Darray txa采样器是指与glBindTexture绑定的纹理数组。

glTexImage3D(GL_TEXTURE_2D_ARRAY, 0,  GL_RGBA8, width, height,
             layerCount,  0 GL_RGBA, GL_UNSIGNED_BYTE, texture_array);
...
glGenTextures(1,&texture_ID);
glBindTexture(GL_TEXTURE_2D_ARRAY, texture_ID); 
... 
                               //fragment shader
uniform sampler2Darray txa
...
vec2 tc;
tc.x = (1.0f - tex_coord.x) * tex_quad[0] + tex_coord.x * tex_quad[1];
tc.y = (1.0f - tex_coord.y) * tex_quad[2] + tex_coord.y * tex_quad[3];
vec4 sampled_color = texture(txa, vec3(tc, tex_id));

我尝试在片段着色器中指定两个采样器,但是片段着色器出现编译错误:

uniform sampler2DArray txa;
uniform sampler2DArray txa2;
...
vec4 texture = texture(txa, vec3(tc, tex_id));
vec4 texture2 = texture(txa2, vec3(tc2, tex_id));

我没想到这会起作用,但是,我不确定片段着色器编译器是否检查采样器是否分配了纹理,所以也许有些错误。

我尝试生成并绑定采样器对象,但仍然出现片段着色器错误:

GLuint sampler_IDs[2];
glGenSamplers(2,sampler_IDs);
glBindSampler(texture_ID, sampler_IDs[0]);
glBindSampler(texture_ID, sampler_IDs[1]);

我想坚持使用较低版本的OpenGL,这可能吗?感谢您的任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

错误是由线路引起的

  
vec4 texture = texture(txa, vec3(tc, tex_id));

然后,变量texture的名称等于内置函数texture的名称。该变量是在局部范围内声明的,因此在此范围texture中是一个变量,调用函数texture会导致错误。

重命名变量以解决问题。例如:

vec4 texture1 = texture(txa, vec3(tc, tex_id));
相关问题