Mon, 18 Dec 2023 19:04:44 +0100
add first draft of a scene graph structure
universe@3 | 1 | /* |
universe@3 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@3 | 3 | * Copyright 2023 Mike Becker. All rights reserved. |
universe@3 | 4 | * |
universe@3 | 5 | * Redistribution and use in source and binary forms, with or without |
universe@3 | 6 | * modification, are permitted provided that the following conditions are met: |
universe@3 | 7 | * |
universe@3 | 8 | * 1. Redistributions of source code must retain the above copyright |
universe@3 | 9 | * notice, this list of conditions and the following disclaimer. |
universe@3 | 10 | * |
universe@3 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@3 | 12 | * notice, this list of conditions and the following disclaimer in the |
universe@3 | 13 | * documentation and/or other materials provided with the distribution. |
universe@3 | 14 | * |
universe@3 | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@3 | 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@3 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@3 | 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@3 | 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@3 | 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@3 | 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@3 | 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@3 | 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@3 | 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@3 | 25 | * POSSIBILITY OF SUCH DAMAGE. |
universe@3 | 26 | */ |
universe@3 | 27 | |
universe@16 | 28 | #include "ascension/primitives.h" |
universe@16 | 29 | #include "ascension/error.h" |
universe@16 | 30 | #include "ascension/context.h" |
universe@3 | 31 | |
universe@16 | 32 | #include <string.h> |
universe@16 | 33 | #include <GL/glew.h> |
universe@11 | 34 | |
universe@16 | 35 | static void asc_primitives_init_plane(AscMesh *mesh) { |
universe@16 | 36 | asc_dprintf("Create primitive plane in VBO %u and VAO %u", mesh->vbo, mesh->vao); |
universe@16 | 37 | mesh->vertices = 4; |
universe@16 | 38 | float data[8] = { |
universe@16 | 39 | 0.0f, 0.0f, // bottom left |
universe@16 | 40 | 0.0f, 1.0f, // top left |
universe@16 | 41 | 1.0f, 0.0f, // bottom right |
universe@16 | 42 | 1.0f, 1.0f // top right |
universe@16 | 43 | }; |
universe@16 | 44 | glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); |
universe@16 | 45 | glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW); |
universe@16 | 46 | glBindVertexArray(mesh->vao); |
universe@16 | 47 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); |
universe@16 | 48 | glEnableVertexAttribArray(0); |
universe@16 | 49 | } |
universe@4 | 50 | |
universe@16 | 51 | bool asc_primitives_init(AscPrimitives *primitives) { |
universe@16 | 52 | asc_dprintf("Create primitives for the GL context of active window."); |
universe@16 | 53 | // TODO: more primitives |
universe@4 | 54 | |
universe@16 | 55 | GLuint buffers[1]; |
universe@16 | 56 | GLuint arrays[1]; |
universe@16 | 57 | glGenBuffers(1, buffers); |
universe@16 | 58 | glGenVertexArrays(1, arrays); |
universe@4 | 59 | |
universe@16 | 60 | AscMesh *plane = &(primitives->plane); |
universe@16 | 61 | plane->vbo = buffers[0]; |
universe@16 | 62 | plane->vao = arrays[0]; |
universe@16 | 63 | asc_primitives_init_plane(plane); |
universe@11 | 64 | |
universe@16 | 65 | GLenum error = glGetError(); |
universe@16 | 66 | if (error == GL_NO_ERROR) { |
universe@16 | 67 | return true; |
universe@16 | 68 | } else { |
universe@16 | 69 | asc_error_gl(error, CX_STR("Initialization of primitive meshes failed.")); |
universe@16 | 70 | return false; |
universe@16 | 71 | } |
universe@16 | 72 | } |
universe@11 | 73 | |
universe@16 | 74 | void asc_primitives_destroy(AscPrimitives *primitives) { |
universe@16 | 75 | asc_dprintf("Destroy primitives in GL context of active window."); |
universe@3 | 76 | |
universe@16 | 77 | GLuint buffers[1]; |
universe@16 | 78 | GLuint arrays[1]; |
universe@16 | 79 | |
universe@16 | 80 | buffers[0] = primitives->plane.vbo; |
universe@16 | 81 | arrays[0] = primitives->plane.vao; |
universe@16 | 82 | |
universe@16 | 83 | glDeleteBuffers(1, buffers); |
universe@16 | 84 | glDeleteVertexArrays(1, arrays); |
universe@16 | 85 | |
universe@16 | 86 | memset(primitives, 0, sizeof(AscPrimitives)); |
universe@16 | 87 | } |
universe@16 | 88 | |
universe@16 | 89 | void asc_primitives_draw_plane(void) { |
universe@16 | 90 | AscMesh const *mesh = &(asc_context.active_window->primitives.plane); |
universe@16 | 91 | glBindVertexArray(mesh->vao); |
universe@16 | 92 | glDrawArrays(GL_TRIANGLE_STRIP, 0, mesh->vertices); |
universe@16 | 93 | } |