glutReshapeFunc()没做任何更改

问题描述 投票:0回答:1

我正在尝试在不同的窗口分辨率下保持相同比例的视口。为此,我将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();
}
c++ opengl glut
1个回答
2
投票
const int viewRatio = windowWidth/windowHeight;
[...]
(viewRatio > width/height) ? glViewport(0, 0, width, width/viewRatio) : glViewport(0, 0, height*viewRatio, height);

当您明确需要分数时,请不要使用整数和整数算术。

© www.soinside.com 2019 - 2024. All rights reserved.