[3131] | 1 | /*******************************************/
|
---|
| 2 | /* Shader for normal mapped geoemtry */
|
---|
| 3 | /*******************************************/
|
---|
| 4 |
|
---|
| 5 | // input
|
---|
| 6 | struct vtxin
|
---|
| 7 | {
|
---|
| 8 | float4 position: POSITION;
|
---|
| 9 | float4 normal: NORMAL;
|
---|
[3144] | 10 | float4 color: COLOR0;
|
---|
[3131] | 11 | float4 texCoord: TEXCOORD0;
|
---|
| 12 | };
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | // vtx output
|
---|
| 16 | struct vtxout
|
---|
| 17 | {
|
---|
| 18 | float4 position: POSITION;
|
---|
| 19 | float4 texCoord: TEXCOORD0;
|
---|
| 20 |
|
---|
| 21 | // eye position
|
---|
| 22 | float4 eyePos: TEXCOORD1;
|
---|
| 23 | float4 normal: TEXCOORD2;
|
---|
| 24 |
|
---|
| 25 | float4 worldPos: TEXCOORD3;
|
---|
| 26 | float4 oldWorldPos: TEXCOORD4;
|
---|
[3153] | 27 | float4 tangent: TEXCOORD5;
|
---|
| 28 | float4 bitangent: TEXCOORD6;
|
---|
[3131] | 29 | };
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | // fragment input
|
---|
| 33 | struct fragin
|
---|
| 34 | {
|
---|
| 35 | float4 texCoord: TEXCOORD0;
|
---|
| 36 |
|
---|
| 37 | float4 winPos: WPOS;
|
---|
| 38 | // eye position
|
---|
| 39 | float4 eyePos: TEXCOORD1;
|
---|
| 40 | float4 normal: TEXCOORD2;
|
---|
| 41 | float4 worldPos: TEXCOORD3;
|
---|
| 42 | float4 oldWorldPos: TEXCOORD4;
|
---|
[3153] | 43 | float4 tangent: TEXCOORD5;
|
---|
| 44 | float4 bitangent: TEXCOORD6;
|
---|
[3131] | 45 | };
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | struct pixel
|
---|
| 49 | {
|
---|
| 50 | float4 color: COLOR0;
|
---|
| 51 | float3 normal: COLOR1;
|
---|
| 52 | float3 offsVec: COLOR2;
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | #pragma position_invariant vtx
|
---|
| 57 |
|
---|
| 58 | vtxout vtx(vtxin IN,
|
---|
| 59 | uniform float4x4 viewMatrix,
|
---|
| 60 | uniform float4x4 modelMatrix,
|
---|
| 61 | uniform float4x4 oldModelMatrix
|
---|
| 62 | )
|
---|
| 63 | {
|
---|
| 64 | vtxout OUT;
|
---|
| 65 |
|
---|
[3154] | 66 | float4 tangent = float4(IN.color.xyz * 2.0f - float3(1.0f), 1.0f);
|
---|
| 67 | //float4 tangent = IN.color;
|
---|
[3153] | 68 |
|
---|
| 69 | //float4 tangent = IN.color;
|
---|
[3131] | 70 | OUT.texCoord = IN.texCoord;
|
---|
| 71 |
|
---|
| 72 | // transform the vertex position into eye space
|
---|
| 73 | OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
|
---|
| 74 | // transform the vertex position into post projection space
|
---|
| 75 | OUT.position = mul(glstate.matrix.mvp, IN.position);
|
---|
| 76 | // transform the old vertex position into world space
|
---|
| 77 | OUT.worldPos = mul(modelMatrix, IN.position);
|
---|
| 78 | // transform the old vertex position into world space
|
---|
| 79 | OUT.oldWorldPos = mul(oldModelMatrix, IN.position);
|
---|
[3154] | 80 |
|
---|
[3131] | 81 | // the normal has to be correctly transformed with the inverse transpose
|
---|
[3154] | 82 | // convert to view space
|
---|
[3131] | 83 | OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal);
|
---|
[3154] | 84 | OUT.tangent = mul(glstate.matrix.invtrans.modelview[0], tangent);
|
---|
| 85 |
|
---|
| 86 | OUT.bitangent = float4(cross(OUT.tangent.xyz, OUT.normal.xyz), 1);
|
---|
| 87 | //OUT.bitangent = float4(cross(OUT.normal.xyz, OUT.tangent.xyz), 1);
|
---|
[3131] | 88 |
|
---|
| 89 | return OUT;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
[3144] | 93 |
|
---|
[3146] | 94 | /******************************************************************/
|
---|
| 95 | /* Shader computing the MRT output for normal mapped geometry */
|
---|
| 96 | /******************************************************************/
|
---|
[3144] | 97 |
|
---|
[3163] | 98 |
|
---|
[3153] | 99 | pixel frag(fragin IN,
|
---|
| 100 | uniform float4x4 viewMatrix,
|
---|
| 101 | uniform sampler2D normalMap: TEXUNIT0)
|
---|
[3131] | 102 | {
|
---|
| 103 | pixel pix;
|
---|
[3153] | 104 | // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
|
---|
| 105 | pix.color = glstate.material.diffuse + glstate.material.emission;
|
---|
[3131] | 106 |
|
---|
[3153] | 107 | // eye space depth
|
---|
[3131] | 108 | pix.color.w = length(IN.eyePos.xyz);
|
---|
| 109 | // the scene entity id
|
---|
| 110 | //pix.id = glstate.fog.color.xyz;
|
---|
| 111 | // the offset to the world pos from old frame
|
---|
| 112 | pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
|
---|
| 113 |
|
---|
[3154] | 114 | // compute tanget space to world space trafo =>
|
---|
| 115 | // transform basis vectors back into world space by multiplying with inverse view.
|
---|
| 116 | // since transforming normal with T means to
|
---|
[3131] | 117 | // multiply with the inverse transpose of T, we multiple with
|
---|
| 118 | // Transp(Inv(Inv(view))) = Transp(view)
|
---|
[3154] | 119 | const float3 normal = normalize(mul(transpose(viewMatrix), IN.normal).xyz);
|
---|
| 120 | const float3 tangent = normalize(mul(transpose(viewMatrix), IN.tangent).xyz);
|
---|
| 121 | const float3 bitangent = normalize(mul(transpose(viewMatrix), IN.bitangent).xyz);
|
---|
[3131] | 122 |
|
---|
| 123 | // compute tangent space - world space transform which is the transpose,
|
---|
| 124 | // as it is the inverse of a rotation matrix
|
---|
[3154] | 125 | float3x3 tangToWorldTrafo = transpose(float3x3(tangent, bitangent, normal));
|
---|
[3168] | 126 |
|
---|
[3195] | 127 | //const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz * 2.0f - float3(1.0f);
|
---|
| 128 | const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz;
|
---|
[3198] | 129 | //pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
|
---|
| 130 | pix.normal = normalize(mul(tangToWorldTrafo, tangentSpaceNorm));
|
---|
[3154] | 131 |
|
---|
[3131] | 132 | return pix;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
[3153] | 136 |
|
---|
| 137 | pixel fragtex(fragin IN,
|
---|
| 138 | uniform sampler2D tex: TEXUNIT0,
|
---|
| 139 | uniform float4x4 viewMatrix,
|
---|
| 140 | uniform sampler2D normalMap: TEXUNIT1
|
---|
| 141 | )
|
---|
[3131] | 142 | {
|
---|
[3153] | 143 | float4 texColor = tex2D(tex, IN.texCoord.xy);
|
---|
| 144 |
|
---|
| 145 | // account for alpha blending
|
---|
| 146 | if (texColor.w < 0.5f) discard;
|
---|
| 147 |
|
---|
[3131] | 148 | pixel pix;
|
---|
[3153] | 149 |
|
---|
| 150 | // save color in first render target
|
---|
| 151 | // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
|
---|
| 152 | pix.color = (glstate.material.emission + glstate.material.diffuse) * texColor;
|
---|
[3146] | 153 |
|
---|
[3153] | 154 | // compute eye linear depth
|
---|
[3144] | 155 | pix.color.w = length(IN.eyePos.xyz);
|
---|
[3153] | 156 |
|
---|
[3131] | 157 | // the scene entity id
|
---|
| 158 | //pix.id = glstate.fog.color.xyz;
|
---|
| 159 | // the offset to the world pos from old frame
|
---|
| 160 | pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
|
---|
| 161 |
|
---|
| 162 | // compute tanget space in world space => transform basis vectors back into world space by
|
---|
| 163 | // multiplying with inverse view. since transforming normal with T means to
|
---|
| 164 | // multiply with the inverse transpose of T, we multiple with
|
---|
| 165 | // Transp(Inv(Inv(view))) = Transp(view)
|
---|
| 166 | float3 normal = mul(transpose(viewMatrix), IN.normal).xyz;
|
---|
| 167 | float3 tangent = mul(transpose(viewMatrix), IN.tangent).xyz;
|
---|
| 168 | float3 bitangent = mul(transpose(viewMatrix), IN.bitangent).xyz;
|
---|
| 169 |
|
---|
| 170 | // compute tangent space - world space transform which is the transpose,
|
---|
| 171 | // as it is the inverse of a rotation matrix
|
---|
[3144] | 172 | float3x3 tangToWorldTrafo = transpose(float3x3(IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz));
|
---|
[3153] | 173 | const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz;
|
---|
[3195] | 174 | //pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
|
---|
| 175 | pix.normal = tangentSpaceNorm;
|
---|
[3152] | 176 |
|
---|
[3131] | 177 | return pix;
|
---|
[3153] | 178 | }
|
---|
| 179 |
|
---|