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

Revision 3009, 3.7 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 float maxDepth,
81                          uniform sampler2D tex,
82                          uniform float3 eyePos,
83                          uniform float3 bl,
84                          uniform float3 br,
85                          uniform float3 tl,
86                          uniform float3 tr
87                          )
88{
89        float4 texColor = tex2D(tex, IN.texCoord.xy);
90
91        // account for alpha blending
92        if (texColor.w < 0.5f) discard;
93
94        pixel pix;
95
96        // save color in first render target
97        // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
98        pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor;
99        // save world space normal in third rt
100        pix.norm.xyz = IN.normal;
101        // store projection coordinates with positions (used for ssao)
102        pix.norm.w = IN.projPos.w;
103
104        const float4 projPos = IN.mypos / IN.mypos.w;
105       
106        // hack: squeeze some information about ambient into the texture
107        //pix.col.w = glstate.material.emission.x;
108
109        // compute eye linear depth
110        const float2 screenCoord = projPos.xy * 0.5f + 0.5f;
111        const float magView = length(Interpol(screenCoord, bl, br, tl, tr));
112        pix.col.w = length(eyePos - IN.worldPos.xyz) * maxDepth / magView;
113
114#if 0
115        // save world position in second render target
116        pix.pos = IN.worldPos.xyz * maxDepth;
117#endif
118
119        return pix;
120}
121
122
123pixel frag(fragin IN,
124                   uniform float maxDepth,
125                   uniform float3 eyePos,
126                   uniform float3 bl,
127                   uniform float3 br,
128                   uniform float3 tl,
129                   uniform float3 tr)
130{
131        pixel pix;
132
133        // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
134        pix.col = glstate.material.diffuse + glstate.material.emission;
135
136        pix.norm.xyz = IN.normal;
137       
138        // store projection coordinates with positions (used for ssao)
139        pix.norm.w = IN.mypos.w;
140
141        const float4 projPos = IN.mypos / IN.mypos.w;
142
143        // hack: squeeze some information about the ambient term into the target
144        //pix.col.w = glstate.material.emission.x;
145       
146        // compute eye linear depth and scale with lenght to avoid sqr root in pixel shader
147        float2 screenCoord = projPos.xy * 0.5f + 0.5f;
148        const float magView = length(Interpol(screenCoord, bl, br, tl, tr));
149        pix.col.w = length(eyePos - IN.worldPos.xyz) * maxDepth / magView;
150
151#if 0
152        pix.pos = IN.worldPos.xyz * maxDepth;
153#endif
154
155        return pix;
156}
Note: See TracBrowser for help on using the repository browser.