为什么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();
    }
}

最佳答案

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

为确保此视图矩阵适用于您要在调用glPushMatrix之后调用gluLookAt的所有实体。确保在for循环后与调用glPopMatrix平衡。

关于c++ - gluLookAt无法正常工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19939678/

10-10 22:35