动态更改gluPerspective()的参数?

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

为了理解gluPerspective()是如何工作的,我想用键盘的箭头动态地改变它的参数(第一个是角度)。

如何在回调函数中设置矩阵模式时使其工作并且是否有问题?

这是我到目前为止的代码,但按下左箭头或右箭头时没有任何反应:

double angle = 45;

void renderScene(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0, 1, 1, 1);

    glBegin(GL_TRIANGLES);
    glVertex3f(-2, -2, -5.0);
    glVertex3f(2, 0.0, -5.0);
    glVertex3f(0.0, 2, -5.0);
    glEnd();

    glutSwapBuffers();
}

void keyboard(int c, int x, int y) {

    switch (c) {
    case GLUT_KEY_LEFT:
        angle -= 15;
        break;

    case GLUT_KEY_RIGHT:
        angle += 15;
        break;
    }
}

void changeSize(int w, int h) {

    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if (h == 0)
        h = 1;
    float ratio = 1.0* w / h;

    // Use the Projection Matrix
    glMatrixMode(GL_PROJECTION);

    // Reset Matrix
    glLoadIdentity();

    // Set the viewport to be the entire window
    glViewport(0, 0, w, h);

    // Set the correct perspective.
    gluPerspective(angle, ratio, 1, 1000);

    // Get Back to the Modelview
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(200, 300);//optional
    glutInitWindowSize(800, 600); //optional
    glutCreateWindow("OpenGL First Window");
    glutDisplayFunc(renderScene);

    glutSpecialFunc(keyboard);
    glutReshapeFunc(changeSize);

    glutMainLoop();

    return 0;
}
c++ opengl glut glu
2个回答
0
投票

传递给glutReshapeFunc的函数仅在窗口调整大小时调用。因此,gluPerspective的新参数仅在此之后使用。例如,您可以在调整角度后手动调用该函数:

void keyboard(int c, int x, int y) {

    switch (c) {
    case GLUT_KEY_LEFT:
        angle -= 15;
        break;

    case GLUT_KEY_RIGHT:
        angle += 15;
        break;
    }

    changeSize(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
}

0
投票

您尚未通知显示回调函数更新新角度。 在键盘功能的最后一个调用函数glutPostRedisplay()以查看更新的角度。

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