jshbrntt
jshbrntt

Reputation: 5405

gluLookAt Not Working?

Why isn't gluLookAt working?

It makes no difference to the scene as if it's not even being called.

Is it because I'm using glMatrixMode(GL_MODELVIEW); if I don't include it I get nothing on screen.

I'm using GLFW, GLEW, and GLM.

// game.cpp:
Car* car;
void Game::init()
{
    //  LOADING SHADERS:
    loadShaders();

    //  CREATING ENTITIES:
    car = new Car();
    //cube->position.x = -0;
    car->position.z = -2;
    entities.push_back(car);

    //  INITIAL GL PARAMETERS:
    glEnable(GL_CULL_FACE);                         //  BACK FACE CULLING
    glEnable(GL_DEPTH_TEST);                        //  DRAW NON-OBSCURED
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);      //  FILL POLYGON MODE

    glClearColor(0.3f, 0.5f, 0.9f, 0.0f);           //  BACKGROUND COLOR

    glMatrixMode(GL_PROJECTION);
    gluPerspective(CAMERA_FOVY, (GLdouble)WINDOW_WIDTH / (GLdouble)WINDOW_HEIGHT, CAMERA_ZNEAR, CAMERA_ZFAR);
}

void Game::update()
{
    for (int i = 0; i != entities.size(); ++i)
    {
        entities[i]->update();
    }
}

void Game::render()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();
    gluLookAt(0, 0, 0, car->position.x, car->position.y, car->position.z, 0, -1, 0);

    for (int i = 0; i != entities.size(); ++i)
    {
        glLoadIdentity();
        glTranslatef(entities[i]->position.x, entities[i]->position.y, entities[i]->position.z);
        glRotatef(entities[i]->rotation.x, 1.0f, 0.0f, 0.0f);
        glRotatef(entities[i]->rotation.y, 0.0f, 1.0f, 0.0f);
        glRotatef(entities[i]->rotation.z, 0.0f, 0.0f, 1.0f);
        entities[i]->render();
    }
}

Upvotes: 0

Views: 2770

Answers (1)

Troubadour
Troubadour

Reputation: 13431

You are calling glLoadIdentity at the beginning of each iteration of the entity for loop. On the first iteration this will replace the matrix that was set by gluLookAt.

To ensure this view matrix applies to all your entities you want to call glPushMatrix after the call to gluLookAt. Make sure to balance this with a call to glPopMatrix after your for loop.

Upvotes: 4

Related Questions