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?