我正在尝试制作一款赛车游戏,但在屏幕上显示多个视口(viewport)时遇到了问题。我想在屏幕的右下角放一个小的“迷你 map ”(这实际上是与主相机场景不同的 View )但不知何故,第二个视口(viewport)没有出现,我只能看到主摄像头的 View 。
下面是我的代码:
void mainRender() {
updateState();
setViewport(0, windowWidth, 0, windowHeight);
setWindow();
renderScene();
setViewport(0, windowWidth, 0, windowHeight/2);
renderScene();
gluLookAt(0,88,0,
1 ,0 ,0,
0.0,1.0,0.0);
Sleep(30);
}
void setWindow() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)windowWidth/(GLfloat)windowHeight,0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(posX,posY + 0.025 * std::abs(sin(headPosAux*PI/180)),posZ,
posX + sin(roty*PI/180),posY + 0.025 * std::abs(sin(headPosAux*PI/180)) + cos(rotx*PI/180),posZ -cos(roty*PI/180),
0.0,1.0,0.0);
}
void setViewport(GLint left, GLint right, GLint bottom, GLint top){
glViewport(left, bottom, right - left, top - bottom);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(windowWidth,windowHeight);
glutInitWindowPosition(windowXPos,windowYPos);
/**
Store main window id so that glui can send it redisplay events
*/
mainWindowId = glutCreateWindow("TF");
glutDisplayFunc(mainRender);
glutReshapeFunc(onWindowReshape);
/**
Register keyboard events handlers
*/
glutKeyboardFunc(onKeyDown);
glutKeyboardUpFunc(onKeyUp);
mainInit();
glutMainLoop();
return 0;
}
最佳答案
I don't draw in the second viewport, I just change the position of the camera, so it can look to the map from upwards and it will look like a minimap (that's what I use the lookAt function for).
我认为您对 opengl 的工作原理有一些严重的误解。只是改变 gluLookAt 不会做任何事情。如果您想从不同的角度查看场景,则必须在更改视口(viewport)后重新绘制整个场景。如果你想显示一个小 map ,那么你应该正常绘制整个场景,将视口(viewport)设置为小 map ,设置俯视相机,然后再次绘制整个场景。
关于c++ - OpenGL 上的多个视口(viewport),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11161699/