forked from WenlinMao/SleepWalking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeTextureProgram.cpp
More file actions
247 lines (202 loc) · 7.5 KB
/
Copy pathShapeTextureProgram.cpp
File metadata and controls
247 lines (202 loc) · 7.5 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "ShapeTextureProgram.hpp"
#include <signal.h>
#include "gl_compile_program.hpp"
#include "gl_errors.hpp"
#ifdef _MSC_VER
#define DEBUG_BREAK __debugbreak()
#elif __APPLE__
#define DEBUG_BREAK __builtin_trap()
#else
#define DEBUG_BREAK raise(SIGTRAP)
#endif
#define ASSERT(x) if (!(x)) DEBUG_BREAK;
#define GLCall(x) GLClearError();\
x;\
ASSERT(GLLogCall(#x, __FILE__, __LINE__))
#define TEXT_DEFAULT_COLOR glm::u8vec4(0x00, 0x00, 0x00, 0xff)
#define TEXT_HIGHLIGHT_COLOR glm::u8vec4(0xff)
#define BOX_HIGHLIGHT_COLOR glm::u8vec4(0xde, 0xf2, 0x37, 0xff)
#define BOX_DEFAULT_COLOR glm::u8vec4(0x77, 0x93, 0x7c, 0xff)
static void GLClearError()
{
while (glGetError() != GL_NO_ERROR);
}
static bool GLLogCall(const char* function, const char* file, int line)
{
while (GLenum error = glGetError())
{
std::cout << "[OpenGL Error] ( " << error << " ) : " << function << " " << file << ": " << line << std::endl;
return false;
}
return true;
}
Load< ShapeTextureProgram > shape_texture_program(LoadTagEarly);
static unsigned int default_index_buffer_content[]{0, 1, 2, 1, 2, 3};
static GLuint default_index_buffer{ 0 };
static GLuint default_texture{ 0 };
static Load<void> load_default_index_buffer(LoadTagEarly, []() {
GLCall(glGenBuffers(1, &default_index_buffer));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, default_index_buffer));
GLCall(glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(unsigned int), default_index_buffer_content, GL_STATIC_DRAW));
});
static Load<void> load_default_texture(LoadTagEarly, []() {
GLCall(glGenTextures(1, &default_texture));
GLCall(glBindTexture(GL_TEXTURE_2D, default_texture));
std::vector< glm::u8vec4 > tex_data(1, glm::u8vec4(0xff));
GLCall(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data.data()));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
GLCall(glBindTexture(GL_TEXTURE_2D, 0));
});
ShapeTextureProgram::ShapeTextureProgram()
{
GLCall(program = gl_compile_program(
//vertex shader:
"#version 330\n"
"in vec4 Position;\n"
"in vec4 Color;\n"
"in vec2 TexCoord;\n"
"out vec4 color;\n"
"out vec2 texCoord;\n"
"void main() {\n"
" gl_Position = Position;\n"
" color = Color;\n"
" texCoord = TexCoord;\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"
));
GLCall(Position_vec4 = glGetAttribLocation(program, "Position"));
GLCall(Color_vec4 = glGetAttribLocation(program, "Color"));
GLCall(TexCoord_vec2 = glGetAttribLocation(program, "TexCoord"));
GLCall(GLuint TEX_sampler2D = glGetUniformLocation(program, "TEX"));
GLCall(glUseProgram(program));
GLCall(glUniform1i(TEX_sampler2D, 0));
GLCall(glUseProgram(0));
}
ShapeTextureProgram::~ShapeTextureProgram()
{
GLCall(glDeleteProgram(program));
program = 0;
}
GLuint ShapeTextureProgram::GetVao(GLuint vertex_buffer) const
{
GLuint vao;
GLCall(glGenVertexArrays(1, &vao));
GLCall(glBindVertexArray(vao));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer));
GLCall(glEnableVertexAttribArray(Position_vec4));
GLCall(glVertexAttribPointer(Position_vec4, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, Position)));
GLCall(glEnableVertexAttribArray(Color_vec4));
GLCall(glVertexAttribPointer(Color_vec4, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (const void*)offsetof(Vertex, Color)));
GLCall(glEnableVertexAttribArray(TexCoord_vec2));
GLCall(glVertexAttribPointer(TexCoord_vec2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const void*)offsetof(Vertex, TexCoord)));
return vao;
}
void ShapeTextureProgram::BoxDrawable::Clear()
{
if (vertex_buffer)
{
GLCall(glDeleteBuffers(1, &vertex_buffer));
vertex_buffer = 0;
}
if (vertex_array)
{
GLCall(glDeleteVertexArrays(1, &vertex_array));
vertex_array = 0;
}
}
void ShapeTextureProgram::SetBox(BoxDrawable& drawable, const glm::vec4& box, const glm::u8vec4 color) const
{
drawable.Clear();
GLCall(glGenBuffers(1, &drawable.vertex_buffer));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, drawable.vertex_buffer));
Vertex vertices[]{
{{box[0], box[1]}, color, {0, 1}},
{{box[2], box[1]}, color, {1, 1}},
{{box[0], box[3]}, color, {0, 0}},
{{box[2], box[3]}, color, {1, 0}}
};
drawable.color = color;
GLCall(glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(Vertex), static_cast<const void*>(vertices), GL_STATIC_DRAW));
drawable.vertex_array = shape_texture_program->GetVao(drawable.vertex_buffer);
}
// Code from https://github.com/GenBrg/MarryPrincess/blob/master/Texture2DProgram.cpp
void ShapeTextureProgram::DrawBox(const BoxDrawable& drawable) const
{
GLCall(glUseProgram(program));
GLCall(glBindVertexArray(drawable.vertex_array));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, default_index_buffer));
GLCall(glActiveTexture(GL_TEXTURE0));
GLCall(glBindTexture(GL_TEXTURE_2D, default_texture));
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, static_cast<const void *>(0)));
}
GLuint ShapeTextureProgram::GetTextureId(const FT_Bitmap& bitmap) const
{
GLuint texture_id;
GLCall(glPixelStorei(GL_UNPACK_ALIGNMENT, 1));
GLCall(glGenTextures(1, &texture_id));
GLCall(glBindTexture(GL_TEXTURE_2D, texture_id));
GLCall(glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA,
bitmap.width,
bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
bitmap.buffer
));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_ONE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_ONE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_ONE));
GLCall(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED));
return texture_id;
}
void ShapeTextureProgram::DeleteTextureId(const GLuint texture_id) const
{
GLCall(glDeleteTextures(1, &texture_id));
}
void ShapeTextureProgram::DrawFont(const Vertex* vertices, const GLuint texture_id) const
{
GLCall(glUseProgram(program));
GLuint vertex_buffer, vertex_array;
GLCall(glGenBuffers(1, &vertex_buffer));
GLCall(glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer));
GLCall(glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(Vertex), static_cast<const void*>(vertices), GL_STATIC_DRAW));
vertex_array = GetVao(vertex_buffer);
GLCall(glBindVertexArray(vertex_array));
GLCall(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, default_index_buffer));
GLCall(glActiveTexture(GL_TEXTURE0));
GLCall(glBindTexture(GL_TEXTURE_2D, texture_id));
GLCall(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, static_cast<const void*>(0)));
GLCall(glDeleteBuffers(1, &vertex_buffer));
GLCall(glDeleteVertexArrays(1, &vertex_array));
}
void ShapeTextureProgram::SetBoxHighlight(BoxDrawable& drawable, const glm::vec4& box) const
{
if (drawable.color != BOX_HIGHLIGHT_COLOR)
SetBox(drawable, box, BOX_HIGHLIGHT_COLOR);
DrawBox(drawable);
}
void ShapeTextureProgram::ResetBoxHighlight(BoxDrawable& drawable, const glm::vec4& box) const
{
if (drawable.color != BOX_DEFAULT_COLOR)
SetBox(drawable, box, BOX_DEFAULT_COLOR);
DrawBox(drawable);
}