-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandardShading.fragmentshader
More file actions
executable file
·81 lines (67 loc) · 2.96 KB
/
StandardShading.fragmentshader
File metadata and controls
executable file
·81 lines (67 loc) · 2.96 KB
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
#version 330 core
// Interpolated values from the vertex shaders
in vec4 vs_vertexColor;
in vec3 Position_worldspace;
in vec3 Normal_cameraspace;
in vec3 EyeDirection_cameraspace;
in vec3 LightDirection_cameraspace;
in vec3 LightDirection_cameraspace2;
// Ouput data
out vec3 color;
// Values that stay constant for the whole mesh.
uniform mat4 MV;
uniform vec3 LightPosition_worldspace;
uniform vec3 LightPosition_worldspace2;
void main(){
// Light emission properties
// You probably want to put them as uniforms
vec3 LightColor = vec3(1,0,1);
vec3 LightColor2 = vec3(0,1,1);
float LightPower = 40.0f;
// Material properties
vec3 MaterialDiffuseColor = vs_vertexColor.rgb;
vec3 MaterialAmbientColor = vec3(0.1,0.1,0.1) * MaterialDiffuseColor;
vec3 MaterialSpecularColor = vec3(0.1,0.1,0.1)* MaterialDiffuseColor;
// Distance to the light
float distance = length( LightPosition_worldspace - Position_worldspace );
float distance2 = length( LightPosition_worldspace2 - 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 l2 = normalize( LightDirection_cameraspace2 );
// 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 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 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 cosAlpha2 = clamp( dot( E,R2 ), 0,1 );
//color = vs_vertexColor.rgb;
//color = MaterialDiffuseColor * LightColor * LightPower * cosTheta / (distance*distance)
//+ MaterialDiffuseColor * LightColor2 * LightPower * cosTheta2 / (distance2*distance2);
color =
// Ambient : simulates indirect lighting
MaterialAmbientColor +
// Diffuse : "color" of the object
MaterialDiffuseColor * LightColor * LightPower * cosTheta / (distance*distance) +
// Specular : reflective highlight, like a mirror
MaterialSpecularColor * LightColor * LightPower * pow(cosAlpha,100) / (distance*distance)
// Ambient : simulates indirect lighting
//MaterialAmbientColor +
// Diffuse : "color" of the object
+ MaterialDiffuseColor * LightColor2 * LightPower * cosTheta2 / (distance2*distance2) +
// Specular : reflective highlight, like a mirror
MaterialSpecularColor * LightColor2 * LightPower * pow(cosAlpha2,100) / (distance2*distance2);
}