Sign in to follow this  

world space from depth

This topic is 2843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to reconstruct world space position from view space depth , following the tutorial here. I'm not sure I'm using the correct matrices. Instead of static RGB values the colors 'rotate' as I rotate or translate my view. Can anyone see any obvious errors or give me any pointers? I have a GBuffer storing spherical coordinate normals and depth in an RGBA16F texture. Here's the code... demo.cpp:
[on resize]
float tanFovDiv2 = tan(mFOV * (M_PI / 180.0f)) / 2.0f;

mFarHeight = tanFovDiv2 * mFar;
dmFarWidth = mFarHeight * aspect;

[on draw - fullscreen quad for directional light]
glUseProgram(mLightShaderDirectional.getProgramId());
glUniform1i(mLightShaderDirectional.uniform_location("gbuffer"),0);
glUniform3f(mLightShaderDirectional.uniform_location("eye_pos"),mPos[0],mPos[1],mPos[2]);
glUniform3f(mLightShaderDirectional.uniform_location("camera"),mFarWidth,mFarHeight,mFar);
glBegin(GL_QUADS);
  glVertex2f( 1.0f, 1.0f);
  glVertex2f( 1.0f,-1.0f);
  glVertex2f(-1.0f,-1.0f);
  glVertex2f(-1.0f, 1.0f);
glEnd();

directional light shader:
[vertex shader]
uniform vec3 camera;

varying vec3 corner;

void main()
{
    gl_TexCoord[0].xy = gl_Vertex.xy * 0.5 + 0.5;
    corner = vec3(-camera.x * gl_Vertex.x,-camera.y * gl_Vertex.y,camera.z) * mat3(gl_ModelViewMatrixInverse);
    
    gl_Position.xy = gl_Vertex.xy;
}

[fragment shader]
uniform sampler2D gbuffer;
uniform vec3 eye_pos;

varying vec3 corner;

vec3 SphericalToCartesian(vec2 spherical)
{
  vec2 sinCosTheta, sinCosPhi;

  spherical = spherical * 2.0f - 1.0f;
  float sc = spherical.x;
  sinCosTheta.x = sin(sc);
  sinCosTheta.y = cos(sc);
  sinCosPhi = vec2(sqrt(1.0 - spherical.y * spherical.y), spherical.y);

  return vec3(sinCosTheta.y * sinCosPhi.x, sinCosTheta.x * sinCosPhi.x, sinCosPhi.y);    
}

void main(void)
{
    vec3 light_dir = normalize(vec3(1.0,1.0,0.5));
    vec3 gbu = texture2D(gbuffer, gl_TexCoord[0].xy).xyz;
    vec3 norm = SphericalToCartesian(gbu.xy);
    
    vec3 eyeVec = -((corner * gbu.z) + eye_pos);
    
    float specular = max(pow(dot(reflect(normalize(eyeVec), norm), light_dir), 100), 0.0);
        
    float NL = dot(norm,light_dir);
    
    //gl_FragColor = vec4(vec3(NL),specular * NL);
    
    // DEBUG output
    //gl_FragColor = vec4(vec3(specular * NL),0.0);
    //gl_FragColor = vec4(norm,0.0f);
    //gl_FragColor = vec4(corner,0.0f);
    //gl_FragColor = vec4(eye_pos,0.0f);

    gl_FragColor = vec4(eyeVec,0.0f);

    //gl_FragColor = vec4(vec3(gbu.z),0.0f);
}

Cheers

Share this post


Link to post
Share on other sites

This topic is 2843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this