-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvertShader_7_1.glsl
More file actions
53 lines (48 loc) · 1.62 KB
/
vertShader_7_1.glsl
File metadata and controls
53 lines (48 loc) · 1.62 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
#version 430
layout (location=0) in vec3 vertPos;
layout (location=1) in vec3 vertNormal;
out vec4 varyingColor;
struct PositionalLight
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 position;
};
struct Material
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
};
uniform vec4 globalAmbient;
uniform PositionalLight light;
uniform Material material;
uniform mat4 m_matrix;
uniform mat4 v_matrix;
uniform mat4 p_matrix;
uniform mat4 norm_matrix; // for transforming normals
void main(void)
{
vec4 color;
// convert vertex position to world space,
// convert normal to world space, and
// calculate world space light vector (from vertex to light)
vec4 P = m_matrix * vec4(vertPos, 1.0);
vec3 N = normalize((norm_matrix * vec4(vertNormal,1.0)).xyz);
vec3 L = normalize(light.position - P.xyz);
// view vector is from vertex to camera, camera loc is extracted from view matrix
vec3 V = normalize(-v_matrix[3].xyz - P.xyz);
// R is reflection of -L with respect to surface normal N
vec3 R = reflect(-L,N);
// ambient, diffuse, and specular contributions
vec3 ambient = ((globalAmbient * material.ambient) + (light.ambient * material.ambient)).xyz;
vec3 diffuse = light.diffuse.xyz * material.diffuse.xyz * max(dot(N,L), 0.0);
vec3 specular =
material.specular.xyz * light.specular.xyz * pow(max(dot(R,V), 0.0f), material.shininess);
// send the color output to the fragment shader
varyingColor = vec4((ambient + diffuse + specular), 1.0);
// send the position to the fragment shader, as before
gl_Position = p_matrix * v_matrix * m_matrix * vec4(vertPos,1.0);
}