diff options
Diffstat (limited to 'src/common')
23 files changed, 474 insertions, 0 deletions
diff --git a/src/common/client.rs b/src/common/client.rs new file mode 100644 index 0000000..69ac6b3 --- /dev/null +++ b/src/common/client.rs @@ -0,0 +1,48 @@ +#![allow(dead_code, unused_variables)] +mod assets; + +use std::collections::HashMap; +use teleia::*; + +pub struct Game { + assets: assets::Assets, +} + +impl Game { + pub async fn new(ctx: &context::Context) -> Self { + Self { + assets: assets::Assets::new(ctx), + } + } +} + +impl teleia::state::Game for Game { + fn initialize_audio(&self, ctx: &context::Context, st: &state::State, actx: &audio::Context) -> HashMap<String, audio::Audio> { + HashMap::from_iter(vec![ + ("test".to_owned(), audio::Audio::new(&actx, include_bytes!("client/assets/audio/test.wav"))), + ]) + } + fn finish_title(&mut self, _st: &mut state::State) {} + fn mouse_press(&mut self, _ctx: &context::Context, _st: &mut state::State) {} + fn mouse_move(&mut self, _ctx: &context::Context, _st: &mut state::State, _x: i32, _y: i32) {} + fn update(&mut self, ctx: &context::Context, _st: &mut state::State) -> Option<()> { + Some(()) + } + fn render(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> { + ctx.clear(); + self.assets.font.render_text( + ctx, + &glam::Vec2::new(0.0, 0.0), + "hello computer", + ); + st.bind_2d(ctx, &self.assets.shader_flat); + self.assets.texture_test.bind(ctx); + self.assets.shader_flat.set_position_2d( + ctx, + &glam::Vec2::new(40.0, 40.0), + &glam::Vec2::new(16.0, 16.0), + ); + self.assets.mesh_square.render(ctx); + Some(()) + } +} diff --git a/src/common/client/assets.rs b/src/common/client/assets.rs new file mode 100644 index 0000000..742f9ea --- /dev/null +++ b/src/common/client/assets.rs @@ -0,0 +1,23 @@ +use teleia::*; + +pub struct Assets { + pub font: font::Bitmap, + pub shader_flat: shader::Shader, + pub mesh_square: mesh::Mesh, + pub texture_test: texture::Texture, +} + +impl Assets { + pub fn new(ctx: &context::Context) -> Self { + Self { + font: font::Bitmap::new(ctx), + shader_flat: shader::Shader::new( + ctx, + include_str!("assets/shaders/flat/vert.glsl"), + include_str!("assets/shaders/flat/frag.glsl"), + ), + mesh_square: mesh::Mesh::from_obj(ctx, include_bytes!("assets/meshes/square.obj")), + texture_test: texture::Texture::new(ctx, include_bytes!("assets/textures/test.png")), + } + } +} diff --git a/src/common/client/assets/audio/test.wav b/src/common/client/assets/audio/test.wav Binary files differnew file mode 100644 index 0000000..0eabe85 --- /dev/null +++ b/src/common/client/assets/audio/test.wav diff --git a/src/common/client/assets/meshes/square.obj b/src/common/client/assets/meshes/square.obj new file mode 100644 index 0000000..7328a6c --- /dev/null +++ b/src/common/client/assets/meshes/square.obj @@ -0,0 +1,15 @@ +# Blender 3.6.2 +# www.blender.org +o Cube +v -1.000000 -1.000000 0.000000 +v 1.000000 -1.000000 0.000000 +v -1.000000 1.000000 0.000000 +v 1.000000 1.000000 0.000000 +vn -0.0000 -0.0000 -1.0000 +vt 0.0 1.0 +vt 1.0 0.0 +vt 0.0 0.0 +vt 1.0 1.0 +s 0 +f 3/1/1 2/2/1 1/3/1 +f 3/1/1 4/4/1 2/2/1 diff --git a/src/common/client/assets/shaders/flat/frag.glsl b/src/common/client/assets/shaders/flat/frag.glsl new file mode 100644 index 0000000..7006a2b --- /dev/null +++ b/src/common/client/assets/shaders/flat/frag.glsl @@ -0,0 +1,11 @@ +uniform sampler2D texture_data; + +void main() +{ + vec2 tcfull = vec2(vertex_texcoord.x, 1.0 - vertex_texcoord.y); + vec4 texel = texture(texture_data, tcfull); + if (texel.a != 1.0) { + discard; + } + frag_color = texel; +} diff --git a/src/common/client/assets/shaders/flat/vert.glsl b/src/common/client/assets/shaders/flat/vert.glsl new file mode 100644 index 0000000..e324f7e --- /dev/null +++ b/src/common/client/assets/shaders/flat/vert.glsl @@ -0,0 +1,4 @@ +void main() +{ + default_main(); +}
\ No newline at end of file diff --git a/src/common/client/assets/textures/test.png b/src/common/client/assets/textures/test.png Binary files differnew file mode 100644 index 0000000..1f1edca --- /dev/null +++ b/src/common/client/assets/textures/test.png diff --git a/src/common/overlay.rs b/src/common/overlay.rs new file mode 100644 index 0000000..d7a0d87 --- /dev/null +++ b/src/common/overlay.rs @@ -0,0 +1,125 @@ +#![allow(dead_code, unused_variables)] +mod assets; +mod terminal; +mod fig; + +use teleia::*; + +use std::{collections::HashMap, f32::consts::PI}; +use lexpr::sexp; + +pub struct Overlay { + assets: assets::Assets, + model: scene::Scene, + model_neck_base: glam::Mat4, + model_fb: framebuffer::Framebuffer, + terminal: terminal::Terminal, + fig: fig::Client, + tracking_eyes: (f32, f32), + tracking_neck: glam::Quat, +} + +impl Overlay { + pub async fn new(ctx: &context::Context) -> Self { + let model = scene::Scene::from_gltf(ctx, include_bytes!("overlay/assets/scenes/lcolonq.vrm")); + let model_neck_base = model.nodes_by_name.get("J_Bip_C_Neck") + .and_then(|i| model.nodes.get(*i)) + .expect("failed to find neck joint") + .transform; + Self { + assets: assets::Assets::new(ctx), + model, + model_neck_base, + model_fb: framebuffer::Framebuffer::new( + ctx, + &glam::Vec2::new(terminal::WIDTH as _, terminal::HEIGHT as _), + &glam::Vec2::ZERO + ), + terminal: terminal::Terminal::new(ctx), + fig: fig::Client::new("shiro:32050", &[ + sexp!((avatar toggle)), + sexp!((avatar text)), + sexp!((avatar frame)), + sexp!((avatar reset)), + sexp!((avatar tracking)), + ]), + tracking_eyes: (1.0, 1.0), + tracking_neck: glam::Quat::IDENTITY, + } + } + pub fn handle_tracking(&mut self, msg: fig::Message) -> 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()?; + let euler = msg.data.get(1)?; + let euler_x = euler.get(0)?.as_str()?.parse::<f32>().ok()?.to_radians(); + let euler_y = PI - euler.get(1)?.as_str()?.parse::<f32>().ok()?.to_radians(); + let euler_z = euler.get(2)?.as_str()?.parse::<f32>().ok()?.to_radians() + PI/2.0; + self.tracking_eyes = (eye_left, eye_right); + self.tracking_neck = glam::Quat::from_euler(glam::EulerRot::XYZ, euler_x, euler_y, euler_z); + Some(()) + } + pub fn handle_text(&mut self, msg: fig::Message) -> Option<()> { + let s = msg.data.get(0)?.as_str()?; + self.terminal.fill_string(s); + Some(()) + } +} + +impl teleia::state::Game for Overlay { + fn initialize_audio(&self, ctx: &context::Context, st: &state::State, actx: &audio::Context) -> HashMap<String, audio::Audio> { + HashMap::new() + } + fn finish_title(&mut self, _st: &mut state::State) {} + fn mouse_press(&mut self, _ctx: &context::Context, _st: &mut state::State) {} + fn mouse_move(&mut self, _ctx: &context::Context, _st: &mut state::State, _x: i32, _y: i32) {} + fn update(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> { + st.projection = glam::Mat4::perspective_lh( + PI / 4.0, + terminal::WIDTH as f32 / terminal::HEIGHT as f32, + 0.1, 10.0 + ); + st.move_camera( + ctx, + &glam::Vec3::new(0.0, 0.0, 1.0), + &glam::Vec3::new(0.0, 0.0, -1.0), + &glam::Vec3::new(0.0, 1.0, 0.0), + ); + while let Some(msg) = self.fig.pump() { + let malformed = format!("malformed {} data: {}", msg.event, msg.data); + if msg.event == sexp!((avatar tracking)) { + if self.handle_tracking(msg).is_none() { log::warn!("{}", malformed) } + } else if msg.event == sexp!((avatar text)) { + if self.handle_text(msg).is_none() { log::warn!("{}", malformed) } + } else { + log::info!("received unhandled event {} with data: {}", msg.event, msg.data); + } + } + 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); + } + Some(()) + } + fn render(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> { + self.model_fb.bind(ctx); + ctx.clear_color(glam::Vec4::new(0.0, 0.0, 0.0, 1.0)); + ctx.clear(); + st.bind_3d(ctx, &self.assets.shader_scene); + self.assets.shader_scene.set_position_3d( + ctx, + &glam::Mat4::from_translation( + glam::Vec3::new(0.0, -1.6, 0.5), + ), + ); + self.model.render(ctx, &self.assets.shader_scene); + st.render_framebuffer.bind(ctx); + self.terminal.update(ctx, &self.model_fb); + self.terminal.render(ctx, &glam::Vec2::new(400.0, 200.0)); + // self.model_fb.blit( + // ctx, &st.render_framebuffer, + // &glam::Vec2::new(ctx.render_width / 2.0 - 512.0, ctx.render_height / 2.0 - 512.0), + // &glam::Vec2::new(128.0, 128.0) + // ); + Some(()) + } +} diff --git a/src/common/overlay/assets.rs b/src/common/overlay/assets.rs new file mode 100644 index 0000000..9a0cefc --- /dev/null +++ b/src/common/overlay/assets.rs @@ -0,0 +1,27 @@ +use teleia::*; + +pub struct Assets { + pub font: font::Bitmap, + pub shader_flat: shader::Shader, + pub shader_scene: shader::Shader, + pub mesh_square: mesh::Mesh, +} + +impl Assets { + pub fn new(ctx: &context::Context) -> Self { + Self { + font: font::Bitmap::new(ctx), + shader_flat: shader::Shader::new( + ctx, + include_str!("assets/shaders/flat/vert.glsl"), + include_str!("assets/shaders/flat/frag.glsl"), + ), + shader_scene: shader::Shader::new( + ctx, + include_str!("assets/shaders/scene/vert.glsl"), + include_str!("assets/shaders/scene/frag.glsl") + ), + mesh_square: mesh::Mesh::from_obj(ctx, include_bytes!("assets/meshes/square.obj")), + } + } +} diff --git a/src/common/overlay/assets/audio/test.wav b/src/common/overlay/assets/audio/test.wav Binary files differnew file mode 100644 index 0000000..0eabe85 --- /dev/null +++ b/src/common/overlay/assets/audio/test.wav diff --git a/src/common/overlay/assets/fonts/iosevka-comfy-regular.ttf b/src/common/overlay/assets/fonts/iosevka-comfy-regular.ttf Binary files differnew file mode 100644 index 0000000..b474adc --- /dev/null +++ b/src/common/overlay/assets/fonts/iosevka-comfy-regular.ttf diff --git a/src/common/overlay/assets/fonts/terminus.png b/src/common/overlay/assets/fonts/terminus.png Binary files differnew file mode 100644 index 0000000..287810b --- /dev/null +++ b/src/common/overlay/assets/fonts/terminus.png diff --git a/src/common/overlay/assets/fonts/terminus.ttf b/src/common/overlay/assets/fonts/terminus.ttf Binary files differnew file mode 100644 index 0000000..d125e63 --- /dev/null +++ b/src/common/overlay/assets/fonts/terminus.ttf diff --git a/src/common/overlay/assets/meshes/square.obj b/src/common/overlay/assets/meshes/square.obj new file mode 100644 index 0000000..7328a6c --- /dev/null +++ b/src/common/overlay/assets/meshes/square.obj @@ -0,0 +1,15 @@ +# Blender 3.6.2 +# www.blender.org +o Cube +v -1.000000 -1.000000 0.000000 +v 1.000000 -1.000000 0.000000 +v -1.000000 1.000000 0.000000 +v 1.000000 1.000000 0.000000 +vn -0.0000 -0.0000 -1.0000 +vt 0.0 1.0 +vt 1.0 0.0 +vt 0.0 0.0 +vt 1.0 1.0 +s 0 +f 3/1/1 2/2/1 1/3/1 +f 3/1/1 4/4/1 2/2/1 diff --git a/src/common/overlay/assets/scenes/lcolonq.vrm b/src/common/overlay/assets/scenes/lcolonq.vrm Binary files differnew file mode 100644 index 0000000..49fa4a4 --- /dev/null +++ b/src/common/overlay/assets/scenes/lcolonq.vrm diff --git a/src/common/overlay/assets/shaders/flat/frag.glsl b/src/common/overlay/assets/shaders/flat/frag.glsl new file mode 100644 index 0000000..7006a2b --- /dev/null +++ b/src/common/overlay/assets/shaders/flat/frag.glsl @@ -0,0 +1,11 @@ +uniform sampler2D texture_data; + +void main() +{ + vec2 tcfull = vec2(vertex_texcoord.x, 1.0 - vertex_texcoord.y); + vec4 texel = texture(texture_data, tcfull); + if (texel.a != 1.0) { + discard; + } + frag_color = texel; +} diff --git a/src/common/overlay/assets/shaders/flat/vert.glsl b/src/common/overlay/assets/shaders/flat/vert.glsl new file mode 100644 index 0000000..e324f7e --- /dev/null +++ b/src/common/overlay/assets/shaders/flat/vert.glsl @@ -0,0 +1,4 @@ +void main() +{ + default_main(); +}
\ No newline at end of file diff --git a/src/common/overlay/assets/shaders/scene/frag.glsl b/src/common/overlay/assets/shaders/scene/frag.glsl new file mode 100644 index 0000000..81e08b9 --- /dev/null +++ b/src/common/overlay/assets/shaders/scene/frag.glsl @@ -0,0 +1,11 @@ +uniform sampler2D texture_data; + +void main() +{ + vec4 texel = texture(texture_data, vertex_texcoord); + if (texel.a != 1.0) { + discard; + } + + frag_color = vec4(texel.rgb, texel.a); +} diff --git a/src/common/overlay/assets/shaders/scene/vert.glsl b/src/common/overlay/assets/shaders/scene/vert.glsl new file mode 100644 index 0000000..64f400c --- /dev/null +++ b/src/common/overlay/assets/shaders/scene/vert.glsl @@ -0,0 +1,18 @@ +in vec4 joint; +in vec4 weight; + +uniform mat4 joint_matrices[128]; + +void main() +{ + vertex_texcoord = texcoord; + vertex_normal = (normal_matrix * vec4(normal, 1.0)).xyz; + mat4 skin + = weight.x * joint_matrices[int(joint.x)] + + weight.y * joint_matrices[int(joint.y)] + + weight.z * joint_matrices[int(joint.z)] + + weight.w * joint_matrices[int(joint.w)]; + vec3 pos = (position * skin * vec4(vertex, 1.0)).xyz; + vertex_view_vector = camera_pos - pos; + gl_Position = projection * view * vec4(pos, 1.0); +} diff --git a/src/common/overlay/assets/textures/test.png b/src/common/overlay/assets/textures/test.png Binary files differnew file mode 100644 index 0000000..1f1edca --- /dev/null +++ b/src/common/overlay/assets/textures/test.png diff --git a/src/common/overlay/fig.rs b/src/common/overlay/fig.rs new file mode 100644 index 0000000..b60f8e9 --- /dev/null +++ b/src/common/overlay/fig.rs @@ -0,0 +1,40 @@ +use std::io::{BufRead, Write}; + +#[derive(Debug, Clone)] +pub struct Message { + pub event: lexpr::Value, + pub data: lexpr::Value, +} + +pub struct Client { + reader: std::io::BufReader<std::net::TcpStream>, +} +impl Client { + pub fn new(addr: &str, subs: &[lexpr::Value]) -> Self { + let mut socket = std::net::TcpStream::connect(addr).expect("failed to connect to message bus"); + socket.set_nonblocking(true).expect("failed to set message bus socket nonblocking"); + for s in subs { + write!(socket, "(sub {})\n", s).expect("failed to send subscribe message to bus"); + } + let reader = std::io::BufReader::new(socket); + Self { reader, } + } + pub fn pump(&mut self) -> Option<Message> { + let mut buf = String::new(); + match self.reader.read_line(&mut buf) { + Ok(l) => match lexpr::from_str(&buf) { + Ok(v) => { + match v.as_cons() { + Some(cs) => { + Some(Message { event: cs.car().clone(), data: cs.cdr().clone() }) + }, + _ => { log::error!("malformed message bus input s-expression: {}", v); None }, + } + }, + Err(e) => { log::error!("malformed message bus input line: {}", e); None }, + }, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => None, + Err(e) => panic!("IO error on message bus: {}", e), + } + } +} diff --git a/src/common/overlay/terminal.rs b/src/common/overlay/terminal.rs new file mode 100644 index 0000000..a2382b1 --- /dev/null +++ b/src/common/overlay/terminal.rs @@ -0,0 +1,122 @@ +use teleia::*; + +pub const WIDTH: usize = 64; +pub const HEIGHT: usize = 64; + +#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq)] +pub struct Pos { + pub x: i32, pub y: i32, +} +impl Pos { + pub fn new(x: i32, y: i32) -> Self { Self { x, y } } +} +impl std::ops::Add for Pos { + type Output = Pos; + fn add(self, rhs: Self) -> Self { Self {x: self.x + rhs.x, y: self.y + rhs.y } } +} + +#[derive(Debug, Clone)] +pub struct CharPair { + pub first: char, + pub second: Option<char>, +} +impl Default for CharPair { + fn default() -> Self { + Self { + first: 'a', + second: Some('b'), + } + } +} + +pub struct Layer<T> { + pub data: [T; WIDTH * HEIGHT], +} +impl<T> Layer<T> { + pub fn new() -> Self where T: Default { + Self { + data: [(); WIDTH * HEIGHT].map(|_| T::default()), + } + } + pub fn get(&self, p: Pos) -> Option<&T> { + if p.x < 0 || p.x >= WIDTH as _ || p.y < 0 || p.y >= HEIGHT as _ { return None } + let idx = p.x as usize + p.y as usize * WIDTH; + Some(&self.data[idx]) + } + pub fn set(&mut self, p: Pos, x: T) { + if p.x < 0 || p.x >= WIDTH as _ || p.y < 0 || p.y >= HEIGHT as _ { return } + let idx = p.x as usize + p.y as usize * WIDTH; + self.data[idx] = x; + } +} +impl Layer<CharPair> { + pub fn from_str(&mut self, s: &str) { + let chars: Vec<char> = s.chars().collect(); + if chars.is_empty() { return } + let mut i: usize = 0; + for row in 0..64 { + for col in 0..64 { + let first = chars[i]; i += 1; i %= chars.len(); + let second = Some(chars[i]); i += 1; i %= chars.len(); + self.set(Pos::new(col, row), CharPair { first, second }); + } + } + } +} +impl Layer<glam::Vec3> { + pub fn from_framebuffer(&mut self, ctx: &context::Context, fb: &framebuffer::Framebuffer) { + fb.get_pixels(ctx, &mut self.data); + } +} + +pub struct Terminal { + pub font: font::Bitmap, + pub base_color: Layer<glam::Vec3>, + pub set_color: Layer<glam::Vec3>, + pub set_char: Layer<CharPair>, +} +impl Terminal { + pub fn new(ctx: &context::Context) -> Self { + let mut set_char = Layer::new(); + set_char.from_str("lcolonq"); + Self { + font: font::Bitmap::from_image(ctx, 6, 12, 96, 72, include_bytes!("assets/fonts/terminus.png")), + base_color: Layer::new(), + set_color: Layer::new(), + set_char, + } + } + pub fn get_color(&self, pos: Pos) -> glam::Vec3 { + if let Some(c) = self.base_color.get(pos) { + c.clone() + } else { + glam::Vec3::new(1.0, 1.0, 1.0) + } + } + pub fn update(&mut self, ctx: &context::Context, fb: &framebuffer::Framebuffer) { + self.base_color.from_framebuffer(ctx, fb); + } + pub fn fill_string(&mut self, s: &str) { + self.set_char.from_str(s); + } + pub fn render(&self, ctx: &context::Context, pos: &glam::Vec2) { + let mut s = String::new(); + let mut colors = Vec::new(); + for row in 0..64 { + for col in 0..64 { + let pos = Pos::new(col, row); + let new = if let Some(p) = self.set_char.get(pos) { + format!("{}{}", p.first, if let Some(snd) = p.second { snd } else { ' ' }) + } else { + String::from(" ") + }; + s += &new; + let c = self.get_color(pos); + colors.push(c); colors.push(c); + } + s += "\n"; + colors.push(glam::Vec3::new(1.0, 1.0, 1.0)); + } + self.font.render_text_helper(ctx, pos, &s, &colors); + } +} diff --git a/src/common/server.rs b/src/common/server.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/common/server.rs |
