source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/normalMappingFwd.cg @ 3154

Revision 3154, 6.1 KB checked in by mattausch, 16 years ago (diff)

normal mappingt: first working version

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 - 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
98pixel frag(fragin IN,
99                   uniform float4x4 viewMatrix,
100                   uniform sampler2D normalMap: TEXUNIT0)
101{
102        pixel pix;
103        // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
104        pix.color = glstate.material.diffuse + glstate.material.emission;
105       
106        // eye space depth
107        pix.color.w = length(IN.eyePos.xyz);
108        // the scene entity id
109        //pix.id = glstate.fog.color.xyz;
110        // the offset to the world pos from old frame
111        pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
112
113        // compute tanget space to world space trafo =>
114        // transform basis vectors back into world space by multiplying with inverse view.
115        // since transforming normal with T means to
116        // multiply with the inverse transpose of T, we multiple with
117        // Transp(Inv(Inv(view))) = Transp(view)
118        float3 normal = normalize(mul(transpose(viewMatrix), IN.normal).xyz);
119        float3 tangent = normalize(mul(transpose(viewMatrix), IN.tangent).xyz);
120        float3 bitangent = normalize(mul(transpose(viewMatrix), IN.bitangent).xyz);
121
122        // compute tangent space - world space transform which is the transpose,
123        // as it is the inverse of a rotation matrix
124        //float3x3 tangToWorldTrafo = transpose(float3x3(tangent, bitangent, normal));
125        //float3x3 tangToWorldTrafo = transpose(float3x3(bitangent, tangent, normal));
126        //float3x3 tangToWorldTrafo = transpose(float3x3(tangent, normal, bitangent));
127        //float3x3 tangToWorldTrafo = transpose(float3x3(normal, bitangent, tangent));
128        float3x3 tangToWorldTrafo = transpose(float3x3(normal, tangent, bitangent));
129
130        //const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz;
131        //const float3 tangentSpaceNorm = float3(0, 1, 0);
132        const float3 tangentSpaceNorm = float3(0, 0, 1);
133       
134        pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
135        //pix.color.xyz = float3(abs(dot(tangent.xyz, normal.xyz)));
136        //pix.color.xyz = float3(abs(dot(bitangent.xyz, tangent.xyz)));
137        //pix.color.xyz = float3(abs(dot(normalize(IN.normal.xyz), normalize(IN.tangent.xyz))));
138        //pix.color.xyz = float3(abs(dot(IN.normal.xyz, IN.tangent.xyz)));
139        //pix.color.xyz = normal.xyz;
140        //pix.color.xyz = IN.tangent.xyz;
141
142        return pix;
143}
144
145
146
147pixel fragtex(fragin IN,
148                          uniform sampler2D tex: TEXUNIT0,
149                          uniform float4x4 viewMatrix,
150                          uniform sampler2D normalMap: TEXUNIT1
151                          )
152{
153        float4 texColor = tex2D(tex, IN.texCoord.xy);
154
155        // account for alpha blending
156        if (texColor.w < 0.5f) discard;
157
158        pixel pix;
159
160        // save color in first render target
161        // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
162        pix.color = (glstate.material.emission + glstate.material.diffuse) * texColor;
163       
164        // compute eye linear depth
165        pix.color.w = length(IN.eyePos.xyz);
166
167        // the scene entity id
168        //pix.id = glstate.fog.color.xyz;
169        // the offset to the world pos from old frame
170        pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
171
172        // compute tanget space in world space => transform basis vectors back into world space by
173        // multiplying with inverse view. since transforming normal with T means to
174        // multiply with the inverse transpose of T, we multiple with
175        // Transp(Inv(Inv(view))) = Transp(view)
176        float3 normal = mul(transpose(viewMatrix), IN.normal).xyz;
177        float3 tangent = mul(transpose(viewMatrix), IN.tangent).xyz;
178        float3 bitangent = mul(transpose(viewMatrix), IN.bitangent).xyz;
179
180        // compute tangent space - world space transform which is the transpose,
181        // as it is the inverse of a rotation matrix
182        float3x3 tangToWorldTrafo = transpose(float3x3(IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz));
183        const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz;
184        pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
185
186        return pix;
187}
188
Note: See TracBrowser for help on using the repository browser.