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