25 * POSSIBILITY OF SUCH DAMAGE. |
25 * POSSIBILITY OF SUCH DAMAGE. |
26 */ |
26 */ |
27 |
27 |
28 #include "ascension/error.h" |
28 #include "ascension/error.h" |
29 #include "ascension/mesh.h" |
29 #include "ascension/mesh.h" |
|
30 |
|
31 #include <cx/allocator.h> |
30 |
32 |
31 #include <GL/glew.h> |
33 #include <GL/glew.h> |
32 |
34 |
33 void asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count) { |
35 void asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count) { |
34 asc_dprintf("Allocate mesh buffers for %u meshes.", count); |
36 asc_dprintf("Allocate mesh buffers for %u meshes.", count); |
70 #endif |
72 #endif |
71 } |
73 } |
72 |
74 |
73 void asc_mesh_destroy(AscMesh *mesh) { |
75 void asc_mesh_destroy(AscMesh *mesh) { |
74 asc_mesh_free_buffers(mesh, 1); |
76 asc_mesh_free_buffers(mesh, 1); |
75 free(mesh->vtx_data); |
77 cxFreeDefault(mesh->vtx_data); |
76 } |
78 } |
77 |
79 |
78 void asc_mesh_init_plane_2d(AscMesh *mesh, struct asc_mesh_init_plane_2d_args args) { |
80 void asc_mesh_init_plane_2d(AscMesh *mesh, struct asc_mesh_init_plane_2d_args args) { |
79 if (mesh->vbo == 0) { |
81 if (mesh->vbo == 0) { |
80 asc_mesh_allocate_buffers(mesh, 1); |
82 asc_mesh_allocate_buffers(mesh, 1); |
82 |
84 |
83 unsigned required_memory = 4 * sizeof(AscMeshVertex2d); |
85 unsigned required_memory = 4 * sizeof(AscMeshVertex2d); |
84 |
86 |
85 // free any previous data |
87 // free any previous data |
86 if (mesh->vtx_data && mesh->vtx_data_size < required_memory) { |
88 if (mesh->vtx_data && mesh->vtx_data_size < required_memory) { |
87 free(mesh->vtx_data); |
89 cxFreeDefault(mesh->vtx_data); |
88 mesh->vtx_data = NULL; |
90 mesh->vtx_data = NULL; |
89 } |
91 } |
90 |
92 |
91 // allocate memory |
93 // allocate memory |
92 AscMeshVertex2d *data; |
94 AscMeshVertex2d *data; |
93 if (mesh->vtx_data == NULL) { |
95 if (mesh->vtx_data == NULL) { |
94 asc_dprintf("Create plane in VBO %u and VAO %u", mesh->vbo, mesh->vao); |
96 asc_dprintf("Create plane in VBO %u and VAO %u", mesh->vbo, mesh->vao); |
95 mesh->vtx_data_size = required_memory; |
97 mesh->vtx_data_size = required_memory; |
96 data = malloc(mesh->vtx_data_size); |
98 data = cxMallocDefault(mesh->vtx_data_size); |
97 mesh->vtx_data = (float*) data; |
99 mesh->vtx_data = (float*) data; |
98 } else { |
100 } else { |
99 data = (AscMeshVertex2d*) mesh->vtx_data; |
101 data = (AscMeshVertex2d*) mesh->vtx_data; |
100 } |
102 } |
101 mesh->vtx_count = 4; |
103 mesh->vtx_count = 4; |