Sign in to follow this  
dgp

How to move from glOrtho to glPerspective ?

Recommended Posts

Hi, I was rendering a map whose bounding coordinates(UTM) are set as glOrtho(3427065.250000,3443999.250000,5788323.50000,5800691.500000); I could see the map and everything seems to work fine. But when I tried to use gluPerspective(45.0, 1.0, 1.0, 20.0) for viewing my map in perspective nothing appears on my screen.I tried to convert the UTM coordinates to lat/long and draw but to no avail.I have used default values for look-at function. gluLookAt(0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0); I know that something is wrong with LookAt function.Is there a specific procedure when one decides to go from displaying 2D using glOrtho() to displaying things in 3d Perspective by means of gluPerspective()? Thanks.

Share this post


Link to post
Share on other sites
In ortho mode your camera behaves like a box around the origin.
In perspective the camera behaves like a pyramid with the tip being the eye (first 3 floats).

If you draw your map in 2d along the XY plane, you need to translate the camera back away from the plane to see it
gluLookAt(0.0,0.0,-50000.0,0.0,0.0,0.0,0.0,1.0,0.0);

But, your ortho mode coordinates are not centered on 0.0 so you also need to translate your camera over ~400000 to get it near the area you were looking at in ortho mode.

gluLookAt(4000000.0,4000000.0,-50000.0,0.0,0.0,0.0,0.0,1.0,0.0);

Lastly, your near and far clip planes now mean even more than they did in ortho mode (since you likely drew at x,y,0 in ortho). If you want to translate back 50000 to get a good view of your map your gluPerspective call need to have the near and far planes adjusted to something more reasonable for your giant world.
Like 500 and 100000.

I'd highly suggest shrinking your world somehow, or you will quickly end up with z-fighting and float precision problems if you try to add more 3d stuff to your map.


The usual "specific procedure" is usually that people use ortho mode for HUD elements that they want to always match the users screen (and be ontop of the 3d world), while perspective mode is used for all the 3d geometry.

Share this post


Link to post
Share on other sites
In your lookAt function you set eye = (0,0,0) and center/camera position = (0,0,0).
I'm not sure what the result is, since your view direction vector is also (0,0,0).

According to the this the scaling/rotation part of the resulting view matrix would be 0 everywhere, which would cause everything to be scaled to size 0.

Try setting either eye or center to another vector like (0,0,1).

Share this post


Link to post
Share on other sites
Hi KulSeran,

I followed your suggestion of shrinking my world and this time tried to draw only a simple line.I even set the translation-x,y coordinates of eye position to near my world coordinates.Still I am facing the same problem.See the source code below.In the following code glOrtho() works fine but gluPerspective() fails even in this case.



///////////////////
float eyeX=0.0;
float eyeY=0.0;
float eyeZ=2.0;


float lookX=0.0;
float lookY=0.0;
float lookZ=-5.0;

void initializeGL()
{
glClearColor (0.0, 0.0, 0.0, 0.0);

}

void resizeGL(int w, int h)
{
if(h<=0) h=1 ;
glViewport (0, 0, (GLsizei) w, (GLsizei) h);


glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
//Assign Bounding Box Coordinates of Shapefile to glOrtho()
//glOrtho(3.41,3.45,5.7,5.84,-10,10);//This works fine
gluPerspective(45.0,(GLfloat)w/(GLfloat)h, 1.0, 200.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();

eyeX=3.41;
eyeY=3.45;
gluLookAt(eyeX,eyeY,eyeZ,lookX,lookY,lookZ,0.0,1.0,0.0);//Problem here

}


void paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINES);
glVertex3f(3.427,5.788,0.0);
glVertex3f(3.443,5.800,0.0);
glEnd();
glFlush();

}
///////////////

Share this post


Link to post
Share on other sites
Seems as if your line is outside the view frustum.

I didn't do any calculations, but your camera is placed at (3.41, 3.45, 2) looking down at (0,0,-5). Your line's end point lies at (3.443, 5.800, 0) which is above the camera, but since the camera is looking down, that point is out of view. The same applies to the start point and hence the entire line.

The following picture should illustrate this (not correct, since ascii art :) ):

c = camera location
x = end point

perspective (pyramid shaped frustum -> x outside):
__
x__-- |
c--__ |
--__|

ortho (box shaped frustum -> x inside):

_______
|x |
c| |
|_______|



Try moving/rotating your camera or moving your line.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this