Jump to content
  • Advertisement
Sign in to follow this  
Hassanbasil

light pre-pass

This topic is 2548 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

Hello everyone,

I've been trying to implement light pre-pass recently, following this tutorial, i'm using Direct3D11 (shader model 4)
my problem is getting the pixel position in my pixel shader, here's the code (light map building shader):


float2 PostProjToScreen ( float4 position )
{
float2 screenPos = position.xy / position.w;
return 0.5f * ( float2 ( screenPos.x + 1, 1 - screenPos.y ) );
}

float2 HalfPixel ( void )
{
return float2 ( 0.5f / screenSize.x, 0.5f / screenSize.y );
}

VS_OUT VS ( VS_IN input )
{
VS_OUT output;
output.position = mul ( float4 ( input.position, 1.0f ), gWVP );
output.tex = ((output.position.xy + float2(1.0f,1.0f))/2.0f);
output.tex.y = 1.0f - output.tex.y;
return output;
}
float4 PS ( VS_OUT input ) : SV_TARGET
{
float2 texC = input.tex; //PostProjToScreen ( input.position ) + HalfPixel ( ); doesn't work

float z = depthMap.Sample ( gSampler, texC );
float x = texC.x * 2.0f - 1.0f;
float y = (1.0f - texC.y) * 2.0f - 1.0f;
float4 vProjectedPos = float4 ( x, y, z, 1.0f );
float4 vPositionVS = mul ( vProjectedPos, gProjMatrixInverse );

float3 pixelPosition = vPositionVS.xyz / vPositionVS.w; //littile problems with this variable also
float3 pixelNormal = normalMap.Sample ( gSampler, texC );

float3 lDir = -normalize ( gLightDir );
float nl = 1.0f / dot ( pixelNormal, lDir );

float3 camDir = normalize ( gEyePosW - pixelPosition );

// Calculate specular term
float3 h = normalize ( lDir + camDir );
float spec = pow ( saturate ( dot ( pixelNormal, h ) ), gSpecPower );

return float4 ( gLightColor * nl, spec );
}


so here's the whole thing: "PostProjToScreen ( input.position ) + HalfPixel ( )" line didn't work at all, it changed when my rendered shape changed which means it's not the real coords of the pixel (am i right? :D), so i took it out and replaced it with the texcoords from the vertex shader, this only works when i render a fullscreen quad (to implement directional light, for example), but i wan't a more "general" solution so that it could work with other primitives/types of light

now the second thing is: the pixel z position is not being calculated correctly: i tested (with working texcoords from vertex shader) x and y and they seem to be fine, but the z seems to be buggy, im not sure how to debug this because im a little confused about it so i would appreciate some help.

note: the depth map is automatically generated by hardware, i didn't output it myself (i tried doing it myself, the depth map looked weird, alot of big flickers everywhere, even the model depth is screwed up)

Thank you for your time.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!