0
我正嘗試渲染幀緩衝區中的所有內容。當啓用GL_DEPTH_TEST時,在幀緩衝區中渲染不會生效
所以我做了深度紋理和顏色紋理附着幀緩衝區如下所示: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();
}
反射呈現關閉深度測試。這隻出現。
把你的渲染代碼放在這裏,否則很難看到問題出在哪裏 –
問題解決了, 我忘了清除新的framebuffer –
@ user2362969:然後發表那個答案。 –