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

Revision 2988, 2.8 KB checked in by mattausch, 16 years ago (diff)

eye linear depth not working

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  float4 norm: 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
67pixel fragtex(fragin IN,
68                          uniform sampler2D dirtTex,
69                          uniform float maxDepth,
70                          uniform sampler2D tex,
71                          uniform float3 currentPos)
72{
73        pixel pix;
74
75        float4 texColor = tex2D(tex, IN.texCoord.xy);
76
77        // save color in first render target
78        // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
79        pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor;
80       
81        // save world position in second render target
82        pix.pos = IN.worldPos * maxDepth;
83        // save world space normal in third rt
84        pix.norm.xyz = IN.normal;
85
86        // store projection coordinates with positions (used for ssao)
87        pix.norm.w = IN.projPos.w;
88        // write the depth
89        pix.pos.w = IN.mypos.z / IN.mypos.w;
90
91        // account for alpha blending
92        if (pix.col.w < 0.5f)
93                discard;
94
95        // hack: squeeze some information about ambient into the texture
96        //pix.col.w = glstate.material.emission.x;
97        pix.col.w = length((currentPos - IN.worldPos) * maxDepth);
98
99        return pix;
100}
101
102
103pixel frag(fragin IN, uniform float maxDepth, uniform float3 currentPos)
104{
105        pixel pix;
106
107        // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
108        pix.col = glstate.material.diffuse + glstate.material.emission;
109        pix.pos = IN.worldPos * maxDepth;
110
111        pix.norm.xyz = IN.normal;
112       
113        // store projection coordinates with positions (used for ssao)
114        pix.norm.w = IN.mypos.w;
115        // the projected depth
116        pix.pos.w = IN.mypos.z / IN.mypos.w;
117       
118        // hack: squeeze some information about the ambient term into the target
119        //pix.col.w = glstate.material.emission.x;
120        pix.col.w = length((currentPos - IN.worldPos) * maxDepth);
121
122        return pix;
123}
Note: See TracBrowser for help on using the repository browser.