src/mesh.c

changeset 118
830608f7e7d9
parent 117
d1267f656a97
equal deleted inserted replaced
117:d1267f656a97 118:830608f7e7d9
78 void asc_mesh_init_plane_2d(AscMesh *mesh, struct asc_mesh_init_plane_2d_args args) { 78 void asc_mesh_init_plane_2d(AscMesh *mesh, struct asc_mesh_init_plane_2d_args args) {
79 if (mesh->vbo == 0) { 79 if (mesh->vbo == 0) {
80 asc_mesh_allocate_buffers(mesh, 1); 80 asc_mesh_allocate_buffers(mesh, 1);
81 } 81 }
82 82
83 unsigned required_memory = 4 * sizeof(AscMeshVertex2d);
84
83 // free any previous data 85 // free any previous data
84 free(mesh->vtx_data); 86 if (mesh->vtx_data && mesh->vtx_data_size < required_memory) {
87 free(mesh->vtx_data);
88 mesh->vtx_data = NULL;
89 }
90
91 // allocate memory
92 AscMeshVertex2d *data;
93 if (mesh->vtx_data == NULL) {
94 asc_dprintf("Create plane in VBO %u and VAO %u", mesh->vbo, mesh->vao);
95 mesh->vtx_data_size = required_memory;
96 data = malloc(mesh->vtx_data_size);
97 mesh->vtx_data = (float*) data;
98 } else {
99 data = (AscMeshVertex2d*) mesh->vtx_data;
100 }
101 mesh->vtx_count = 4;
85 102
86 // default values 103 // default values
87 args.size.x = ASC_NONZERO_OR(1.f, args.size.x); 104 args.size.x = ASC_NONZERO_OR(1.f, args.size.x);
88 args.size.y = ASC_NONZERO_OR(1.f, args.size.y); 105 args.size.y = ASC_NONZERO_OR(1.f, args.size.y);
89 args.uv_scale.x = ASC_NONZERO_OR(1.f, args.uv_scale.x); 106 args.uv_scale.x = ASC_NONZERO_OR(1.f, args.uv_scale.x);
90 args.uv_scale.y = ASC_NONZERO_OR(1.f, args.uv_scale.y); 107 args.uv_scale.y = ASC_NONZERO_OR(1.f, args.uv_scale.y);
91 108
92 asc_dprintf("Create plane in VBO %u and VAO %u", mesh->vbo, mesh->vao); 109 // bottom left
93 mesh->vtx_count = 4; 110 data[0].pos = asc_vec2f_new(0.0f, 0.0f);
94 AscMeshVertex2d data[4] = { 111 data[0].uv = asc_vec2f_new(args.uv_offset.x, args.uv_offset.y);
95 { 112 // top left
96 .pos = {{0.0f, 0.0f}}, 113 data[1].pos = asc_vec2f_new(0.0f, args.size.y);
97 .uv = {{args.uv_offset.x, args.uv_offset.y}}, 114 data[1].uv = asc_vec2f_new(args.uv_offset.x, args.uv_offset.y + args.uv_scale.y);
98 } , // bottom left 115 // bottom right
99 { 116 data[2].pos = asc_vec2f_new(args.size.x, 0.0f);
100 .pos = {{0.0f, args.size.y}}, 117 data[2].uv = asc_vec2f_new(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y);
101 .uv = {{args.uv_offset.x, args.uv_offset.y + args.uv_scale.y}}, 118 // top right
102 }, // top left 119 data[3].pos = asc_vec2f_new(args.size.x, args.size.y);
103 { 120 data[3].uv = asc_vec2f_new(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y);
104 .pos = {{args.size.x, 0.0f}},
105 .uv = {{args.uv_offset.x + args.uv_scale.x, args.uv_offset.y}},
106 }, // bottom right
107 {
108 .pos = {{args.size.x, args.size.y}},
109 .uv = {{args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y}},
110 } // top right
111 };
112 mesh->vtx_data = malloc(sizeof(data));
113 memcpy(mesh->vtx_data, data, sizeof(data));
114 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); 121 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
115 glBufferData(GL_ARRAY_BUFFER, sizeof(data), mesh->vtx_data, GL_STATIC_DRAW); 122 glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW);
116 // TODO: this should not be repeated for every adjustment - but it will be moved to the batch renderer anyway 123 // TODO: this should not be repeated for every adjustment - but it will be moved to the batch renderer anyway
117 glBindVertexArray(mesh->vao); 124 glBindVertexArray(mesh->vao);
118 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, pos)); 125 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, pos));
119 glEnableVertexAttribArray(0); 126 glEnableVertexAttribArray(0);
120 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, uv)); 127 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, uv));

mercurial