您的位置:首页 > 运维架构 > Linux

linux上在不建立窗口的情况下使用opengl

2017-03-31 15:18 323 查看
之前遇到的问题是,在linux上基于ffmpeg增加使用opengl的模块,opengl只是用来映射投影等等,并不需要窗口显示到screen。所以需要使用一种方法不会创建screen还能对opengl进行初始化和正常使用。

首先需要启动linux下的 window X图形界面,目的是利用OpenGL 3.0增加的接口glXCreateContextAttribsARB。该接口同时还需要GLX1.4以上版本的支持。

首先,拿到GLX函数接口

typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
typedef Bool (*glXMakeContextCurrentARBProc)(Display*, GLXDrawable, GLXDrawable, GLXContext);
static glXCreateContextAttribsARBProc glXCreateContextAttribsARB = NULL;
static glXMakeContextCurrentARBProc glXMakeContextCurrentARB = NULL;
...
glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc) glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" );
glXMakeContextCurrentARB = (glXMakeContextCurrentARBProc) glXGetProcAddressARB( (const GLubyte *) "glXMakeContextCurrent" );


接下来,需要连接到display。如果displayName参数为NULL,XOpenDisplay将会用环境变量里的$DISPLAY代替。
const char *displayName = NULL;
Display* display = XOpenDisplay( displayName );


配置framebuffer
static int visualAttribs[] = { None };
int numberOfFramebufferConfigurations = 0;
GLXFBConfig* fbConfigs = glXChooseFBConfig( display, DefaultScreen(display), visualAttribs, &numberOfFramebufferConfigurations );

创建context。需要最低OpenGL Version3.0。
int context_attribs[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 0,
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
None
};

glContext = glXCreateContextAttribs(s->display, fbConfigs[0], 0, 1, context_attribs);

最后让context开始显示。我们不绑定windows surface,而是绑定到pbuffer
int pbufferAttribs[] = {
GLX_PBUFFER_WIDTH, 32,
GLX_PBUFFER_HEIGHT, 32,
None
};
s->pbuffer = glXCreatePbuffer(s->display, fbConfigs[0], pbufferAttribs);

XFree(fbConfigs);
XSync(s->display, False);
if(!glXMakeContextCurrent(s->display, s->pbuffer, s->pbuffer, s->glContext[0]))
{
//error case here
}

到此就完成了不调用窗口的初始化过程,接下来就可以正常进行glewInit()使用opengl的api了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: