-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandardShading.vertexshader
51 lines (42 loc) · 2.24 KB
/
StandardShading.vertexshader
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
#version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec4 vertexPosition_modelspace;
layout(location = 1) in vec4 vertexColor;
layout(location = 2) in vec3 vertexNormal_modelspace;
// Output data ; will be interpolated for each fragment.
out vec4 vs_vertexColor;
out vec3 Position_worldspace;
out vec3 Normal_cameraspace;
out vec3 EyeDirection_cameraspace;
out vec3 LightDirection_cameraspace;
out vec3 Light1Direction_cameraspace;
out vec3 Light2Direction_cameraspace;
// Values that stay constant for the whole mesh.
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
uniform vec3 LightPosition_worldspace;
uniform vec3 Light1Position_worldspace;
uniform vec3 Light2Position_worldspace;
void main(){
gl_PointSize = 5.0;
// Output position of the vertex, in clip space : MVP * position
gl_Position = P * V * M * vertexPosition_modelspace;
// Position of the vertex, in worldspace : M * position
Position_worldspace = (M * vertexPosition_modelspace).xyz;
// Vector that goes from the vertex to the camera, in camera space.
// In camera space, the camera is at the origin (0,0,0).
vec3 vertexPosition_cameraspace = ( V * M * vertexPosition_modelspace).xyz;
EyeDirection_cameraspace = vec3(0,0,0) - vertexPosition_cameraspace;
// Vector that goes from the vertex to the light, in camera space. M is ommited because it's identity.
vec3 LightPosition_cameraspace = ( V * vec4(LightPosition_worldspace,1)).xyz;
vec3 Light1Position_cameraspace = ( V * vec4(Light1Position_worldspace,1)).xyz;
vec3 Light2Position_cameraspace = ( V * vec4(Light2Position_worldspace,1)).xyz;
LightDirection_cameraspace = LightPosition_cameraspace + EyeDirection_cameraspace;
Light1Direction_cameraspace = Light1Position_cameraspace + EyeDirection_cameraspace;
Light2Direction_cameraspace = Light2Position_cameraspace + EyeDirection_cameraspace;
// Normal of the the vertex, in camera space
Normal_cameraspace = ( V * M * vec4(vertexNormal_modelspace, 0)).xyz; // Only correct if ModelMatrix does not scale the model ! Use its inverse transpose if not.
// UV of the vertex. No special space for this one.
vs_vertexColor = vertexColor;
}