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 | float4 eyePos: TEXCOORD1; // eye position
|
---|
19 | float4 normal: TEXCOORD2;
|
---|
20 | };
|
---|
21 |
|
---|
22 |
|
---|
23 | // fragment input
|
---|
24 | struct fragin
|
---|
25 | {
|
---|
26 | float4 color: COLOR0;
|
---|
27 | //float4 position: POSITION;
|
---|
28 | float4 texCoord: TEXCOORD0;
|
---|
29 |
|
---|
30 | float4 winPos: WPOS;
|
---|
31 | float4 eyePos: TEXCOORD1; // eye position
|
---|
32 | float4 normal: TEXCOORD2;
|
---|
33 | };
|
---|
34 |
|
---|
35 |
|
---|
36 | struct pixel
|
---|
37 | {
|
---|
38 | float4 col: COLOR0;
|
---|
39 | float3 norm: COLOR1;
|
---|
40 | float3 pos: COLOR2;
|
---|
41 | };
|
---|
42 |
|
---|
43 |
|
---|
44 | #pragma position_invariant vtx
|
---|
45 |
|
---|
46 | vtxout vtx(vtxin IN)
|
---|
47 | {
|
---|
48 | vtxout OUT;
|
---|
49 |
|
---|
50 | OUT.color = IN.color;
|
---|
51 | OUT.texCoord = IN.texCoord;
|
---|
52 |
|
---|
53 | // transform the vertex position into eye space
|
---|
54 | OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
|
---|
55 | // transform the vertex position into post projection space
|
---|
56 | OUT.position = mul(glstate.matrix.mvp, IN.position);
|
---|
57 |
|
---|
58 | OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal);
|
---|
59 |
|
---|
60 | return OUT;
|
---|
61 | }
|
---|
62 |
|
---|
63 | #pragma position_invariant fragtex
|
---|
64 |
|
---|
65 | pixel fragtex(fragin IN,
|
---|
66 | uniform sampler2D tex,
|
---|
67 | uniform float4x4 viewMatrix
|
---|
68 | )
|
---|
69 | {
|
---|
70 | float4 texColor = tex2D(tex, IN.texCoord.xy);
|
---|
71 |
|
---|
72 | // account for alpha blending
|
---|
73 | if (texColor.w < 0.5f) discard;
|
---|
74 |
|
---|
75 | pixel pix;
|
---|
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 | // save world space normal in rt => transform back into world space by
|
---|
81 | // multiplying with inverse view. since transforming normal with T means to
|
---|
82 | // multiply with the inverse transpose of T, we multiple with Transp(Inv(Inv(view))) = Transp(view)
|
---|
83 | pix.norm = mul(transpose(viewMatrix), IN.normal);
|
---|
84 | // compute eye linear depth
|
---|
85 | pix.col.w = length(IN.eyePos.xyz);
|
---|
86 | // hack: squeeze some information about ambient into the texture
|
---|
87 | //pix.col.w = glstate.material.emission.x;
|
---|
88 |
|
---|
89 | return pix;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | #pragma position_invariant frag
|
---|
94 |
|
---|
95 | pixel frag(fragin IN, uniform float4x4 viewMatrix)
|
---|
96 | {
|
---|
97 | pixel pix;
|
---|
98 | // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
|
---|
99 | pix.col = glstate.material.diffuse + glstate.material.emission;
|
---|
100 | // save world space normal in rt => transform back into world space by
|
---|
101 | // multiplying with inverse view. since transforming normal with T means to
|
---|
102 | // multiply with the inverse transpose of T, we multiple with Transp(Inv(Inv(view))) = Transp(view)
|
---|
103 | pix.norm = mul(transpose(viewMatrix), IN.normal);
|
---|
104 | // eye space depth
|
---|
105 | pix.col.w = length(IN.eyePos.xyz);
|
---|
106 | // hack: squeeze some information about the ambient term into the target
|
---|
107 | //pix.col.w = glstate.material.emission.x;
|
---|
108 |
|
---|
109 | return pix;
|
---|
110 | } |
---|