Linux使用OpenGL 3.2+ w / FBO在屏幕外渲染

时间:2014-02-18 10:58:58

标签: linux opengl fbo glx

我有ubuntu机器,以及用OS X编写的命令行应用程序,它使用FBO在屏幕外渲染。这是代码的一部分。

        this->systemProvider->setupContext(); //be careful with this one. to add thingies to identify if a context is set up or not
    this->systemProvider->useContext();
    glewExperimental = GL_TRUE;
    glewInit();


    GLuint framebuffer, renderbuffer, depthRenderBuffer;

    GLuint imageWidth = _viewPortWidth,
            imageHeight = _viewPortHeight;

    //Set up a FBO with one renderbuffer attachment
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

    glGenRenderbuffers(1, &renderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB, imageWidth, imageHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);


    //Now bind a depth buffer to the FBO
    glGenRenderbuffers(1, &depthRenderBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, depthRenderBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, _viewPortWidth, _viewPortHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);

“系统提供程序”是围绕OS X的NSOpenGLContext的C ++包装器,它仅用于创建渲染上下文并使其成为当前,而不将其与窗口相关联。所有渲染都发生在FBO中。

我正在尝试使用与GLX相同的Linux(Ubuntu)方法,但我很难做到这一点,因为我发现GLX需要一个像素缓冲区。

我正在尝试遵循本教程:

http://renderingpipeline.com/2012/05/windowless-opengl/

最后它使用一个像素缓冲区来使上下文变为当前,我听说它已弃用了,我们应该放弃它而选择Frame Buffer Objects,这是对的(我可能错了)。

有没有人有更好的方法或想法?

1 个答案:

答案 0 :(得分:4)

我不知道这是否是最佳解决方案,但它肯定对我有用。

将函数绑定到我们可以使用的局部变量

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

我们的对象作为类属性:

Display *display;
GLXPbuffer pbuffer;
GLXContext openGLContext;

设置上下文:

    glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc) glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" );
    glXMakeContextCurrentARB   = (glXMakeContextCurrentARBProc)   glXGetProcAddressARB( (const GLubyte *) "glXMakeContextCurrent");

    display = XOpenDisplay(NULL);
    if (display == NULL){
        std::cout  << "error getting the X display";
    }

    static int visualAttribs[] = {None};
    int numberOfFrameBufferConfigurations;
    GLXFBConfig *fbConfigs = glXChooseFBConfig(display, DefaultScreen(display), visualAttribs, &numberOfFrameBufferConfigurations);

    int context_attribs[] = {
        GLX_CONTEXT_MAJOR_VERSION_ARB ,3,
        GLX_CONTEXT_MINOR_VERSION_ARB, 2,
        GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
        GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
        None
    };

    std::cout << "initialising context...";
    this->openGLContext = glXCreateContextAttribsARB(display, fbConfigs[0], 0, True, context_attribs);

    int pBufferAttribs[] = {
        GLX_PBUFFER_WIDTH, (int)this->initialWidth,
        GLX_PBUFFER_HEIGHT, (int)this->initialHeight,
        None
    };

    this->pbuffer = glXCreatePbuffer(display, fbConfigs[0], pBufferAttribs);
    XFree(fbConfigs);
    XSync(display, False);

使用上下文:

if(!glXMakeContextCurrent(display, pbuffer, pbuffer, openGLContext)){
    std::cout << "error with content creation\n";
}else{
    std::cout << "made a context the current context\n";
}

之后,人们可以正常使用FBO,就像在任何其他场合一样。直到今天,我的问题实际上没有答案(如果有更好的选择),所以我只是提供一个对我有用的解决方案。在我看来,GLX不像OpenGL那样使用像素缓冲区的概念,因此我的困惑。渲染屏幕外的首选方法是FBO,但是要在Linux上创建OpenGL上下文,必须创建像素缓冲区(GLX类型)。之后,使用我在问题中提供的代码的FBO将按预期工作,就像在OS X上一样。