# Is this process for generating an OpenGL perspective projection correct?

I have been programming OpenGL for a while now and I have successfully created a perspective camera, however, I can't help but shake the feeling that I am doing it wrong. The code I am using is below, is there something wrong with how I am constructing the perspective projection?

int MainWindow_SDL::execute(QCoreApplication &app)
{
SDL_Init(SDL_INIT_EVERYTHING);
glewInit();

// ...

glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_LIGHTING);
gluPerspective(50, 1.33, 1, 1000);
gluLookAt(3, m_xAxisLocation, 4, 0, 0, 0, 0, 1, 0);
glewExperimental = GL_TRUE;

// ...

GameStart();
app.exit();

return 0;
}


I also set it up in the game loop:

void MainWindow_SDL::GameLoop()
{
OpenGLRenderer t_renderer;
t_renderer.PreGameRender();

while (m_state == GameState::RUNNING)
{
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
processInput();
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
gluPerspective(50, 1.33, 1, 1000);
gluLookAt(m_xAxisLocation, m_yAxisLocation, m_zAxisLocation,
0, 0, 0, 0, 1, 0);

t_renderer.DuringGameRender();

SDL_GL_SwapWindow(m_window);
glPopMatrix();
}

t_renderer.PostGameRender();

GameEnd();
}

• We don't generally do code review (you may want to look here for that). However, your basic question about whether or not you set up the GL projection matrix is reasonable. I've trimmed the extraneous code to focus on that issue. – Josh Jul 19 '16 at 0:02
• Also, if you're not already aware, the functions you're using (the GL matrix stack and such) imply you're using a very old version of OpenGL. They are almost all deprecated in modern OpenGL (if that matters to you). – Josh Jul 19 '16 at 0:04

glMatrixMode(GL_MODELVIEW);
glDisable(GL_LIGHTING);
gluPerspective(50, 1.33, 1, 1000);
gluLookAt(3, m_xAxisLocation, 4, 0, 0, 0, 0, 1, 0);


This kind of thing is generally wrong. What've you've done here is set the active matrix stack to the model-view stack; this is responsible for generating the matrix transform that will bring your rendered geometry into view space (for fixed-function lighting).

However, while this stack is active you're calling gluPerspective, which builds a perspective projection matrix and multiplies it onto the stack. Then you build a look-at (view) transform using glLookAt.

Generally you want to put the projection matrix into the dedicated projection matrix stack by doing things this way:

glMatrixMode(GL_PROJECTION);