forked from 15-466/15-466-f23-base4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureProgram.cpp
83 lines (64 loc) · 2.56 KB
/
TextureProgram.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
#include "TextureProgram.hpp"
#include "gl_compile_program.hpp"
#include "gl_errors.hpp"
Scene::Drawable::Pipeline texture_program_pipeline;
Load< TextureProgram > texture_program(LoadTagEarly, []() -> TextureProgram const * {
TextureProgram *ret = new TextureProgram();
//----- build the pipeline template -----
texture_program_pipeline.program = ret->program;
texture_program_pipeline.OBJECT_TO_CLIP_mat4 = ret->OBJECT_TO_CLIP_mat4;
//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);
texture_program_pipeline.textures[0].texture = tex;
texture_program_pipeline.textures[0].target = GL_TEXTURE_2D;
return ret;
});
TextureProgram::TextureProgram() {
//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"
"in vec4 Position;\n"
"in vec2 TexCoord;\n"
"out vec2 texCoord;\n"
"void main() {\n"
" gl_Position = OBJECT_TO_CLIP * Position;\n"
" texCoord = TexCoord;\n"
"}\n"
,
//fragment shader:
"#version 330\n"
"uniform sampler2D TEX;\n"
"in vec2 texCoord;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" fragColor = texture(TEX, texCoord);\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");
TexCoord_vec2 = glGetAttribLocation(program, "TexCoord");
//look up the locations of uniforms:
OBJECT_TO_CLIP_mat4 = glGetUniformLocation(program, "OBJECT_TO_CLIP");
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
}
TextureProgram::~TextureProgram() {
glDeleteProgram(program);
program = 0;
}