-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopengl-cells-instancing.cpp
265 lines (197 loc) · 9.93 KB
/
opengl-cells-instancing.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Cells storage in instance VBO:
// - Allocate block of X bytes
// - Use block for contiguous array of CellInstances
// - Record CellInstance positions in QuadTree Cells
// TODO: Separate instance types for the different cell types.
void
update_cell_instance(CellInstancing *cell_instancing, u32 cell_instance_position, CellInstance *cell_instance)
{
update_buffer_element(L_CellInstancing, &cell_instancing->cell_instances_vbo, cell_instance_position, cell_instance);
}
void
add_cell_instance(CellInstancing *cell_instancing, Cell *cell)
{
CellInstance cell_instance = {
.world_cell_position_x = (s32)cell->x,
.world_cell_position_y = (s32)cell->y,
.world_cell_offset = {0, 0},
// TODO: This is temporary until we have the different cell types in different instance arrays.
.colour = get_cell_color(cell->type)
};
cell->opengl_instance_position = new_buffer_element(L_CellInstancing, &cell_instancing->cell_instances_vbo, &cell_instance);
log(L_CellInstancing, u8("n_cell_instances: %u, out of cell_instances_vbo_size: %u"),
cell_instancing->cell_instances_vbo.elements_used, cell_instancing->cell_instances_vbo.total_elements);
}
void
recurse_adding_all_cell_instances(CellInstancing *cell_instancing, QuadTree *tree)
{
if (tree)
{
for (u32 cell_index = 0;
cell_index < tree->used;
++cell_index)
{
Cell *cell = tree->cells + cell_index;
add_cell_instance(cell_instancing, cell);
}
recurse_adding_all_cell_instances(cell_instancing, tree->top_right);
recurse_adding_all_cell_instances(cell_instancing, tree->top_left);
recurse_adding_all_cell_instances(cell_instancing, tree->bottom_right);
recurse_adding_all_cell_instances(cell_instancing, tree->bottom_left);
}
}
void
add_all_cell_instances(CellInstancing *cell_instancing, QuadTree *tree)
{
glBindVertexArray(cell_instancing->vao);
recurse_adding_all_cell_instances(cell_instancing, tree);
print_gl_errors();
log(L_CellInstancing, u8("Added all cell instances."));
glBindVertexArray(0);
}
void
remove_cell_instance(CellInstancing *cell_instancing, Maze *maze, Cell *cell)
{
OpenGL_Buffer *cell_instances_vbo = &cell_instancing->cell_instances_vbo;
u32 cell_instance_position = cell->opengl_instance_position;
cell->opengl_instance_position = INVALID_GL_BUFFER_ELEMENT_POSITION;
log(L_CellInstancing, u8("Attempting to remove cell instance %u."), cell_instance_position);
remove_buffer_element(cell_instances_vbo, cell_instance_position);
// If the removed element is less or equal than the new number of
// elements, then another element has been moved into it's
// position, and the Cell needs to know it's new position.
if (cell_instance_position <= cell_instances_vbo->elements_used)
{
log(L_CellInstancing, u8("Updating moved cell instance"));
glBindBuffer(cell_instances_vbo->binding_target, cell_instances_vbo->id);
CellInstance moved_cell_instance;
glGetBufferSubData(cell_instances_vbo->binding_target, cell_instance_position, cell_instances_vbo->element_size, &moved_cell_instance);
glBindBuffer(cell_instances_vbo->binding_target, 0);
Cell *moved_cell = get_cell(maze, moved_cell_instance.world_cell_position_x, moved_cell_instance.world_cell_position_y);
if (moved_cell)
{
moved_cell->opengl_instance_position = cell_instance_position;
}
else
{
log(L_CellInstancing, u8("Could not get cell being moved from the Maze to update it's instance position"));
}
}
log(L_CellInstancing, u8("n_cell_instances: %u, out of cell_instances_vbo_size: %u"), cell_instances_vbo->elements_used, cell_instances_vbo->total_elements);
}
void
draw_instanced_cells(CellInstancing *cell_instancing, Panning *panning, mat4 projection_matrix)
{
glUseProgram(cell_instancing->shader_program);
CellUniforms *uniforms = &cell_instancing->uniforms;
glUniformMatrix4fv(uniforms->mat4_projection_matrix.location, 1, GL_TRUE, projection_matrix.es);
glUniform1i(uniforms->int_render_origin_cell_x.location, panning->world_maze_pos.cell_x);
glUniform1i(uniforms->int_render_origin_cell_y.location, panning->world_maze_pos.cell_y);
glUniform2f(uniforms->vec2_render_origin_offset.location, panning->world_maze_pos.offset.x, panning->world_maze_pos.offset.y);
glUniform1f(uniforms->float_scale.location, panning->zoom);
glBindVertexArray(cell_instancing->vao);
glDrawElementsInstanced(GL_TRIANGLES, cell_instancing->cell_vertex_ibo.elements_used, GL_UNSIGNED_SHORT, 0, cell_instancing->cell_instances_vbo.elements_used);
glBindVertexArray(0);
}
// Setup functions
void
setup_cell_vertex_vbo_attributes(OpenGL_Buffer *cell_vertex_vbo)
{
glBindBuffer(cell_vertex_vbo->binding_target, cell_vertex_vbo->id);
GLuint attribute_vertex = 8;
glVertexAttribPointer(attribute_vertex, 2, GL_FLOAT, GL_FALSE, sizeof(vec2), 0);
glEnableVertexAttribArray(attribute_vertex);
}
void
setup_cell_vertex_vbo(OpenGL_Buffer *cell_vertex_vbo, const vec2 *vertices, u32 n_vertices)
{
cell_vertex_vbo->id = create_buffer();
cell_vertex_vbo->element_size = sizeof(vec2);
cell_vertex_vbo->total_elements = n_vertices;
cell_vertex_vbo->elements_used = n_vertices;
cell_vertex_vbo->binding_target = GL_ARRAY_BUFFER;
cell_vertex_vbo->usage = GL_STATIC_DRAW;
cell_vertex_vbo->setup_attributes_func = setup_cell_vertex_vbo_attributes;
glBindBuffer(cell_vertex_vbo->binding_target, cell_vertex_vbo->id);
glBufferData(cell_vertex_vbo->binding_target, cell_vertex_vbo->element_size * cell_vertex_vbo->elements_used, vertices, cell_vertex_vbo->usage);
cell_vertex_vbo->setup_attributes_func(cell_vertex_vbo);
}
void
setup_cell_vertex_ibo_attributes(OpenGL_Buffer *cell_vertex_ibo)
{}
void
setup_cell_vertex_ibo(OpenGL_Buffer *cell_vertex_ibo, const GLushort *indices, u32 n_indices)
{
cell_vertex_ibo->id = create_buffer();
cell_vertex_ibo->element_size = sizeof(GLushort);
cell_vertex_ibo->total_elements = n_indices;
cell_vertex_ibo->elements_used = n_indices;
cell_vertex_ibo->binding_target = GL_ELEMENT_ARRAY_BUFFER;
cell_vertex_ibo->usage = GL_STATIC_DRAW;
cell_vertex_ibo->setup_attributes_func = setup_cell_vertex_ibo_attributes;
glBindBuffer(cell_vertex_ibo->binding_target, cell_vertex_ibo->id);
glBufferData(cell_vertex_ibo->binding_target, cell_vertex_ibo->element_size * cell_vertex_ibo->elements_used, indices, cell_vertex_ibo->usage);
cell_vertex_ibo->setup_attributes_func(cell_vertex_ibo);
}
void
setup_cell_instances_vbo_attributes(OpenGL_Buffer *cell_instances_vbo)
{
glBindBuffer(cell_instances_vbo->binding_target, cell_instances_vbo->id);
GLuint attribute_world_cell_position_x = 0;
glEnableVertexAttribArray(attribute_world_cell_position_x);
glVertexAttribIPointer(attribute_world_cell_position_x, 1, GL_INT, sizeof(CellInstance), (void *)offsetof(CellInstance, world_cell_position_x));
glVertexAttribDivisor(attribute_world_cell_position_x, 1);
GLuint attribute_world_cell_position_y = 1;
glEnableVertexAttribArray(attribute_world_cell_position_y);
glVertexAttribIPointer(attribute_world_cell_position_y, 1, GL_INT, sizeof(CellInstance), (void *)offsetof(CellInstance, world_cell_position_y));
glVertexAttribDivisor(attribute_world_cell_position_y, 1);
GLuint attribute_world_cell_offset = 2;
glEnableVertexAttribArray(attribute_world_cell_offset);
glVertexAttribPointer(attribute_world_cell_offset, sizeof(vec2)/sizeof(r32), GL_FLOAT, GL_FALSE, sizeof(CellInstance), (void *)offsetof(CellInstance, world_cell_offset));
glVertexAttribDivisor(attribute_world_cell_offset, 1);
GLuint attribute_colour = 4;
glEnableVertexAttribArray(attribute_colour);
glVertexAttribPointer(attribute_colour, sizeof(vec4)/sizeof(r32), GL_FLOAT, GL_FALSE, sizeof(CellInstance), (void *)offsetof(CellInstance, colour));
glVertexAttribDivisor(attribute_colour, 1);
}
void
setup_cell_instances_vbo(OpenGL_Buffer *cell_instances_vbo)
{
cell_instances_vbo->id = create_buffer();
cell_instances_vbo->element_size = sizeof(CellInstance);
cell_instances_vbo->total_elements = 16;
cell_instances_vbo->elements_used = 0;
cell_instances_vbo->binding_target = GL_ARRAY_BUFFER;
cell_instances_vbo->usage = GL_STATIC_DRAW;
cell_instances_vbo->setup_attributes_func = setup_cell_instances_vbo_attributes;
glBindBuffer(cell_instances_vbo->binding_target, cell_instances_vbo->id);
glBufferData(cell_instances_vbo->binding_target, cell_instances_vbo->element_size * cell_instances_vbo->total_elements, 0, cell_instances_vbo->usage);
cell_instances_vbo->setup_attributes_func(cell_instances_vbo);
}
b32
setup_cell_instancing(CellInstancing *cell_instancing)
{
b32 success = true;
const u8 *filenames[] = {u8("cell-instancing.glvs"), u8("cell-instancing.glfs")};
GLenum shader_types[] = {GL_VERTEX_SHADER, GL_FRAGMENT_SHADER};
success &= create_shader_program(filenames, shader_types, array_count(shader_types), &cell_instancing->shader_program);
glUseProgram(cell_instancing->shader_program);
cell_instancing->vao = create_vao();
glBindVertexArray(cell_instancing->vao);
setup_cell_vertex_vbo(&cell_instancing->cell_vertex_vbo, CELL_VERTICES, array_count(CELL_VERTICES));
setup_cell_vertex_ibo(&cell_instancing->cell_vertex_ibo, CELL_TRIANGLE_INDICES, array_count(CELL_TRIANGLE_INDICES));
setup_cell_instances_vbo(&cell_instancing->cell_instances_vbo);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
CellUniforms *uniforms = &cell_instancing->uniforms;
uniforms->mat4_projection_matrix.name = u8("projection_matrix");
uniforms->int_render_origin_cell_x.name = u8("render_origin_cell_x");
uniforms->int_render_origin_cell_y.name = u8("render_origin_cell_y");
uniforms->vec2_render_origin_offset.name = u8("render_origin_offset");
uniforms->float_scale.name = u8("scale");
success &= get_uniform_locations(cell_instancing->shader_program, uniforms->array, array_count(uniforms->array));
success &= print_gl_errors();
log(L_CellInstancing, u8("OpenGL VBOs setup finished."));
return success;
}