【发布时间】:2014-03-18 20:35:18
【问题描述】:
我不知道我做错了什么。这是我的 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。
【问题讨论】:
标签: opengl textures opengl-3 vertex-shader