| 27 |
27 |
| 28 #include "ascension/primitives.h" |
28 #include "ascension/primitives.h" |
| 29 #include "ascension/error.h" |
29 #include "ascension/error.h" |
| 30 #include "ascension/context.h" |
30 #include "ascension/context.h" |
| 31 |
31 |
| 32 #include <string.h> |
|
| 33 #include <GL/glew.h> |
32 #include <GL/glew.h> |
| 34 |
33 |
| 35 static void asc_primitives_init_plane(AscMesh *mesh) { |
34 void asc_primitives_init_plane(AscMesh *mesh) { |
| 36 asc_dprintf("Create primitive plane in VBO %u and VAO %u", mesh->vbo, mesh->vao); |
35 if (mesh->vbo == 0) { |
| |
36 if (mesh->vao > 0) { |
| |
37 asc_dprintf("!!! Mesh with VAO %u has no VBO - this is most likely a programming error !!!", mesh->vao); |
| |
38 } |
| |
39 asc_mesh_allocate_buffers(mesh, 1); |
| |
40 } |
| |
41 asc_dprintf("Create plane in VBO %u and VAO %u", mesh->vbo, mesh->vao); |
| 37 mesh->vertices = 4; |
42 mesh->vertices = 4; |
| 38 float data[8] = { |
43 float data[8] = { |
| 39 0.0f, 0.0f, // bottom left |
44 0.0f, 0.0f, // bottom left |
| 40 0.0f, 1.0f, // top left |
45 0.0f, 1.0f, // top left |
| 41 1.0f, 0.0f, // bottom right |
46 1.0f, 0.0f, // bottom right |
| 46 glBindVertexArray(mesh->vao); |
51 glBindVertexArray(mesh->vao); |
| 47 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); |
52 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); |
| 48 glEnableVertexAttribArray(0); |
53 glEnableVertexAttribArray(0); |
| 49 } |
54 } |
| 50 |
55 |
| 51 bool asc_primitives_init(AscPrimitives *primitives) { |
|
| 52 asc_dprintf("Create primitives for the GL context of active window."); |
|
| 53 // TODO: more primitives |
|
| 54 |
|
| 55 GLuint buffers[1]; |
|
| 56 GLuint arrays[1]; |
|
| 57 glGenBuffers(1, buffers); |
|
| 58 glGenVertexArrays(1, arrays); |
|
| 59 |
|
| 60 AscMesh *plane = &(primitives->plane); |
|
| 61 plane->vbo = buffers[0]; |
|
| 62 plane->vao = arrays[0]; |
|
| 63 asc_primitives_init_plane(plane); |
|
| 64 |
|
| 65 GLenum error = glGetError(); |
|
| 66 if (error == GL_NO_ERROR) { |
|
| 67 return true; |
|
| 68 } else { |
|
| 69 asc_error_gl(error, CX_STR("Initialization of primitive meshes failed.")); |
|
| 70 return false; |
|
| 71 } |
|
| 72 } |
|
| 73 |
|
| 74 void asc_primitives_destroy(AscPrimitives *primitives) { |
|
| 75 asc_dprintf("Destroy primitives in GL context of active window."); |
|
| 76 |
|
| 77 GLuint buffers[1]; |
|
| 78 GLuint arrays[1]; |
|
| 79 |
|
| 80 buffers[0] = primitives->plane.vbo; |
|
| 81 arrays[0] = primitives->plane.vao; |
|
| 82 |
|
| 83 glDeleteBuffers(1, buffers); |
|
| 84 glDeleteVertexArrays(1, arrays); |
|
| 85 |
|
| 86 memset(primitives, 0, sizeof(AscPrimitives)); |
|
| 87 } |
|
| 88 |
|
| 89 void asc_primitives_draw_plane(void) { |
56 void asc_primitives_draw_plane(void) { |
| 90 AscMesh const *mesh = &(asc_active_window->glctx.primitives.plane); |
57 AscMesh const *mesh = &(asc_active_window->glctx.primitives.plane); |
| 91 glBindVertexArray(mesh->vao); |
58 glBindVertexArray(mesh->vao); |
| 92 glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vertices); |
59 glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vertices); |
| 93 } |
60 } |