加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 01:46:48 所属栏目:Linux 来源:网络整理
导读:我有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(
我有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,depthRenderBuffer);

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

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

我正在尝试按照本教程:

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

最后它使用一个像素缓冲区来使上下文变为当前,我听说它已被弃用,我们应该放弃它而支持帧缓冲对象,这是对的(我可能错了).

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

解决方法

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

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

typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*,GLXFBConfig,GLXContext,Bool,const int*);
typedef Bool (*glXMakeContextCurrentARBProc)(Display*,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],True,context_attribs);

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

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

使用上下文:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读