Search code examples
c++openglperspectiveglu

How to use gluPerspective only once?


glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

//set viewpoint
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(VIEW_ANGLE,Screen_Ratio,NEAR_CLIP,FAR_CLIP);
gluLookAt(0,5,5, 0,0,0, 0,1,0);

//transform model 1
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(Theta, 0,1,0);

//draw model 1
glBegin(GL_QUADS);
...
glEnd();

The code above works fine, but is there any way to remove the call to gluPerspective?

What I mean is, I would like to call it only once in initialization, instead of repeatedly during each rendering.


Solution

  • You call gluPerspective there, because it belongs there. OpenGL is not a scene graph where you initialize things. It's a state driven drawing API. The projection matrix is a state and every serious graphics application changes this state multiple times throughout a single frame rendering.

    OpenGL does not know geometrical objects, positions and cameras. It just pushes points, lines and triangles through a processing pipeline, and draws the result to the screen. After something has been drawn, OpenGL has no recollection of it, whatsoever.

    I mean calling it only once in initialization.

    OpenGL is not initialized (except creation of the rendering context, but actually this is part of the operating system's graphics stack, not OpenGL). Sure, you upload textures and buffer object data to it, but that can happen anytime.