-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureFragmentShader.fragmentshader
88 lines (77 loc) · 3.63 KB
/
TextureFragmentShader.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
77
78
79
80
81
82
83
84
85
86
87
88
#version 330 core
// Interpolated values from the vertex shaders
//in vec4 vs_vertexColor;
in vec2 UV;
in vec3 Position_worldspace;
in vec3 Normal_cameraspace;
in vec3 EyeDirection_cameraspace;
in vec3 LightDirection_cameraspace;
in vec3 Light1Direction_cameraspace;
in vec3 Light2Direction_cameraspace;
// Ouput data
out vec3 color;
// Values that stay constant for the whole mesh.
uniform mat4 MV;
uniform vec3 LightPosition_worldspace;
uniform vec3 Light1Position_worldspace;
uniform vec3 Light2Position_worldspace;
uniform sampler2D myTextureSampler;
void main(){
// Light emission properties
// You probably want to put them as uniforms
vec3 LightColor = vec3(1,1,1);
vec3 Light1Color = vec3(0.5,0,0.5);
vec3 Light2Color = vec3(0.5,0.5,0);
float LightPower = 200.0f;
float Light1Power = 50.0f;
float Light2Power = 50.0f;
// Material properties
//vec3 MaterialDiffuseColor = vs_vertexColor.rgb;
vec3 MaterialDiffuseColor = texture( myTextureSampler, UV ).rgb;
vec3 MaterialAmbientColor = vec3(0.9,0.9,0.9) * texture( myTextureSampler, UV ).rgb;
vec3 MaterialSpecularColor = vec3(0.3,0.3,0.3) * texture( myTextureSampler, UV ).rgb;
// Distance to the light
float distance = length( LightPosition_worldspace - Position_worldspace );
float distance1 = length(Light1Position_worldspace - Position_worldspace);
float distance2 = length(Light2Position_worldspace - Position_worldspace);
// Normal of the computed fragment, in camera space
vec3 n = normalize( Normal_cameraspace );
// Direction of the light (from the fragment to the light)
vec3 l = normalize( LightDirection_cameraspace );
vec3 l1 = normalize( Light1Direction_cameraspace );
vec3 l2 = normalize( Light2Direction_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 perpendicular to the triangle -> 0
// - light is behind the triangle -> 0
float cosTheta = clamp( dot( n,l ), 0,1 );
float cosTheta1 = clamp( dot( n, l1 ), 0, 1);
float cosTheta2 = clamp( dot( n, l2 ), 0, 1);
// Eye vector (towards the camera)
vec3 E = normalize(EyeDirection_cameraspace);
// Direction in which the triangle reflects the light
vec3 R = reflect(-l,n);
vec3 R1 = reflect(-l1, n);
vec3 R2 = reflect(-l2, n);
// Cosine of the angle between the Eye vector and the Reflect vector,
// clamped to 0
// - Looking into the reflection -> 1
// - Looking elsewhere -> < 1
float cosAlpha = clamp( dot( E,R ), 0,1 );
float cosAlpha1 = clamp( dot( E, R1 ), 0, 1);
float cosAlpha2 = clamp( dot( E, R2 ), 0, 1);
//color = vs_vertexColor.rgb;
//color = texture( myTextureSampler, UV ).rgb;
color =
// Ambient : simulates indirect lighting
MaterialAmbientColor +
// Diffuse : "color" of the object
MaterialDiffuseColor * LightColor * LightPower * cosTheta / (distance*distance) +
MaterialDiffuseColor * Light1Color * Light1Power * cosTheta1 / (distance1 * distance1) +
MaterialDiffuseColor * Light2Color * Light2Power * cosTheta2 / (distance2 * distance2) +
// Specular : reflective highlight, like a mirror
MaterialSpecularColor * LightColor * LightPower * pow(cosAlpha,5) / (distance*distance) +
MaterialSpecularColor * Light1Color * Light1Power * pow(cosAlpha1,5) / (distance1 * distance1) +
MaterialSpecularColor * Light2Color * Light2Power * pow(cosAlpha2,5) / (distance2 * distance2);
}