gluLook不工作?

时间:2013-11-12 20:54:49

标签: c++ opengl game-engine

为什么gluLookAt不工作?

它对场景没有任何影响,就好像它甚至没有被调用一样。

是因为我使用glMatrixMode(GL_MODELVIEW);如果我不包含它我在屏幕上什么都没有。

我正在使用GLFW,GLEW和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();
    }
}

1 个答案:

答案 0 :(得分:4)

您在实体for循环的每次迭代开始时调用glLoadIdentity。在第一次迭代中,这将替换由gluLookAt设置的矩阵。

要确保此视图矩阵适用于您在调用glPushMatrix后要呼叫gluLookAt的所有实体。确保在for循环后调用glPopMatrix来平衡这一点。