OpenGL coordinate system

Started by
12 comments, last by Zakwayda 14 years, 1 month ago
http://pyopengl.sourceforge.net/documentation/manual/gluUnProject.3G.html
Amateurs practice until they do it right.Professionals practice until they never do it wrong.
Advertisement
After a lot of struggle with UnProject, I switched to glOrtho. It seems to work great for what I need, except zooming in and out. This was trivial with gluPerspective. :(


My problem is when I zoom out, it's as if the vanishing point in gluPerspective were the top left corner of the window. Everything shrinks into that corner when I really just want to zoom out and have objects stay in their place on the screen. What do I have to do to accomplish this?

Here's my function to change the glOrtho arguments


void GameplayState::SetProjection(){        GLint viewport[4];                     glGetIntegerv(GL_VIEWPORT, viewport);        int zoomedWidth = viewport[2] / (2.0f*this->zoomFactor);        int zoomedHeight = viewport[3] / (2.0f*this->zoomFactor);        glOrtho(0, zoomedWidth, zoomedHeight, 0, 0, 1);        glMatrixMode(GL_MODELVIEW);        glLoadIdentity();}
You can either translate the objects to the new position or, ironically, make glOrtho work with the origin being at the center of the screen. Something like glOrtho(-zoomedWidth/2, zoomedWidth/2, zoomedHeight/2, -zoomedHeight/2, 0, 1); should do the trick.
Quote:Original post by kibokun
After a lot of struggle with UnProject, I switched to glOrtho. It seems to work great for what I need, except zooming in and out. This was trivial with gluPerspective. :(


My problem is when I zoom out, it's as if the vanishing point in gluPerspective were the top left corner of the window. Everything shrinks into that corner when I really just want to zoom out and have objects stay in their place on the screen. What do I have to do to accomplish this?

Here's my function to change the glOrtho arguments


void GameplayState::SetProjection(){        GLint viewport[4];                     glGetIntegerv(GL_VIEWPORT, viewport);        int zoomedWidth = viewport[2] / (2.0f*this->zoomFactor);        int zoomedHeight = viewport[3] / (2.0f*this->zoomFactor);        glOrtho(0, zoomedWidth, zoomedHeight, 0, 0, 1);        glMatrixMode(GL_MODELVIEW);        glLoadIdentity();}
glOrtho() taks the left, right, bottom, and top bounds of the visible region as arguments. In your case, the view remains achored at a corner because you always make the top-left corner (0, 0). If you don't want the view to be anchored at the top-left corner, then don't achor the view at the top-left corner :)

It might help to think of your orthographic projection in terms of a center (focal) point, an aspect ratio, and a zoom value. Here's an example of how you might derive the glOrtho() arguments from these values:
float yExtent = zoom;float xExtent = yExtent * aspectRatio;float left = center.x - xExtent;float right = center.x + xExtent;// You can swap these if you want +y to go from top to bottom:float bottom = center.y - yExtent;float top = center.y + yExtent;
You can then move your virtual camera around by modifying its 'center' value, and zoom in and out by modifying the 'zoom' value.

This topic is closed to new replies.

Advertisement