opengl - 如何在 glut 中指定键盘的方向键?

标签 opengl glut

我想使用向上、向下、向左、向右作为 opengl + glut 应用程序控件的一部分。我如何引用我的“keyboard”中的键' 函数(我传递给 glutKeyboardFunc 的那个)?

最佳答案

您需要 glutSpecialFunc .

例如 -

#include<GL/gl.h>
#include<GL/glut.h>
#include<stdio.h>

void
init(void)
{
    /*initialize the x-y co-ordinate*/
    glClearColor(0,0,0,0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-320, 319,-240, 239);
    glClear (GL_COLOR_BUFFER_BIT);

    glFlush();
}

void
catchKey(int key, int x, int y)
{
    if(key == GLUT_KEY_LEFT)    
        printf("Left key is pressed\n");
    else if(key == GLUT_KEY_RIGHT)
        printf("Right key is pressed\n");
    else if(key == GLUT_KEY_DOWN)
        printf("Down key is pressed\n");
    else if(key == GLUT_KEY_UP)
        printf("Up key is pressed\n");
}

int
main(int argc, char *argv[])
{
    double x_0, y_0, x_1, y_1;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(400, 400);

    glutCreateWindow("Special key");
    init();
    glutSpecialFunc(catchKey);
    glutMainLoop();
}

关于opengl - 如何在 glut 中指定键盘的方向键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4249133/

相关文章:

c++ - 多次调用glGetTexImage时出现缓冲区溢出错误

c - 删除 GLUT 函数调用

c++ - 热气球不可见

C++ 四元数旋转不按预期工作

c - OpenGL/glut 背景多边形似乎与前景多边形重叠

c++ - 使用 glutPostRedisplay() 绘制锯齿线

opengl - 水平和垂直限制 FOV

c++ - 来自 .ply 文件的 Garbage Face 相关信息

qt - QGLWidget和硬件加速?

linux - 使用 OpenGL 和 Freeglut 在 Linux 中显示 RGB 窗口