src/mesh.c

changeset 220
6b266e907f89
parent 208
658bccb1bf73
child 221
14eddd43b3f7
equal deleted inserted replaced
219:62508d957a22 220:6b266e907f89
30 30
31 #include <cx/allocator.h> 31 #include <cx/allocator.h>
32 32
33 #include <GL/glew.h> 33 #include <GL/glew.h>
34 34
35 void asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count) { 35 int asc_mesh_allocate_buffers(AscMesh *mesh, unsigned count) {
36 asc_dprintf("Allocate mesh buffers for %u meshes.", count); 36 asc_dprintf("Allocate mesh buffers for %u meshes.", count);
37 GLuint buffers[count]; 37 GLuint buffers[count];
38 GLuint arrays[count]; 38 GLuint arrays[count];
39 glGenBuffers(count, buffers); 39 glGenBuffers(count, buffers);
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 asc_error_catch_all_gl(); 45 return asc_error_catch_all_gl();
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);
77 asc_mesh_free_buffers(mesh, 1); 77 asc_mesh_free_buffers(mesh, 1);
78 cxFreeDefault(mesh->vtx_data); 78 cxFreeDefault(mesh->vtx_data);
79 } 79 }
80 80
81 void asc_mesh_init_plane_2d(AscMesh *mesh, struct asc_mesh_init_plane_2d_args args) { 81 void asc_mesh_init_plane_2d(AscMesh *mesh, struct asc_mesh_init_plane_2d_args args) {
82 if (mesh->vbo == 0) { 82 if (mesh->vao == 0) {
83 asc_mesh_allocate_buffers(mesh, 1); 83 if (asc_mesh_allocate_buffers(mesh, 1)) return;
84 // bind the buffer and configure the vertex attributes
85 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
86 glBindVertexArray(mesh->vao);
87 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, pos));
88 glEnableVertexAttribArray(0);
89 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, uv));
90 glEnableVertexAttribArray(1);
91 } else {
92 // only bind the buffer for updating the data
93 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
84 } 94 }
85 95
86 unsigned required_memory = 4 * sizeof(asc_vertex2d); 96 unsigned required_memory = 4 * sizeof(asc_vertex2d);
87 97
88 // free any previous data 98 // free any previous data
119 data[2].pos = ASC_VEC2F(args.size.x, 0.0f); 129 data[2].pos = ASC_VEC2F(args.size.x, 0.0f);
120 data[2].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y); 130 data[2].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y);
121 // top right 131 // top right
122 data[3].pos = ASC_VEC2F(args.size.x, args.size.y); 132 data[3].pos = ASC_VEC2F(args.size.x, args.size.y);
123 data[3].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y); 133 data[3].uv = ASC_VEC2F(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y);
124 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
125 glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW); 134 glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW);
126 // TODO: this should not be repeated for every adjustment - but it will be moved to the batch renderer anyway
127 glBindVertexArray(mesh->vao);
128 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, pos));
129 glEnableVertexAttribArray(0);
130 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, uv));
131 glEnableVertexAttribArray(1);
132 135
136 // TODO: replace with specific error handling for setting the buffer
133 asc_error_catch_all_gl(); 137 asc_error_catch_all_gl();
134 } 138 }

mercurial