Results 1 to 3 of 3

Thread: Reconstruct world position from depth texture (libgdx)

  1. #1
    Newbie Newbie
    Join Date
    May 2015
    Posts
    2

    Reconstruct world position from depth texture (libgdx)

    What I'm attempting to do, is to recreate the world position from a depth texture.
    I am using a FBO to render the depth texture and store it, and then I am using a full-screen quad in order to render the shaders.
    I am using libgdx for Java, however I cannot find anything relating to this for libgdx. Just hoping an expert can point out a fatal flaw

    Here is my vertex shader:

    Code :
                     attribute vec4 a_Position;     
                     attribute vec4 a_Color;
                     attribute vec2 a_texCoords;
                     varying vec4 v_Color;
                     varying vec2 v_texCoords;
                     void main()                   
                     {                             
                        v_Color = a_Color;
                        v_texCoords = a_texCoords;
                        gl_Position =   a_Position;   
                     }


    And the fragment shader:

    Code :
                     #ifdef GL_ES
                     precision mediump float;
                     #endif
                     varying vec4 v_Color;
                     varying vec2 v_texCoords;
                     uniform sampler2D u_depthMap;
                     uniform mat4 u_invProjView;
     
                     vec3 getPosition(vec2 uv, float depth) {
                          vec4 pos = vec4(uv, depth, 1.0)*2.0-1.0;
                          pos = u_invProjView * pos;
                          pos = pos/pos.w;
                          return pos.xyz;
                     }
     
     
                     void main()                                   
                     {                                           
                       float depth = texture2D(u_depthMap, v_texCoords).r;
     
                       gl_FragColor = vec4(getPosition(v_texCoords, depth), 1.0);
                     }

    Before, and after I move the camera, respectively:

    HEiV3qO.jpgvFed7ap.jpg
    It seems to me that there is some thing wrong with the matrices, however I cannot see anything wrong. I am passing in the camera's inverted projectionView matrix.

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,675
    It's not clear from "v_texCoords = a_texCoords" that `a_texCoords` values are on the range [-1, 1]. Because if they're only [0, 1], then that's wrong.

    For the rest of your problems, read this.

  3. #3
    Newbie Newbie
    Join Date
    May 2015
    Posts
    2
    Quote Originally Posted by Alfonse Reinheart View Post
    It's not clear from "v_texCoords = a_texCoords" that `a_texCoords` values are on the range [-1, 1]. Because if they're only [0, 1], then that's wrong.

    For the rest of your problems, read this.
    Thanks for your reply.
    I tried changing it to "v_texCoords = a_texCoords * 2.0 - 1.0", but things just turned out worse, becoming even more distorted. It's possible, but I don't think it's the texture coordinates causing the problem.

Similar Threads

  1. how to reconstruct depth from texture?
    By basteagui in forum OpenGL: General
    Replies: 2
    Last Post: 02-09-2016, 06:45 AM
  2. Replies: 4
    Last Post: 10-20-2014, 11:15 AM
  3. Getting World position from depth buffer in FS
    By al.davidson23 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 10-13-2010, 01:54 PM
  4. screen space to world position using depth texture
    By th3w1zard in forum OpenGL: GLSL
    Replies: 4
    Last Post: 11-13-2009, 06:15 AM
  5. The way to reconstruct position in Leadwerks
    By LangFox in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 07-29-2009, 10:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean