Complete Rust port of the cel-shaded spinning cubes OpenGL demo.
cargo run --releasecargo build --releaseThe executable will be at target/release/dynoworld-rust.exe
- Mouse Movement: Rotate camera (orbital)
- Mouse Scroll: Zoom in/out
- ESC: Toggle mouse capture on/off
- +/=: Increase light brightness
- -: Decrease light brightness
dynoworld/
├── Cargo.toml # Rust dependencies
├── build.rs # OpenGL bindings generator
├── src/
│ ├── main.rs # Rust port (680 lines)
│ └── shaders/
│ ├── vertex.glsl # Vertex shader (unchanged)
│ └── fragment.glsl # Fragment shader (unchanged)
- Memory Safety: All OpenGL calls wrapped in
unsafeblocks - Ownership Model: Clear ownership of vertex data, camera state
- Pattern Matching: Event handling using Rust's match expressions
- Type Safety: Strongly typed matrices, cubes, camera
| C++ | Rust |
|---|---|
float m[16] |
m: [f32; 16] |
struct Mat4 { ... } |
struct Mat4 { ... } |
| Global variables | Module-level mutable state |
| Manual memory management | Automatic via ownership |
| Callbacks (function pointers) | Event polling with match |
- No global state mutation: Camera and cubes are local to
main() - Event polling vs callbacks: Rust GLFW uses iterator pattern
- Explicit unsafe blocks: All OpenGL FFI calls marked unsafe
- Vec instead of arrays: Dynamic vertex data storage
- Pattern matching: Cleaner input handling than if/else chains
- glfw (0.58): Window management and input
- gl (0.14): OpenGL 3.3 bindings
- gl_generator (0.14): Build-time GL function loading
Compiled with --release flag for optimization:
- Same rendering performance as C++ version
- Zero-cost abstractions
- LLVM optimizations enabled
The GLSL shaders are 100% identical to the C++ version:
- Vertex shader: Position transformation, normal calculation
- Fragment shader: Cel shading with 4-level quantization, rim lighting
- Type safety (no implicit casts)
- Memory safety (no use-after-free)
- Pattern matching (cleaner control flow)
- Cargo dependency management
- More verbose (explicit types, unsafe blocks)
- Steeper learning curve (ownership model)
- Larger binary size (includes Rust std)
- Debug build:
target/debug/dynoworld-rust.exe(~10MB) - Release build:
target/release/dynoworld-rust.exe(~2MB) - Incremental compilation cache in
target/
Consider using:
- glium: Safe OpenGL wrapper (reduces unsafe code)
- nalgebra or cgmath: Mature linear algebra libraries
- wgpu: Modern graphics API (Vulkan/Metal/DX12)