From 3efd3026d22c71e95a853985f3f50f52147d287e Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Sun, 13 Oct 2024 23:46:06 -0400 Subject: Instanced rendering, mouse move event --- src/assets/shaders/text/frag.glsl | 6 +++++- src/context.rs | 2 +- src/font.rs | 7 ++++++- src/lib.rs | 3 +++ src/mesh.rs | 7 +++++++ src/shader.rs | 8 ++++++-- src/state.rs | 17 +++++++++++++++++ 7 files changed, 45 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/assets/shaders/text/frag.glsl b/src/assets/shaders/text/frag.glsl index 3a1694f..94d27c9 100644 --- a/src/assets/shaders/text/frag.glsl +++ b/src/assets/shaders/text/frag.glsl @@ -10,6 +10,7 @@ uniform int font_width; uniform int font_height; uniform int text_width; uniform int text_height; +uniform vec3 text_color; in vec2 vertex_texcoord; out vec4 frag_color; @@ -51,5 +52,8 @@ void main() vec4 texel = texture(texture_data, texcoord_final); if (texel.rgb == vec3(0.0, 0.0, 0.0)) discard; + texel.r = text_color.r; + texel.g = text_color.g; + texel.b = text_color.b; frag_color = texel; -} \ No newline at end of file +} diff --git a/src/context.rs b/src/context.rs index 7dca01f..dc1d3a2 100644 --- a/src/context.rs +++ b/src/context.rs @@ -40,7 +40,7 @@ impl Context { let gl = web_sys::window() .and_then(|win| win.document()) .and_then(|doc| { - let dst = doc.get_element_by_id("oubliette-parent")?; + let dst = doc.get_element_by_id("teleia-parent")?; let canvas = web_sys::Element::from(window.canvas().expect("failed to find canvas")); dst.append_child(&canvas).ok()?; let c = canvas.dyn_into::().ok()?; diff --git a/src/font.rs b/src/font.rs index d35abf0..393b1f5 100644 --- a/src/font.rs +++ b/src/font.rs @@ -24,7 +24,7 @@ impl Font { } } - pub fn render_text(&self, ctx: &context::Context, pos: &glam::Vec2, text: &str) { + pub fn render_text_helper(&self, ctx: &context::Context, pos: &glam::Vec2, text: &str, color: &glam::Vec3) { let mut width = 0; let mut linewidth = 0; let mut height = CHAR_HEIGHT; @@ -52,6 +52,7 @@ impl Font { self.shader.set_i32(ctx, "font_height", FONT_HEIGHT as _); self.shader.set_i32(ctx, "text_width", width as _); self.shader.set_i32(ctx, "text_height", height as _); + self.shader.set_vec3(ctx, "text_color", color as _); self.shader.set_mat4( ctx, "view", &glam::Mat4::from_scale( @@ -79,4 +80,8 @@ impl Font { self.font.bind(ctx); ctx.render_no_geometry(); } + + pub fn render_text(&self, ctx: &context::Context, pos: &glam::Vec2, text: &str) { + self.render_text_helper(ctx, pos, text, &glam::Vec3::new(1.0, 1.0, 1.0)); + } } diff --git a/src/lib.rs b/src/lib.rs index 50add89..453b0dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,6 +81,9 @@ where winit::event::WindowEvent::Focused(false) => { st.keys = state::Keys::new(); }, + winit::event::WindowEvent::CursorMoved { position, ..} => { + st.mouse_moved(&ctx, position.x as f32, position.y as f32, game); + }, winit::event::WindowEvent::MouseInput { button, state, diff --git a/src/mesh.rs b/src/mesh.rs index 2f54903..aba9957 100644 --- a/src/mesh.rs +++ b/src/mesh.rs @@ -115,4 +115,11 @@ impl Mesh { ctx.gl.draw_elements(glow::TRIANGLES, self.index_count as _, glow::UNSIGNED_INT, 0); } } + + pub fn render_instanced(&self, ctx: &context::Context, count: u64) { + unsafe { + ctx.gl.bind_vertex_array(Some(self.vao)); + ctx.gl.draw_elements_instanced(glow::TRIANGLES, self.index_count as _, glow::UNSIGNED_INT, 0, count as _); + } + } } diff --git a/src/shader.rs b/src/shader.rs index ec0f24b..71f2a7c 100644 --- a/src/shader.rs +++ b/src/shader.rs @@ -188,14 +188,14 @@ impl Shader { self.set_mat4(&ctx, "normal_matrix", &position.inverse().transpose()); } - pub fn set_position_2d(&self, ctx: &context::Context, pos: &glam::Vec2, dims: &glam::Vec2) { + pub fn set_position_2d_helper(&self, ctx: &context::Context, pos: &glam::Vec2, dims: &glam::Vec2, rot: &glam::Quat) { let halfwidth = dims.x / 2.0; let halfheight = dims.y / 2.0; self.set_mat4( &ctx, "position", &glam::Mat4::from_scale_rotation_translation( glam::Vec3::new(halfwidth, halfheight, 1.0), - glam::Quat::IDENTITY, + rot.clone(), glam::Vec3::new( -context::RENDER_WIDTH / 2.0 + pos.x + halfwidth, context::RENDER_HEIGHT / 2.0 - pos.y - halfheight, @@ -205,6 +205,10 @@ impl Shader { ); } + pub fn set_position_2d(&self, ctx: &context::Context, pos: &glam::Vec2, dims: &glam::Vec2) { + self.set_position_2d_helper(ctx, pos, dims, &glam::Quat::IDENTITY) + } + pub fn set_texture_offset(&self, ctx: &context::Context, inc: i32, x: i32, y: i32) { let count = inc as f32; let ratio = 1.0 / count; diff --git a/src/state.rs b/src/state.rs index 83f729e..de22d13 100644 --- a/src/state.rs +++ b/src/state.rs @@ -10,6 +10,8 @@ pub trait Game { fn initialize_audio(&self, ctx: &context::Context, st: &State, actx: &audio::Context) -> HashMap; fn finish_title(&mut self, st: &mut State); + fn mouse_move(&mut self, ctx: &context::Context, st: &mut State, x: i32, y: i32); + fn mouse_press(&mut self, ctx: &context::Context, st: &mut State); fn update(&mut self, ctx: &context::Context, st: &mut State) -> Option<()>; fn render(&mut self, ctx: &context::Context, st: &mut State) -> Option<()>; } @@ -282,6 +284,20 @@ impl State { ); } + pub fn mouse_moved( + &mut self, + ctx: &context::Context, + x: f32, y: f32, + game: &mut G + ) where G: Game + { + let rx = ((x - self.screen.offsets.x) * context::RENDER_WIDTH / self.screen.dims.x) as i32; + let ry = ((y - self.screen.offsets.y) * context::RENDER_HEIGHT / self.screen.dims.y) as i32; + if !(rx < 0 || rx >= context::RENDER_WIDTH as i32 || ry < 0 || ry >= context::RENDER_HEIGHT as i32) { + game.mouse_move(ctx, self, rx, ry); + } + } + pub fn mouse_pressed( &mut self, ctx: &context::Context, @@ -295,6 +311,7 @@ impl State { })); game.finish_title(self); } + game.mouse_press(ctx, self); } pub fn mouse_released( -- cgit v1.2.3