-
Notifications
You must be signed in to change notification settings - Fork 0
/
LitColorTextureProgram.cpp
106 lines (88 loc) · 3.73 KB
/
LitColorTextureProgram.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "LitColorTextureProgram.hpp"
#include "gl_compile_program.hpp"
#include "gl_errors.hpp"
Scene::Drawable::Pipeline lit_color_texture_program_pipeline;
Load< LitColorTextureProgram > lit_color_texture_program(LoadTagEarly, []() -> LitColorTextureProgram const * {
LitColorTextureProgram *ret = new LitColorTextureProgram();
//----- build the pipeline template -----
lit_color_texture_program_pipeline.program = ret->program;
lit_color_texture_program_pipeline.OBJECT_TO_CLIP_mat4 = ret->OBJECT_TO_CLIP_mat4;
lit_color_texture_program_pipeline.OBJECT_TO_LIGHT_mat4x3 = ret->OBJECT_TO_LIGHT_mat4x3;
lit_color_texture_program_pipeline.NORMAL_TO_LIGHT_mat3 = ret->NORMAL_TO_LIGHT_mat3;
//make a 1-pixel white texture to bind by default:
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
std::vector< glm::u8vec4 > tex_data(1, glm::u8vec4(0xff));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
lit_color_texture_program_pipeline.textures[0].texture = tex;
lit_color_texture_program_pipeline.textures[0].target = GL_TEXTURE_2D;
return ret;
});
LitColorTextureProgram::LitColorTextureProgram() {
//Compile vertex and fragment shaders using the convenient 'gl_compile_program' helper function:
program = gl_compile_program(
//vertex shader:
"#version 330\n"
"uniform mat4 OBJECT_TO_CLIP;\n"
"uniform mat4x3 OBJECT_TO_LIGHT;\n"
"uniform mat3 NORMAL_TO_LIGHT;\n"
"in vec4 Position;\n"
"in vec3 Normal;\n"
"in vec4 Color;\n"
"in vec2 TexCoord;\n"
"out vec3 position;\n"
"out vec3 normal;\n"
"out vec4 color;\n"
"out vec2 texCoord;\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"
" texCoord = TexCoord;\n"
"}\n"
,
//fragment shader:
"#version 330\n"
"uniform sampler2D TEX;\n"
"in vec3 position;\n"
"in vec3 normal;\n"
"in vec4 color;\n"
"in vec2 texCoord;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" vec3 n = normalize(normal);\n"
" vec3 l = normalize(vec3(0.1, 0.1, 1.0));\n"
" vec4 albedo = texture(TEX, texCoord) * color;\n"
//simple hemispherical lighting model:
" vec3 light = mix(vec3(0.0,0.0,0.1), vec3(1.0,1.0,0.95), dot(n,l)*0.5+0.5);\n"
" fragColor = vec4(light*albedo.rgb, albedo.a);\n"
"}\n"
);
//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");
Normal_vec3 = glGetAttribLocation(program, "Normal");
Color_vec4 = glGetAttribLocation(program, "Color");
TexCoord_vec2 = glGetAttribLocation(program, "TexCoord");
//look up the locations of uniforms:
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");
GLuint TEX_sampler2D = glGetUniformLocation(program, "TEX");
//set TEX to always refer to texture binding zero:
glUseProgram(program); //bind program -- glUniform* calls refer to this program now
glUniform1i(TEX_sampler2D, 0); //set TEX to sample from GL_TEXTURE0
glUseProgram(0); //unbind program -- glUniform* calls refer to ??? now
}
LitColorTextureProgram::~LitColorTextureProgram() {
glDeleteProgram(program);
program = 0;
}