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

之前遇到的问题是,在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了。





  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值