source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg @ 3126

Revision 3126, 3.3 KB checked in by mattausch, 16 years ago (diff)

started to include normal mapping

Line 
1// input
2struct vtxin
3{
4  float4 position: POSITION;
5  float4 normal: NORMAL;
6  float4 color: COLOR0;
7  float4 texCoord: TEXCOORD0;
8};
9
10
11// vtx output
12struct vtxout
13{
14        float4 position: POSITION;
15        float4 texCoord: TEXCOORD0;   
16
17        float4 color: COLOR0; 
18         // eye position
19        float4 eyePos: TEXCOORD1;
20        float4 normal: TEXCOORD2;
21        float4 worldPos: TEXCOORD3;
22        float4 oldWorldPos: TEXCOORD4;
23};
24
25
26// fragment input
27struct fragin
28{
29        float4 color: COLOR0; 
30        float4 texCoord: TEXCOORD0;   
31
32        float4 winPos: WPOS;
33        // eye position
34        float4 eyePos: TEXCOORD1;
35        float4 normal: TEXCOORD2;
36        float4 worldPos: TEXCOORD3;
37        float4 oldWorldPos: TEXCOORD4;
38};
39
40
41struct pixel
42{
43        float4 col: COLOR0;
44        float3 norm: COLOR1;
45        float3 offsVec: COLOR2;
46};
47
48
49#pragma position_invariant vtx
50
51vtxout vtx(vtxin IN,
52                   uniform float4x4 viewMatrix,
53                   uniform float4x4 modelMatrix,
54                   uniform float4x4 oldModelMatrix)
55{
56        vtxout OUT;
57
58        OUT.color = IN.color;
59        OUT.texCoord = IN.texCoord;
60
61        // transform the vertex position into eye space
62        OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
63        // transform the vertex position into post projection space
64        OUT.position = mul(glstate.matrix.mvp, IN.position);
65        // transform the old vertex position into world space
66        OUT.worldPos = mul(modelMatrix, IN.position);
67        // transform the old vertex position into world space
68        OUT.oldWorldPos = mul(oldModelMatrix, IN.position);
69        // the normal has to be correctly transformed with the inverse transpose
70        OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal);
71
72        return OUT;
73}
74
75
76pixel fragtex(fragin IN,
77                          uniform sampler2D tex: TEXUNIT0,
78                          uniform float4x4 viewMatrix
79                          )
80{
81        float4 texColor = tex2D(tex, IN.texCoord.xy);
82
83        // account for alpha blending
84        if (texColor.w < 0.5f) discard;
85
86        pixel pix;
87
88        // save color in first render target
89        // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
90        pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor;
91        // save world space normal in rt => transform back into world space by
92        // multiplying with inverse view. since transforming normal with T means to
93        // multiply with the inverse transpose of T, we multiple with
94        // Transp(Inv(Inv(view))) = Transp(view)
95        pix.norm = mul(transpose(viewMatrix), IN.normal).xyz;
96        // compute eye linear depth
97        pix.col.w = length(IN.eyePos.xyz);
98
99        // the scene entity id
100        //pix.id = glstate.fog.color.xyz;
101        // the offset to the world pos from old frame
102        pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
103
104        return pix;
105}
106
107
108pixel frag(fragin IN, uniform float4x4 viewMatrix)
109{
110        pixel pix;
111        // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
112        pix.col = glstate.material.diffuse + glstate.material.emission;
113        // save world space normal in rt => transform back into world space by
114        // multiplying with inverse view. since transforming normal with T means to
115        // multiply with the inverse transpose of T, we multiple with Transp(Inv(Inv(view))) = Transp(view)
116        pix.norm = mul(transpose(viewMatrix), IN.normal).xyz;
117        // eye space depth
118        pix.col.w = length(IN.eyePos.xyz);
119        // the scene entity id
120        //pix.id = glstate.fog.color.xyz;
121        // the offset to the world pos from old frame
122        pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
123
124        return pix;
125}
Note: See TracBrowser for help on using the repository browser.