如何正确使用glutSpecialFunc?

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

我想在按[[UO时使我的方块上升。

void displayScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0,x,0); glBegin(GL_QUADS); glVertex3f(-0.4,-0.4, -5.0); glVertex3f( 0.4,-0.4, -5.0); glVertex3f( 0.4, 0.4, -5.0); glVertex3f(-0.4, 0.4, -5.0); glEnd(); //x = x + 0.1; glutSwapBuffers(); }
我正在使用gluSpecialFunc。

void ProcessSpecialKeys(unsigned char key, int x, int y) { if (key == GLUT_KEY_UP) { x = x + 0.1; } glutPostRedisplay(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(0,0); glutCreateWindow("OpenGL Window"); init(); glutDisplayFunc(displayScene); glutSpecialFunc(ProcessSpecialKeys); //glutKeyboardFunc(ProcessKeys); glutMainLoop(); return 0; }

当我将x+=0.1留在displayScene中时,当我按任意键时,正方形会上升。

我使用glutSpecialFunc不正确吗?因为我以前使用过它,所以它可以正常工作。我想念什么?

opengl codeblocks glut
1个回答
1
投票
glutSpecialFunc在按住某个键时不会连续调用。按下某个键时,将调用一次回调。glutSpecialFunc提供freeglut回调,当释放键时将调用此回调。

按下

UP时设置状态,释放UP时复位状态:

glutSpecialUpFunc
glutSpecialUpFunc
根据int main(int argc, char** argv)
{
    // [...]
    glutSpecialFunc(ProcessSpecialKeys);
    glutSpecialUpFunc(ReleaseSpecialKeys);
    // [...]
}
的状态更改int keyUpPressed = 0;

void ProcessSpecialKeys(unsigned char key, int x, int y)
{
    if (key == GLUT_KEY_UP)
        keyUpPressed = 1;
}

void ReleaseSpecialKeys(unsigned char key, int x, int y)
{
    if (key == GLUT_KEY_UP)
        keyUpPressed = 0;
}
。通过在x中调用keyUpPressed连续重绘场景

glutPostRedisplay

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