[25] | 1 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 2 | // |
---|
| 3 | // shadowcasterfp.cg |
---|
| 4 | // |
---|
| 5 | // Hamilton Chong |
---|
| 6 | // (c) 2006 |
---|
| 7 | // |
---|
| 8 | // This is an example fragment shader for shadow caster objects. |
---|
| 9 | // |
---|
| 10 | ///////////////////////////////////////////////////////////////////////////////// |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | // Define outputs from vertex shader. |
---|
| 14 | struct VertexOut |
---|
| 15 | { |
---|
| 16 | float4 position : POSITION; // can't rely on access to this |
---|
| 17 | float4 pos : TEXCOORD0; // position of fragment (in homogeneous coordinates) |
---|
| 18 | float4 normal : TEXCOORD1; // un-normalized normal in object space |
---|
| 19 | float4 modelPos : TEXCOORD2; // coordinates of model in object space at this point |
---|
| 20 | }; |
---|
| 21 | |
---|
| 22 | struct FragmentOut |
---|
| 23 | { |
---|
| 24 | float4 color : COLOR0; |
---|
| 25 | }; |
---|
| 26 | |
---|
| 27 | FragmentOut main( VertexOut In, // fragment to process |
---|
| 28 | uniform float uDepthOffset, // offset amount (constant in eye space) |
---|
| 29 | uniform float4x4 uProjection // projection matrix |
---|
| 30 | ) |
---|
| 31 | { |
---|
| 32 | FragmentOut Out; |
---|
| 33 | |
---|
| 34 | // compute the "normalized device coordinates" (no viewport applied yet) |
---|
| 35 | float4 postproj = In.pos / In.pos.w; |
---|
| 36 | |
---|
| 37 | // get the normalized normal of the geometry seen at this point |
---|
| 38 | float4 normal = normalize(In.normal); |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | // -- Computing Depth Bias Quantities ----------------------------- |
---|
| 42 | |
---|
| 43 | // We now compute the change in z that would signify a push in the z direction |
---|
| 44 | // by 1 unit in eye space. Note that eye space z is related in a nonlinear way to |
---|
| 45 | // screen space z, so this is not just a constant. |
---|
| 46 | // ddepth below is how much screen space z at this point would change for that push. |
---|
| 47 | // NOTE: computation of ddepth likely differs from OpenGL's glPolygonOffset "unit" |
---|
| 48 | // computation, which is allowed to be vendor specific. |
---|
| 49 | float4 dpwdz = mul(uProjection, float4(0.0, 0.0, 1.0, 0.0)); |
---|
| 50 | float4 dpdz = (dpwdz - (postproj * dpwdz.w)) / In.pos.w; |
---|
| 51 | float ddepth = abs(dpdz.z); |
---|
| 52 | |
---|
| 53 | // -- End depth bias helper section -------------------------------- |
---|
| 54 | |
---|
| 55 | // We now compute the depth of the fragment. This is the actual depth value plus |
---|
| 56 | // our depth bias. The depth bias depends on how uncertain we are about the z value |
---|
| 57 | // plus some constant push in the z direction. The exact coefficients to use are |
---|
| 58 | // up to you, but at least it should be somewhat intuitive now what the tradeoffs are. |
---|
| 59 | float depthval = postproj.z /* + (0.5 * dzlen)*/ + (uDepthOffset * ddepth); |
---|
| 60 | depthval = (0.5 * depthval) + 0.5; // put into [0,1] range instead of [-1,1] |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | Out.color = float4(depthval, depthval * depthval, depthval, 0.0); |
---|
| 64 | return Out; |
---|
| 65 | } |
---|