This is something that has frustrated me for a long time (dozens of hours) in front of the computer & google but i still haven't solved it. I'm pretty new to openGL and i started reading tutorials on the net for the last 2-3 months as well experimenting with some code.
My main problem is that when i moved from Orthographic projection to Perspective
i have been blasting all the time how to manage and draw a simple shape on the screen. I know very little of matrices but the thing is that in Orthographic projection i specify the x,y,z coordinates of the object and those are the window coordinates. In perspective i haven't seen a thing how the coordinates are applied. I have seen ranges from -1 to 1, or even -20 to 20 etc. I read that you can specify the coordinates on openGL but how i'm supposed to do that?
In Orthographic i do this:
Window size is 800x600
i want the box to be the whole window so,
glVertex2i( 0, 600 );
glVertex2i( 0, 0 );
glVertex2i( 800, 0 );
glVertex2i( 800, 600 );
But what about projection? When i put 1 it's on the end of the window and -1 on the start. How opengl decides the start/end of window and why it's -1 to 1?
First you set up your projection and modelview matrices for 3D projection:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, ratio, near_clip, far_clip);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyex, eyey, eyez, lookatx, lookaty, lookatz, upx, upy, upz);
gluLookAt takes 3 vectors, the position of the "eye" of the camera, a look at point and an "up-vector". If you call gluLookAt(0.0, 0.0, -10.0, 0,0,0, 0,1,0) This means the camera sits at 0,0,-10 and looks along the z axis to the origin 0,0,0 and "up" is along the positive y axis. If you then make a simple draw call to opengl for a single quad, it should show up in your rendering:
glColor3f(1.0f,1.0f,1.0f); //set white color (or any contrast to your background)
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
You either draw in a clockwise or counterclockwise order, but not in a mix of those. You can control culling with glEnable/Disable(GL_CULL_FACE) and glCullFace.
Window size is 800x600 i want the box to be the whole window so
When you are working with 2D projections (Orthographic) you can easily set the size of your coordinate system to match the size of your window/viewport. This is not applicable in 3D projection as the boundaries are now a 3D viewing frustrum, or a 3D volume from the near clip to the far clip planes. Coordinates are then projected onto the 2D surface by the model-view-projection matrix. You can easily enter coordinates like for a quad of width 800 and height 600, but to see it you need to take the camera far away to capture the whole quad in view.
Quote from comments:
gluPerspective( 45.0f, (double)w / (double)h, 1.0f, 100.0f ) is supposed to set a perspective with near= 1 and far= 100. But since openGL is right-handed, positive z is out of the screen so near should be 100 and far 1. How come i see in every tutorial near less than far? It doesn't make sense..
Answer (see link above 3D viewing frustrum):
A viewing frustum is 3D volume in a scene positioned relative to the viewport's camera. The shape of the volume affects how models are projected from camera space onto the screen