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