我正在尝试通过 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/