src/primitives.c

changeset 16
c5dde81b6fb2
parent 14
9dbfc0031887
child 44
b3da4096c607
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/primitives.c	Wed Nov 15 22:51:40 2023 +0100
@@ -0,0 +1,93 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ * Copyright 2023 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "ascension/primitives.h"
+#include "ascension/error.h"
+#include "ascension/context.h"
+
+#include <string.h>
+#include <GL/glew.h>
+
+static void asc_primitives_init_plane(AscMesh *mesh) {
+    asc_dprintf("Create primitive plane in VBO %u and VAO %u", mesh->vbo, mesh->vao);
+    mesh->vertices = 4;
+    float data[8] = {
+            0.0f, 0.0f, // bottom left
+            0.0f, 1.0f,    // top left
+            1.0f, 0.0f, // bottom right
+            1.0f, 1.0f    // top right
+    };
+    glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo);
+    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
+    glBindVertexArray(mesh->vao);
+    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
+    glEnableVertexAttribArray(0);
+}
+
+bool asc_primitives_init(AscPrimitives *primitives) {
+    asc_dprintf("Create primitives for the GL context of active window.");
+    // TODO: more primitives
+
+    GLuint buffers[1];
+    GLuint arrays[1];
+    glGenBuffers(1, buffers);
+    glGenVertexArrays(1, arrays);
+
+    AscMesh *plane = &(primitives->plane);
+    plane->vbo = buffers[0];
+    plane->vao = arrays[0];
+    asc_primitives_init_plane(plane);
+
+    GLenum error = glGetError();
+    if (error == GL_NO_ERROR) {
+        return true;
+    } else {
+        asc_error_gl(error, CX_STR("Initialization of primitive meshes failed."));
+        return false;
+    }
+}
+
+void asc_primitives_destroy(AscPrimitives *primitives) {
+    asc_dprintf("Destroy primitives in GL context of active window.");
+
+    GLuint buffers[1];
+    GLuint arrays[1];
+
+    buffers[0] = primitives->plane.vbo;
+    arrays[0] = primitives->plane.vao;
+
+    glDeleteBuffers(1, buffers);
+    glDeleteVertexArrays(1, arrays);
+
+    memset(primitives, 0, sizeof(AscPrimitives));
+}
+
+void asc_primitives_draw_plane(void) {
+    AscMesh const *mesh = &(asc_context.active_window->primitives.plane);
+    glBindVertexArray(mesh->vao);
+    glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vertices);
+}
\ No newline at end of file

mercurial