-
Notifications
You must be signed in to change notification settings - Fork 0
/
prova.glsl
70 lines (65 loc) · 2.46 KB
/
prova.glsl
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
precision highp float;
uniform vec4 mDiffColor;
uniform vec4 mSpecColor;
uniform float mSpecPower;
uniform sampler2D textureFile;
uniform float textureInfluence;
uniform vec3 lightDirection;
uniform vec3 lightPosition;
uniform vec4 lightColor;
uniform int lightType;
uniform vec3 eyePosition;
varying vec3 fsNormal;
varying vec3 fsPosition;
varying vec2 fsUVs;
//Function to create different lights types
//int lt = the selected light source type
//vec3 pos = the surface position
vec4 lightModel(int lt, vec3 pos) {
//The normalize light direction
vec3 nLightDir;
//Float to store light dimension and cone length
float lDim, lCone;
lDim = 1.0;
if(lt == 1) { //Directional light
nLightDir = - normalize(lightDirection);
} else if(lt == 2) { //Point light
nLightDir = normalize(lightPosition - pos);
} else if(lt == 3) { //Point light (decay)
float lLen = length(lightPosition - pos);
nLightDir = normalize(lightPosition - pos);
lDim = 160.0 / (lLen * lLen);
} else if(lt == 4) { //Spot light
nLightDir = normalize(lightPosition - pos);
lCone = -dot(nLightDir, normalize(lightDirection));
if(lCone < 0.5) {
lDim = 0.0;
} else if(lCone > 0.7) {
lDim = 1.0;
} else {
lDim = (lCone - 0.5) / 0.2;
}
}
return vec4(nLightDir, lDim);
}
void main() {
vec3 nEyeDirection = normalize(eyePosition - fsPosition);
vec3 nNormal = normalize(fsNormal);
//Instead of computing it this way because directional
//Now we call a function to define light direction and size.
//vec3 nlightDirection = - normalize(lightDirection);
vec4 lm = lightModel(lightType, fsPosition);
vec3 nlightDirection = lm.rgb;
float lightDimension = lm.a;
//Computing the color contribution from the texture
vec4 diffuseTextureColorMixture = texture2D(textureFile, fsUVs) *
textureInfluence + mDiffColor * (1.0 - textureInfluence);
//Computing the diffuse component of light (no ambient considered)
vec4 diffuse = diffuseTextureColorMixture * lightColor *
clamp(dot(nlightDirection, nNormal), 0.0, 1.0) * lightDimension;
//Reflection vector for Phong model
vec3 reflection = -reflect(nlightDirection, nNormal);
vec4 specular = mSpecColor * lightColor * pow(clamp(dot(reflection,
nEyeDirection),0.0, 1.0), mSpecPower) * lightDimension;
gl_FragColor = min(diffuse + specular, vec4(1.0, 1.0, 1.0, 1.0));
}