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

Revision 3126, 3.3 KB checked in by mattausch, 16 years ago (diff)

started to include normal mapping

RevLine 
[3004]1// input
2struct vtxin
3{
4  float4 position: POSITION;
[3045]5  float4 normal: NORMAL;
[3004]6  float4 color: COLOR0;
7  float4 texCoord: TEXCOORD0;
8};
9
[3041]10
[3004]11// vtx output
12struct vtxout
13{
[3041]14        float4 position: POSITION;
15        float4 texCoord: TEXCOORD0;   
[3004]16
[3041]17        float4 color: COLOR0; 
[3117]18         // eye position
19        float4 eyePos: TEXCOORD1;
[3045]20        float4 normal: TEXCOORD2;
[3113]21        float4 worldPos: TEXCOORD3;
22        float4 oldWorldPos: TEXCOORD4;
[3004]23};
24
25
26// fragment input
27struct fragin
28{
29        float4 color: COLOR0; 
30        float4 texCoord: TEXCOORD0;   
31
[3017]32        float4 winPos: WPOS;
[3117]33        // eye position
34        float4 eyePos: TEXCOORD1;
[3045]35        float4 normal: TEXCOORD2;
[3113]36        float4 worldPos: TEXCOORD3;
37        float4 oldWorldPos: TEXCOORD4;
[3004]38};
39
40
41struct pixel
42{
[3005]43        float4 col: COLOR0;
[3017]44        float3 norm: COLOR1;
[3113]45        float3 offsVec: COLOR2;
[3004]46};
47
[3034]48
[3092]49#pragma position_invariant vtx
[3004]50
[3113]51vtxout vtx(vtxin IN,
52                   uniform float4x4 viewMatrix,
53                   uniform float4x4 modelMatrix,
54                   uniform float4x4 oldModelMatrix)
[3004]55{
56        vtxout OUT;
57
58        OUT.color = IN.color;
59        OUT.texCoord = IN.texCoord;
60
61        // transform the vertex position into eye space
[3034]62        OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
63        // transform the vertex position into post projection space
[3004]64        OUT.position = mul(glstate.matrix.mvp, IN.position);
[3113]65        // transform the old vertex position into world space
66        OUT.worldPos = mul(modelMatrix, IN.position);
67        // transform the old vertex position into world space
68        OUT.oldWorldPos = mul(oldModelMatrix, IN.position);
[3118]69        // the normal has to be correctly transformed with the inverse transpose
70        OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal);
71
[3004]72        return OUT;
73}
74
75
76pixel fragtex(fragin IN,
[3126]77                          uniform sampler2D tex: TEXUNIT0,
[3045]78                          uniform float4x4 viewMatrix
[3004]79                          )
80{
81        float4 texColor = tex2D(tex, IN.texCoord.xy);
82
83        // account for alpha blending
84        if (texColor.w < 0.5f) discard;
85
86        pixel pix;
87
88        // save color in first render target
89        // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
90        pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor;
[3045]91        // save world space normal in rt => transform back into world space by
92        // multiplying with inverse view. since transforming normal with T means to
[3113]93        // multiply with the inverse transpose of T, we multiple with
94        // Transp(Inv(Inv(view))) = Transp(view)
95        pix.norm = mul(transpose(viewMatrix), IN.normal).xyz;
[3041]96        // compute eye linear depth
97        pix.col.w = length(IN.eyePos.xyz);
[3113]98
[3110]99        // the scene entity id
[3113]100        //pix.id = glstate.fog.color.xyz;
101        // the offset to the world pos from old frame
102        pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
[3004]103
104        return pix;
105}
106
107
[3046]108pixel frag(fragin IN, uniform float4x4 viewMatrix)
[3004]109{
110        pixel pix;
111        // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
112        pix.col = glstate.material.diffuse + glstate.material.emission;
[3045]113        // save world space normal in rt => transform back into world space by
114        // multiplying with inverse view. since transforming normal with T means to
115        // multiply with the inverse transpose of T, we multiple with Transp(Inv(Inv(view))) = Transp(view)
[3113]116        pix.norm = mul(transpose(viewMatrix), IN.normal).xyz;
[3041]117        // eye space depth
[3034]118        pix.col.w = length(IN.eyePos.xyz);
[3110]119        // the scene entity id
[3113]120        //pix.id = glstate.fog.color.xyz;
121        // the offset to the world pos from old frame
122        pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
123
[3004]124        return pix;
[2928]125}
Note: See TracBrowser for help on using the repository browser.