简体   繁体   English

glTexImage2D(条纹纹理)的OpenGL(pyglet)问题

[英]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. 尝试使用glTexImage2D()编程方式生成的位移贴图传递到顶点着色器,但是由于某些原因,即使从数组中上传的每个值都相同,顶点似乎也无法从上传的图像中获得统一的值。 。

The python code is as follows: python代码如下:

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. 您正在经历的剥离是由于错误地确定了pix数组中数据的大小。

GL_LUMINANCE is a single component image format and the size of your component here is 1 -byte ( GL_UNSIGNED_BYTE in glTexImage2D (...) ). GL_LUMINANCE是单个组件图像格式,此处组件的大小为1字节( glTexImage2D (...) GL_UNSIGNED_BYTE 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. 实际上,您实际上仅对每4个像素中的一个赋予颜色,因为您正在创建GLuint数组(其中最高的24位全为0 ),然后告诉GL您正在为其传递GLubyte数组。

To resolve this, you need to change: 要解决此问题,您需要更改:

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

To this instead: 为此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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