【发布时间】:2014-02-06 06:29:46
【问题描述】:
我在启用 3D 加速的 vbox 上的 OpenGL 示例遇到了一个奇怪的问题
- 来宾:Ubuntu 12.04
- 主机:Windows 7、nvidia Graphics
- vbox 版本 4.3.6,安装了来宾添加
当 3D 加速被禁用时,此应用程序在 vbox 上正常运行,我也在独立的 Linux PC 上进行了检查。 在启用 3D 加速的情况下运行相同的程序时,它无法获取 GL 函数指针并给出错误 - function no-op
App 很简单,主线程创建 2 个线程。
- 主线程 - 创建线程 1,创建线程 2
- 线程 1 - 创建用于渲染的 X 窗口
- 线程 2 - 创建渲染线程(在 X 窗口上绘制 OpenGL 四边形)。
这是示例应用的代码。
#include<stdio.h>
#include<stdlib.h>
#include<X11/X.h>
#include<X11/Xlib.h>
#include<GL/gl.h>
#include<GL/glx.h>
//#include<GL/glu.h>
#include <dlfcn.h> /*dlopen*/
#include <pthread.h>
#include <unistd.h> /*sleep*/
Display *dpy;
Display *dpy2;
Window root;
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
XVisualInfo *vi;
XVisualInfo *vi2;
Colormap cmap;
XSetWindowAttributes swa;
Window win;
GLXContext glc;
XWindowAttributes gwa;
XEvent xev;
bool render;
void DrawAQuad()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1., 1., -1., 1., 1., 20.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);
glTranslatef(0.0, 0.0, -10.0);
glBegin(GL_QUADS);
glColor3f(1., 0., 0.); glVertex3f(-.75, -.75, 0.);
glColor3f(0., 1., 0.); glVertex3f( .75, -.75, 0.);
glColor3f(0., 0., 1.); glVertex3f( .75, .75, 0.);
glColor3f(1., 1., 0.); glVertex3f(-.75, .75, 0.);
glEnd();
}
void *CreateMainWindow(void* threadID)
{
dpy = XOpenDisplay(NULL);
if(dpy == NULL)
{
printf("\n\tWindow Thread: cannot connect to X server\n\n");
exit(0);
}
root = DefaultRootWindow(dpy);
printf("\n *** CreateWindow: xopendisplay over *** \n");
vi = (XVisualInfo*)glXChooseVisual(dpy, 0, att);
if(vi == NULL)
{
printf("\n\tWindow Thread: no appropriate visual found\n\n");
exit(0);
}
else
{
printf("\n\tWindow Thread: visual %p selected\n", (void *)vi->visualid);
}
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
swa.colormap = cmap;
swa.event_mask = ExposureMask | KeyPressMask;
win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
XMapWindow(dpy, win);
XStoreName(dpy, win, "VERY SIMPLE APPLICATION");
while(1)
{
XNextEvent(dpy, &xev);
printf("\nXEVENT\n");
if(xev.type == Expose)
{
render = true;
}
else if(xev.type == KeyPress)
{
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
render = false;
break;
//exit(0);
}
}
}
void *RenderThread(void* threadID)
{
vi2 = (XVisualInfo*)glXChooseVisual(dpy2, 0, att);
printf("\n\tRenderThread : visual %p selected\n", (void *)vi2->visualid);
glc = (GLXContext)glXCreateContext(dpy2, vi2, NULL, GL_TRUE);
glXMakeCurrent(dpy2, win, glc);
glEnable(GL_DEPTH_TEST);
while(render)
{
//XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, 600, 600);
DrawAQuad();
glXSwapBuffers(dpy2, win);
} /* this closes while(render) */
glXMakeCurrent(dpy2, None, NULL);
glXDestroyContext(dpy2, glc);
XCloseDisplay(dpy2);
}
int main(int argc, char *argv[])
{
render = true;
pthread_t thread1;
pthread_t thread2;
char *temp1;
char *temp2;
//For Async issue
if(!XInitThreads())
{
fprintf(stderr, "XInitThread failed\n");
return 0;
}
//Create Main Window
int err = pthread_create(&thread1, NULL, CreateMainWindow, (void*)temp1);
if (err != 0)
printf("\n ERROR::can't create thread1 :[%d]", err);
else
printf("\n Thread1 created successfully\n");
sleep(1); // Wait for thread 1 to complete
dpy2 = XOpenDisplay(NULL);
if(dpy2 == NULL)
{
printf("\n\tMain : cannot connect to X server\n\n");
exit(0);
}
//Create Render Thread
err = pthread_create(&thread2, NULL, RenderThread, (void*)temp2);
if (err != 0)
printf("\n ERROR::can't create thread2 :[%d]", err);
else
printf("\n Thread2 created successfully\n");
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
} /* this is the } which closes int main(int argc, char *argv[]) { */
并编译代码 -
g++ -o quad quad.cpp -lGL -lX11 -lXmu -lXi -lpthread -lm
帮助我了解问题出在哪里
【问题讨论】:
-
你正在打一场艰苦的战斗,逆流而上。我同意 datenwolfs 的回答。您在虚拟化下执行此操作的事实有点……有趣。
-
您好,您找到解决方案了吗?我遇到了同样的问题(英特尔/Win7 64 主机,Scinetific Linux 64 来宾,安装了 vbox GA)。我有一个 QT 应用程序,它在专用的高优先级线程中执行 opengl 调用。主线程上的 OGL 调用工作,hp 线程上的调用是 nop(什么都不做,没有错误)...
标签: c++ multithreading opengl ubuntu virtualbox