I've been trying to get my world space position of my pixel but I'm missing something.

I'm using a orthographic view for a 2.5d game. My depth is linear and this is my code.

    float3 lightPos  = lightPosition;
    float2 texCoord = PostProjToScreen(PSIn.lightPosition)+halfPixel;

    float depth = tex2D(depthMap, texCoord);

    float4 position; 

    position.x = texCoord.x *2-1;
    position.y = (1-texCoord.y)*2-1;
    position.z = depth.r;
    position.w = 1;
    position = mul(position, inViewProjection);
    //position.xyz/=position.w; // I comment it but even without it it doesn't work

    float4 normal = (tex2D(normalMap, texCoord)-.5f) * 2;
    normal = normalize(normal);

    float3 lightDirection = normalize(lightPos-position);
    float att = saturate(1.0f - length(lightDirection) /attenuation);
    float lightning = saturate (dot(normal, lightDirection));

    lightning*= brightness;
    return float4(lightColor* lightning*att, 1);

I'm using a sphere but it's not working the way I want. I reproject the texture properly onto the sphere but the light coordinates in the pixel shader seems to be stuck at zero even if I move the light volume update properly.

  • \$\begingroup\$ Your depth is linear because you are using orthographic projection. This means you don't need to do perspective divide (divide by w component that is) and so that's why it won't change anything. You sure your invViewProj matrix is correct? the inverse of product of matricies is the product in reverse ... that is (AB)^-1 = B^-1 * A^-1. Also what does the position look like? Are they way off the screen or just a bit? \$\endgroup\$ – ChaoSXDemon Jul 28 '17 at 17:11

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.