1
votes

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 texture offset

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 output

Texture lookup changed to: texture2D(u_texture, vec2(v_uv.x, 0.0)); (y forced to 0) V (or UV.y) coordinate set to 0

1
Might be an alignment issue. Set the proper GL_UNPACK_… parameters with glPixelStore before uploading the texture data. - datenwolf
That definitely sounds feasible. I'll set params based on the information I see on opengl.org/sdk/docs/man2/xhtml/glPixelStore.xml (See table) - Kyle
Thanks, GL_UNPACK_ROW_LENGTH was set to 640. That's most definitely the difference in the offset. I would like to accept that as an answer, if you would post it as one. - Kyle

1 Answers

1
votes

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