-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture2DProgram.cpp
More file actions
75 lines (62 loc) · 2.37 KB
/
Texture2DProgram.cpp
File metadata and controls
75 lines (62 loc) · 2.37 KB
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
#include "Texture2DProgram.hpp"
#include "gl_compile_program.hpp"
#include "gl_errors.hpp"
Load<Texture2DProgram> texture2d_program(LoadTagEarly);
Texture2DProgram::Texture2DProgram() {
//Compile vertex and fragment shaders using the convenient 'gl_compile_program' helper function:
program = gl_compile_program(
//vertex shader:
"#version 330\n"
"in vec4 Position;\n"
"in vec4 Color;\n"
"in vec2 TexCoord;\n"
"out vec2 texCoord;\n"
"out vec4 color;\n"
"void main() {\n"
" gl_Position = Position;\n"
" texCoord = TexCoord;\n"
" color = Color;\n"
"}\n"
,
//fragment shader:
"#version 330\n"
"uniform sampler2D TEX;\n"
"in vec4 color;\n"
"in vec2 texCoord;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" fragColor = texture(TEX, texCoord) * color;\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");
Color_vec4 = glGetAttribLocation(program, "Color");
TexCoord_vec2 = glGetAttribLocation(program, "TexCoord");
//look up the locations of uniforms:
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
}
Texture2DProgram::~Texture2DProgram() {
glDeleteProgram(program);
program = 0;
}
// source: https://github.com/GenBrg/MarryPrincess/blob/master/Texture2DProgram.cpp#L81
GLuint Texture2DProgram::GetVao(GLuint vertex_buffer) const
{
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glEnableVertexAttribArray(Position_vec4);
glVertexAttribPointer(Position_vec4, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, Position));
glEnableVertexAttribArray(Color_vec4);
glVertexAttribPointer(Color_vec4, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (const void*)offsetof(Vertex, Color));
glEnableVertexAttribArray(TexCoord_vec2);
glVertexAttribPointer(TexCoord_vec2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, TexCoord));
return vao;
}