I’m totally new to OpenGL, and am confused about how to do perspective. Right now I have a module that draws texture mapped squares using Ortho projection:
glOrtho(0.0, xBound, 0.0, yBound, -1.0, 1.0);
But now I need to combine my stuff with another person’s, who is using the perspective projection for her drawings:
When I used her setting, I just get a black screen. And also even if I can experiment around with x and ys, I also need to detect mouse clicks, which don’t work anymore.
Could somebody help me?
Thanks a lot!
I have a slight feeling you might be looking in the wrong direction ( try looking towards -ve Z).
Also, what are the Z co-ordinates of the objects you are drawing? If they are less than 1.0, they will be clipped by the front plane. Try placing your initial camera position (look-at point) right in front of one of your objects (same x and y) and slightly back from it (-z).
Come and tell us the results!
Thanks a lot for your advise!
So now I have everything working with the following setting:
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
//glFrustum(-xBound, xBound, -yBound, yBound, 2.0, 60.0);
double fovy = (atan(yBound / 2.0)) * 360.0 / 3.1415926;
gluPerspective(fovy, w/h, 2.0, 60.0);
gluLookAt(0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
The commented out glFrustrum works the same as the gluPerspective. Seems like what I specify for near and far doesn’t matter as long as the eyez for gluLookAt is set to be the same as near.
I’m slowing understanding how these work. One thing I’m confused is the z coordinates. So many sites online say that for glFrustrum, (left, bottom, -zNear) and (right, top, -zNear) are mapped to the lower-left and upper-right corners of the window. So in my case, the z-coordinate right on the window is -2.0? And then gluLookAt sets the zEye at +2.0. Is this +2.0 relative to the window or to z=0.0?
Also, does the same thing work for gluPerspective? I mean, is the surface of the window at z = -near?
Thanks a lot!
Oh, and I’m drawing my squares on z = 0.0