检查代码中是否正确设置了OpenGL上下文。若代码中使用了多个OpenGL上下文,则需要确保每个上下文在调用bgfx前都已经成功地绑定到当前线程中。另外,确保OpenGL上下文与当前所用的窗口匹配,即上下文对应于正确的X窗口ID。以下示例代码演示如何通过GLX库的函数来正确设置OpenGL上下文并解决glxBadDrawable错误:
#include
#include
int main(int argc, char** argv) {
Display* dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
printf("Error opening display.\n");
return 1;
}
int scr = DefaultScreen(dpy);
Window root = RootWindow(dpy, scr);
int glAttribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
XVisualInfo* vi = glXChooseVisual(dpy, scr, glAttribs);
if (vi == NULL) {
printf("Error finding compatible visual.\n");
return 1;
}
GLXContext ctx = glXCreateContext(dpy, vi, NULL, True);
if (ctx == NULL) {
printf("Error creating GLX context.\n");
return 1;
}
Window win = XCreateWindow(dpy, root, 0, 0, 640, 480, 0, vi->depth, InputOutput, vi->visual, 0, NULL);
XMapWindow(dpy, win);
glXMakeCurrent(dpy, win, ctx);
// Now we have a valid OpenGL context and window
// We can initialize bgfx with this context
bgfx::renderFrame();
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, ctx);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
return 0;
}