Skip to main content
GameDev.net gamedev.net
🔒 Locked

world space from depth

Started by JPulham Mar 1, 2010 at 12:53 PM 0 replies 1.3k views
Original Post
JPulham
JPulham
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.