[2810] | 1 | // input |
---|
| 2 | struct vtxin |
---|
| 3 | { |
---|
| 4 | float4 position: POSITION; |
---|
| 5 | float3 normal: NORMAL; |
---|
| 6 | float4 color: COLOR0; |
---|
| 7 | float4 texCoord: TEXCOORD0; |
---|
| 8 | }; |
---|
| 9 | |
---|
| 10 | // vtx output |
---|
| 11 | struct vtxout |
---|
| 12 | { |
---|
| 13 | float4 position: POSITION; // eye space |
---|
| 14 | float4 texCoord: TEXCOORD0; |
---|
| 15 | |
---|
| 16 | float4 color: COLOR0; |
---|
| 17 | float4 worldPos: TEXCOORD1; // world position |
---|
[2811] | 18 | float3 normal: TEXCOORD2; |
---|
[2834] | 19 | float4 mypos: TEXCOORD3; |
---|
[2810] | 20 | }; |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | // fragment input |
---|
| 24 | struct fragin |
---|
| 25 | { |
---|
[2891] | 26 | float4 color: COLOR0; |
---|
| 27 | float4 position: POSITION; // eye space |
---|
| 28 | float4 texCoord: TEXCOORD0; |
---|
[2810] | 29 | |
---|
[2891] | 30 | float4 projPos: WPOS; |
---|
| 31 | float4 worldPos: TEXCOORD1; // world position |
---|
| 32 | float3 normal: TEXCOORD2; |
---|
| 33 | float4 mypos: TEXCOORD3; |
---|
[2810] | 34 | }; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | struct pixel |
---|
| 38 | { |
---|
| 39 | float4 col: COLOR0; |
---|
[2999] | 40 | //float4 pos: COLOR1; |
---|
| 41 | float2 pos: COLOR1; |
---|
[2822] | 42 | float4 norm: COLOR2; |
---|
[2810] | 43 | }; |
---|
| 44 | |
---|
[2951] | 45 | #pragma position_invariant vtx |
---|
[2810] | 46 | |
---|
[2818] | 47 | vtxout vtx(vtxin IN, |
---|
[2834] | 48 | const uniform float4x4 ModelViewProj, |
---|
[2952] | 49 | uniform float4x4 ModelView) |
---|
[2810] | 50 | { |
---|
[2952] | 51 | vtxout OUT; |
---|
[2810] | 52 | |
---|
[2952] | 53 | OUT.color = IN.color; |
---|
| 54 | OUT.texCoord = IN.texCoord; |
---|
[2818] | 55 | |
---|
[2952] | 56 | //OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position); |
---|
| 57 | OUT.worldPos = mul(ModelView, IN.position); |
---|
[2958] | 58 | // transform the vertex position into eye space |
---|
| 59 | OUT.position = mul(glstate.matrix.mvp, IN.position); |
---|
[2810] | 60 | |
---|
[2952] | 61 | OUT.normal = IN.normal; |
---|
| 62 | OUT.mypos = OUT.position; |
---|
| 63 | |
---|
| 64 | return OUT; |
---|
[2810] | 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
[2999] | 68 | // bilinear interpolation |
---|
| 69 | inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
|
---|
| 70 | {
|
---|
| 71 | float3 x1 = lerp(bl, tl, w.y);
|
---|
| 72 | float3 x2 = lerp(br, tr, w.y);
|
---|
| 73 | float3 v = lerp(x1, x2, w.x);
|
---|
| 74 |
|
---|
| 75 | return v;
|
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
[2822] | 79 | pixel fragtex(fragin IN, |
---|
[2964] | 80 | uniform sampler2D dirtTex, |
---|
| 81 | uniform float maxDepth, |
---|
[2984] | 82 | uniform sampler2D tex, |
---|
[2999] | 83 | uniform float3 eyePos, |
---|
| 84 | uniform float3 bl,
|
---|
| 85 | uniform float3 br,
|
---|
| 86 | uniform float3 tl, |
---|
| 87 | uniform float3 tr) |
---|
[2810] | 88 | { |
---|
[2999] | 89 | float4 texColor = tex2D(tex, IN.texCoord.xy); |
---|
| 90 | |
---|
| 91 | // account for alpha blending |
---|
| 92 | if (texColor.w < 0.5f) discard; |
---|
| 93 | |
---|
[2865] | 94 | pixel pix; |
---|
[2836] | 95 | |
---|
[2866] | 96 | // save color in first render target |
---|
[2974] | 97 | // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term) |
---|
[2964] | 98 | pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor; |
---|
[2999] | 99 | |
---|
[2951] | 100 | // save world position in second render target |
---|
[2999] | 101 | //pix.pos = IN.worldPos * maxDepth; |
---|
[2974] | 102 | // save world space normal in third rt |
---|
[2867] | 103 | pix.norm.xyz = IN.normal; |
---|
[2836] | 104 | |
---|
[2866] | 105 | // store projection coordinates with positions (used for ssao) |
---|
[2974] | 106 | pix.norm.w = IN.projPos.w; |
---|
[2836] | 107 | |
---|
[2999] | 108 | const float4 projPos = IN.mypos / IN.mypos.w; |
---|
| 109 | // store the projected depth |
---|
| 110 | pix.pos.y = projPos.z; |
---|
[2810] | 111 | |
---|
[2999] | 112 | float2 screenCoord = projPos.xy * 0.5f + 0.5f; |
---|
| 113 | const float magView = length(Interpol(screenCoord, bl, br, tl, tr)); |
---|
| 114 | |
---|
[2974] | 115 | // hack: squeeze some information about ambient into the texture |
---|
[2984] | 116 | //pix.col.w = glstate.material.emission.x; |
---|
[2865] | 117 | |
---|
[2999] | 118 | // eye linear depth |
---|
| 119 | pix.col.w = (length(eyePos - IN.worldPos.xyz) * maxDepth) / magView; |
---|
| 120 | pix.pos.x = pix.col.x; |
---|
| 121 | |
---|
[2865] | 122 | return pix; |
---|
[2810] | 123 | } |
---|
[2819] | 124 | |
---|
| 125 | |
---|
[2999] | 126 | pixel frag(fragin IN, |
---|
| 127 | uniform float maxDepth, |
---|
| 128 | uniform float3 eyePos, |
---|
| 129 | uniform float3 bl,
|
---|
| 130 | uniform float3 br, |
---|
| 131 | uniform float3 tl, |
---|
| 132 | uniform float3 tr) |
---|
[2819] | 133 | { |
---|
[2865] | 134 | pixel pix; |
---|
[2819] | 135 | |
---|
[2960] | 136 | // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term) |
---|
| 137 | pix.col = glstate.material.diffuse + glstate.material.emission; |
---|
[2999] | 138 | //pix.pos = IN.worldPos * maxDepth; |
---|
[2873] | 139 | |
---|
[2867] | 140 | pix.norm.xyz = IN.normal; |
---|
[2873] | 141 | |
---|
[2974] | 142 | // store projection coordinates with positions (used for ssao) |
---|
| 143 | pix.norm.w = IN.mypos.w; |
---|
[2999] | 144 | |
---|
| 145 | const float4 projPos = IN.mypos / IN.mypos.w; |
---|
| 146 | // store the projected depth |
---|
| 147 | pix.pos.y = projPos.z; |
---|
| 148 | |
---|
| 149 | float2 screenCoord = projPos.xy * 0.5f + 0.5f; |
---|
| 150 | const float magView = length(Interpol(screenCoord, bl, br, tl, tr)); |
---|
| 151 | |
---|
[2867] | 152 | // hack: squeeze some information about the ambient term into the target |
---|
[2984] | 153 | //pix.col.w = glstate.material.emission.x; |
---|
[2999] | 154 | pix.col.w = (length(eyePos - IN.worldPos.xyz) * maxDepth) / magView; |
---|
| 155 | pix.pos.x = pix.col.x; |
---|
[2865] | 156 | |
---|
| 157 | return pix; |
---|
[2928] | 158 | } |
---|