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

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