1 | /*******************************************/
|
---|
2 | /* Shader for normal mapped geoemtry */
|
---|
3 | /*******************************************/
|
---|
4 |
|
---|
5 | // input
|
---|
6 | struct vtxin
|
---|
7 | {
|
---|
8 | float4 position: POSITION;
|
---|
9 | float4 normal: NORMAL;
|
---|
10 | float4 color: COLOR0;
|
---|
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;
|
---|
27 | float4 tangent: TEXCOORD5;
|
---|
28 | float4 bitangent: TEXCOORD6;
|
---|
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;
|
---|
43 | float4 tangent: TEXCOORD5;
|
---|
44 | float4 bitangent: TEXCOORD6;
|
---|
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 |
|
---|
66 | float4 tangent = float4(IN.color.xyz * 2.0f - float3(1.0f), 1.0f);
|
---|
67 | //float4 tangent = IN.color;
|
---|
68 |
|
---|
69 | //float4 tangent = IN.color;
|
---|
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);
|
---|
80 |
|
---|
81 | // the normal has to be correctly transformed with the inverse transpose
|
---|
82 | // convert to view space
|
---|
83 | OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal);
|
---|
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);
|
---|
88 |
|
---|
89 | return OUT;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | /******************************************************************/
|
---|
95 | /* Shader computing the MRT output for normal mapped geometry */
|
---|
96 | /******************************************************************/
|
---|
97 |
|
---|
98 |
|
---|
99 | pixel frag(fragin IN,
|
---|
100 | uniform float4x4 viewMatrix,
|
---|
101 | uniform sampler2D normalMap: TEXUNIT0)
|
---|
102 | {
|
---|
103 | pixel pix;
|
---|
104 | // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
|
---|
105 | pix.color = glstate.material.diffuse + glstate.material.emission;
|
---|
106 |
|
---|
107 | // eye space depth
|
---|
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 |
|
---|
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
|
---|
117 | // multiply with the inverse transpose of T, we multiple with
|
---|
118 | // Transp(Inv(Inv(view))) = Transp(view)
|
---|
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);
|
---|
122 |
|
---|
123 | // compute tangent space - world space transform which is the transpose,
|
---|
124 | // as it is the inverse of a rotation matrix
|
---|
125 | float3x3 tangToWorldTrafo = transpose(float3x3(tangent, bitangent, normal));
|
---|
126 |
|
---|
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;
|
---|
129 | //pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
|
---|
130 | pix.normal = normalize(mul(tangToWorldTrafo, tangentSpaceNorm));
|
---|
131 |
|
---|
132 | return pix;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 |
|
---|
137 | pixel fragtex(fragin IN,
|
---|
138 | uniform sampler2D tex: TEXUNIT0,
|
---|
139 | uniform float4x4 viewMatrix,
|
---|
140 | uniform sampler2D normalMap: TEXUNIT1
|
---|
141 | )
|
---|
142 | {
|
---|
143 | float4 texColor = tex2D(tex, IN.texCoord.xy);
|
---|
144 |
|
---|
145 | // account for alpha blending
|
---|
146 | if (texColor.w < 0.5f) discard;
|
---|
147 |
|
---|
148 | pixel pix;
|
---|
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;
|
---|
153 |
|
---|
154 | // compute eye linear depth
|
---|
155 | pix.color.w = length(IN.eyePos.xyz);
|
---|
156 |
|
---|
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
|
---|
172 | float3x3 tangToWorldTrafo = transpose(float3x3(IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz));
|
---|
173 | const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz;
|
---|
174 | //pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
|
---|
175 | pix.normal = tangentSpaceNorm;
|
---|
176 |
|
---|
177 | return pix;
|
---|
178 | }
|
---|
179 |
|
---|