Mon, 18 Dec 2023 19:04:44 +0100
add first draft of a scene graph structure
universe@15 | 1 | /* |
universe@15 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@15 | 3 | * Copyright 2023 Mike Becker. All rights reserved. |
universe@15 | 4 | * |
universe@15 | 5 | * Redistribution and use in source and binary forms, with or without |
universe@15 | 6 | * modification, are permitted provided that the following conditions are met: |
universe@15 | 7 | * |
universe@15 | 8 | * 1. Redistributions of source code must retain the above copyright |
universe@15 | 9 | * notice, this list of conditions and the following disclaimer. |
universe@15 | 10 | * |
universe@15 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@15 | 12 | * notice, this list of conditions and the following disclaimer in the |
universe@15 | 13 | * documentation and/or other materials provided with the distribution. |
universe@15 | 14 | * |
universe@15 | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@15 | 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@15 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@15 | 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@15 | 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@15 | 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@15 | 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@15 | 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@15 | 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@15 | 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@15 | 25 | * POSSIBILITY OF SUCH DAMAGE. |
universe@15 | 26 | */ |
universe@15 | 27 | |
universe@15 | 28 | #include "ascension/shader.h" |
universe@15 | 29 | #include "ascension/files.h" |
universe@15 | 30 | #include "ascension/error.h" |
universe@15 | 31 | |
universe@15 | 32 | #include <GL/glew.h> |
universe@16 | 33 | #include <string.h> |
universe@15 | 34 | |
universe@16 | 35 | AscShaderFont ASC_SHADER_FONT; |
universe@15 | 36 | |
universe@15 | 37 | |
universe@15 | 38 | AscShader asc_shader_compile(unsigned int type, |
universe@15 | 39 | char const *code, |
universe@15 | 40 | int length) { |
universe@15 | 41 | GLuint id = glCreateShader(type); |
universe@15 | 42 | if (id == 0) { |
universe@15 | 43 | asc_error("glCreateShader failed"); |
universe@15 | 44 | return (AscShader) {0}; |
universe@15 | 45 | } |
universe@15 | 46 | |
universe@15 | 47 | GLint success; |
universe@15 | 48 | glShaderSource(id, 1, &code, &length); |
universe@15 | 49 | glCompileShader(id); |
universe@15 | 50 | glGetShaderiv(id, GL_COMPILE_STATUS, &success); |
universe@15 | 51 | if (success) { |
universe@15 | 52 | asc_dprintf("Shader %u compiled", id); |
universe@15 | 53 | return (AscShader) {id}; |
universe@15 | 54 | } else { |
universe@15 | 55 | char *log = malloc(1024); |
universe@15 | 56 | glGetShaderInfoLog(id, 1024, NULL, log); |
universe@15 | 57 | glDeleteShader(id); |
universe@15 | 58 | asc_error(log); |
universe@15 | 59 | free(log); |
universe@15 | 60 | return (AscShader) {0}; |
universe@15 | 61 | } |
universe@15 | 62 | } |
universe@15 | 63 | |
universe@15 | 64 | AscShader asc_shader_compilef(unsigned int type, |
universe@15 | 65 | char const *filename) { |
universe@15 | 66 | asc_file code = asc_file_mmap_rdonly(filename); |
universe@15 | 67 | if (code.ptr == NULL) { |
universe@15 | 68 | asc_error("Mapping shader file into memory failed"); |
universe@15 | 69 | return (AscShader) {0}; |
universe@15 | 70 | } |
universe@15 | 71 | AscShader ret = asc_shader_compile(type, code.ptr, code.length); |
universe@15 | 72 | asc_file_unmap(code); |
universe@15 | 73 | return ret; |
universe@15 | 74 | } |
universe@15 | 75 | |
universe@15 | 76 | AscShaderProgram asc_shader_link(AscShader vertex, |
universe@15 | 77 | AscShader fragment) { |
universe@15 | 78 | if (vertex.id == 0 || fragment.id == 0) { |
universe@15 | 79 | asc_dprintf("Skip linking shader program - shaders have not been loaded correctly."); |
universe@15 | 80 | return (AscShaderProgram) {0}; |
universe@15 | 81 | } |
universe@15 | 82 | |
universe@15 | 83 | GLint success; |
universe@15 | 84 | GLint id = glCreateProgram(); |
universe@15 | 85 | if (id <= 0) { |
universe@15 | 86 | asc_error("glCreateProgram failed"); |
universe@15 | 87 | return (AscShaderProgram) {0}; |
universe@15 | 88 | } |
universe@15 | 89 | glAttachShader(id, vertex.id); |
universe@15 | 90 | glAttachShader(id, fragment.id); |
universe@15 | 91 | glLinkProgram(id); |
universe@15 | 92 | glGetProgramiv(id, GL_LINK_STATUS, &success); |
universe@15 | 93 | glDetachShader(id, vertex.id); |
universe@15 | 94 | glDetachShader(id, fragment.id); |
universe@15 | 95 | if (success) { |
universe@15 | 96 | asc_dprintf("Shader Program %u linked (vtf: %u, frag: %u)", id, vertex.id, fragment.id); |
universe@16 | 97 | AscShaderProgram prog; |
universe@16 | 98 | prog.id = id; |
universe@16 | 99 | prog.model = glGetUniformLocation(id, "model"); |
universe@16 | 100 | prog.view = glGetUniformLocation(id, "view"); |
universe@16 | 101 | prog.projection = glGetUniformLocation(id, "projection"); |
universe@16 | 102 | return prog; |
universe@15 | 103 | } else { |
universe@15 | 104 | char *log = malloc(1024); |
universe@15 | 105 | glGetProgramInfoLog(id, 1024, NULL, log); |
universe@15 | 106 | glDeleteShader(id); |
universe@15 | 107 | asc_error(log); |
universe@15 | 108 | free(log); |
universe@15 | 109 | return (AscShaderProgram) {0}; |
universe@15 | 110 | } |
universe@15 | 111 | } |
universe@15 | 112 | |
universe@15 | 113 | void asc_shader_destroy(AscShader shader) { |
universe@15 | 114 | if (shader.id > 0) { |
universe@15 | 115 | asc_dprintf("Delete Shader %u", shader.id); |
universe@15 | 116 | glDeleteShader(shader.id); |
universe@15 | 117 | } |
universe@15 | 118 | shader.id = 0; |
universe@15 | 119 | } |
universe@15 | 120 | |
universe@15 | 121 | void asc_shader_program_destroy(AscShaderProgram program) { |
universe@15 | 122 | if (program.id > 0) { |
universe@15 | 123 | asc_dprintf("Delete Shader Program %u", program.id); |
universe@15 | 124 | glDeleteProgram(program.id); |
universe@15 | 125 | } |
universe@15 | 126 | program.id = 0; |
universe@15 | 127 | } |
universe@15 | 128 | |
universe@15 | 129 | static AscShaderProgram asc_shader_compile_link_discard( |
universe@15 | 130 | char const *vtxName, char const *fragName) { |
universe@15 | 131 | AscShader font_vtx = asc_shader_compilef(GL_VERTEX_SHADER, vtxName); |
universe@15 | 132 | AscShader font_frag = asc_shader_compilef(GL_FRAGMENT_SHADER, fragName); |
universe@15 | 133 | AscShaderProgram prog = asc_shader_link(font_vtx, font_frag); |
universe@15 | 134 | asc_shader_destroy(font_vtx); |
universe@15 | 135 | asc_shader_destroy(font_frag); |
universe@15 | 136 | return prog; |
universe@15 | 137 | } |
universe@15 | 138 | |
universe@15 | 139 | void asc_shader_initialize_predefined(void) { |
universe@16 | 140 | ASC_SHADER_FONT.base = asc_shader_compile_link_discard("shader/font_vtx.glsl", "shader/font_frag.glsl"); |
universe@16 | 141 | ASC_SHADER_FONT.surface = glGetUniformLocation(ASC_SHADER_FONT.base.id, "surface"); |
universe@15 | 142 | } |
universe@15 | 143 | |
universe@15 | 144 | void asc_shader_destroy_predefined(void) { |
universe@16 | 145 | asc_shader_program_destroy(ASC_SHADER_FONT.base); |
universe@15 | 146 | } |