source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/normalMapping.cg @ 3163

Revision 3163, 6.3 KB checked in by mattausch, 16 years ago (diff)

getting better results using the position for the filter

Line 
1/*******************************************/
2/*    Shader for normal mapped geoemtry    */
3/*******************************************/
4
5// input
6struct vtxin
7{
8  float4 position: POSITION;
9  float4 normal: NORMAL;
10  float4 color: COLOR0;
11  float4 texCoord: TEXCOORD0;
12};
13
14
15// vtx output
16struct 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
33struct 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
48struct pixel
49{
50        float4 color: COLOR0;
51        float3 normal: COLOR1;
52        float3 offsVec: COLOR2;
53};
54
55
56#pragma position_invariant vtx
57
58vtxout 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
99pixel 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        //float3x3 tangToWorldTrafo = transpose(float3x3(bitangent, tangent, normal));
127        //float3x3 tangToWorldTrafo = transpose(float3x3(tangent, normal, bitangent));
128        //float3x3 tangToWorldTrafo = transpose(float3x3(normal, bitangent, tangent));
129        //float3x3 tangToWorldTrafo = transpose(float3x3(normal, tangent, bitangent));
130
131        const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz * 2.0f - float3(1.0f);
132        //const float3 tangentSpaceNorm = float3(0, 1, 0);
133        //const float3 tangentSpaceNorm = float3(0, 0, 1);
134       
135        pix.normal = normalize(mul(tangToWorldTrafo, tangentSpaceNorm));
136        //pix.normal = normalize(tangentSpaceNorm);
137        //pix.color.xyz = float3(abs(dot(tangent.xyz, normal.xyz)));
138        //pix.color.xyz = float3(abs(dot(bitangent.xyz, tangent.xyz)));
139        //pix.color.xyz = float3(abs(dot(normalize(IN.normal.xyz), normalize(IN.tangent.xyz))));
140        //pix.color.xyz = float3(abs(dot(IN.normal.xyz, IN.tangent.xyz)));
141        //pix.color.xyz = normal.xyz;
142        //pix.color.xyz = IN.tangent.xyz;
143        //pix.color.xyz = float3(IN.texCoord.x,0,0);
144        //pix.color.xyz = float3(0,IN.texCoord.y,0);
145       
146        return pix;
147}
148
149
150
151pixel fragtex(fragin IN,
152                          uniform sampler2D tex: TEXUNIT0,
153                          uniform float4x4 viewMatrix,
154                          uniform sampler2D normalMap: TEXUNIT1
155                          )
156{
157        float4 texColor = tex2D(tex, IN.texCoord.xy);
158
159        // account for alpha blending
160        if (texColor.w < 0.5f) discard;
161
162        pixel pix;
163
164        // save color in first render target
165        // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
166        pix.color = (glstate.material.emission + glstate.material.diffuse) * texColor;
167       
168        // compute eye linear depth
169        pix.color.w = length(IN.eyePos.xyz);
170
171        // the scene entity id
172        //pix.id = glstate.fog.color.xyz;
173        // the offset to the world pos from old frame
174        pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
175
176        // compute tanget space in world space => transform basis vectors back into world space by
177        // multiplying with inverse view. since transforming normal with T means to
178        // multiply with the inverse transpose of T, we multiple with
179        // Transp(Inv(Inv(view))) = Transp(view)
180        float3 normal = mul(transpose(viewMatrix), IN.normal).xyz;
181        float3 tangent = mul(transpose(viewMatrix), IN.tangent).xyz;
182        float3 bitangent = mul(transpose(viewMatrix), IN.bitangent).xyz;
183
184        // compute tangent space - world space transform which is the transpose,
185        // as it is the inverse of a rotation matrix
186        float3x3 tangToWorldTrafo = transpose(float3x3(IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz));
187        const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz;
188        pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
189
190        return pix;
191}
192
Note: See TracBrowser for help on using the repository browser.