尝试在 OSX 中创建多个帧缓冲区对象而不是 Linux 时出现问题

Problems trying to create multiple Frame Buffer Objects in OSX but not Linux

本文关键字:Linux 问题 对象 缓冲区 OSX 创建      更新时间:2023-10-16

我正在编写一个包含小散点图集合的应用程序(称为 ProjectionAxes )。每当获取新数据时,我都会重新渲染整个绘图,而是创建了一个纹理并渲染到该纹理,然后将纹理渲染为四边形。 每个绘图都是一个对象,并具有自己的纹理、渲染缓冲区对象和帧缓冲区对象。

这在Linux下运行良好,但在OSX下则不然。 当我在 Linux 上运行该程序时,每个对象都会创建自己的纹理、FBO 和 RBO,并且一切都很好。但是,当我在OSX上运行相同的代码时,对象不会生成单独的FBO,但似乎都使用相同的FBO。

在我的测试程序中,我创建了两个ProjectionAxes实例。在第一次调用plot()时,轴会检测到纹理尚未创建,然后生成它们。在此生成过程中,我显示 textureId、RBOid 和 FBOid 的整数值。当我运行我的代码时,这是我在 Linux 下运行程序时得到的输出:

ProjectionAxes::plot() --> Texture is invalid regenerating it!
Creating a new texture, textureId:1
Creating a new frame buffer object fboID:1 rboID:1
ProjectionAxes::plot() --> Texture is invalid regenerating it!
Creating a new texture, textureId:2
Creating a new frame buffer object fboID:2 rboID:2

对于 OSX:

ProjectionAxes::plot() --> Texture is invalid regenerating it!
Creating a new texture, textureId:1
Creating a new frame buffer object fboID:1 rboID:1
ProjectionAxes::plot() --> Texture is invalid regenerating it!
Creating a new texture, textureId:2
Creating a new frame buffer object fboID:1 rboID:2

请注意,在 Linux 下,两个 FBO 具有不同的 ID,而在 OSX 下则没有。我需要做什么才能向OSX表明我希望每个对象使用自己的FBO?

以下是我用来创建FBO的代码:

void ProjectionAxes::createFBO(){
    std::cout<<"Creating a new frame buffer object";//<<std::endl;
    glDeleteFramebuffers(1, &fboId);
    glDeleteRenderbuffers(1, &rboId);
    // Generate and Bind the frame buffer
    glGenFramebuffersEXT(1, &fboId);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboId);
    // Generate and bind the new Render Buffer
    glGenRenderbuffersEXT(1, &rboId);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rboId);
    std::cout<<" fboID:"<<fboId<<" rboID:"<<rboId<<std::endl;
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, texWidth, texHeight);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
    // Attach the texture to the framebuffer
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureId, 0);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, rboId);
    // If the FrameBuffer wasn't created then we have a bigger problem. Abort the program.
    if(!checkFramebufferStatus()){
        std::cout<<"FrameBufferObject not created! Are you running the newest version of OpenGL?"<<std::endl;
        std::cout<<"FrameBufferObjects are REQUIRED! Quitting!"<<std::endl;
        exit(1);
    }
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}

这是我用来创建纹理的代码:

void ProjectionAxes::createTexture(){
    texWidth = BaseUIElement::width;
    texHeight = BaseUIElement::height;
    std::cout<<"Creating a new texture,";
    // Delete the old texture
    glDeleteTextures(1, &textureId);
    // Generate a new texture 
    glGenTextures(1, &textureId);
    std::cout<<" textureId:"<<textureId<<std::endl;
    // Bind the texture, and set the appropriate parameters
    glBindTexture(GL_TEXTURE_2D, textureId);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glGenerateMipmap(GL_TEXTURE_2D);
    // generate a new FrameBufferObject
    createFBO();
    // the texture should now be valid, set the flag appropriately
    isTextureValid = true;
}

尝试暂时注释掉glDelete*函数调用。你可能正在用手柄做一些奇怪的事情。