简体   繁体   中英

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. 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).

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). I can't see this being a UV issue anyway.

I'm using SDL2 on OSX, which may be the source of the issue. The result is good on Linux/SDL2, so I'm utterly clueless as to what state variable could possibly cause this.

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) 紫外线输出

Texture lookup changed to: texture2D(u_texture, vec2(v_uv.x, 0.0)); (y forced to 0) V(或UV.y)坐标设置为0

Might be an alignment issue. Set the proper GL_UNPACK_… parameters with glPixelStore before uploading the texture data.

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