-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVertexShader.vert
More file actions
56 lines (45 loc) · 2.36 KB
/
VertexShader.vert
File metadata and controls
56 lines (45 loc) · 2.36 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
//-------------------------------------------------------
// Filename: VertexShader.vert
//
// Description: A vertex shader that implements coloring based upon attenuation and diffusion.
//
// Creator: Professor Corey McBride for MEEN 570 - Brigham Young University
//
// Based On: http://www.learnopengles.com/android-lesson-two-ambient-and-diffuse-lighting/
//
// Creation Date: 10/10/16
//
// Owner: Corey McBride
//-------------------------------------------------------
#version 120
uniform mat4 u_MVPMatrix; // A constant representing the combined model/view/projection matrix.
uniform mat4 u_MVMatrix; // A constant representing the combined model/view matrix.
uniform vec3 u_LightPos; // The position of the light in eye space.
//uniform vec3 u_Color;
attribute vec3 a_Color;
attribute vec3 a_Position; // Per-vertex position information we will pass in.
attribute vec3 a_Normal; // Per-vertex normal information we will pass in.
varying vec3 v_Color; // This will be passed into the fragment shader.
// The entry point for our vertex shader.
void main()
{
// Transform the vertex into eye space.
vec3 modelViewVertex = vec3(u_MVMatrix * vec4(a_Position,1.0));
// Transform the normal's orientation into eye space.
vec3 modelViewNormal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));
// Will be used for attenuation.
float distance = length(u_LightPos - modelViewVertex);
// Get a lighting direction vector from the light to the vertex.
vec3 lightVector = normalize(u_LightPos - modelViewVertex);
// Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
// pointing in the same direction then it will get max illumination.
float diffuse = max(dot(modelViewNormal, lightVector), .1);
// Attenuate the light based on distance.
diffuse = diffuse * (1.0 / (1.0 + (0.01 * distance * distance)));
diffuse = clamp(dot(modelViewNormal,lightVector),0.0,1.0);
// Multiply the color by the illumination level. It will be interpolated across the triangle.
v_Color = a_Color * diffuse;
// gl_Position is a special variable used to store the final position.
// Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
gl_Position = u_MVPMatrix * vec4(a_Position,1.0);
}