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

Revision 2999, 3.6 KB checked in by mattausch, 16 years ago (diff)

trying to use smaller fbo

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 pos: COLOR1;
41  float2 pos: COLOR1;
42  float4 norm: COLOR2;
43};
44
45#pragma position_invariant vtx
46
47vtxout vtx(vtxin IN,
48                   const uniform float4x4 ModelViewProj,
49                   uniform float4x4 ModelView)
50{
51        vtxout OUT;
52
53        OUT.color = IN.color;
54        OUT.texCoord = IN.texCoord;
55
56        //OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);
57        OUT.worldPos = mul(ModelView, IN.position);
58        // transform the vertex position into eye space
59        OUT.position = mul(glstate.matrix.mvp, IN.position);
60
61        OUT.normal = IN.normal;
62        OUT.mypos = OUT.position;
63
64        return OUT;
65}
66
67
68// bilinear interpolation
69inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
70{
71        float3 x1 = lerp(bl, tl, w.y);
72        float3 x2 = lerp(br, tr, w.y);
73        float3 v = lerp(x1, x2, w.x);
74
75        return v;
76}
77
78
79pixel fragtex(fragin IN,
80                          uniform sampler2D dirtTex,
81                          uniform float maxDepth,
82                          uniform sampler2D tex,
83                          uniform float3 eyePos,
84                          uniform float3 bl,
85                          uniform float3 br,
86                          uniform float3 tl,
87                          uniform float3 tr)
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
100        // save world position in second render target
101        //pix.pos = IN.worldPos * maxDepth;
102        // save world space normal in third rt
103        pix.norm.xyz = IN.normal;
104
105        // store projection coordinates with positions (used for ssao)
106        pix.norm.w = IN.projPos.w;
107
108        const float4 projPos = IN.mypos / IN.mypos.w;
109        // store the projected depth
110        pix.pos.y = projPos.z;
111
112        float2 screenCoord = projPos.xy * 0.5f + 0.5f;
113        const float magView = length(Interpol(screenCoord, bl, br, tl, tr));
114
115        // hack: squeeze some information about ambient into the texture
116        //pix.col.w = glstate.material.emission.x;
117
118        // eye linear depth
119        pix.col.w = (length(eyePos - IN.worldPos.xyz) * maxDepth) / magView;
120        pix.pos.x = pix.col.x;
121
122        return pix;
123}
124
125
126pixel frag(fragin IN,
127                   uniform float maxDepth,
128                   uniform float3 eyePos,
129                   uniform float3 bl,
130                   uniform float3 br,
131                   uniform float3 tl,
132                   uniform float3 tr)
133{
134        pixel pix;
135
136        // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
137        pix.col = glstate.material.diffuse + glstate.material.emission;
138        //pix.pos = IN.worldPos * maxDepth;
139
140        pix.norm.xyz = IN.normal;
141       
142        // store projection coordinates with positions (used for ssao)
143        pix.norm.w = IN.mypos.w;
144
145        const float4 projPos = IN.mypos / IN.mypos.w;
146        // store the projected depth
147        pix.pos.y = projPos.z;
148
149        float2 screenCoord = projPos.xy * 0.5f + 0.5f;
150        const float magView = length(Interpol(screenCoord, bl, br, tl, tr));
151
152        // hack: squeeze some information about the ambient term into the target
153        //pix.col.w = glstate.material.emission.x;
154        pix.col.w = (length(eyePos - IN.worldPos.xyz) * maxDepth) / magView;
155        pix.pos.x = pix.col.x;
156
157        return pix;
158}
Note: See TracBrowser for help on using the repository browser.