src/primitives.c

changeset 16
c5dde81b6fb2
parent 14
9dbfc0031887
child 44
b3da4096c607
equal deleted inserted replaced
15:362b7659dc76 16:c5dde81b6fb2
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * Copyright 2023 Mike Becker. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "ascension/primitives.h"
29 #include "ascension/error.h"
30 #include "ascension/context.h"
31
32 #include <string.h>
33 #include <GL/glew.h>
34
35 static void asc_primitives_init_plane(AscMesh *mesh) {
36 asc_dprintf("Create primitive plane in VBO %u and VAO %u", mesh->vbo, mesh->vao);
37 mesh->vertices = 4;
38 float data[8] = {
39 0.0f, 0.0f, // bottom left
40 0.0f, 1.0f, // top left
41 1.0f, 0.0f, // bottom right
42 1.0f, 1.0f // top right
43 };
44 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
45 glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
46 glBindVertexArray(mesh->vao);
47 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
48 glEnableVertexAttribArray(0);
49 }
50
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) {
90 AscMesh const *mesh = &(asc_context.active_window->primitives.plane);
91 glBindVertexArray(mesh->vao);
92 glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vertices);
93 }

mercurial