我在OpenGL中遇到一个奇怪的问题。首先介绍一些背景知识:在我的程序中,我正在使用两个带有完整mipmap补全的纹理来实现Bloom效果。每个mipmap级别都附加到一个FBO,以便我可以在每个级别上执行两部分的高斯模糊处理,然后再重新组合它们以使其绽放。这部分都工作得很好,我可以渲染一个美丽的旋转发光对象。
当我尝试实施深度测试时,问题就开始了。
我向与每个纹理的0级相对应的FBO添加了深度图。然后我启用了GL_DEPTH_TEST。启用GL_DEPTH_TEST后,屏幕开始闪烁,并且对象不再旋转。
我已将代码简化为最简单的代码,并且似乎只有在启用GL_DEPTH_TEST的FBO渲染时才会发生。 如果我在启用GL_DEPTH_TEST的情况下渲染到默认帧缓冲区,则不会发生此问题。 如果在禁用GL_DEPTH_TEST的情况下渲染到FBO,则不会发生此问题。
下面的代码是我的代码的简化版本,只是为了说明似乎重现此问题的OpenGL调用顺序。请
我是否将深度缓冲区正确添加到FBO?
// Create a set of framebuffers, one for each mipmap level of a texture.
void FrameBuffer::Initialise(int xs, int ys)
{
xSizeTexture = PowerOfTwoAboveOrEqualTo(xs);
ySizeTexture = PowerOfTwoAboveOrEqualTo(ys);
numLevels = Log2(max(xSizeTexture, ySizeTexture));
// Generate one FB for each texture level
glGenFramebuffers(numLevels, frameBufferID);
glGenTextures(1, &textureID);
glBindTexture( GL_TEXTURE_2D, textureID);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, xSizeTexture, ySizeTexture, 0, GL_RGB, GL_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, numLevels-1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glGenerateMipmap(GL_TEXTURE_2D);
// Bind each texture level to one FB
for (int i = 0; i < numLevels; i++)
{
glBindFramebuffer(GL_FRAMEBUFFER, frameBufferID[i]);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureID, i);
}
attachment = GL_COLOR_ATTACHMENT0;
glDrawBuffers(1, &attachment);
// Add a depth buffer to the top FB
glBindFramebuffer(GL_FRAMEBUFFER, frameBufferID[0]);
glGenRenderbuffers(1, &depthrenderbufferID);
glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbufferID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xSizeTexture, ySizeTexture);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbufferID);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout << "Failed to create frame buffer" << endl;
else
cout << "Created buffer OK" << endl;
// Also initialise vertex array VAO
// ...
}
void FrameBuffer::Use()
{
glBindFramebuffer(GL_FRAMEBUFFER, frameBufferID[0]);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, 0);
}
void FrameBuffer::CopyToMain()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, textureID);
glViewport(0, 0, 1024, 1024);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
}
void MainClass::Initialise()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
framebuffer.Initialise(1024, 1024);
}
void MainClass::Render()
{
if (!initialised)
{
Initialise();
initialised = true;
}
framebuffer.Use();
glViewport(0, 0, 1024, 1024);
glUseProgram(0);
glEnable(GL_DEPTH_TEST); // Causes problems
//glDisable(GL_DEPTH_TEST); // Works fine
t += 0.05;
glLoadIdentity();
glRotatef(sin(t) * 10, 0, 0, 1);
glLineWidth(2.0);
glBegin(GL_LINE_LOOP);
glColor3f(1.0, 0.1, 0.1); glVertex3f(-0.5, -0.5, 0.0);
glColor3f(0.1, 1.0, 0.1); glVertex3f( 0.5, -0.5, 0.0);
glColor3f(0.1, 0.1, 1.0); glVertex3f( 0.5, 0.5, 0.0);
glColor3f(0.1, 0.1, 0.1); glVertex3f(-0.5, 0.5, 0.0);
glEnd();
framebuffer.CopyToMain();
}
我没有得到glGetError()报告的错误。
请原谅C ++代码的伪代码本质。只是为了说明进行OpenGL调用的顺序。
答案 0 :(得分:1)
您尝试过opengl调试功能吗?
我想,这里是绑定一个指针(数组名称),而不是gl名称,应该是frameBufferID [0]。
glBindFramebuffer(GL_FRAMEBUFFER, frameBufferID);
答案 1 :(得分:1)
您忘了包括:
glDepthFunc(GL_LEQUAL);