From 39b97eb9909a18b5c90e55de380326e308334476 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Fri, 10 Feb 2023 00:46:20 +0100 Subject: [PATCH] gui changes, use glsl 3.0 es --- kubi/shaders/colored.frag | 4 ++- kubi/shaders/colored.vert | 4 ++- kubi/shaders/gui/progressbar.frag | 4 ++- kubi/shaders/gui/progressbar.vert | 2 +- kubi/shaders/selection_box.frag | 4 ++- kubi/shaders/selection_box.vert | 4 ++- kubi/shaders/world.frag | 5 +++- kubi/shaders/world.vert | 4 ++- kubi/src/events.rs | 16 +++++++++++- kubi/src/gui.rs | 41 ++++++++++++++++++++++--------- kubi/src/init.rs | 14 ++++++++--- kubi/src/main.rs | 9 ++++--- 12 files changed, 83 insertions(+), 28 deletions(-) diff --git a/kubi/shaders/colored.frag b/kubi/shaders/colored.frag index 3e09c7a..b7a0615 100644 --- a/kubi/shaders/colored.frag +++ b/kubi/shaders/colored.frag @@ -1,4 +1,6 @@ -#version 150 core +#version 300 es + +precision highp float; out vec4 out_color; uniform vec4 color; diff --git a/kubi/shaders/colored.vert b/kubi/shaders/colored.vert index 083513e..c6b3ef8 100644 --- a/kubi/shaders/colored.vert +++ b/kubi/shaders/colored.vert @@ -1,4 +1,6 @@ -#version 150 core +#version 300 es + +precision highp float; in vec3 position; uniform mat4 model; diff --git a/kubi/shaders/gui/progressbar.frag b/kubi/shaders/gui/progressbar.frag index 1474e79..e598692 100644 --- a/kubi/shaders/gui/progressbar.frag +++ b/kubi/shaders/gui/progressbar.frag @@ -1,4 +1,6 @@ -#version 150 core +#version 300 es + +precision highp float; in vec2 v_uv; out vec4 out_color; diff --git a/kubi/shaders/gui/progressbar.vert b/kubi/shaders/gui/progressbar.vert index 9d48236..2b43f0c 100644 --- a/kubi/shaders/gui/progressbar.vert +++ b/kubi/shaders/gui/progressbar.vert @@ -1,4 +1,4 @@ -#version 150 core +#version 300 es in vec2 position; out vec2 v_uv; diff --git a/kubi/shaders/selection_box.frag b/kubi/shaders/selection_box.frag index 42b54ed..d18a665 100644 --- a/kubi/shaders/selection_box.frag +++ b/kubi/shaders/selection_box.frag @@ -1,4 +1,6 @@ -#version 150 core +#version 300 es + +precision highp float; out vec4 color; uniform vec4 u_color; diff --git a/kubi/shaders/selection_box.vert b/kubi/shaders/selection_box.vert index f825013..049488d 100644 --- a/kubi/shaders/selection_box.vert +++ b/kubi/shaders/selection_box.vert @@ -1,4 +1,6 @@ -#version 150 core +#version 300 es + +precision highp float; in vec3 position; uniform ivec3 u_position; diff --git a/kubi/shaders/world.frag b/kubi/shaders/world.frag index 566efaf..f6054c2 100644 --- a/kubi/shaders/world.frag +++ b/kubi/shaders/world.frag @@ -1,4 +1,7 @@ -#version 150 core +#version 300 es + +precision highp float; +precision lowp sampler2DArray; in vec3 v_normal; in vec2 v_uv; diff --git a/kubi/shaders/world.vert b/kubi/shaders/world.vert index de3d2e7..996a1ec 100644 --- a/kubi/shaders/world.vert +++ b/kubi/shaders/world.vert @@ -1,4 +1,6 @@ -#version 150 core +#version 300 es + +precision highp float; //TODO pack this data: // uint position_normal_uv diff --git a/kubi/src/events.rs b/kubi/src/events.rs index b7154da..45fcfff 100644 --- a/kubi/src/events.rs +++ b/kubi/src/events.rs @@ -1,6 +1,7 @@ use glam::UVec2; -use shipyard::{World, Component, AllStoragesViewMut, SparseSet}; +use shipyard::{World, Component, AllStoragesViewMut, SparseSet, NonSendSync, UniqueView}; use glium::glutin::event::{Event, DeviceEvent, DeviceId, WindowEvent}; +use crate::rendering::Renderer; pub mod player_actions; @@ -50,6 +51,19 @@ pub fn process_glutin_events(world: &mut World, event: &Event<'_, ()>) { } } +pub fn initial_resize_event( + mut storages: AllStoragesViewMut, +) { + let (w, h) = { + let renderer = storages.borrow::>>().unwrap(); + renderer.display.get_framebuffer_dimensions() + }; + storages.add_entity(( + EventComponent, + WindowResizedEvent(UVec2::new(w, h)) + )); +} + pub fn clear_events( mut all_storages: AllStoragesViewMut, ) { diff --git a/kubi/src/gui.rs b/kubi/src/gui.rs index 8662cab..8f78889 100644 --- a/kubi/src/gui.rs +++ b/kubi/src/gui.rs @@ -1,6 +1,6 @@ -use shipyard::{Component, Unique, Workload, IntoWorkload, AllStoragesView, AllStoragesViewMut}; -use glam::{Vec2, Vec4, Mat3, vec2, Mat4}; -use crate::{color::color_hex, transform::Transform2d}; +use shipyard::{Component, Unique, Workload, IntoWorkload, AllStoragesView, AllStoragesViewMut, View, UniqueView, NonSendSync, UniqueViewMut, IntoIter}; +use glam::{Vec4, Mat3, vec2, Mat4}; +use crate::{color::color_hex, transform::Transform2d, events::WindowResizedEvent, rendering::Renderer}; pub mod text_widget; pub mod progressbar; @@ -8,7 +8,7 @@ pub mod progressbar; use progressbar::{render_progressbars, ProgressbarComponent}; //TODO compute gui scale on window resize -#[derive(Unique, Clone, Copy, Debug)] +#[derive(Unique, Clone, Copy, Debug, Default)] pub struct GuiView(pub Mat4); #[derive(Component, Clone, Copy, Debug, Default)] @@ -30,27 +30,44 @@ impl Default for SecondaryColor { } } +fn update_gui_view( + mut view: UniqueViewMut, + resize: View, +) { + let Some(&size) = resize.iter().next() else { + return + }; + let [w, h] = size.0.to_array(); + view.0 = Mat4::orthographic_rh_gl(0.0, w as f32, h as f32, 0.0, -1.0, 1.0); +} + +pub fn init_gui( + storages: AllStoragesView +) { + storages.add_unique(GuiView::default()); +} + +pub fn update_gui() -> Workload { + ( + update_gui_view + ).into_workload() +} + pub fn render_gui() -> Workload { ( render_progressbars ).into_workload() } -pub fn init_gui( - storages: AllStoragesView, -) { - storages.add_unique(GuiView(Mat4::orthographic_rh_gl(0.0, 1.0, 1.0, 0.0, 0.0, 1.0))); -} - pub fn gui_testing( mut storages: AllStoragesViewMut, ) { storages.add_entity(( GuiComponent, Transform2d(Mat3::from_scale_angle_translation( - vec2(0.25, 0.05), + vec2(1920., 16.), 0., - vec2(0.5, 0.25) + vec2(0., 0.) )), ProgressbarComponent { progress: 0.33 diff --git a/kubi/src/init.rs b/kubi/src/init.rs index 4f96843..ad6d2b2 100644 --- a/kubi/src/init.rs +++ b/kubi/src/init.rs @@ -1,11 +1,17 @@ -use std::net::SocketAddr; -use shipyard::AllStoragesView; -use std::env; +use shipyard::{AllStoragesView, NonSendSync, UniqueView}; +use glium::{Version, Api}; +use std::{env, net::SocketAddr}; use crate::{ networking::{GameType, ServerAddress}, - state::GameState + state::GameState, rendering::Renderer }; +pub fn assert_renderer( + renderer: NonSendSync> +) { + assert!(renderer.display.is_glsl_version_supported(&Version(Api::GlEs, 3, 0)), "GLES 3.0 is not supported"); +} + pub fn initialize_from_args( all_storages: AllStoragesView, ) { diff --git a/kubi/src/main.rs b/kubi/src/main.rs index 0440b0b..3505389 100644 --- a/kubi/src/main.rs +++ b/kubi/src/main.rs @@ -48,7 +48,7 @@ use settings::load_settings; use camera::compute_cameras; use events::{ clear_events, process_glutin_events, - player_actions::generate_move_events + player_actions::generate_move_events, initial_resize_event }; use input::{init_input, process_inputs}; use fly_controller::update_controllers; @@ -67,11 +67,13 @@ use delta_time::{DeltaTime, init_delta_time}; use cursor_lock::{insert_lock_state, update_cursor_lock_state, lock_cursor_now}; use control_flow::{exit_on_esc, insert_control_flow_unique, SetControlFlow}; use state::{is_ingame, is_ingame_or_loading, is_loading}; -use init::initialize_from_args; -use gui::{render_gui, init_gui, gui_testing}; +use init::{initialize_from_args, assert_renderer}; +use gui::{render_gui, init_gui, gui_testing, update_gui}; fn startup() -> Workload { ( + assert_renderer, + initial_resize_event, load_settings, load_prefabs, init_primitives, @@ -106,6 +108,7 @@ fn update() -> Workload { apply_queued_blocks, ).into_workload().run_if(is_ingame), compute_cameras, + update_gui, ).into_workload() } fn render() -> Workload {