【发布时间】:2019-11-23 19:38:34
【问题描述】:
- 如何为一个纹理数组创建多个采样器?
到目前为止,我依靠 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,可以吗?感谢您的帮助,谢谢!
【问题讨论】:
-
“我收到片段着色器的编译错误:” - 错误信息是什么?
-
错误 C1105:不能调用非函数
-
@Rabbid76 感谢您再次修复我的错误! :D 所以我不需要在着色器程序之外创建采样器,除非它们具有不同的采样设置? IE。他们都继承自
glTexParameteri? -
glTexParameteri不设置全局状态。它为当前绑定到指定目标的单个纹理对象设置参数(在您的情况下,目标是GL_TEXTURE_2D_ARRAY)。您必须为每个纹理对象设置参数。
标签: opengl textures fragment-shader