Original question
I want to use "gluPerspective", "glViewport" and "gluLookAt" to control my camera and screen.
What functions should I apply to which matrix mode? And in what order should I / should use them?
For example, I'm trying to set up my screen and camera as follows: (But it does not work!)
glMatrixMode(GL_PROJECTION) // Apply following to projection matrix - is this correct? glLoadIdentity(); // Reset first glPerspective(45.0, (double)w/(double)h, 1.0, 200.0); // Set perspective glViewport(0, 0, w, h); // Set viewport glMatrixMode(GL_MODELVIEW); // Apply following to modelview - should glViewport come under here? glLoadIdentity(); // Reset first gluLookAt(px, py, pz, cx, cy, cz, ux, uy, uz); // Set position, centre and then up vectors // This surely comes after calling GL_MODELVIEW?
I looked through the online documentation and I understand the functions, not where they should go and in what order!
Some time later...
Now a few months later, and I'm adding quick editing to show the system I'm using to render with OpenGL. This will help others who see this issue in the future.
I mainly use two methods.
Method 1:
This method groups everything together.
This recreates the viewport for rendering over the entire area inside the window. Using sfml, you will do something like window.width () or window.height () or something similar depending on what window tools you use (glut, glfw, sdl, etc.). ..
You will need to choose one of three from "glOrtho", "gluPerspective" and "glFrustrum". There is also "gluOrtho2D", but use this with caution! (I prefer to set near and far planes with "glOrtho".) You also need to replace "..." with your arguments with a function.
// The third step is to clear the screen and set your camera / geometry position glClear(GL_COLOR_BUFFER_BIT); // use bitwise OR ('||') with 'GL_DEPTH_BUFFER_BIT' and 'GL_STENCIL_BUFFER_BIT' if required gluLookAt( ... ); // I like to use gluLookAt, or at least I _italic_used to! Now I implement my own camera... // That is another fun thing you should try if you are comfortable with 3D geometry and hard math! // The fourth step is to draw your scene. You may want to put lighting stuff first or in with the drawing glutWireTeapot( ... );
Method 2:
The second method is the same as above, but to move the first step into a separate function. Then you should define window resizing events and call this function. With oversaturation, you can specify a callback. Using SFML, you can detect an event when a window is resized. I forget how the SDL works, but it does. I have not yet learned how glfw works.
Hope this helps you.
Some OpenGL newbies (perhaps I myself turned them on at one time) are trying to point out translations of cameras to the PROJECTION matrix. Do not do this . I head it, and it scares the lighting and maybe other things.