1 | // final pixel output: |
|
|
2 | // data from pixel shader to frame buffer |
|
|
3 | struct vertexOut { |
1 | struct vertexOut { |
4 | float4 HPosition : POSITION; |
2 | float4 HPosition : POSITION; |
5 | float4 TexCoord : TEXCOORD0; |
3 | float4 TexCoord : TEXCOORD0; |
6 | float3 LightVec : TEXCOORD1; |
4 | float3 LightVec : TEXCOORD1; |
|
|
5 | float3 EyeVec : TEXCOORD2; |
7 | float3 WorldNormal : TEXCOORD2; |
6 | float3 WorldNormal : TEXCOORD3; |
8 | float3 WorldPos : TEXCOORD3; |
|
|
9 | float3 WorldView : TEXCOORD4; |
|
|
10 | }; |
7 | }; |
11 | |
8 | |
12 | struct pixelOut { |
9 | struct pixelOut { |
13 | float4 col : COLOR; |
10 | float4 col : COLOR; |
14 | }; |
11 | }; |
15 | // pixel shader |
12 | |
16 | pixelOut main(vertexOut IN, // input from vertex shade |
13 | pixelOut main(vertexOut IN) |
17 | uniform float SpecExpon, // constant parameters fro |
|
|
18 | uniform float4 AmbiColor, // application |
|
|
19 | uniform float4 SurfColor, |
|
|
20 | uniform float4 LightColor |
|
|
21 | ) |
|
|
22 | { |
14 | { |
23 | pixelOut OUT; // output of the pixel shader |
15 | pixelOut OUT; // output of the pixel shader |
|
|
16 | |
|
|
17 | float SpecExpon = 120; |
|
|
18 | float4 LightColor = { 1, 1, 1, 1 }; |
|
|
19 | |
24 | float3 Ln = normalize(IN.LightVec); |
20 | float3 Ln = IN.LightVec; |
25 | float3 Nn = normalize(IN.WorldNormal); |
21 | float3 Nn = IN.WorldNormal; |
26 | float3 Vn = normalize(IN.WorldView); |
22 | float3 Vn = IN.EyeVec; |
27 | float3 Hn = normalize(Vn + |
23 | |
28 | // scalar product between light and normal vectors: |
24 | float3 Hn = 0.5 * (Ln + Vn); |
29 | float ldn = dot(Ln,Nn); |
25 | float ldn = dot (Ln, Nn); |
30 | // scalar product between halfway and normal vectors: |
|
|
31 | float hdn = dot(Hn,Nn); |
26 | float hdn = dot (Hn, Nn); |
|
|
27 | |
32 | // specialized "lit" function computes weights for |
28 | // specialized "lit" function computes weights for |
33 | // diffuse and specular parts: |
29 | // diffuse and specular parts: |
34 | float4 litV = lit(ldn,hdn,SpecExpon); |
30 | float4 litV = lit (ldn, hdn, SpecExpon); |
35 | float4 diffContrib = |
31 | float4 diffContrib = glstate.material.diffuse * (litV.y * LightColor + glstate.lightmodel.ambient); |
36 | SurfColor * ( litV.y * LightColor + AmbiColor); |
|
|
37 | float4 specContrib = litV.y*litV.z * LightColor; |
32 | float4 specContrib = litV.y * litV.z * LightColor; |
38 | // sum of diffuse and specular contributions: |
|
|
39 | float4 result = diffContrib + specContrib; |
33 | float4 result = diffContrib + specContrib; |
|
|
34 | |
40 | OUT.col = result; |
35 | OUT.col = result; |
41 | return OUT; // output of pixel shader |
36 | return OUT; |
42 | } |
37 | } |
43 | |
38 | |