A simple, dependency-free 3D cube rendered in your terminal using C. This project is a fun exercise in 3D graphics fundamentals, built from scratch.
You should really see it in action, the gif above makes it look worse than it is!
This is a small project to render a spinning 3D cube in the terminal. The goal was to write it in C with no external dependencies, focusing on the core concepts of 3D graphics:
- 3D Math from Scratch: All the logic for 3D point rotation and perspective projection is self-contained.
- Software Z-Buffering: Correctly handles depth and occlusion for a proper 3D effect.
- Terminal Rendering: Draws directly to a character-based frame buffer, which is then printed to the console.
- Clean & Modular Code: The logic is separated into modules for math, rendering, input, and the cube object itself.
cube/
├── src/ # Source files
│ ├── main.c # Main program loop and initialization
│ ├── math3d.c # 3D mathematics and rotation calculations
│ ├── renderer.c # Rendering engine and buffer management
│ ├── input.c # Terminal input handling and controls
│ └── cube.c # Cube-specific drawing and animation logic
├── include/ # Header files
│ ├── math3d.h # 3D structures and function declarations
│ ├── renderer.h # Rendering system declarations
│ ├── input.h # Input handling declarations
│ ├── cube.h # Cube logic declarations
│ └── constants.h # Constants used throughout the project
├── Makefile # Build system configuration
├── README.md # This file
├── CHANGELOG.md # Project change log
├── LICENSE # Project license file
└── demo.gif # Animated demonstration of the cube rendering
You'll need a C compiler (like GCC
) and make
. The code is written in C99 + GNU Extensions and should work on any POSIX-compatible system (Linux, macOS, WSL).
# Clone the repo
git clone https://github.com/D-Heger/cube.git
cd cube
# Compile and build the executable
make
# Running the executable
make run
# Or
./cube
The Makefile
also offers other useful commands:
# Clean build artifacts
make clean
# Rebuild from scratch
make rebuild
- The cube will rotate on its own.
- Press the
1
key to exit cleanly. - If needed, you can always stop the program with
Ctrl+C
.
See CHANGELOG.md for a detailed list of changes and release history.
This project is open-source and available under the MIT License.
- D. Heger