2
\$\begingroup\$

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();

    // ...

    glLoadIdentity();
    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);
        glLoadIdentity();
        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();
}
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – user1430 Jul 19 '16 at 0:02
  • \$\begingroup\$ 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). \$\endgroup\$ – user1430 Jul 19 '16 at 0:04
2
\$\begingroup\$
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);
glLoadIdentity();
gluPerspective(50, 1.33, 1, 1000);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(m_xAxisLocation, m_yAxisLocation, m_zAxisLocation, 0, 0, 0, 0, 1, 0);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.