我正在尝试渲染到纹理,然后复制该纹理并通过后处理运行它,然后将过滤后的图像与原始图像合并。我已经得到它所以我可以渲染到纹理,然后处理它并显示它,但我只能处理一次。我真的不确定会出现什么问题。
这是我的FBO设置代码:
for (int i = 0; i < 3; i++) {
glGenFramebuffers(1, &postfboId[i]);
glGenTextures(1, &postTextureId[i]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, postTextureId[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
//float color[4] = {0.0, 0.0, 0.0, 1.0};
//glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, screenSize.x, screenSize.y, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
// create a framebuffer object
glBindFramebuffer(GL_FRAMEBUFFER, postfboId[i]);
glGenRenderbuffers(1, &depthrenderbuffer[i]);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer[i]);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, screenSize.x, screenSize.y);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer[i]);
// attach the texture to FBO depth attachment point
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, postTextureId[i], 0);
}
GLenum DrawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2};
glDrawBuffers(1, DrawBuffers);
// check FBO status
FBOstatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(FBOstatus != GL_FRAMEBUFFER_COMPLETE)
printf("hey man, you might want to sit down a minute, i didn't want to have to say this to you, but the FBO failed.\n");
我想也许我需要glDrawBuffers来制作3而不是1,但这只会导致FBO不完整。 这是我的渲染代码:
glBindFramebuffer(GL_FRAMEBUFFER,postfboId[0]);
sf::Vector2u size = screenSize;
changeSize(size.x, size.y);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
//clear viewport
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(prog);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
setLookat();
glCullFace(GL_BACK);
setLights();
drawObjs();
glPopMatrix();
//first round post process
glClearColor(0, 0, 0, 1);
glUseProgram(postProg);
glBindFramebuffer(GL_FRAMEBUFFER,postfboId[1]);
glUniform1i(postProcessMode, 1);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPushMatrix();
glLoadIdentity();
glOrtho(0,screenSize.x,0,screenSize.y,-1,20);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPushMatrix();
glLoadIdentity();
glColor4f(1,1,1,1);
glUniform1i(texUniform3, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,postTextureId[0]);
glEnable(GL_TEXTURE_2D);
glTranslated(0,0,-19.5);
glBegin(GL_QUADS);
glTexCoord2d(0,0);glVertex3f(0,0,0);
glTexCoord2d(1,0);glVertex3f(screenSize.x,0,0);
glTexCoord2d(1,1);glVertex3f(screenSize.x,screenSize.y,0);
glTexCoord2d(0,1);glVertex3f(0,screenSize.y,0);
glEnd();
// round 2 post
glUniform1i(postProcessMode, 2);
glBindFramebuffer(GL_FRAMEBUFFER,0);
glUniform1i(texUniform3, 3);
glActiveTexture(GL_TEXTURE3);
// bind postTextureId[0] to draw real screen.
glBindTexture(GL_TEXTURE_2D,postTextureId[1]);
glEnable(GL_TEXTURE_2D);
glTranslated(0,0,0.5);
glBegin(GL_QUADS);
glTexCoord2d(0,0);glVertex3f(0,0,0);
glTexCoord2d(1,0);glVertex3f(screenSize.x,0,0);
glTexCoord2d(1,1);glVertex3f(screenSize.x,screenSize.y,0);
glTexCoord2d(0,1);glVertex3f(0,screenSize.y,0);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
无论出于何种原因,将FBO纹理绘制到另一个FBO会导致无意义,但将FBO纹理绘制到屏幕上就可以了。此外,所有3个FBO都工作,而不是同时工作,这很奇怪,因为我有另一个FBO只有一个深度缓冲区,可以正确显示阴影贴图。
答案 0 :(得分:2)
一个显而易见的问题是,除了第一个渲染目标之外,您还缺少glClear()
个调用。你在绑定fbo 0之后调用它一次:
glBindFramebuffer(GL_FRAMEBUFFER,postfboId[0]);
...
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
但是你还需要在绑定fbo 1之后清除:
glBindFramebuffer(GL_FRAMEBUFFER,postfboId[1]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
并绑定默认帧缓冲区后:
glBindFramebuffer(GL_FRAMEBUFFER,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
您的glPushMatrix()
和glPopMatrix()
部分电话看起来也有点可疑。但是如果不看完整个代码就很难判断它们是不正确的。您可能需要仔细检查它们是否均衡,并且在调用它们时您处于正确的矩阵模式。例如,在这个序列中:
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPushMatrix();
glLoadIdentity();
glPopMatrix()
和glPushMatrix()
来电是多余的。如果有的话,它们可能是有害的,因为如果您之前没有呼叫glPushMatrix()
,则会出现错误。否则,它将首先将前一个矩阵复制到顶部条目,但随后立即用身份矩阵覆盖它。这与单独调用glLoadIdentity()
相同。