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

Revision 3146, 5.1 KB checked in by mattausch, 16 years ago (diff)

normal mapping hack not working yet. found problems with ssao if the geometry is not tesselated enough (especially with smoothed
normals: one can see the underlying tesselation!

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        float4 tangent: TEXCOORD6; 
22         // eye position
23        float4 eyePos: TEXCOORD1;
24        float4 normal: TEXCOORD2;
25
26        float4 worldPos: TEXCOORD3;
27        float4 oldWorldPos: TEXCOORD4;
28        float4 bitangent: TEXCOORD5;
29};
30
31
32// fragment input
33struct fragin
34{
35        float4 tangent: TEXCOORD6; 
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;
44        float4 bitangent: TEXCOORD5;
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        OUT.tangent = IN.color;
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
81        OUT.bitangent = float4(cross(IN.color.xyz, IN.normal.xyz), 1);
82
83        return OUT;
84}
85
86
87
88/******************************************************************/
89/*   Shader computing the MRT output for normal mapped geometry   */
90/******************************************************************/
91
92
93pixel 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
128        float3x3 tangToWorldTrafo = transpose(float3x3(IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz));
129        const float3 tangentSpaceNorm = tex2Dlod(normalMap, float4(IN.texCoord.xy, 0, 0)).xyz;
130        pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
131
132        return pix;
133}
134
135
136pixel frag(fragin IN,
137                   uniform float4x4 viewMatrix,
138                   uniform sampler2D normalMap: TEXUNIT0)
139{
140        pixel pix;
141        // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
142        pix.color = glstate.material.diffuse + glstate.material.emission;
143       
144        // eye space depth
145        pix.color.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        // 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
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;
163        pix.normal = mul(tangToWorldTrafo, tangentSpaceNorm);
164        pix.normal = tangentSpaceNorm;
165        return pix;
166}
Note: See TracBrowser for help on using the repository browser.