linux - 使用 xorg 在 Linux 下的 FreeGLUT 中共享上下文

标签 linux opengl freeglut xorg openglcontext

我正在尝试通过 FreeGLUT 库将 OpenGL 与共享上下文一起使用(因为在窗口之间共享纹理)...它工作正常,我可以共享纹理,但我在程序结束时或在用鼠标关闭窗口期间失败了。 ..

我已经验证了模拟问题的代码:( http://pastie.org/9437038 )

// file: main.c
// compile: gcc -o test -lglut main.c
// compile: gcc -o test -lglut -DTIME_LIMIT main.c

#include "GL/freeglut.h"

#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
  int winA, winB, winC;
  int n;

  glutInit(&argc, argv);

  glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE , GLUT_ACTION_CONTINUE_EXECUTION);
  //glutSetOption(GLUT_RENDERING_CONTEXT, GLUT_USE_CURRENT_CONTEXT);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  winA = glutCreateWindow("Test A");
  glutSetOption(GLUT_RENDERING_CONTEXT, GLUT_USE_CURRENT_CONTEXT);
  winB = glutCreateWindow("Test B");
  winC = glutCreateWindow("Test C");

  printf("loop\n");
  #ifdef TIME_LIMIT
  for (n=0;n<50;n++)
  {
    glutMainLoopEvent();

    usleep(5000);
  }
  #else //TIMELIMIT
  glutMainLoop();
  #endif // TIME_LIMIT

  printf("Destroy winC\n");
  glutDestroyWindow(winC);
  printf("Destroy winB\n");
  glutDestroyWindow(winB);
  printf("Destroy winA\n");
  glutDestroyWindow(winA);

  printf("Normal end\n");

  return 0;
}

输出:

loop
X Error of failed request:  GLXBadContext
  Major opcode of failed request:  153 (GLX)
  Minor opcode of failed request:  4 (X_GLXDestroyContext)
  Serial number of failed request:  113
  Current serial number in output stream:  114
Segmentation fault

TIME_LIMIT 输出:

loop
Destroy winC
Destroy winB
Destroy winA
Segmentation fault

无需调用 glutSetOption(GLUT_RENDERING_CONTEXT, GLUT_USE_CURRENT_CONTEXT);,它运行良好。

有人知道我做错了什么吗?

最佳答案

GLUT_USE_CURRENT_CONTEXT 选项创建共享上下文。它只是意味着相同 GL 上下文用于所有窗口。您只有一个 GL 上下文,并在您第一次销毁使用它的窗口时销毁它,因此其他销毁调用失败。我所知道的 GLUT 实现实际上都不支持 GL 上下文共享。

GLUT_USE_CURRENT_CONTEXT 更像是一个 hack(无论如何它也不是 GLUT 规范的一部分),并且不是真正的良好实现。它可以使用一些引用计数在最后一个使用它的窗口被销毁之前销毁上下文,但事实并非如此。

关于linux - 使用 xorg 在 Linux 下的 FreeGLUT 中共享上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25085662/

相关文章:

opengl - 如何有效地在openGL中渲染大量对象?

c++ - 获取 "undefined reference to ` glBegin'“到以前的工作项目,make 文件中需要更新什么?

java - 无法更新 Solr 搜索结果 - 错误 #500 无法创建主管

opengl - glBlendFunc 和 glClearColor alpha 参数

iphone - 有多少 iPhone 应用程序可以在 Linux 上开发和测试?

opengl - 绕相机轴旋转

c++ - gluLookAt 不断旋转相机

opengl - 如何通过opengl混合RGB和BGRA原始图像?

JAVA 2D 游戏 Linux 糟糕的 FameRate

c++ - 大多数守护程序应用程序如何在 Linux 中进行日志记录?