-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_pixel_lighting.frag
66 lines (52 loc) · 1.75 KB
/
model_pixel_lighting.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#version 120
uniform sampler2D texture0;
struct light {
vec4 position;
vec4 ambient;
vec4 diffuse;
vec4 specular;
};
uniform light light0;
uniform vec4 material_ambient;
uniform vec4 material_diffuse;
uniform vec4 material_specular;
uniform vec4 material_emissive;
uniform float material_shininess;
varying vec4 color; // Input color
varying vec2 texCoord0; // Input tex coord
varying vec3 normal; // Input normal
varying vec3 light_pos; // Input light position
varying vec3 half_vector; // Input half vector
varying vec4 vertex_ambient; // Vertex ambient light value
varying vec4 vertex_diffuse; // Vertex diffuse light value
varying float blend_factor;
void main(void)
{
vec4 texColor = texture2D(texture0, texCoord0.st);
if (texColor.a < 0.1)
discard;
// Renormalise normal.
vec3 N = normalize(normal);
// Renormalise light position.
vec3 L = normalize(light_pos);
//Calculate angle between normal and light direction.
float NdotL = max(dot(N,L), 0.0);
vec4 final_color = color;
//Only perform lighting calculations if the surface is facing toward the light source.
if (NdotL > 0.0)
{
// Add diffuse contribution.
final_color += vertex_diffuse * NdotL;
//Calculate and normalize half vector.
vec3 HV = normalize(half_vector);
//Calculate angle between normal and HV.
float NdotHV = max(dot(N, HV), 0.0);
//Calculate specular component.
final_color += material_specular * light0.specular * pow(NdotHV, material_shininess);
}
//Add ambient component.
final_color += vertex_ambient;
gl_FragColor = final_color * texColor; // Copy color to output
//vec4 fogColor = vec4(0.5, 0.5, 0.5, 0.5);
//gl_FragColor = mix(fogColor, fragColor, blend_factor);
}