简体   繁体   English

FBO在OSX中无法正常工作

[英]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: 在这里我初始化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. 这里应该发生的是“scene1”在附加的Framebuffer纹理中呈现。 And "scene2" should show up in the screen. 并且“scene2”应该出现在屏幕上。 "scene2" does show up in the screen, but when I use the rendered Framebuffer texture, this is what appears: “scene2”确实显示在屏幕上,但是当我使用渲染的Framebuffer纹理时,会出现以下情况: 看起来像一些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. 根据你所写的内容我已经理解DrawScene2()应该渲染整个场景,而DrawScene1()应该渲染成DrawScene2()生成的部分。

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 (). 既然你没有放弃DrawScene2()的任何细节(或整个代码),我只能在这里做出有根据的猜测:问题将是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). 由于你在这里使用DrawScene1()的结果作为渲染源,你可能只是将整个FBO打到渲染目标上,而不是将其视为常规纹理,即在它获得之前为它设置正确的世界和纹理坐标。渲染(即DrawScene2()渲染的门的边界,你想用DrawScene1()生成的东西“绘制”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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