深度值和FBO

时间:2012-12-19 09:04:52

标签: c++ opengl

a lovely depth texture

这是我在尝试实施阴影贴图时不断看到gDebugger的可爱图像。作为参考我正在http://fabiensanglard.net/shadowmapping/index.php使用教程。我尝试了几个不同的结果,不同的结果,主要是一个具有平坦深度值的FBO(没有轻微的变化表明奇怪的深度范围)或这个真棒人。我已经尝试了大量不同的东西,但唉,没有完成FBO失败,我大多回到这个。作为参考,这里是gDebugger吐出的实际深度缓冲区:

depth

所以,考虑到所有这些,这是我的代码。 Shader代码被隐瞒了,因为我还没有在我的FBO中获得值得处理的东西。

以下是我如何初始化FBO / Depth tex:

    int shadow_width = 1024;
    int shadow_height = 1024;

    // Try to use a texture depth component
    glGenTextures(1, &depth_tex);
    glBindTexture(GL_TEXTURE_2D, depth_tex);

    // GL_LINEAR does not make sense for depth texture. However, next tutorial shows usage of GL_LINEAR and PCF
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    // Remove artefact on the edges of the shadowmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

    // No need to force GL_DEPTH_COMPONENT24, drivers usually give you the max precision if available 
    glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 
        shadow_width, shadow_height, 0, GL_DEPTH_COMPONENT, 
        GL_FLOAT, 0);
    // attach the texture to FBO depth attachment point 
    glBindTexture(GL_TEXTURE_2D, 0);

    // create a framebuffer object
    glGenFramebuffers(1, &fbo_id);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo_id);

    // Instruct openGL that we won't bind a color texture with the currently binded FBO
    glDrawBuffer(GL_NONE);
    glReadBuffer(GL_NONE);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D, depth_tex, 0);

    // switch back to window-system-provided framebuffer
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

这是我的渲染代码:

    glLoadIdentity();
    float lpos[4] = {0,0,0,1};
    float lpos3[4] = {5000,-500,5000,1};

    glBindTexture(GL_TEXTURE_2D,0);
    glBindFramebuffer(GL_FRAMEBUFFER,fbo_id);
    glViewport(0, 0, 1024, 1024);
    glClear(GL_DEPTH_BUFFER_BIT);
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 
    setup_mats(lpos[0],lpos[1],lpos[2],lpos3[0],lpos3[1],lpos3[2]);

    glCullFace(GL_FRONT);
    mdlman.render_models(R_STENCIL);
    mdlman.render_model(R_STENCIL,1);
    set_tex_mat();
    glBindFramebuffer(GL_FRAMEBUFFER,0);

    glViewport( 0, 0, (GLsizei)800, (GLsizei)600 );
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    this->r_cam->return_LookAt();
    gluPerspective(45,800/600,0.5f,2000.0f);
    glMatrixMode(GL_PROJECTION);
    glLoadMatrixf(&r_cam->getProjectionMatrix()[0][0]);
    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(&r_cam->getViewMatrix()[0][0]);

    glCullFace(GL_BACK);
    mdlman.render_models(R_NORM);

以下是我为FBO绘制对象的方法:

        glEnableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_VERTEX_ARRAY);
        glBindBuffer(GL_ARRAY_BUFFER,models.at(mdlid).t_buff);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,models.at(mdlid).i_buff);

        glNormalPointer(GL_FLOAT,sizeof(vert),BUFFER_OFFSET(sizeof(GLfloat)*2));
        glVertexPointer(3, GL_FLOAT,sizeof(vert),BUFFER_OFFSET(sizeof(GLfloat)*5));         

        glDrawElements(GL_TRIANGLES,(models.at(mdlid).f_index.size())*3,GL_UNSIGNED_INT,0);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);

一些结束注释:gDebugger的深度缓冲图像来自灯光POV。我的相机是基于四元数的,并且使用与OGL对齐的矩阵用于它的lookAt函数(用于转换到光位置/视图。)如果需要,我可以提供代码。其他所有内容都是从教程中复制的,以确保我没有做或设置一些愚蠢的东西。

0 个答案:

没有答案