我无法弄清楚我做错了什么。这是我的pyopengl代码的相关部分。
首先,我初始化包括采样器和纹理在内的所有内容。 bind_context
是我创建的上下文管理器,调用gl.glUseProgram(self.program_index)
。
with self.bind_context():
# Create sampler.
self.sampler_object = glGenSamplers(1)
# Create texture.
self.texture = gl.glGenTextures(1)
# Bind sampler to texture unit, and set parameters.
gl.glActiveTexture(gl.GL_TEXTURE0 + self.texture_unit)
gl.glBindSampler(self.texture_unit, self.sampler_object)
gl.glSamplerParameteri(self.sampler_object,
gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE)
gl.glSamplerParameteri(self.sampler_object,
gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR)
gl.glSamplerParameteri(self.sampler_object,
gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR)
# Set uniform with texture unit.
self.obs_index_to_height(self, self.texture_unit)
偶尔我需要更新纹理,所以我这样做:
def update_texture(self, data):
# Bind texture to texture unit, set paramters and upload texture.
# ActiveTexture must precede TexParameter, BindTexture,
# and TexImage.
print("Setting", data, data.shape[0])
gl.glActiveTexture(gl.GL_TEXTURE0 + self.texture_unit)
gl.glBindTexture(gl.GL_TEXTURE_1D, self.texture)
gl.glTexImage1D(gl.GL_TEXTURE_1D,
0,
0x8236, # gl.GL_R32UI,
data.shape[0],
0,
gl.GL_RED_INTEGER,
gl.GL_UNSIGNED_INT,
data)
打印:
Setting [ 0 1 2 3 4 5 6 7 8 9 10 11 0 0 0 0] 16
我的顶点着色器是
#version 330
uniform mat4 view;
uniform mat4 projection;
uniform vec2 dimensions;
uniform uint obs_index_base;
uniform usampler1D obs_index_to_height;
uniform vec4 color;
in float time;
in uint obs_index;
void main()
{
float height = int(obs_index + obs_index_base);
height = texelFetch(
obs_index_to_height,
//int(obs_index + obs_index_base),
int(5),
0).r;
gl_Position = projection * view * vec4(time, -height, 0.0, 1.0);
}
从视觉输出中可以清楚地看出,所提取的纹素不是所需的5,而是0。
答案 0 :(得分:0)
知道了。我的glGenSamplers正在返回一个列表,我将结果解释为值。我猜这个采样器并没有受到约束。不知道为什么这不会引起错误,或者我怎么会发现错误。