-
Notifications
You must be signed in to change notification settings - Fork 0
/
fshader.glsl
76 lines (70 loc) · 2.34 KB
/
fshader.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
71
72
73
74
75
#ifdef GL_ES
// Set default precision to medium
precision mediump int;
precision mediump float;
#endif
uniform sampler2D texture;
layout(origin_upper_left) varying vec4 gl_FragCoord;
//attribute vec4 a_position;
varying vec2 v_texcoord;
uniform vec4 playerpos;
uniform mat4 mvpmatrix;
uniform float radius;
uniform vec4 objectwcoord;
uniform int test;
//! [0]
uniform int numLights;
uniform struct Light {
vec4 position;
vec3 color; //a.k.a the color of the light
float attenuation;
float ambientCoefficient;
float coneAngle;
float maxAngle;
float dist;
float maxDist;
vec3 coneDirection;
} allLights[10];
vec3 ApplyLight(Light light, vec3 surfaceColor, vec3 normal, vec3 surfacePos) {
vec3 surfaceToLight = normalize(light.position.xyz - surfacePos);
float distanceToLight = length(light.position.xyz - surfacePos);
if(distanceToLight < light.maxDist){
float facteur = 1.0f;
if(light.dist < distanceToLight){
float diff = distanceToLight - light.dist;
facteur = 1.0f;
}
float attenuation = 1.0 / (1.0 + light.attenuation * distanceToLight * distanceToLight);
float lightToSurfaceAngle = degrees(acos(dot(-surfaceToLight, normalize(light.coneDirection))));
float facteur2 = 1.0f;
if(lightToSurfaceAngle > light.coneAngle){
float diff = lightToSurfaceAngle - light.coneAngle;
facteur2 = 1.0f ;
}
if(lightToSurfaceAngle > light.maxAngle){
return vec3(0.0,0.0,0.0);
}
vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.color;
return (ambient+ attenuation) * facteur * facteur2;
}
return vec3(0.0,0.0,0.0);
}
void main()
{
vec4 base = texture2D(texture, v_texcoord);
if(base.a < 0.5){ discard;}
if (test < 1)
{
vec3 linearColor = vec3(0);
for(int i = 0; i < numLights; ++i){
linearColor += ApplyLight(allLights[i], vec3(0.15f,0.15f,0.15f), vec3(0,0,1), vec3(gl_FragCoord.x,gl_FragCoord.y,gl_FragCoord.z));
}
if(linearColor.x < 0.12) linearColor.x =0.12;
if(linearColor.y < 0.12) linearColor.y =0.12;
if(linearColor.z < 0.12) linearColor.z =0.12;
gl_FragColor = vec4(linearColor.x*base.x,linearColor.y*base.y,linearColor.z*base.z,1.0);
}
else
gl_FragColor = base;
}
//! [0]