Texture mapping

All I want to do is drawing a GL_QUAD with a texture on it, but there’s something I can’t understand…

this is my init code:


	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, w, 0.0, h, -1.0, 1.0);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glViewport(0, 0, w, h);

	glEnable(GL_TEXTURE_2D);

This code should set a coordinate system like this:


0,h--------------w,h
|                 |
|                 |
|                 |
|                 |
|                 |
0,0------------w,0

now I want to draw a square in the bottom-left corner with dimensions w/2 x h/2 and blit a texture on it, so, considering this the texture coordinate system:


0,1--------------1,1
|                 |
|                 |
|                 |
|                 |
|                 |
0,0------------1,0

the texture and screen coordinate systems should match.
but if I use this code:


	glBegin(GL_QUADS);

	glTexCoord2d(0.0,0.0);
	glVertex2f(0.0, 0.0);

	glTexCoord2d(1.0,0.0);
	glVertex2f(W/2, 0.0);

	glTexCoord2d(1.0,1.0);
	glVertex2f(W/2, H/2);

	glTexCoord2d(0.0,1.0);
	glVertex2f(0.0, H/2);

	glEnd();

The square is drawn in the bottom-left corner, but the texture on it is flipped vertically.

If I want to blit the texture in the right way I have to use this code:


	glBegin(GL_QUADS);

	glTexCoord2f(0.0, 1.0);
	glVertex2f(0.0, 0.0);

	glTexCoord2f(1.0, 1.0);
	glVertex2f(W/2, 0);

	glTexCoord2f(1.0, 0.0);
	glVertex2f(W/2, H/2);

	glTexCoord2f(0.0, 0.0);
	glVertex2f(0.0, H/2);

	glEnd();

or change the glOrtho parameters to: glOrtho(0.0, w, h, 0.0, -1.0, 1.0); // bottom and top swapped
so changing the coordinate system of the screen (and of course the square is blitted on the top-left corner).

Why does this happen?

It looks to be correct.

A common problem often reside in the texture loader : some image files are stored bottom to top.

maybe I’m misunderstanding something about textures…

This is my image:

This is the full code:
http://rafb.net/p/1MkdVI40.html

According to the texture coordinate system, if I try to access to the texel (0,0) I should get a green element (I suppose), right?

I’ve also copied the texel data from the texture to an array using glGetTexImage so to check how they are stored and the output of the first bytes is somtehing like:
1 0 0 1 , 1 0 0 1 , 1 0 0 1 , …
This means, I suppose, that the first bytes store the red texels (as expected).

These are the combinations I get with different combinations of settings for glOrtho and drawing code (read the first post or click on the thumbnails for code details).

glOrtho(0.0, W, 0.0, H, -1.0, 1.0) and drawing code 1

glOrtho(0.0, W, 0.0, H, -1.0, 1.0) and drawing code 2

glOrtho(0.0, W, H, 0.0, -1.0, 1.0) and drawing code 1

glOrtho(0.0, W, H, 0.0, -1.0, 1.0) and drawing code 2

Is this normal? Is this wrong? Am I doing something wrong?

Just RTFM, it is in the doc :
http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/teximage2d.html

“The first element corresponds to the lower left corner of
the texture image. Subsequent elements progress left-to-
right through the remaining texels in the lowest row of the
texture image, and then in successively higher rows of the
texture image. The final element corresponds to the upper
right corner of the texture image.”

So this means that the texture is stored bottom to top (bottom-left texel of the texture corresponds to top-left pixel of the image), doesn’t it?
This explains everything…

Well, this is really weird for me according to my 2D-graphic background (SDL), but it’s ok, I just needed to know.

Cheers ZbuffeR.