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

Revision 3016, 3.6 KB checked in by mattausch, 16 years ago (diff)
Line 
1// input
2struct vtxin
3{
4  float4 position: POSITION;
5  float3 normal: NORMAL;
6  float4 color: COLOR0;
7  float4 texCoord: TEXCOORD0;
8};
9
10// vtx output
11struct vtxout
12{
13  float4 position: POSITION; // eye space
14  float4 texCoord: TEXCOORD0;   
15
16  float4 color: COLOR0; 
17  float4 worldPos: TEXCOORD1; // world position
18  float3 normal: TEXCOORD2;
19  float4 mypos: TEXCOORD3;
20};
21
22
23// fragment input
24struct fragin
25{
26        float4 color: COLOR0; 
27        float4 position: POSITION; // eye space
28        float4 texCoord: TEXCOORD0;   
29
30        float4 projPos: WPOS;
31        float4 worldPos: TEXCOORD1; // world position
32        float3 normal: TEXCOORD2;
33        float4 mypos: TEXCOORD3;
34};
35
36
37struct pixel
38{
39        float4 col: COLOR0;
40        float4 norm: COLOR1;
41        float3 pos: COLOR2;
42};
43
44#pragma position_invariant vtx
45
46vtxout vtx(vtxin IN,
47                   const uniform float4x4 ModelViewProj,
48                   uniform float4x4 ModelView)
49{
50        vtxout OUT;
51
52        OUT.color = IN.color;
53        OUT.texCoord = IN.texCoord;
54
55        //OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);
56        OUT.worldPos = mul(ModelView, IN.position);
57        // transform the vertex position into eye space
58        OUT.position = mul(glstate.matrix.mvp, IN.position);
59
60        OUT.normal = IN.normal;
61        OUT.mypos = OUT.position;
62
63        return OUT;
64}
65
66
67// bilinear interpolation
68inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
69{
70        float3 x1 = lerp(bl, tl, w.y);
71        float3 x2 = lerp(br, tr, w.y);
72        float3 v = lerp(x1, x2, w.x);
73
74        return v;
75}
76
77
78pixel fragtex(fragin IN,
79                          uniform sampler2D dirtTex,
80                          uniform sampler2D tex,
81                          uniform float3 eyePos,
82                          uniform float3 bl,
83                          uniform float3 br,
84                          uniform float3 tl,
85                          uniform float3 tr
86                          )
87{
88        float4 texColor = tex2D(tex, IN.texCoord.xy);
89
90        // account for alpha blending
91        if (texColor.w < 0.5f) discard;
92
93        pixel pix;
94
95        // save color in first render target
96        // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
97        pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor;
98        // save world space normal in third rt
99        pix.norm.xyz = IN.normal;
100        // store projection coordinates with positions (used for ssao)
101        pix.norm.w = IN.projPos.w;
102
103        const float4 projPos = IN.mypos / IN.mypos.w;
104       
105        // hack: squeeze some information about ambient into the texture
106        //pix.col.w = glstate.material.emission.x;
107
108        // compute eye linear depth
109        const float2 screenCoord = projPos.xy * 0.5f + 0.5f;
110        const float magView = length(Interpol(screenCoord, bl, br, tl, tr));
111        pix.col.w = length(eyePos - IN.worldPos.xyz) / magView;
112
113#if 0
114        // save world position in second render target
115        pix.pos = IN.worldPos.xyz * maxDepth;
116#endif
117
118        return pix;
119}
120
121
122pixel frag(fragin IN,
123                   uniform float3 eyePos,
124                   uniform float3 bl,
125                   uniform float3 br,
126                   uniform float3 tl,
127                   uniform float3 tr)
128{
129        pixel pix;
130
131        // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
132        pix.col = glstate.material.diffuse + glstate.material.emission;
133
134        pix.norm.xyz = IN.normal;
135       
136        // store projection coordinates with positions (used for ssao)
137        pix.norm.w = IN.mypos.w;
138
139        const float4 projPos = IN.mypos / IN.mypos.w;
140
141        // hack: squeeze some information about the ambient term into the target
142        //pix.col.w = glstate.material.emission.x;
143       
144        // compute eye linear depth and scale with lenght to avoid sqr root in pixel shader
145        float2 screenCoord = projPos.xy * 0.5f + 0.5f;
146        const float magView = length(Interpol(screenCoord, bl, br, tl, tr));
147        pix.col.w = length(eyePos - IN.worldPos.xyz)  / magView;
148
149#if 0
150        pix.pos = IN.worldPos.xyz * maxDepth;
151#endif
152
153        return pix;
154}
Note: See TracBrowser for help on using the repository browser.