73 void asc_mesh_destroy(AscMesh *mesh) { |
73 void asc_mesh_destroy(AscMesh *mesh) { |
74 asc_mesh_free_buffers(mesh, 1); |
74 asc_mesh_free_buffers(mesh, 1); |
75 free(mesh->vtx_data); |
75 free(mesh->vtx_data); |
76 } |
76 } |
77 |
77 |
78 void asc_mesh_plane_2d(AscMesh *mesh) { |
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 |
|
83 // free any previous data |
82 free(mesh->vtx_data); |
84 free(mesh->vtx_data); |
|
85 |
|
86 // default values |
|
87 if (args.size.x == 0.0f) args.size.x = 1.0f; |
|
88 if (args.size.y == 0.0f) args.size.y = 1.0f; |
|
89 if (args.uv_scale.x == 0.0f) args.uv_scale.x = 1.0f; |
|
90 if (args.uv_scale.y == 0.0f) args.uv_scale.y = 1.0f; |
|
91 |
83 asc_dprintf("Create plane in VBO %u and VAO %u", mesh->vbo, mesh->vao); |
92 asc_dprintf("Create plane in VBO %u and VAO %u", mesh->vbo, mesh->vao); |
84 mesh->vtx_count = 4; |
93 mesh->vtx_count = 4; |
85 AscMeshVertex2d data[4] = { |
94 AscMeshVertex2d data[4] = { |
86 { |
95 { |
87 .pos = {{0.0f, 0.0f}}, |
96 .pos = {{0.0f, 0.0f}}, |
88 .uv = {{0.0f, 0.0f}}, |
97 .uv = {{args.uv_offset.x, args.uv_offset.y}}, |
89 } , // bottom left |
98 } , // bottom left |
90 { |
99 { |
91 .pos = {{0.0f, 1.0f}}, |
100 .pos = {{0.0f, args.size.y}}, |
92 .uv = {{0.0f, 1.0f}}, |
101 .uv = {{args.uv_offset.x, args.uv_offset.y + args.uv_scale.y}}, |
93 }, // top left |
102 }, // top left |
94 { |
103 { |
95 .pos = {{1.0f, 0.0f}}, |
104 .pos = {{args.size.x, 0.0f}}, |
96 .uv = {{1.0f, 0.0f}}, |
105 .uv = {{args.uv_offset.x + args.uv_scale.x, args.uv_offset.y}}, |
97 }, // bottom right |
106 }, // bottom right |
98 { |
107 { |
99 .pos = {{1.0f, 1.0f}}, |
108 .pos = {{args.size.x, args.size.y}}, |
100 .uv = {{1.0f, 1.0f}}, |
109 .uv = {{args.uv_offset.x + args.uv_scale.x, args.uv_offset.y + args.uv_scale.y}}, |
101 } // top right |
110 } // top right |
102 }; |
111 }; |
103 mesh->vtx_data = malloc(sizeof(data)); |
112 mesh->vtx_data = malloc(sizeof(data)); |
104 memcpy(mesh->vtx_data, data, sizeof(data)); |
113 memcpy(mesh->vtx_data, data, sizeof(data)); |
105 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); |
114 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); |
106 glBufferData(GL_ARRAY_BUFFER, sizeof(data), mesh->vtx_data, GL_STATIC_DRAW); |
115 glBufferData(GL_ARRAY_BUFFER, sizeof(data), 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 |
107 glBindVertexArray(mesh->vao); |
117 glBindVertexArray(mesh->vao); |
108 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, pos)); |
118 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, pos)); |
109 glEnableVertexAttribArray(0); |
119 glEnableVertexAttribArray(0); |
110 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, uv)); |
120 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(AscMeshVertex2d), (void*)offsetof(AscMeshVertex2d, uv)); |
111 glEnableVertexAttribArray(1); |
121 glEnableVertexAttribArray(1); |