Skip to content

Instantly share code, notes, and snippets.

View matthewjberger's full-sized avatar
🦀
Programming something in Rust, probably

Matthew J. Berger matthewjberger

🦀
Programming something in Rust, probably
View GitHub Profile
@matthewjberger
matthewjberger / decompose.rs
Created January 7, 2024 08:11
Decompose a nalgebra_glm 4x4 transformation matrix into translation, rotation, and scaling (accounts for non-uniform scaling)
pub fn decompose_matrix(
matrix: &nalgebra_glm::Mat4,
) -> (nalgebra_glm::Vec3, nalgebra_glm::Quat, nalgebra_glm::Vec3) {
let translation = nalgebra_glm::Vec3::new(matrix.m14, matrix.m24, matrix.m34);
let (scale_x, scale_y, scale_z) = (
nalgebra_glm::length(&nalgebra_glm::Vec3::new(matrix.m11, matrix.m12, matrix.m13)),
nalgebra_glm::length(&nalgebra_glm::Vec3::new(matrix.m21, matrix.m22, matrix.m23)),
nalgebra_glm::length(&nalgebra_glm::Vec3::new(matrix.m31, matrix.m32, matrix.m33)),
);
@matthewjberger
matthewjberger / walk.rs
Last active January 7, 2024 04:01
Recursively walk a petgraph directed graph
fn visit_graph_recursively<N, E, VM>(
graph: &petgraph::graph::DiGraph<N, E>,
visit_map: &mut VM,
node_idx: petgraph::graph::NodeIndex,
) where
N: std::fmt::Debug,
VM: petgraph::visit::VisitMap<petgraph::graph::NodeIndex>,
{
// Mark the current node as visited
visit_map.visit(node_idx);
@matthewjberger
matthewjberger / tinycommand.rs
Created January 6, 2024 23:55
A tiny command pattern in rust
#[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum Command {
#[default]
Empty,
Exit,
}
impl Command {
pub fn execute(&mut self, context: &mut Context) {
@matthewjberger
matthewjberger / tinytimestep.rs
Last active January 6, 2024 23:55
Fixed timestep example
use std::time::{Instant};
#[derive(Clone)] // Derive the Clone trait to enable cloning of State
struct State {
// Example fields representing the state of the physics object
position: f64,
velocity: f64,
}
// Function to update the physics state of the object
@matthewjberger
matthewjberger / app.rs
Created January 2, 2024 21:20
OpenGL (glow) 3D interactive triangle in wasm and native using eframe. (This replaces app.rs in eframe, assumes "eframe/glow" feature is enabled)
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
pub struct TemplateApp {
angle: f32,
rotating_triangle: std::sync::Arc<egui::mutex::Mutex<RotatingTriangle>>,
}
impl TemplateApp {
/// Called once before the first frame.
pub fn new<'a>(cc: &'a eframe::CreationContext<'a>) -> Self {
let gl = cc.gl.as_ref().expect("Failed to get a GL context");
@matthewjberger
matthewjberger / tinypubsub.rs
Last active January 1, 2024 06:16
Tiny pubsub broker in rust
// [dependencies]
// uuid = { version = "1.6.1", features = ["v4"] }
use std::collections::{HashMap, VecDeque};
#[derive(Clone, Debug)]
pub enum Message {
String(String),
}
@matthewjberger
matthewjberger / upgrade.md
Last active December 30, 2023 08:56
force upgrade your rust app cargo.toml dependencies

Force upgrade your rust app cargo.toml dependencies

cargo install cargo-edit
cargo upgrade --ignore-rust-version --incompatible
@matthewjberger
matthewjberger / Cargo.toml
Created December 28, 2023 22:43
Put a pixel on the screen using SDL2's software renderer in Rust
[package]
name = "chitauri"
version = "0.1.0"
edition = "2021"
[dependencies]
sdl2 = { version = "0.36.0", features = ["bundled"] }
@matthewjberger
matthewjberger / scenegraph.rs
Created December 17, 2023 13:15
Scenegraph
use petgraph::graph::{DiGraph, NodeIndex};
use petgraph::Direction;
use nalgebra::{Vector3, Isometry3};
use std::collections::HashMap;
#[derive(Debug, Clone)]
enum SceneNode {
Spatial(SpatialNode),
Mesh { base: SpatialNode, vertices: Vec<Vector3<f32>> },
}
@matthewjberger
matthewjberger / bytecode.rs
Created December 17, 2023 08:52
Bytecode emulation pattern in rust
struct Emulator {
register: [u8; 2], // Two general-purpose registers
program_counter: usize,
memory: [u8; 256], // Simplified memory
}
enum Instruction {
Load(u8, u8), // Load a value into a register
Add, // Add two registers
Subtract, // Subtract two registers