我有一段时间这个问题,但找不到解决方案。
在这里,我初始化帧缓冲区:
//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);
我以这种方式在render函数中使用它:
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();
这里应该发生的是在附加的Framebuffer纹理中渲染了“ scene1”。屏幕上将显示“ scene2”。
屏幕上确实出现了“ scene2”,但是当我使用渲染的Framebuffer纹理时,出现的是:
谁能告诉我怎么了?
最佳答案
从您写的内容中,我了解到DrawScene2()应该呈现整个场景,而DrawScene1()应该呈现成DrawScene2()产生的一部分。
由于您没有给出DrawScene2()的任何细节(或整个代码),因此我只能在这里进行有根据的猜测:问题将是DrawScene2()。由于您在此处使用DrawScene1()的结果作为渲染源,因此您可能只是将整个FBO打在了渲染目标上,而不是将其视为常规纹理,即在获取之前为其设置了适当的世界和纹理坐标渲染(即,要用DrawScene1()生成的东西“绘制”的DrawScene2()渲染的门的边界)。