| 42 } |
42 } |
| 43 asc_error_catch_all_gl(); |
43 asc_error_catch_all_gl(); |
| 44 } |
44 } |
| 45 |
45 |
| 46 void asc_mesh_free_buffers(AscMesh *mesh, unsigned count) { |
46 void asc_mesh_free_buffers(AscMesh *mesh, unsigned count) { |
| |
47 if (count == 1 && mesh->vbo == 0) return; // hack to skip this function until we remove it |
| 47 asc_dprintf("Free mesh buffers for %u meshes.", count); |
48 asc_dprintf("Free mesh buffers for %u meshes.", count); |
| 48 GLuint buffers[count]; |
49 GLuint buffers[count]; |
| 49 GLuint arrays[count]; |
50 GLuint arrays[count]; |
| 50 |
51 |
| 51 for (unsigned i = 0; i < count; i++) { |
52 for (unsigned i = 0; i < count; i++) { |
| 58 glDeleteBuffers(count, buffers); |
59 glDeleteBuffers(count, buffers); |
| 59 glDeleteVertexArrays(count, arrays); |
60 glDeleteVertexArrays(count, arrays); |
| 60 asc_error_catch_all_gl(); |
61 asc_error_catch_all_gl(); |
| 61 } |
62 } |
| 62 |
63 |
| 63 void asc_mesh_draw_triangle_strip(AscMesh *mesh) { |
64 void asc_mesh_draw_triangle_strip(const AscMesh *mesh) { |
| 64 glBindVertexArray(mesh->vao); |
65 glBindVertexArray(mesh->vao); |
| 65 glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vertices); |
66 glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vtx_count); |
| 66 #ifndef NDEBUG |
67 #ifndef NDEBUG |
| 67 // only unbind in debug mode to detect accidental re-use of the wrong VAO |
68 // only unbind in debug mode to detect accidental re-use of the wrong VAO |
| 68 glBindVertexArray(0); |
69 glBindVertexArray(0); |
| 69 #endif |
70 #endif |
| 70 } |
71 } |
| |
72 |
| |
73 void asc_mesh_destroy(AscMesh *mesh) { |
| |
74 asc_mesh_free_buffers(mesh, 1); |
| |
75 free(mesh->vtx_data); |
| |
76 } |
| |
77 |
| |
78 void asc_mesh_plane_2d(AscMesh *mesh) { |
| |
79 if (mesh->vbo == 0) { |
| |
80 asc_mesh_allocate_buffers(mesh, 1); |
| |
81 } |
| |
82 free(mesh->vtx_data); |
| |
83 asc_dprintf("Create plane in VBO %u and VAO %u", mesh->vbo, mesh->vao); |
| |
84 mesh->vtx_count = 4; |
| |
85 AscMeshVertex2d data[4] = { |
| |
86 { |
| |
87 .pos = {{0.0f, 0.0f}}, |
| |
88 .uv = {{0.0f, 0.0f}}, |
| |
89 } , // bottom left |
| |
90 { |
| |
91 .pos = {{0.0f, 1.0f}}, |
| |
92 .uv = {{0.0f, 1.0f}}, |
| |
93 }, // top left |
| |
94 { |
| |
95 .pos = {{1.0f, 0.0f}}, |
| |
96 .uv = {{1.0f, 0.0f}}, |
| |
97 }, // bottom right |
| |
98 { |
| |
99 .pos = {{1.0f, 1.0f}}, |
| |
100 .uv = {{1.0f, 1.0f}}, |
| |
101 } // top right |
| |
102 }; |
| |
103 mesh->vtx_data = malloc(sizeof(data)); |
| |
104 memcpy(mesh->vtx_data, data, sizeof(data)); |
| |
105 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); |
| |
106 glBufferData(GL_ARRAY_BUFFER, sizeof(data), mesh->vtx_data, GL_STATIC_DRAW); |
| |
107 glBindVertexArray(mesh->vao); |
| |
108 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, pos)); |
| |
109 glEnableVertexAttribArray(0); |
| |
110 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, uv)); |
| |
111 glEnableVertexAttribArray(1); |
| |
112 } |