diff options
| author | LLLL Colonq <llll@colonq> | 2025-08-14 22:28:39 -0400 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2025-08-14 22:28:39 -0400 |
| commit | e4ded2c09e6c378040f80e80886aa9c087fe14b4 (patch) | |
| tree | 74984adf49dde6a1fe7b3b22c0d9c3e2168df267 /crates/renderer/src/overlay/shader.rs | |
| parent | 4fb92d6fa3ce2d93c2ce720429f46aa104972674 (diff) | |
Automata rendering
Diffstat (limited to 'crates/renderer/src/overlay/shader.rs')
| -rw-r--r-- | crates/renderer/src/overlay/shader.rs | 180 |
1 files changed, 25 insertions, 155 deletions
diff --git a/crates/renderer/src/overlay/shader.rs b/crates/renderer/src/overlay/shader.rs index 97e4d92..81e29f4 100644 --- a/crates/renderer/src/overlay/shader.rs +++ b/crates/renderer/src/overlay/shader.rs @@ -3,11 +3,11 @@ use teleia::*; use std::{collections::HashMap, f32::consts::PI}; use lexpr::sexp; use base64::prelude::*; -use device_query::DeviceQuery; use glow::HasContext; -use crate::{assets, fig, toggle, background}; +use crate::{assets, fig, toggle, input, background}; +use super::{drawing, automata}; pub struct Chat { author: String, @@ -26,103 +26,11 @@ impl Chat { } } -const DRAWING_WIDTH: usize = 1920 / 4; -const DRAWING_HEIGHT: usize = 1080 / 4; -pub enum DrawingCommand { - None, - Drawing, - EraseAll, -} -pub struct Drawing { - tex: texture::Texture, - pixels: [u8; DRAWING_WIDTH * DRAWING_HEIGHT], - last_point: Option<(i32, i32)>, - shader_white: shader::Shader, - shader_background: shader::Shader, -} -impl Drawing { - pub fn new(ctx: &context::Context) -> Self { - let shader_background = shader::Shader::new( - ctx, - include_str!("../assets/shaders/background/vert.glsl"), - include_str!("../assets/shaders/background/frag.glsl"), - ); - shader_background.set_i32(ctx, "background", 1); - Self { - tex: texture::Texture::new_empty(ctx), - pixels: [0; DRAWING_WIDTH * DRAWING_HEIGHT], - last_point: None, - shader_white: shader::Shader::new( - ctx, - include_str!("../assets/shaders/white/vert.glsl"), - include_str!("../assets/shaders/white/frag.glsl"), - ), - shader_background, - } - } - pub fn coord(&self, x: usize, y: usize) -> Option<usize> { - if x >= DRAWING_WIDTH || y >= DRAWING_HEIGHT { - None - } else { - Some(x + y * DRAWING_WIDTH) - } - } - pub fn set(&mut self, val: u8, x: i32, y: i32) { - self.coord(x as usize, y as usize).map(|idx| self.pixels[idx] = val); - } - pub fn point(&mut self, val: u8, x: i32, y: i32) { - self.set(val, x, y - 1); - self.set(val, x - 1, y); - self.set(val, x, y); - self.set(val, x + 1, y); - self.set(val, x, y + 1); - } - pub fn line(&mut self, val: u8, (mut x0, mut y0): (i32, i32), (x1, y1): (i32, i32)) { - let dx = (x1 - x0).abs(); - let sx = if x0 < x1 { 1 } else { -1 }; - let dy = -((y1 - y0).abs()); - let sy = if y0 < y1 { 1 } else { -1 }; - let mut error = dx + dy; - loop { - self.point(val, x0, y0); - let e2 = 2 * error; - if e2 >= dy { - if x0 == x1 { break; } - error += dy; - x0 += sx; - } - if e2 <= dx { - if y0 == y1 { break; } - error += dx; - y0 += sy; - } - } - } - pub fn upload(&self, ctx: &context::Context) { - unsafe { - let err = ctx.gl.get_error(); - self.tex.bind(ctx); - ctx.gl.tex_image_2d( - glow::TEXTURE_2D, - 0, - glow::R8 as i32, - DRAWING_WIDTH as i32, - DRAWING_HEIGHT as i32, - 0, - glow::RED, - glow::UNSIGNED_BYTE, - Some(&self.pixels), - ); - ctx.gl.generate_mipmap(glow::TEXTURE_2D); - } - } -} - pub struct Overlay { assets: assets::Assets, model: scene::Scene, model_neck_base: glam::Mat4, - fig: fig::Client, + fig: fig::SexpClient, fig_binary: fig::BinaryClient, tracking_eyes: (f32, f32), tracking_mouth: f32, @@ -134,9 +42,10 @@ pub struct Overlay { muzak_author: Option<String>, chat: Chat, toggles: toggle::Toggles, + input: input::Input, backgrounds: background::Backgrounds, - drawing: Drawing, - device: device_query::DeviceState, + drawing: drawing::Drawing, + automata: automata::Board, } impl Overlay { @@ -147,11 +56,13 @@ impl Overlay { .expect("failed to find neck joint") .transform; let throwshade = newton_throwshade::ThrowShade::new(); + let mut automata = automata::Board::new(ctx); + automata.test_glider(); Self { assets: assets::Assets::new(ctx), model, model_neck_base, - fig: fig::Client::new("shiro:32050", &[ + fig: fig::SexpClient::new("shiro:32050", &[ sexp!((avatar toggle)), sexp!((avatar toggle set)), sexp!((avatar toggle unset)), @@ -166,7 +77,7 @@ impl Overlay { sexp!((avatar overlay cursor)), sexp!((avatar overlay emacs)), ]), - fig_binary: fig::BinaryClient::new("shiro:32051", &[ + fig_binary: fig::BinaryClient::new("shiro:32051", false, &[ b"background frame" ]), tracking_eyes: (1.0, 1.0), @@ -180,21 +91,9 @@ impl Overlay { chat: Chat::new(), toggles: toggle::Toggles::new(), backgrounds: background::Backgrounds::new(ctx), - drawing: Drawing::new(ctx), - device: device_query::DeviceState::new(), - } - } - fn get_mouse(&self) -> (i32, i32) { - self.device.get_mouse().coords - } - fn get_drawing_command(&mut self) -> DrawingCommand { - let keys = self.device.get_keys(); - if keys.contains(&device_query::Keycode::LMeta) { - DrawingCommand::Drawing - } else if keys.contains(&device_query::Keycode::RMeta) { - DrawingCommand::EraseAll - } else { - DrawingCommand::None + input: input::Input::new(), + drawing: drawing::Drawing::new(ctx), + automata, } } pub fn handle_reset(&mut self, ctx: &context::Context) { @@ -203,7 +102,7 @@ impl Overlay { self.throwshade.shader = None; self.toggles.reset(); } - pub fn handle_tracking(&mut self, msg: fig::Message) -> Option<()> { + pub fn handle_tracking(&mut self, msg: fig::SexpMessage) -> Option<()> { let eyes = msg.data.get(0)?; let eye_left = eyes.get(0)?.as_str()?.parse::<f32>().ok()?; let eye_right = eyes.get(1)?.as_str()?.parse::<f32>().ok()?; @@ -220,7 +119,7 @@ impl Overlay { pub fn handle_overlay_shader( &mut self, ctx: &context::Context, st: &state::State, - msg: fig::Message + msg: fig::SexpMessage ) -> Option<()> { let ba = BASE64_STANDARD.decode(msg.data.get(0)?.as_str()?).ok()?; let author = String::from_utf8_lossy(&ba); @@ -233,7 +132,7 @@ impl Overlay { } Some(()) } - pub fn handle_overlay_muzak(&mut self, msg: fig::Message) -> Option<()> { + pub fn handle_overlay_muzak(&mut self, msg: fig::SexpMessage) -> Option<()> { let ba = BASE64_STANDARD.decode(msg.data.get(0)?.as_str()?).ok()?; let author = String::from_utf8_lossy(&ba); self.muzak_author = Some(author.to_string()); @@ -243,7 +142,7 @@ impl Overlay { self.muzak_author = None; Some(()) } - pub fn handle_overlay_chat(&mut self, msg: fig::Message) -> Option<()> { + pub fn handle_overlay_chat(&mut self, msg: fig::SexpMessage) -> Option<()> { let ba = BASE64_STANDARD.decode(msg.data.get(0)?.as_str()?).ok()?; let a = String::from_utf8_lossy(&ba); let bs = BASE64_STANDARD.decode(msg.data.get(1)?.as_str()?).ok()?; @@ -256,27 +155,16 @@ impl Overlay { self.chat.biblicality = biblicality; Some(()) } - pub fn handle_overlay_cursor(&mut self, msg: fig::Message) -> Option<()> { + pub fn handle_overlay_cursor(&mut self, msg: fig::SexpMessage) -> Option<()> { let cursor_x = msg.data.get(0)?.as_i64()? as f32; let cursor_y = msg.data.get(1)?.as_i64()? as f32; self.emacs_cursor = (cursor_x, cursor_y); Some(()) } - pub fn handle_overlay_emacs(&mut self, msg: fig::Message) -> Option<()> { + pub fn handle_overlay_emacs(&mut self, msg: fig::SexpMessage) -> Option<()> { self.emacs_heartrate = msg.data.get(0)?.as_i64()? as i32; Some(()) } - pub fn render_drawing(&self, ctx: &context::Context, st: &mut state::State) { - st.bind_2d(ctx, &self.drawing.shader_background); - self.drawing.tex.bind(ctx); - self.backgrounds.drawing.bind_index(ctx, 1); - self.drawing.shader_background.set_position_2d( - ctx, - &glam::Vec2::new(0.0, 0.0), - &glam::Vec2::new(1920.0, 1080.0) - ); - self.assets.mesh_square.render(ctx); - } } impl teleia::state::Game for Overlay { @@ -293,7 +181,7 @@ impl teleia::state::Game for Overlay { &glam::Vec3::new(0.0, 0.0, -1.0), &glam::Vec3::new(0.0, 1.0, 0.0), ); - let (x, y) = self.get_mouse(); + let (x, y) = self.input.get_mouse(); self.mouse_cursor = (x as f32, y as f32); while let Some(msg) = self.fig.pump() { let malformed = format!("malformed {} data: {}", msg.event, msg.data); @@ -338,6 +226,8 @@ impl teleia::state::Game for Overlay { if let Some(n) = self.model.nodes_by_name.get("J_Bip_C_Neck").and_then(|i| self.model.nodes.get_mut(*i)) { n.transform = self.model_neck_base * glam::Mat4::from_quat(self.tracking_neck); } + self.drawing.update(ctx, st, &mut self.input); + self.automata.update(ctx, st); Ok(()) } fn render(&mut self, ctx: &context::Context, st: &mut state::State) -> Erm<()> { @@ -378,7 +268,7 @@ impl teleia::state::Game for Overlay { &glam::Vec2::new(1100.0, 300.0), &glam::Vec2::new(800.0, 600.0) ); - self.assets.mesh_square.render(ctx); + st.mesh_square.render(ctx); } let mut authors = Vec::new(); if let Some(_) = &self.throwshade.shader { @@ -389,28 +279,8 @@ impl teleia::state::Game for Overlay { } let astr: String = authors.join(", "); self.assets.font.render_text(ctx, &glam::Vec2::new(0.0, 0.0), &astr); - match self.get_drawing_command() { - DrawingCommand::Drawing => { - let (sx, sy) = self.get_mouse(); - let x = sx / 4; - let y = sy / 4; - if let Some(last) = self.drawing.last_point { - self.drawing.line(1, last, (x, y)); - } else { - self.drawing.point(1, x, y); - } - self.drawing.last_point = Some((x, y)); - }, - DrawingCommand::EraseAll => { - self.drawing.pixels.fill(0); - self.drawing.last_point = None; - }, - DrawingCommand::None => { - self.drawing.last_point = None; - }, - } - self.drawing.upload(ctx); - self.render_drawing(ctx, st); + self.drawing.render(ctx, st, &self.backgrounds); + self.automata.render(ctx, st); Ok(()) } } |
