Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: calculate zBuffer-accept wrong values

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    26

    calculate zBuffer-accept wrong values

    calculate zBuffer-accept wrong values
    calculate zBuffer-accept wrong values

    I am trying to calculate my zBuffer and I accept wrong values. the model that I am loading has to have diffrent Zbuffer values but I dont accept them my values are defined ZNEAR 1.0f ZFAR 1500.0f my code is:

    Code cpp:
    //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;
    }

    After that I am calling a func that loads all this:

    Code cpp:
    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;

    Please help me ... I tried to understand what is wrong in my calculation but didnt understand
    maybe the projection is not right?

    or the ZFAR and ZNEAR
    ??
    I changed it to:

    #define ZNEAR 400.0f
    #define ZFAR 700.0f

    and I am calculating the zbuffer and this is not equal to what I accept
    Last edited by Dark Photon; 03-31-2014 at 05:12 PM.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    786
    Please use [code][/code] around source code snippets.

    I'm not sure I understand what your question is. Could you perhaps try again, stating clearly what you want the code to do and what it does instead, thanks!
    Also, as posted the function get_gl_depth() does a bunch of work copying data to a temporary buffer which is then immediately thrown away (i.e. deleted), that doesn't seem terribly useful to me

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2014
    Posts
    26
    I am trying to calculate the zBuffer values.
    my camera Pos is : (0,0,-500)
    my Target point is-(0,0,0.000001)

    I tried this func:

    Code cpp:
        float convertZ(float n, float f, float z)
        {
            float clip_z = (z - 0.5f) * 2.0f;
            return  2.0f*f*n/(clip_z*(f-n)-(f+n));
        }
     
        GLfloat get_gl_depth()
        {
     
            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 = -convertZ(ZNEAR, ZFAR, ptr[i]);
                    *current++ = unsigned short(distance + 0.5);
                }
            }
     
     
     
            if (result==false)
            {
                  cout << "The tiff file was not created\n"; 
            }
     
            glUnmapBuffer( GL_PIXEL_PACK_BUFFER );
            glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
     
            delete[] buffer;
     
     
     
            return 0;
     
     
        }

    now I am loading a model and I want to calculate the values of the zBuffer:
    1. Is this calculation of the zBuffer correct?

    I expect to accept the real distance-how can I check my calculation?

    Thsnks.
    Last edited by Dark Photon; 03-31-2014 at 05:13 PM.

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,220
    For a perspective frustum with glDepthRange 0..1:

    Code glsl:
    eye.z = near * far / ((depth * (far - near)) - far);

    where eye.z = eye-space Z and depth is the 0..1 depth buffer value.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •