80 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) { |
81 if (mesh->vbo == 0) { |
81 if (mesh->vbo == 0) { |
82 asc_mesh_allocate_buffers(mesh, 1); |
82 asc_mesh_allocate_buffers(mesh, 1); |
83 } |
83 } |
84 |
84 |
85 unsigned required_memory = 4 * sizeof(AscMeshVertex2d); |
85 unsigned required_memory = 4 * sizeof(asc_vertex2d); |
86 |
86 |
87 // free any previous data |
87 // free any previous data |
88 if (mesh->vtx_data && mesh->vtx_data_size < required_memory) { |
88 if (mesh->vtx_data && mesh->vtx_data_size < required_memory) { |
89 cxFreeDefault(mesh->vtx_data); |
89 cxFreeDefault(mesh->vtx_data); |
90 mesh->vtx_data = NULL; |
90 mesh->vtx_data = NULL; |
91 } |
91 } |
92 |
92 |
93 // allocate memory |
93 // allocate memory |
94 AscMeshVertex2d *data; |
94 asc_vertex2d *data; |
95 if (mesh->vtx_data == NULL) { |
95 if (mesh->vtx_data == NULL) { |
96 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); |
97 mesh->vtx_data_size = required_memory; |
97 mesh->vtx_data_size = required_memory; |
98 data = cxMallocDefault(mesh->vtx_data_size); |
98 data = cxMallocDefault(mesh->vtx_data_size); |
99 mesh->vtx_data = (float*) data; |
99 mesh->vtx_data = (float*) data; |
100 } else { |
100 } else { |
101 data = (AscMeshVertex2d*) mesh->vtx_data; |
101 data = (asc_vertex2d*) mesh->vtx_data; |
102 } |
102 } |
103 mesh->vtx_count = 4; |
103 mesh->vtx_count = 4; |
104 |
104 |
105 // default values |
105 // default values |
106 args.size.x = ASC_NONZERO_OR(1.f, args.size.x); |
106 args.size.x = ASC_NONZERO_OR(1.f, args.size.x); |
122 data[3].uv = asc_vec2f_new(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y); |
122 data[3].uv = asc_vec2f_new(args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y); |
123 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); |
123 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); |
124 glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW); |
124 glBufferData(GL_ARRAY_BUFFER, mesh->vtx_data_size, mesh->vtx_data, GL_STATIC_DRAW); |
125 // TODO: this should not be repeated for every adjustment - but it will be moved to the batch renderer anyway |
125 // TODO: this should not be repeated for every adjustment - but it will be moved to the batch renderer anyway |
126 glBindVertexArray(mesh->vao); |
126 glBindVertexArray(mesh->vao); |
127 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, pos)); |
127 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, pos)); |
128 glEnableVertexAttribArray(0); |
128 glEnableVertexAttribArray(0); |
129 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, uv)); |
129 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(asc_vertex2d), (void*)offsetof(asc_vertex2d, uv)); |
130 glEnableVertexAttribArray(1); |
130 glEnableVertexAttribArray(1); |
131 } |
131 } |