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 7 of 7

Thread: Easiest way to reconstruct eye space position

  1. #1
    Intern Newbie
    Join Date
    Aug 2010
    Posts
    38

    Easiest way to reconstruct eye space position

    Hi,

    I'm tring to reconstruct eye space position from depth buffer, but my result doesn't look correct.

    This is my current and one of the shortest implementations i found:
    Code :
        float depth = texture2D(depthBuffer, texCoord).r;
        vec2 xy = texCoord * 2.0 - 1.0;
        vec4 clipPos = vec4(xy, depth, 1.0);
        // I pass inverseProjectionMatrix as uniform
        vec4 homo = inverseProjectionMatrix * clipPos;
        vec3 position = homo.xyz / homo.w;
        gl_FragColor =  vec4(position, 1.0);
    (from: http://www.gamedev.net/topic/577382-...on-from-depth/)

    This gives me only green-yellow-red colors and no visible 3D objects:


    But the correct result should look like this (top right)?


    I have tried out many examples. Some of them were too difficult and others gave also artifacts etc. If i visualize depth texture, then everything looks correct.

    Can anybody see mistake in above code?

  2. #2
    Junior Member Regular Contributor Kopelrativ's Avatar
    Join Date
    Apr 2011
    Posts
    214

    Re: Easiest way to reconstruct eye space position

    Why do you want to do it?

    Seems it is easier and faster to provide it using a uniform?

  3. #3
    Intern Newbie
    Join Date
    Aug 2010
    Posts
    38

    Re: Easiest way to reconstruct eye space position

    Quote Originally Posted by Kopelrativ
    Why do you want to do it?

    Seems it is easier and faster to provide it using a uniform?
    The code above is a lighting pass fragment shader in deferred renderer.

    GLSL version is 4.1.

  4. #4
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948

    Re: Easiest way to reconstruct eye space position

    I actually bookmarked a post of mine on this forum to remind myself of how the math works.

    I really ought to make a GL Wiki page out of that.

  5. #5
    Intern Newbie
    Join Date
    Aug 2010
    Posts
    38

    Re: Easiest way to reconstruct eye space position

    Quote Originally Posted by Alfonse Reinheart
    I actually bookmarked a post of mine on this forum to remind myself of how the math works.

    I really ought to make a GL Wiki page out of that.
    Amazing! Everything is working perfectly now! Thank you so much!

  6. #6
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948

    Re: Easiest way to reconstruct eye space position

    Can you post your code, so that others can benefit?

  7. #7
    Intern Newbie
    Join Date
    Aug 2010
    Posts
    38

    Re: Easiest way to reconstruct eye space position

    Quote Originally Posted by Alfonse Reinheart
    Can you post your code, so that others can benefit?
    Sure

    Code :
    in vec2 texCoord;
    uniform float nearPlane;
    uniform float farPlane;
    uniform sampler2D depthBuffer;
    uniform mat4 inverseProjectionMatrix;
     
    #define ZNEAR nearPlane
    #define ZFAR farPlane
    #define A (ZNEAR + ZFAR)
    #define B (ZNEAR - ZFAR)
    #define C (2.0 * ZNEAR * ZFAR)
    #define D (ndcPos.z * B)
    #define ZEYE (-C / (A + D))
     
    void main() {
        float depth = texture2D(depthBuffer, texCoord).r;
        vec3 ndcPos = (vec3 (texCoord, depth) - 0.5) * 2.0;
        vec4 clipPos;
        clipPos.w = -ZEYE;
        clipPos.xyz = ndcPos * clipPos.w;
        vec4 eyePos = inverseProjectionMatrix * clipPos;
        gl_FragColor =  vec4(eyePos);
    }

Posting Permissions

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