简体   繁体   English

OpenGL纹理偏移问题

[英]OpenGL texture offset issue

I'm running into an issue with a texture's rows being offset. 我遇到了纹理的行被偏移的问题。 As a test I'm creating a 512x4 texture from/to GL_RGBA. 作为测试,我正在GL_RGBA中创建512x4纹理。 sizeof(_color) outputs 4. It should just be a gradient that goes from black to blue on the X. It looks like it's being read from an offset, as sometimes it ends with corrupted results that last about 1/3rd of a row (width). sizeof(_color)输出4。它应该是X上从黑色到蓝色的渐变。它看起来像是从偏移量中读取的,有时它的结尾是损坏的结果,持续大约行的1/3(宽度)。

If I change the fragment shader to output UV coordinates on the red and green channels, I get UV coordinates that look like they're properly set, such as 0,0 (TL) 1,0 (TR), 0,1 (BL) and 1,1 (BR). 如果更改片段着色器以在红色和绿色通道上输出UV坐标,则会得到看起来正确设置的UV坐标,例如0,0(TL)1,0(TR),0,1(BL )和1,1(BR)。 I can't see this being a UV issue anyway. 无论如何,我看不到这是紫外线问题。

I'm using SDL2 on OSX, which may be the source of the issue. 我在OSX上使用SDL2,这可能是问题的根源。 The result is good on Linux/SDL2, so I'm utterly clueless as to what state variable could possibly cause this. 在Linux / SDL2上,结果是很好的,所以对于什么状态变量可能导致这种情况我一无所知。

OpenGL纹理偏移

typedef struct {
    unsigned char r,g,b,a;
} _color;

glGenTextures(1, &self->id);
glBindTexture(GL_TEXTURE_2D, self->id);

_color* c = (_color*)malloc(sizeof(_color) * 512 * 4);
_color* pc = c;
for( int y = 0; y < 4; y++ )
    for( int x = 0; x < 512; x++ ) {
        pc->r = 0;
        pc->g = 0;
        pc->b = (u_char)((float)x / (float)512 * 255.0);
        pc->a = 255;
        pc++;
    }

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void *)c);

printf("glGetError = %d\n", glGetError()); // Outputs no error

free(c);

UV mapped to gl_FragColor (red = U, green = V) UV映射到gl_FragColor(红色= U,绿色= V) 紫外线输出

Texture lookup changed to: texture2D(u_texture, vec2(v_uv.x, 0.0)); 纹理查找更改为:texture2D(u_texture,vec2(v_uv.x,0.0)); (y forced to 0) (y强制为0) V(或UV.y)坐标设置为0

Might be an alignment issue. 可能是对齐问题。 Set the proper GL_UNPACK_… parameters with glPixelStore before uploading the texture data. 在上载纹理数据之前,请使用glPixelStore设置适当的GL_UNPACK_…参数。

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

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