-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlighting.frag
107 lines (97 loc) · 3.88 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#version 400 core
// Light structure
struct LightProperties {
int type;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec4 position;
vec4 direction;
float spotCutoff;
float spotExponent;
};
// Material structure
struct MaterialProperties {
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
};
const int MaxLights = 8;
layout (std140) uniform LightBuffer {
LightProperties Lights[MaxLights];
};
const int MaxMaterials = 8;
layout (std140) uniform MaterialBuffer {
MaterialProperties Materials[MaxMaterials];
};
// Selected material
uniform int Material;
// Number of lights
uniform int NumLights;
uniform int LightOn[MaxLights];
out vec4 fragColor;
in vec4 Position;
in vec3 Normal;
in vec3 View;
void main()
{
vec3 rgb = vec3(0.0f);
vec3 NormNormal = normalize(Normal);
vec3 NormView = normalize(View);
for (int i = 0; i < NumLights; i++) {
// If light is not off
if (LightOn[i] != 0) {
// add ambient component
if (Lights[i].type != 0) {
rgb += vec3(Lights[i].ambient*Materials[Material].ambient);
}
// Directional Light
if (Lights[i].type == 1) {
vec3 LightDirection = -normalize(vec3(Lights[i].direction));
vec3 HalfVector = normalize(LightDirection + NormView);
// Diffuse
float diff = max(0.0f, dot(NormNormal, LightDirection));
rgb += diff*vec3(Lights[i].diffuse*Materials[Material].diffuse);
if (diff > 0.0) {
// Specular term
float spec = pow(max(0.0f, dot(Normal, HalfVector)), Materials[Material].shininess);
rgb += spec*vec3(Lights[i].specular*Materials[Material].specular);
}
}
// Point light
if (Lights[i].type == 2) {
vec3 LightDirection = normalize(vec3(Lights[i].position - Position));
vec3 HalfVector = normalize(LightDirection + NormView);
// Diffuse
float diff = max(0.0f, dot(NormNormal, LightDirection));
rgb += diff*vec3(Lights[i].diffuse*Materials[Material].diffuse);
if (diff > 0.0) {
// Specular term
float spec = pow(max(0.0f, dot(Normal, HalfVector)), Materials[Material].shininess);
rgb += spec*vec3(Lights[i].specular*Materials[Material].specular);
}
}
// Spot light
if (Lights[i].type == 3) {
vec3 LightDirection = normalize(vec3(Lights[i].position - Position));
// Determine if inside cone
float spotCos = dot(LightDirection, -normalize(vec3(Lights[i].direction)));
float coneCos = cos(radians(Lights[i].spotCutoff));
if (spotCos >= coneCos) {
vec3 HalfVector = normalize(LightDirection + NormView);
float attenuation = pow(spotCos, Lights[i].spotExponent);
// Diffuse
float diff = max(0.0f, dot(NormNormal, LightDirection))*attenuation;
rgb += diff*vec3(Lights[i].diffuse*Materials[Material].diffuse);
if (diff > 0.0) {
// Specular term
float spec = pow(max(0.0f, dot(Normal, HalfVector)), Materials[Material].shininess)*attenuation;
rgb += spec*vec3(Lights[i].specular*Materials[Material].specular);
}
}
}
}
}
fragColor = vec4(min(rgb,vec3(1.0)), Materials[Material].ambient.a);
}