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

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

added eye space depth

RevLine 
[2810]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
[2811]18  float3 normal: TEXCOORD2;
[2834]19  float4 mypos: TEXCOORD3;
[2810]20};
21
22
23// fragment input
24struct fragin
25{
[2891]26        float4 color: COLOR0; 
27        float4 position: POSITION; // eye space
28        float4 texCoord: TEXCOORD0;   
[2810]29
[2891]30        float4 projPos: WPOS;
31        float4 worldPos: TEXCOORD1; // world position
32        float3 normal: TEXCOORD2;
33        float4 mypos: TEXCOORD3;
[2810]34};
35
36
37struct pixel
38{
39  float4 col: COLOR0;
40  float4 pos: COLOR1;
[2822]41  float4 norm: COLOR2;
[2810]42};
43
[2951]44#pragma position_invariant vtx
[2810]45
[2818]46vtxout vtx(vtxin IN,
[2834]47                   const uniform float4x4 ModelViewProj,
[2952]48                   uniform float4x4 ModelView)
[2810]49{
[2952]50        vtxout OUT;
[2810]51
[2952]52        OUT.color = IN.color;
53        OUT.texCoord = IN.texCoord;
[2818]54
[2952]55        //OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);
56        OUT.worldPos = mul(ModelView, IN.position);
[2958]57        // transform the vertex position into eye space
58        OUT.position = mul(glstate.matrix.mvp, IN.position);
[2810]59
[2952]60        OUT.normal = IN.normal;
61        OUT.mypos = OUT.position;
62
63        return OUT;
[2810]64}
65
66
[2822]67pixel fragtex(fragin IN,
[2964]68                          uniform sampler2D dirtTex,
69                          uniform float maxDepth,
[2984]70                          uniform sampler2D tex,
71                          uniform float3 currentPos)
[2810]72{
[2865]73        pixel pix;
[2836]74
[2964]75        float4 texColor = tex2D(tex, IN.texCoord.xy);
76
[2866]77        // save color in first render target
[2974]78        // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
[2964]79        pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor;
[2867]80       
[2951]81        // save world position in second render target
[2865]82        pix.pos = IN.worldPos * maxDepth;
[2974]83        // save world space normal in third rt
[2867]84        pix.norm.xyz = IN.normal;
[2836]85
[2866]86        // store projection coordinates with positions (used for ssao)
[2974]87        pix.norm.w = IN.projPos.w;
88        // write the depth
89        pix.pos.w = IN.mypos.z / IN.mypos.w;
[2836]90
[2865]91        // account for alpha blending
92        if (pix.col.w < 0.5f)
93                discard;
[2810]94
[2974]95        // hack: squeeze some information about ambient into the texture
[2984]96        //pix.col.w = glstate.material.emission.x;
97        pix.col.w = length(currentPos - IN.worldPos);
[2865]98
99        return pix;
[2810]100}
[2819]101
102
[2984]103pixel frag(fragin IN, uniform float maxDepth, uniform float3 currentPos)
[2819]104{
[2865]105        pixel pix;
[2819]106
[2960]107        // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
108        pix.col = glstate.material.diffuse + glstate.material.emission;
[2865]109        pix.pos = IN.worldPos * maxDepth;
[2873]110
[2867]111        pix.norm.xyz = IN.normal;
[2873]112       
[2974]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       
[2867]118        // hack: squeeze some information about the ambient term into the target
[2984]119        //pix.col.w = glstate.material.emission.x;
120        pix.col.w = length(currentPos - IN.worldPos);
[2865]121
122        return pix;
[2928]123}
Note: See TracBrowser for help on using the repository browser.