-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorProgram.cpp
57 lines (50 loc) · 1.38 KB
/
ColorProgram.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
#include "ColorProgram.hpp"
#include "gl_compile_program.hpp"
#include "gl_errors.hpp"
Load< ColorProgram > color_program(LoadTagEarly);
ColorProgram::ColorProgram() {
//Compile vertex and fragment shaders using the convenient 'gl_compile_program' helper function:
program = gl_compile_program(
//vertex shader:
#ifdef __ANDROID__
"#version 320 es\n"
#else
"#version 330\n"
#endif
"uniform mat4 OBJECT_TO_CLIP;\n"
"in vec4 Position;\n"
"in vec4 Color;\n"
"out vec4 color;\n"
"void main() {\n"
" gl_Position = OBJECT_TO_CLIP * Position;\n"
" color = Color;\n"
"}\n"
,
//fragment shader:
#ifdef __ANDROID__
"#version 320 es\n"
"precision highp int;\n" //overkill but saves re-writing the shader below
"precision highp float;\n" //similar
#else
"#version 330\n"
#endif
"in vec4 color;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" fragColor = color;\n"
"}\n"
,
"ColorProgram"
);
//As you can see above, adjacent strings in C/C++ are concatenated.
// this is very useful for writing long shader programs inline.
//look up the locations of vertex attributes:
Position_vec4 = glGetAttribLocation(program, "Position");
Color_vec4 = glGetAttribLocation(program, "Color");
//look up the locations of uniforms:
OBJECT_TO_CLIP_mat4 = glGetUniformLocation(program, "OBJECT_TO_CLIP");
}
ColorProgram::~ColorProgram() {
glDeleteProgram(program);
program = 0;
}