opengl: Change the origin to upper left corner

2020-03-28 18:00发布

问题:

I am having trouble setting the openGL origin to the upper left corner of the view. So, in my window resize handler, I do something as;

// ox and oy are some offsets and width and height are the 
// required viewport width and height
glViewport(ox, oy, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

This keeps the origin at bottom left and I can render my texture as:

glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0); 
glTexCoord2f(1, 0); glVertex2f(width, 0);                            
glTexCoord2f(1, 1); glVertex2f(width, height);                      
glTexCoord2f(0, 1); glVertex2f(0, height);                 
glEnd();

As far as I can tell from reading the pages here, to flip the origin I simply need to replace the glOrtho call with

glOrtho(0, width, height, 0, -1, 1);

However, doing this and using the render code above does not render my texture anymore and I just see a blank screen.

回答1:

By flipping around the y-axis you flipped the chirality of the world space. Which means that the winding of your faces comes out differently. CCW becomes CW and vice versa. Most likely you have face culling enabled, so to account for the chirality flip you have to swap CCW for CW face culling.