【问题标题】:glutReshapeFunc() doesn't make any changeglutReshapeFunc() 没有做任何改变
【发布时间】: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();
}

【问题讨论】:

    标签: c++ opengl glut


    【解决方案1】:
    const int viewRatio = windowWidth/windowHeight;
    [...]
    (viewRatio > width/height) ? glViewport(0, 0, width, width/viewRatio) : glViewport(0, 0, height*viewRatio, height);
    

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

    【讨论】:

    • 谢谢,但这没有多大意义,因为reshape() 函数必须将宽度和高度作为整数值,并且窗口纵横比在我的情况下是恒定的——它等于 2。无论如何,它即使我使用float 类型也不起作用。
    • 即使widthheight 是整数,您想要进行的计算width/height 也会产生一个非整数值。但是,正如您所写,width/height 将产生一个整数,但不是一个有用的整数。如果viewRatio 是2,那么2 > 1499/500 将评估为false,因为2 > 2false
    • 哦,我错了,谢谢你的解释。但它解决了另一个错误,不是我最初询问的那个?
    猜你喜欢
    • 1970-01-01
    • 2019-04-12
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2020-10-24
    • 2021-03-16
    • 2012-09-07
    相关资源
    最近更新 更多