简体   繁体   中英

OpenGL (pyglet) issue with glTexImage2D (striped texture)

Trying to use glTexImage2D() to pass a programmatically generated displacement map to a vertex shader, but for some reason the vertices don't seem to be getting uniform values from the uploaded image, even though every value in the array being uploaded is the same.

The python code is as follows:

pix = []
for x in range(256):
    for y in range(128):
        pix.append(200)  # pix is just [200, 200, 200, ...]

pix = (GLuint * len(pix))(*pix)

disp_tex_id = GLuint(0)
glGenTextures(1, byref(disp_tex_id))

glActiveTexture(GL_TEXTURE1)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, disp_tex_id)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)

glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 256, 128, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pix)

shader.uniformi('disp_texture', 1)

And the relevant shader code is here:

[vertex]
#version 110
#extension GL_EXT_gpu_shader4 : require

uniform sampler2D disp_texture;

varying vec4 dispColor;

void main() {
  gl_TexCoord[0] = gl_MultiTexCoord0;

  dispColor = texture2D(disp_texture, gl_TexCoord[0].st);
}

[fragment]
#version 110
varying vec4 dispColor;

void main() {
  gl_FragColor = dispColor;
}

I'm currently just displaying the displacement on the sphere as a color value, but instead of a uniformly gray sphere, it's striped.

That striping you are experiencing is due to incorrectly sizing the data in your pix array.

GL_LUMINANCE is a single component image format and the size of your component here is 1 -byte ( GL_UNSIGNED_BYTE in glTexImage2D (...) ).

You are effectively only giving color to one out of every 4 of your texels because you are creating an array of GLuint s (where the highest 24-bits are all 0 ) and then telling GL that you are passing it a GLubyte array.

To resolve this, you need to change:

pix = (GLuint * len(pix))(*pix)

To this instead:

pix = (GLubyte * len(pix))(*pix)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM