-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertex_color_program.cpp
62 lines (57 loc) · 1.99 KB
/
vertex_color_program.cpp
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
#include "vertex_color_program.hpp"
#include "compile_program.hpp"
VertexColorProgram::VertexColorProgram() {
program = compile_program(
"#version 330\n"
"uniform mat4 object_to_clip;\n"
"uniform mat4x3 object_to_light;\n"
"uniform mat3 normal_to_light;\n"
"layout(location=0) in vec4 Position;\n" //note: layout keyword used to make sure that the location-0 attribute is always bound to something
"in vec3 Normal;\n"
"in vec4 Color;\n"
"out vec3 position;\n"
"out vec3 normal;\n"
"out vec4 color;\n"
"void main() {\n"
" gl_Position = object_to_clip * Position;\n"
" position = object_to_light * Position;\n"
" normal = normal_to_light * Normal;\n"
" color = Color;\n"
"}\n"
,
"#version 330\n"
"uniform vec3 sun_direction;\n"
"uniform vec3 sun_color;\n"
"uniform vec3 sky_direction;\n"
"uniform vec3 sky_color;\n"
"in vec3 position;\n"
"in vec3 normal;\n"
"in vec4 color;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" vec3 total_light = vec3(0.0, 0.0, 0.0);\n"
" vec3 n = normalize(normal);\n"
" { //sky (hemisphere) light:\n"
" vec3 l = sky_direction;\n"
" float nl = 0.5 + 0.5 * dot(n,l);\n"
" total_light += nl * sky_color;\n"
" }\n"
" { //sun (directional) light:\n"
" vec3 l = sun_direction;\n"
" float nl = max(0.0, dot(n,l));\n"
" total_light += nl * sun_color;\n"
" }\n"
" fragColor = vec4(color.rgb * total_light, color.a);\n"
"}\n"
);
object_to_clip_mat4 = glGetUniformLocation(program, "object_to_clip");
object_to_light_mat4x3 = glGetUniformLocation(program, "object_to_light");
normal_to_light_mat3 = glGetUniformLocation(program, "normal_to_light");
sun_direction_vec3 = glGetUniformLocation(program, "sun_direction");
sun_color_vec3 = glGetUniformLocation(program, "sun_color");
sky_direction_vec3 = glGetUniformLocation(program, "sky_direction");
sky_color_vec3 = glGetUniformLocation(program, "sky_color");
}
Load< VertexColorProgram > vertex_color_program(LoadTagInit, [](){
return new VertexColorProgram();
});