I can spot one very significant mistake in your code:
GLubyte *pixel_at_pos = clip_pixels + (in_rect_pos * GL_PACK_ALIGNMENT);
Using GL_PACK_ALIGNMENT
here is wrong in two different ways:
Fundamentally,
GL_PACK_ALIGNMENT
is an enum value in OpenGL. It happens to be defined to 0x0D05 (3333 in decimal), and it is not the value of theGL_PACK_ALIGNMENT
GL state, which you can set viaglPixelStore
and query back viaglGet
. In both cases,GL_PACK_ALIGNMENT
is just used to reference which GL state variable is to be accessed.You do not use the alignment correctly. Even if you had used the value queried by
glGetIntegerv(GL_PACK_ALIGNMENT, &alignment)
, your formula would still be wrong. TheGL_PIXEL_PACK
alignment reference the adresses of each row of pixel data. By default, it is set to 4, so if your row would not start at an address divisible by 4, you have to add 1 to 3 padding bytes to make it divisible by 4.