计算zBuffer - 接受错误的值

时间:2014-03-19 06:42:03

标签: c++ opengl 3d zbuffer

计算zBuffer-accept错误值

我正在尝试计算我的zBuffer,我接受错误的值。我加载的模型必须有不同的Zbuffer值,但我不接受它们我的值定义为ZNEAR 1.0f ZFAR 1500.0f我的代码是:

//calculate the zBuffer
GLfloat get_gl_depth()
{
    Pipeline p;
    int w = glutGet( GLUT_WINDOW_WIDTH );
    int h = glutGet( GLUT_WINDOW_HEIGHT );
    glBindBuffer( GL_PIXEL_PACK_BUFFER, pbo );
    glBufferData( GL_PIXEL_PACK_BUFFER, w * h * sizeof( GLfloat ), 0, GL_STREAM_READ );
    glReadPixels( 0, 0, w, h, GL_DEPTH_COMPONENT, GL_FLOAT, 0 );
    GLfloat* ptr = (GLfloat*)glMapBuffer( GL_PIXEL_PACK_BUFFER, GL_READ_ONLY );

    unsigned short* buffer=new unsigned short[w*h];
    unsigned short* current = buffer;
    if (ptr != nullptr)
    {
        for (int i=0;i<w*h;i++)
        {
            GLfloat distance =ZNEAR+(GLfloat(ptr[i])*(ZFAR-ZNEAR));
            *current++ = unsigned short(distance + 0.5);
        }
    }

    glUnmapBuffer( GL_PIXEL_PACK_BUFFER );
    glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );

    delete[] buffer;

    return 0;
}

之后我调用了一个加载所有这些的函数:

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable( GL_DEPTH_TEST );
    glDepthMask(GL_TRUE);

    const float lightZ =  (sinf(m_scale) + 1.0f) / 2.0f;
    PointLight pl[10];
    int numLights = 0;

    pl[numLights++] = CreateLight(0, 0, 0, 255.0f, 0.0f, 0.0f);
    pl[numLights++] = CreateLight(0, 0, 100, 0.0f, 255.0f, 0.0f);
    pl[numLights++] = CreateLight(100, 0, 100, 0.0f, 0.0f, 255.0f);

    m_pEffect->SetPointLights(numLights, pl);


    m_pEffect->SetPointLights(3, pl);

    Pipeline p;

    Vector3f cameraUp = CameraUpVector();
    p.SetCamera(m_cameraPosition, m_cameraTarget, cameraUp);

    p.SetPerspectiveProj(60.0f, WINDOW_WIDTH, WINDOW_HEIGHT, ZNEAR, ZFAR);
    m_pEffect->SetWVP(p.GetWVPTrans());
    m_pEffect->SetWorldMatrix(p.GetWorldTrans());
    m_pEffect->SetDirectionalLight(m_directionalLight);
    m_pEffect->SetEyeWorldPos(m_cameraPosition);
    m_pEffect->SetMatSpecularIntensity(0.0f);
    m_pEffect->SetMatSpecularPower(0);

    m_pMesh->Render();
    get_gl_depth(/*WINDOW_WIDTH, WINDOW_HEIGHT*/);
    glutSwapBuffers();

This is the func that initlize the first values:

Vector3f Pos(0.0f,0.0f,-700.0f);

    Vector3f Target(0.0f, 0.0f, 0.00001f);
    Vector3f Up(0.0, 1.0f, 0.0f);

    m_cameraPosition = Pos;

    m_cameraTarget = Target;    

0 个答案:

没有答案