[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 |
|
---|
[3144] | 21 | float4 tangent: TEXCOORD6;
|
---|
[3131] | 22 | // eye position
|
---|
| 23 | float4 eyePos: TEXCOORD1;
|
---|
| 24 | float4 normal: TEXCOORD2;
|
---|
| 25 |
|
---|
| 26 | float4 worldPos: TEXCOORD3;
|
---|
| 27 | float4 oldWorldPos: TEXCOORD4;
|
---|
[3144] | 28 | float4 bitangent: TEXCOORD5;
|
---|
[3131] | 29 | };
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | // fragment input
|
---|
| 33 | struct fragin
|
---|
| 34 | {
|
---|
[3144] | 35 | float4 tangent: TEXCOORD6;
|
---|
[3131] | 36 | float4 texCoord: TEXCOORD0;
|
---|
| 37 |
|
---|
| 38 | float4 winPos: WPOS;
|
---|
| 39 | // eye position
|
---|
| 40 | float4 eyePos: TEXCOORD1;
|
---|
| 41 | float4 normal: TEXCOORD2;
|
---|
| 42 | float4 worldPos: TEXCOORD3;
|
---|
| 43 | float4 oldWorldPos: TEXCOORD4;
|
---|
[3144] | 44 | float4 bitangent: TEXCOORD5;
|
---|
[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 |
|
---|
[3144] | 66 | OUT.tangent = IN.color;
|
---|
[3131] | 67 | OUT.texCoord = IN.texCoord;
|
---|
| 68 |
|
---|
| 69 | // transform the vertex position into eye space
|
---|
| 70 | OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
|
---|
| 71 | // transform the vertex position into post projection space
|
---|
| 72 | OUT.position = mul(glstate.matrix.mvp, IN.position);
|
---|
| 73 | // transform the old vertex position into world space
|
---|
| 74 | OUT.worldPos = mul(modelMatrix, IN.position);
|
---|
| 75 | // transform the old vertex position into world space
|
---|
| 76 | OUT.oldWorldPos = mul(oldModelMatrix, IN.position);
|
---|
| 77 | // the normal has to be correctly transformed with the inverse transpose
|
---|
| 78 | OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal);
|
---|
| 79 | OUT.tangent = mul(glstate.matrix.invtrans.modelview[0], IN.color);
|
---|
| 80 |
|
---|
[3144] | 81 | OUT.bitangent = float4(cross(IN.color.xyz, IN.normal.xyz), 1);
|
---|
[3131] | 82 |
|
---|
| 83 | return OUT;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
[3144] | 87 |
|
---|
[3146] | 88 | /******************************************************************/
|
---|
| 89 | /* Shader computing the MRT output for normal mapped geometry */
|
---|
| 90 | /******************************************************************/
|
---|
[3144] | 91 |
|
---|
| 92 |
|
---|
[3131] | 93 | pixel fragtex(fragin IN,
|
---|
| 94 | uniform sampler2D tex: TEXUNIT0,
|
---|
| 95 | uniform float4x4 viewMatrix,
|
---|
| 96 | uniform sampler2D normalMap: TEXUNIT1
|
---|
| 97 | )
|
---|
| 98 | {
|
---|
| 99 | float4 texColor = tex2D(tex, IN.texCoord.xy);
|
---|
| 100 |
|
---|
| 101 | // account for alpha blending
|
---|
| 102 | if (texColor.w < 0.5f) discard;
|
---|
| 103 |
|
---|
| 104 | pixel pix;
|
---|
| 105 |
|
---|
| 106 | // save color in first render target
|
---|
| 107 | // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
|
---|
| 108 | pix.color = (glstate.material.emission + glstate.material.diffuse) * texColor;
|
---|
| 109 |
|
---|
| 110 | // compute eye linear depth
|
---|
| 111 | pix.color.w = length(IN.eyePos.xyz);
|
---|
| 112 |
|
---|
| 113 | // the scene entity id
|
---|
| 114 | //pix.id = glstate.fog.color.xyz;
|
---|
| 115 | // the offset to the world pos from old frame
|
---|
| 116 | pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
|
---|
| 117 |
|
---|
| 118 | // compute tanget space in world space => transform basis vectors back into world space by
|
---|
| 119 | // multiplying with inverse view. since transforming normal with T means to
|
---|
| 120 | // multiply with the inverse transpose of T, we multiple with
|
---|
| 121 | // Transp(Inv(Inv(view))) = Transp(view)
|
---|
| 122 | float3 normal = mul(transpose(viewMatrix), IN.normal).xyz;
|
---|
| 123 | float3 tangent = mul(transpose(viewMatrix), IN.tangent).xyz;
|
---|
| 124 | float3 bitangent = mul(transpose(viewMatrix), IN.bitangent).xyz;
|
---|
| 125 |
|
---|
| 126 | // compute tangent space - world space transform which is the transpose,
|
---|
| 127 | // as it is the inverse of a rotation matrix
|
---|
[3146] | 128 | float3x3 tangToWorldTrafo = transpose(float3x3(IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz));
|
---|
[3144] | 129 | const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz;
|
---|
[3131] | 130 | pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
|
---|
| 131 |
|
---|
| 132 | return pix;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
[3144] | 136 | pixel frag(fragin IN,
|
---|
| 137 | uniform float4x4 viewMatrix,
|
---|
[3146] | 138 | uniform sampler2D normalMap: TEXUNIT0)
|
---|
[3131] | 139 | {
|
---|
| 140 | pixel pix;
|
---|
| 141 | // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
|
---|
[3144] | 142 | pix.color = glstate.material.diffuse + glstate.material.emission;
|
---|
[3146] | 143 |
|
---|
[3131] | 144 | // eye space depth
|
---|
[3144] | 145 | pix.color.w = length(IN.eyePos.xyz);
|
---|
[3131] | 146 | // the scene entity id
|
---|
| 147 | //pix.id = glstate.fog.color.xyz;
|
---|
| 148 | // the offset to the world pos from old frame
|
---|
| 149 | pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
|
---|
| 150 |
|
---|
| 151 | // compute tanget space in world space => transform basis vectors back into world space by
|
---|
| 152 | // multiplying with inverse view. since transforming normal with T means to
|
---|
| 153 | // multiply with the inverse transpose of T, we multiple with
|
---|
| 154 | // Transp(Inv(Inv(view))) = Transp(view)
|
---|
| 155 | float3 normal = mul(transpose(viewMatrix), IN.normal).xyz;
|
---|
| 156 | float3 tangent = mul(transpose(viewMatrix), IN.tangent).xyz;
|
---|
| 157 | float3 bitangent = mul(transpose(viewMatrix), IN.bitangent).xyz;
|
---|
| 158 |
|
---|
| 159 | // compute tangent space - world space transform which is the transpose,
|
---|
| 160 | // as it is the inverse of a rotation matrix
|
---|
[3144] | 161 | float3x3 tangToWorldTrafo = transpose(float3x3(IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz));
|
---|
| 162 | const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz;
|
---|
[3131] | 163 | pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
|
---|
[3146] | 164 | pix.normal = tangentSpaceNorm;
|
---|
[3131] | 165 | return pix;
|
---|
| 166 | } |
---|