-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleFragmentShader.fragmentshader
76 lines (63 loc) · 2.77 KB
/
SimpleFragmentShader.fragmentshader
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
#version 330 core
// Interpolated values from the vertex shaders
in vec2 UV;
in vec3 Position_worldspace;
in vec3 Normal_cameraspace;
in vec3 EyeDirection_cameraspace;
in vec3 LightDirection1_cameraspace;
in vec3 LightDirection2_cameraspace;
in vec3 LightDirection3_cameraspace;
in vec3 LightDirection4_cameraspace;
// Ouput data
layout(location = 0) out vec3 color;
// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;
uniform mat4 MV;
uniform vec3 LightPosition1_worldspace;
uniform vec3 LightPosition2_worldspace;
uniform vec3 LightPosition3_worldspace;
uniform vec3 LightPosition4_worldspace;
uniform float LightPower1;
uniform float LightPower2;
uniform float LightPower3;
uniform float LightPower4;
void main(){
// Light emission properties
// You probably want to put them as uniforms
vec3 LightColor = vec3(1, 1, 1);
// Material properties
vec3 MaterialDiffuseColor = vec3(0, 1, 0);
vec3 MaterialAmbientColor = vec3(0.3, 0.3, 0.3) * MaterialDiffuseColor;
// Distance to the light
float distance1 = length(LightPosition1_worldspace - Position_worldspace);
// Normal of the computed fragment, in camera space
vec3 n1 = normalize(Normal_cameraspace);
// Direction of the light (from the fragment to the light)
vec3 l1 = normalize(LightDirection1_cameraspace);
// Cosine of the angle between the normal and the light direction,
// clamped above 0
// - light is at the vertical of the triangle -> 1
// - light is perpendiular to the triangle -> 0
// - light is behind the triangle -> 0
float cosTheta1 = clamp(dot(n1, l1), 0, 1);
float distance2 = length(LightPosition2_worldspace - Position_worldspace);
vec3 n2 = normalize(Normal_cameraspace);
vec3 l2 = normalize(LightDirection2_cameraspace);
float cosTheta2 = clamp(dot(n2, l2), 0, 1);
float distance3 = length(LightPosition3_worldspace - Position_worldspace);
vec3 n3 = normalize(Normal_cameraspace);
vec3 l3 = normalize(LightDirection3_cameraspace);
float cosTheta3 = clamp(dot(n3, l3), 0, 1);
float distance4 = length(LightPosition4_worldspace - Position_worldspace);
vec3 n4 = normalize(Normal_cameraspace);
vec3 l4 = normalize(LightDirection4_cameraspace);
float cosTheta4 = clamp(dot(n4, l4), 0, 1);
color =
// Ambient : simulates indirect lighting
MaterialAmbientColor
// Diffuse : "color" of the object
+ MaterialDiffuseColor * LightColor * LightPower1 * cosTheta1 / (distance1*distance1)
+ MaterialDiffuseColor * LightColor * LightPower2 * cosTheta2 / (distance2*distance2)
+ MaterialDiffuseColor * LightColor * LightPower3 * cosTheta3 / (distance3*distance3)
+ MaterialDiffuseColor * LightColor * LightPower4 * cosTheta4 / (distance4*distance4);
}