0
votes

I am trying to render everything in a frame buffer.

So i made a framebuffer with depth texture and color texture attached as shown here : http://www.opengl.org/wiki/Framebuffer_Object_Examples#Color_texture.2C_Depth_texture

But nothing is rendered if GL_DEPTH_TEST is enabled. What am i doing wrong?

The whole code is too long...

Frame buffer class :

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;
}   

Here used :

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();

}

The reflection is rendered turning off the depth test. This only appears.

1
Put your rendering code here,otherwise it is hard to see where is the problemMichael IV
Problem solved, I forgot to clear the new framebufferRakesh Malik
@user2362969: Then post that as an answer.Nicol Bolas

1 Answers

0
votes

Sorry, I forgot to clear the new framebuffer.