为什么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();
}
}
答案 0 :(得分:4)
您在实体for循环的每次迭代开始时调用glLoadIdentity
。在第一次迭代中,这将替换由gluLookAt
设置的矩阵。
要确保此视图矩阵适用于您在调用glPushMatrix
后要呼叫gluLookAt
的所有实体。确保在for循环后调用glPopMatrix
来平衡这一点。