-
Notifications
You must be signed in to change notification settings - Fork 0
/
v-gouraud-illumination.glsl
47 lines (34 loc) · 1.69 KB
/
v-gouraud-illumination.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
precision mediump float;
attribute vec3 vertexPosition;
attribute vec3 vertexColor;
attribute vec3 vertexNormal;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat3 normalMatrix;
uniform vec4 lightViewPosition;
vec3 lightColor = vec3(0.7, 0.7, 0.7); // Light color for diffuse and specular
float shininess = 0.3; // Shininess factor for specular highlight
vec3 ambientColor = vec3(0.3, 0.3, 0.3);
varying vec3 fragmentColor;
void main() {
// Transform vertex position to view space
vec4 viewPosition = modelViewMatrix * vec4(vertexPosition, 1.0);
// Calculate and normalize light vector
vec3 lightVector = normalize(lightViewPosition.xyz - viewPosition.xyz);
// Transform and normalize the normal
vec3 transformedNormal = normalize(normalMatrix * vertexNormal);
// View direction is the reverse of the view position vector
vec3 viewVector = normalize(-viewPosition.xyz);
// Calculate light intensity (diffuse)
float diffuseLightIntensity = max(dot(lightVector, transformedNormal), 0.0);
// Calculate the reflection vector
vec3 reflectVector = reflect(-lightVector, transformedNormal);
float specularLightIntensity = pow(max(dot(reflectVector, viewVector), 0.0), shininess);
// Combine all three light components
vec3 ambientComponent = ambientColor * vertexColor;
vec3 diffuseComponent = lightColor * vertexColor * diffuseLightIntensity;
vec3 specularComponent = lightColor * specularLightIntensity;
// Output color is the sum of ambient, diffuse, and specular components
fragmentColor = ambientComponent + diffuseComponent + specularComponent;
gl_Position = projectionMatrix * viewPosition;
}