linux - 使用特殊光标时,在 Ubuntu 上重新启动 freeglut/OpenGL C 应用程序失败

标签 linux opengl ubuntu x11 freeglut

我有一个可以根据用户需求关闭和重新打开的 freeglut 应用程序。它在 Windows 和 Ubuntu 上运行良好,除非我指定鼠标光标外观。这是一个最小的工作示例:

#include <stdio.h>
#include <unistd.h>
#include <GL/freeglut.h>

void keyboard(unsigned char key, const int x, const int y);
void display(void);
void closeme(void);
void init_main(int argc, char **argv);

int win_main_no = 0;

int main(int argc, char **argv)
{
    init_main(argc, argv);
    printf("'%s': passed line %i\n", __func__, __LINE__);

    sleep(1);

    init_main(argc, argv);
    printf("'%s': passed line %i\n", __func__, __LINE__);

    return EXIT_SUCCESS;
}

void init_main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

    glutCreateWindow("GLUT Test");

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glutSetCursor(GLUT_CURSOR_CROSSHAIR); /**< this is the problem */

    glClearColor(0., 0., 0., 0.);
    glClearDepth(1.);

    win_main_no = glutGetWindow();

    glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutCloseFunc(closeme);
    glutMainLoop();
}

void closeme(void)
{
    printf("'%s': passed line %i\n", __func__, __LINE__);
    glutDestroyWindow(win_main_no);
    glutLeaveMainLoop();
}

void keyboard(unsigned char key, const int x, const int y)
{
    switch(key)
    {
        case 27:
        printf("'%s': passed line %i\n", __func__, __LINE__);
        glutLeaveMainLoop();
        printf("'%s': passed line %i\n", __func__, __LINE__);
        break;
    }
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-5, 5, -5, 5, -2, 2);

    glColor3f(1., 0., 0.);

    glBegin(GL_POLYGON);
        glVertex2d(-.5, .5);
        glVertex2d(-.5, -.5);
        glVertex2d(.5, -.5);
        glVertex2d(.5, .5);
    glEnd();

    glRasterPos3d(1.05, 0., 0.);
    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, 'x');
    glRasterPos3d(0., 0., 1.05);
    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, 'y');
    glRasterPos3d(0., 1.05, 0.);
    glutBitmapCharacter(GLUT_BITMAP_8_BY_13, 'z');

    glFlush();
    glutSwapBuffers();
}

你可以通过

gcc -O0 -g -Wall -std=c99 -o test test.c -lfreeglut -lglu32 -lopengl32

或(Linux)

gcc -O0 -g -Wall -std=c99 -o test test.c -lGL -lGLU -lglut

在 WINDOWS 7 64 位,freeglut 2.8.0 上,输出为

'keyboard': passed line 61
'keyboard': passed line 63
'closeme': passed line 51
'main': passed line 15
'keyboard': passed line 61
'keyboard': passed line 63
'closeme': passed line 51
'main': passed line 20

但是在 UBUNTU 64 位 14.04 上,存储库中的 freeglut:

'keyboard': passed line 61
'keyboard': passed line 63
'closeme': passed line 51
'main': passed line 15
X Error of failed request:  BadCursor (invalid Cursor parameter)
Major opcode of failed request:  2 (X_ChangeWindowAttributes)
  Resource id in failed request:  0x460000a
  Serial number of failed request:  64
  Current serial number in output stream:  67
'closeme': passed line 51

有没有办法防止这种情况发生(除了告诉在 Linux 上不要使用光标的标志)?这条消息“无效的 Cursor 参数”让我感到困惑,因为相同的标志在第一次运行时就起作用了。关闭窗口时是否需要以某种方式重置它?

最佳答案

这是 freeglut 中的一个错误。

下载源码,看freeglut_cursor.c ,不知道X11显示关闭时谁清理了游标缓存。遗憾的是,答案是没有人。

您可以轻松地自己修复它,并可选择向 freeglut 维护者提交补丁,或者只是向他们提交错误。

关于linux - 使用特殊光标时,在 Ubuntu 上重新启动 freeglut/OpenGL C 应用程序失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28739506/

相关文章:

php - gearman 问题和 php cli

我可以在不使用文件流的情况下编写这段代码吗

c++ - 如何在OpenGL中将汽车朝轮胎方向移动

c++ - 如果我将 glUniform1f 调用放在渲染循环之前,则不会存储统一值

Java Runtime exec 抛出没有这样的文件或权限被拒绝

javascript - 从本地计算机上的 Ubuntu (AWS EC2) 读取文件?

java - SOS - 柏林调度程序无法继续下一项工作

php - 脚本上传到实时服务器后不起作用

linux - mod_perl 看不到/tmp 中的文件

opengl - OCaml:在 GTK 中嵌入 openGL