简体   繁体   中英

FBOs are not working properly in OSX

I had this problem for some time, and I couldn't find a solution.

Here I initialize the Framebuffer:

//Initialize buffers
    glGenBuffers(1, &primaryBuffer);
    glBindBuffer(GL_FRAMEBUFFER, primaryBuffer);

    glGenRenderbuffers(1, &depthBuffer);
    glBindBuffer(GL_RENDERBUFFER, depthBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, Statics::ScreenWidth, Statics::ScreenHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

    glGenTextures(1, &colorTexture);
    glBindTexture(GL_TEXTURE_2D, colorTexture);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,  Statics::ScreenWidth, Statics::ScreenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

    glGenTextures(1, &depthTexture);
    glBindTexture(GL_TEXTURE_2D, depthTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32,  Statics::ScreenWidth, Statics::ScreenHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0);

    int res = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (res == GL_FRAMEBUFFER_COMPLETE)
    {
        printf("GOOD!\n");
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

I use it in the render function this way:

glBindFramebuffer(GL_FRAMEBUFFER, primaryBuffer);
glViewport(0, 0, Statics::ScreenWidth, Statics::ScreenHeight);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

SetCamera();
DrawScene1();

glBindFramebuffer(GL_FRAMEBUFFER, 0);

glViewport(0, 0, Statics::ScreenWidth, Statics::ScreenHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SetCamera();
DrawScene2();

What supposed to happen here is that "scene1" is rendered in the attached Framebuffer texture. And "scene2" should show up in the screen. "scene2" does show up in the screen, but when I use the rendered Framebuffer texture, this is what appears: 看起来像一些GPU内存垃圾! :)

Can anyone tell me what's wrong?

From what you wrote I have understood that DrawScene2() should render the entire scene, while DrawScene1() should render into a part of what DrawScene2() has produced.

Since you haven't given away any details (or the entire code) of DrawScene2(), I can only make an educated guess here: The problem will be DrawScene2 (). Since you are using the result of DrawScene1() as render source here, you probably just slapped the entire FBO on the render target, instead of treating it as a regular texture, ie setting up the proper world and texture coordinates for it before it gets rendered (ie the boundaries of the door rendered by DrawScene2() that you want to "paint" with what DrawScene1() has produced).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM