0
$\begingroup$

I'm trying to manually create a color data to load as a texture with OpenGl for learning purposes. I created an unsigned char array consisting of 4 pixels with 3 channels:

unsigned char data[] = {
                        255, 0, 0,
                        0, 255, 0,
                        0, 0, 255,
                        255, 255, 0
                    };
int width = 2;
int height = 2;

Here is how I'm passing the data:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float color[] = {1, 1, 1, 1};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color);
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_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

However I'm not getting the correct texture colors with this. Although if I specify the same data but choose with width of 4 and height of 1, this works correctly. Am I doing something wrong?

When specifying resolution 4x1 I get what I expect, 4 rows of colors:

enter image description here

However when specifying the texture as 2x2 I expect 4 quadrants with respective color in each, but I get wrong colors in lower two quadrants:

enter image description here

$\endgroup$
  • 1
    $\begingroup$ try glPixelStorei(GL_PACK_ALIGNMENT, 1); on the bound GL_TEXTURE_2D. $\endgroup$ – PaulHK May 11 at 9:18
  • $\begingroup$ @PaulHK Thanks, this fixed it! $\endgroup$ – Lenny White May 11 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.