我正在尝试在帧缓冲区中渲染所有内容。
所以我制作了一个带有深度纹理和颜色纹理的帧缓冲,如下所示:http://www.opengl.org/wiki/Framebuffer_Object_Examples#Color_texture.2C_Depth_texture
但是如果启用GL_DEPTH_TEST,则不会呈现任何内容。 我做错了什么?
整个代码太长了......
帧缓冲类:
class FrameBuffer {
GLuint id;
size_t width, height, nColorAttachMents;
GLuint colorTexture[GL_MAX_COLOR_ATTACHMENTS_EXT];
GLuint depthTexture;
private:
void attachDepthTexture();
void attachColorTexture(GLuint);
public:
void create(size_t, size_t, size_t);
void bindDepthTexture() const;
void bindColorTexture(GLuint) const;
void bind() const;
void unbind() const;
size_t getWidth() const;
size_t getHeight() const;
};
void FrameBuffer::attachDepthTexture() {
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTexture, 0);
}
void FrameBuffer::attachColorTexture(GLuint i) {
i=clamp(i, 0, GL_MAX_COLOR_ATTACHMENTS_EXT-1);
glGenTextures(1, &colorTexture[i]);
glBindTexture(GL_TEXTURE_2D, colorTexture[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT+i, GL_TEXTURE_2D, colorTexture[i], 0);
}
void FrameBuffer::create(size_t width, size_t height, size_t nColorAttachMents=1) {
this->width=width;
this->height=height;
nColorAttachMents=clamp(nColorAttachMents, 0, GL_MAX_COLOR_ATTACHMENTS_EXT);
this->nColorAttachMents=nColorAttachMents;
GLenum FBOstatus;
glGenFramebuffersEXT(1, &id);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
if(nColorAttachMents==0) {
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
}
attachDepthTexture();
for(int i=0; i<nColorAttachMents; i++)
attachColorTexture(i);
FBOstatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(FBOstatus != GL_FRAMEBUFFER_COMPLETE_EXT)
showMessage("GL_FRAMEBUFFER_COMPLETE_EXT failed, CANNOT use FBO", "FBO Create error");
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void FrameBuffer::bindDepthTexture() const {
glBindTexture(GL_TEXTURE_2D, depthTexture);
}
void FrameBuffer::bindColorTexture(GLuint i=0) const {
i=clamp(i, 0, GL_MAX_COLOR_ATTACHMENTS_EXT-1);
glBindTexture(GL_TEXTURE_2D, colorTexture[i]);
}
void FrameBuffer::bind() const {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
}
void FrameBuffer::unbind() const {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
size_t FrameBuffer::getWidth() const {
return width;
}
size_t FrameBuffer::getHeight() const {
return height;
}
此处使用:
void Game::init() {
if(loaded) {
Terrain::init();
mainFrameBuffer.create(Settings::screenWidth, Settings::screenHeight, 1);
...
}
void Game::render() {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(45.0, (float)Game::Settings::screenWidth/Game::Settings::screenHeight, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
setCamera();
frustum.updateFrustum();
if(Settings::shadowOn) makeShadow();
mainFrameBuffer.bind();
if(Settings::renderSkyOn) renderSky();
if(Settings::reflectionOn) renderReflection(Settings::terrainShaderOn, Settings::objectShaderOn);
if(Settings::renderTerrainOn) renderTerrain(Settings::terrainShaderOn, Settings::shadowOn);
if(Settings::renderObjectsOn) renderObjectsUnderwater();
if(Settings::renderWaterOn) renderWater(true);
drawCursor();
if(Settings::renderObjectsOn) renderObjects(Settings::objectShaderOn);
drawMarkers();
renderProjectiles();
renderParticles();
mainFrameBuffer.unbind();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
mainFrameBuffer.bindColorTexture();
glDrawRectangle(-1, 1, 1, -1);
Texture2D::bindNone();
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
渲染反射,关闭深度测试。这只会出现。
答案 0 :(得分:0)
抱歉,我忘了清除新的帧缓冲区。