我正在尝试使用3个FBO实现一个简单的程序,将场景渲染到纹理并显示纹理四边形。我之前使用片段着色器成功完成了此操作,然后投影到纹理四边形,但是如果没有着色器则无法使其工作。可能有什么不对? 首先我设置我的纹理
glGenTextures(3, renderTextureID);
for (GLint i = 0; i < 3; i++)
{
glBindTexture(GL_TEXTURE_2D, renderTextureID[i]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// this may change with window size changes
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, fboWidth, fboHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
}
接下来一些帧缓冲状态
glGenFramebuffersEXT(3, framebufferID);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[0]);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, renderTextureID[0], 0);
GLenum fboStatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(fboStatus != GL_FRAMEBUFFER_COMPLETE_EXT)
{
fprintf(stderr, "FBO Error!");
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[1]);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, renderTextureID[1], 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[2]);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, renderTextureID[2], 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
然后我渲染场景
glViewport(0, 0, fboWidth, fboHeight);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebufferID[0]);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawModels();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
ADDED: glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, renderTextureID[0]);
glEnable(GL_SCISSOR_TEST);
glViewport(windowWidth/2, 0, fboWidth, fboHeight);
glScissor(windowWidth/2, 0, fboWidth, fboHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex2f(-1.0f, -1.0f);
glTexCoord2i(1, 0);
glVertex2f(1.0f, -1.0f);
glTexCoord2i(1, 1);
glVertex2f(1.0f, 1.0f);
glTexCoord2i(0, 1);
glVertex2f(-1.0f, 1.0f);
glEnd();
glDisable(GL_SCISSOR_TEST);
ADDED: glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
结果是黄色方块。如果不使用FBO,绘制模型会绘制黄色线框立方体,因此问题不是这样。我已经阅读了之前关于在生成和绑定纹理后使用glTexParameterf的类似帖子,但我已经完成了这个。
感谢...
答案 0 :(得分:2)
我没有看到最终缓冲区有任何明确的结果。如果情况确实如此,并且您启用了Z测试,那么最终渲染缓冲区上的所有绘图(假设您使用相同的Z缓冲区)将被Z拒绝。
但这是一个疯狂的猜测,因为仍然有很多代码缺失。我假设你没有GL错误?
编辑添加:
您是否在固定功能中启用了纹理? glEnable(GL_TEXTURE_2D);