From 3edbfed8830121aff302c31079135292753d0bf8 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Mon, 5 Dec 2022 21:46:51 -0600 Subject: [PATCH] cleaned up OGRE shaders --- .gitignore | 1 + src/entity/entities.onyx | 1 - src/game.onyx | 5 +--- src/main.onyx | 20 ++++++++-------- src/ogre/font.onyx | 4 +--- src/ogre/immediate.onyx | 6 +---- src/ogre/ogre.onyx | 12 +++++++++- src/ogre/shader.onyx | 10 +++++++- .../assets => src/ogre}/shaders/font.glsl | 0 .../assets => src/ogre}/shaders/imgui.glsl | 0 src/ogre/texture.onyx | 9 +++++--- src/ogre/window.onyx | 23 +++++++++++++++++++ 12 files changed, 63 insertions(+), 28 deletions(-) rename {run_tree/assets => src/ogre}/shaders/font.glsl (100%) rename {run_tree/assets => src/ogre}/shaders/imgui.glsl (100%) diff --git a/.gitignore b/.gitignore index 2f35394..3b21f34 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ src/config.onyx *.wasm run_tree/lib/ +lib/ diff --git a/src/entity/entities.onyx b/src/entity/entities.onyx index 820d84b..f191b82 100644 --- a/src/entity/entities.onyx +++ b/src/entity/entities.onyx @@ -1,6 +1,5 @@ use core -use glfw3 use ogre @Entity_Schematic.{ "Wall" } diff --git a/src/game.onyx b/src/game.onyx index f798106..b4bc5a3 100644 --- a/src/game.onyx +++ b/src/game.onyx @@ -26,10 +26,7 @@ game_init :: () { Audio_Manager.set_volume(0.1); distortion_shader = shader_make("./assets/shaders/crt.glsl"); - shader_link_window_matrix_block(distortion_shader); - shader_link_world_matrix_block(distortion_shader); - - scene_canvas = canvas_make(800, 608); + scene_canvas = canvas_make(800, 608); // This process of queueing the asset bucket should // be made automatic somehow... diff --git a/src/main.onyx b/src/main.onyx index 70d8098..0ee681b 100644 --- a/src/main.onyx +++ b/src/main.onyx @@ -12,10 +12,15 @@ window: Window #if DEBUG { debug_font: Font; } init :: () { + if !ogre_init() { + debug_log(.Critical, "Failed to initialize OGRE!"); + os.exit(1); + } + window = window_create(1200, 900, #cstr "Bar Simulator"); window_use(^window); - ogre_init(); + ogre_setup(); editor_init(); game_init(); @@ -30,7 +35,7 @@ update :: (dt: f32) { input_update(); if is_key_just_up(GLFW_KEY_ESCAPE) { - glfwSetWindowShouldClose(window.glfw_window, true); + window->set_should_close(true); return; } @@ -60,7 +65,7 @@ draw :: () { font_print(debug_font, ~~window.width - font_get_width(debug_font, version_str), 16, version_str); } - glfwSwapBuffers(window.glfw_window); + window->swap_buffers(); } game_draw(); @@ -84,8 +89,8 @@ run :: () { seconds := 0.0; frame_count := 0; - while !glfwWindowShouldClose(window.glfw_window) { - glfwPollEvents(); + while !(window->should_close()) { + window->poll_events(); now = glfwGetTime(); dt := now - last; @@ -109,11 +114,6 @@ main :: () { random.set_seed(os.time()); debug_set_level(.Info); - if !glfwInit() { - debug_log(.Critical, "Failed to initialize GLFW!"); - os.exit(1); - } - init(); run(); deinit(); diff --git a/src/ogre/font.onyx b/src/ogre/font.onyx index 9a9f090..9e95308 100644 --- a/src/ogre/font.onyx +++ b/src/ogre/font.onyx @@ -16,10 +16,8 @@ use opengles fonts_init :: () { map.init(^font_registry); - font_shader = shader_make("./assets/shaders/font.glsl"); + font_shader = shader_make_from_source(#file_contents "./shaders/font.glsl"); shader_use(font_shader); - shader_link_window_matrix_block(font_shader); - shader_link_world_matrix_block(font_shader); glGenVertexArrays(1, ^font_vao); glBindVertexArray(font_vao); diff --git a/src/ogre/immediate.onyx b/src/ogre/immediate.onyx index 0cd0505..0f37c76 100644 --- a/src/ogre/immediate.onyx +++ b/src/ogre/immediate.onyx @@ -8,10 +8,7 @@ immediate_init :: () { memory.alloc_slice(^vertex_data, Maximum_Vertex_Count); vertex_count = 0; - imgui_shader = shader_make(Shader_Path); - shader_use(imgui_shader); - shader_link_window_matrix_block(imgui_shader); - shader_link_world_matrix_block(imgui_shader); + imgui_shader = shader_make_from_source(#file_contents "./shaders/imgui.glsl"); shader_set_uniform(imgui_shader, #cstr "u_texture_enabled", 0.0f); shader_set_uniform(imgui_shader, #cstr "u_texture", 0); @@ -172,7 +169,6 @@ Immediate_Vertex :: struct { } #local { - Shader_Path :: "./assets/shaders/imgui.glsl" imgui_shader: Shader; Maximum_Vertex_Count :: 1023; diff --git a/src/ogre/ogre.onyx b/src/ogre/ogre.onyx index 936d000..4fd1082 100644 --- a/src/ogre/ogre.onyx +++ b/src/ogre/ogre.onyx @@ -1,6 +1,16 @@ package ogre -ogre_init :: () { +use glfw3 {glfwInit} + +ogre_init :: () -> bool { + if !glfwInit() { + return false; + } + + return true; +} + +ogre_setup :: () { shaders_init(); fonts_init(); immediate_init(); diff --git a/src/ogre/shader.onyx b/src/ogre/shader.onyx index 449f6c8..b6bf3fa 100644 --- a/src/ogre/shader.onyx +++ b/src/ogre/shader.onyx @@ -38,7 +38,11 @@ shader_make_from_source :: (shader_source: str) -> Shader { prog := link_program(vs, fs); - return Shader.{vs, fs, prog}; + s := Shader.{vs, fs, prog}; + shader_link_window_matrix_block(s); + shader_link_world_matrix_block(s); + + return s; } shader_use :: (shader: Shader) { @@ -52,11 +56,15 @@ shader_use :: (shader: Shader) { shader_link_window_matrix_block :: (use shader: Shader) { matrix_block_index := glGetUniformBlockIndex(prog, #cstr "u_window_matrix_block"); + if matrix_block_index == GL_INVALID_INDEX do return; + glUniformBlockBinding(prog, matrix_block_index, WINDOW_MATRIX_BLOCK); } shader_link_world_matrix_block :: (use shader: Shader) { matrix_block_index := glGetUniformBlockIndex(prog, #cstr "u_world_matrix_block"); + if matrix_block_index == GL_INVALID_INDEX do return; + glUniformBlockBinding(prog, matrix_block_index, WORLD_MATRIX_BLOCK); } diff --git a/run_tree/assets/shaders/font.glsl b/src/ogre/shaders/font.glsl similarity index 100% rename from run_tree/assets/shaders/font.glsl rename to src/ogre/shaders/font.glsl diff --git a/run_tree/assets/shaders/imgui.glsl b/src/ogre/shaders/imgui.glsl similarity index 100% rename from run_tree/assets/shaders/imgui.glsl rename to src/ogre/shaders/imgui.glsl diff --git a/src/ogre/texture.onyx b/src/ogre/texture.onyx index 64773cc..dbf3157 100644 --- a/src/ogre/texture.onyx +++ b/src/ogre/texture.onyx @@ -12,8 +12,10 @@ Texture :: struct { filename: str; } -texture_lookup :: #match {} -#match texture_lookup (filename: str) -> (Texture, bool) { +texture_lookup :: #match #local {} + +#overload +texture_lookup :: (filename: str) -> (Texture, bool) { if texture_cache->has(filename) { return texture_cache[filename], true; } @@ -23,7 +25,8 @@ texture_lookup :: #match {} return texture_lookup(cast(cstr) buffer); } -#match texture_lookup (path: cstr) -> (Texture, bool) { +#overload +texture_lookup :: (path: cstr) -> (Texture, bool) { filename := string.from_cstr(path); if texture_cache->has(filename) { return texture_cache[filename], true; diff --git a/src/ogre/window.onyx b/src/ogre/window.onyx index 486cf37..3c30bc0 100644 --- a/src/ogre/window.onyx +++ b/src/ogre/window.onyx @@ -50,6 +50,29 @@ window_use :: (w: ^Window) { global_window = w; } +window_should_close :: (w: ^Window) -> bool { + return glfwWindowShouldClose(w.glfw_window); +} + +window_set_should_close :: (w: ^Window, b: bool) { + return glfwSetWindowShouldClose(w.glfw_window, b); +} + +window_swap_buffers :: (w: ^Window) { + glfwSwapBuffers(w.glfw_window); +} + +window_poll_events :: (_: ^Window) { + glfwPollEvents(); +} + +#inject Window { + should_close :: window_should_close; + set_should_close :: window_set_should_close; + swap_buffers :: window_swap_buffers; + poll_events :: window_poll_events; +} + // // These have to be macros because '#export_name' is used on the argument, // which requires that they are compile time known functions. -- 2.25.1