Simple texture drawing

Hello!

I’m having a problem with displaying a very simple texture. Apparently, I’m getting the glTexImage2D function wrong. I tried the following:

	

    glGenTextures(1, texture);

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); //scale linearly when image bigger than texture
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); //scale linearly when image smalled than texture


     unsigned char pixels[] = {
	    255, 0, 0,     0, 255, 0,
	    0, 0, 255,        255, 255, 255
	};

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);

I’m expecting something like this:
[ATTACH=CONFIG]1812[/ATTACH]

However, I’m getting this:
[ATTACH=CONFIG]1813[/ATTACH]

Why is that? I’m expecting that at least the color of the pixels match the RGB values of the array.

Thanks in advance!

Because GL_UNPACK_ALIGNMENT defaults to 4.

Thanks for the reply.

How can I get this example working?

I already tried setting

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

but it remains wrong.

[QUOTE=guimudin;1291936]I already tried setting

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

but it remains wrong.[/QUOTE]
That call needs to go before the glTexImage2D() call.

Alternatively, add a fourth byte to the data for each pixel and use GL_RGBA.

Thanks for the replies! :slight_smile:

I was calling glPixelStorei(GL_UNPACK_ALIGNMENT, 1); before the glTexImage2D(…), and for some reason I just kept getting weird results.
Also, if I add padding to achieve the default 4-byte alignment each row for non-aligned width values, I still get wrong results.

I’m able to fix it by enforcing 4-byte aligned rows, rather using GL_FLOAT with GL_RGB or the GL_RGBA with GL_UNSIGNED_BYTE.

I think I may have misunderstood some concept here, because I read some docs and posts regarding byte alignment, and to me, it was supposed to work with glPixelStorei(GL_UNPACK_ALIGNMENT, 1); or adding the necessary padding to the rows to align them with 4 bytes.