src/mesh.c

changeset 221
14eddd43b3f7
parent 220
6b266e907f89
equal deleted inserted replaced
220:6b266e907f89 221:14eddd43b3f7
40 glGenVertexArrays(count, arrays); 40 glGenVertexArrays(count, arrays);
41 for (unsigned i = 0; i < count; i++) { 41 for (unsigned i = 0; i < count; i++) {
42 mesh[i].vbo = buffers[i]; 42 mesh[i].vbo = buffers[i];
43 mesh[i].vao = arrays[i]; 43 mesh[i].vao = arrays[i];
44 } 44 }
45 return asc_error_catch_all_gl(); 45 return asc_error_catch_gl("Allocating mesh buffers");
46 } 46 }
47 47
48 void asc_mesh_free_buffers(AscMesh *mesh, unsigned count) { 48 void asc_mesh_free_buffers(AscMesh *mesh, unsigned count) {
49 if (count == 1 && mesh->vbo == 0) return; // hack to skip this function until we remove it 49 if (count == 1 && mesh->vbo == 0) return; // hack to skip this function until we remove it
50 asc_dprintf("Free mesh buffers for %u meshes.", count); 50 asc_dprintf("Free mesh buffers for %u meshes.", count);
56 arrays[i] = mesh[i].vao; 56 arrays[i] = mesh[i].vao;
57 mesh[i].vbo = 0; 57 mesh[i].vbo = 0;
58 mesh[i].vao = 0; 58 mesh[i].vao = 0;
59 } 59 }
60 60
61 asc_error_catch_gl("OpenGL has unchecked error flags before deleting mesh buffers");
61 glDeleteBuffers(count, buffers); 62 glDeleteBuffers(count, buffers);
62 glDeleteVertexArrays(count, arrays); 63 glDeleteVertexArrays(count, arrays);
63 asc_error_catch_all_gl(); 64 asc_error_catch_gl("Deleting mesh buffers");
64 } 65 }
65 66
66 void asc_mesh_draw_triangle_strip(const AscMesh *mesh) { 67 void asc_mesh_draw_triangle_strip(const AscMesh *mesh) {
67 glBindVertexArray(mesh->vao); 68 glBindVertexArray(mesh->vao);
68 glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vtx_count); 69 glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vtx_count);
69 asc_error_catch_all_gl(); 70 asc_error_catch_gl("Drawing mesh");
70 #ifndef NDEBUG 71 #ifndef NDEBUG
71 // only unbind in debug mode to detect accidental re-use of the wrong VAO 72 // only unbind in debug mode to detect accidental re-use of the wrong VAO
72 glBindVertexArray(0); 73 glBindVertexArray(0);
73 #endif 74 #endif
74 } 75 }
90 glEnableVertexAttribArray(1); 91 glEnableVertexAttribArray(1);
91 } else { 92 } else {
92 // only bind the buffer for updating the data 93 // only bind the buffer for updating the data
93 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); 94 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
94 } 95 }
96 if (asc_error_catch_gl("Binding VBO or VAO")) return;
95 97
96 unsigned required_memory = 4 * sizeof(asc_vertex2d); 98 unsigned required_memory = 4 * sizeof(asc_vertex2d);
97 99
98 // free any previous data 100 // free any previous data
99 if (mesh->vtx_data && mesh->vtx_data_size < required_memory) { 101 if (mesh->vtx_data && mesh->vtx_data_size < required_memory) {
130 data[2].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y); 132 data[2].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y);
131 // top right 133 // top right
132 data[3].pos = ASC_VEC2F(args.size.x, args.size.y); 134 data[3].pos = ASC_VEC2F(args.size.x, args.size.y);
133 data[3].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y); 135 data[3].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y);
134 glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW); 136 glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW);
135 137 asc_error_catch_gl("Writing VBO data");
136 // TODO: replace with specific error handling for setting the buffer
137 asc_error_catch_all_gl();
138 } 138 }

mercurial