【发布时间】:2020-05-10 09:54:21
【问题描述】:
我试图在不同的窗口分辨率上保持相同的视口比例。为此,我使用glutReshapeFunc() 和reshape() 作为其参数。 reshape() 被调用,计算似乎是正确的,但没有保留视口比率。
此外,reshape() 设置在启动时应用,但当我更改窗口大小时,视口似乎已降至默认值。
如何解决?
代码如下:
const int windowWidth = 1200;
const int windowHeight = 600;
const int viewRatio = windowWidth/windowHeight;
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("Scene");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
initialise();
glutMainLoop();
return 0;
}
void initialise(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(60.0, ((float)windowWidth)/((float)windowHeight), 1.0, 20.0);
}
void reshape(int width, int height) {
(viewRatio > width/height) ? glViewport(0, 0, width, width/viewRatio) : glViewport(0, 0, height*viewRatio, height);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(
0.0, 0.0, 10.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
// TODO: Implement the scene
glutSolidSphere(3.0, 20, 20);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
【问题讨论】: