No description
| src | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| Makefile | ||
| README.md | ||
| scop.en.subject.pdf | ||
SCOP - 3D Object Viewer with OpenGL
📋 Overview
SCOP is a GPU-based 3D object renderer developed as part of the 42 Network / 1337 advanced curriculum. This project serves as an introduction to GPU rendering using OpenGL (via the Glium library in Rust). It parses .obj files, renders 3D models with perspective projection, and supports texture mapping with smooth transitions.
🎯 Project Objectives
- Parse and render 3D objects from
.objfiles - Implement perspective projection for realistic 3D visualization
- Auto-rotate the object around its center of mass
- Support movement along all three axes (X, Y, Z)
- Apply textures with smooth transition effects
- Generate per-face colors for visual distinction
- Implement custom matrix mathematics (no external math libraries)
✨ Features
Core Features
| Feature | Description |
|---|---|
| OBJ Parser | Custom parser for Wavefront .obj files with support for vertices (v) and faces (f) |
| Fan Triangulation | Converts n-gon polygons to triangles for GPU rendering |
| Perspective Projection | Proper 3D perspective with configurable FOV, near/far planes |
| Look-At Camera | View matrix implementation with camera positioning |
| Auto-Rotation | Continuous rotation around the object's center of symmetry |
| Axis Switching | Smooth animated transitions between rotation axes (X, Y, Z) |
| Texture Mapping | PPM texture loading with UV coordinate generation |
| Smooth Transitions | Lerp-based transitions between colored and textured modes |
UV Mapping Algorithms
- Box Projection: UV mapping based on dominant axis direction
- Spherical Projection: Longitude/latitude based UV mapping for spherical objects
🛠️ Technical Stack
| Component | Technology |
|---|---|
| Language | Rust (Edition 2024) |
| Graphics API | OpenGL (via Glium) |
| Windowing | Winit (cross-platform) |
| Shader Language | GLSL 1.40 |
📦 Project Structure
scop/
├── Cargo.toml # Rust package manifest
├── Makefile # Build automation (42 standard)
├── README.md # This file
└── src/
├── main.rs # Application entry, rendering loop, shaders
└── obj_parcer.rs # OBJ file parser and texture loader
🔧 Building
Prerequisites
- Rust toolchain (stable)
- OpenGL compatible GPU
Compile
# Using Makefile (42 standard)
make
# Or using Cargo directly
cargo build --release
Clean
make clean # Remove build artifacts
make fclean # Full clean including binary
make re # Rebuild from scratch
🚀 Usage
./target/release/scop <obj_path> <texture_path> <uv_algorithm>
Arguments
| Argument | Description |
|---|---|
obj_path |
Path to the .obj file to render |
texture_path |
Path to the .ppm texture file |
uv_algorithm |
UV mapping method: box or sphere |
Example
./target/release/scop models/42.obj textures/ponies.ppm box
⌨️ Controls
| Key | Action |
|---|---|
Q |
Toggle texture/color mode (smooth transition) |
T |
Toggle dynamic color animation ("lizard mode") |
W |
Switch to X-axis rotation |
E |
Switch to Y-axis rotation |
R |
Switch to Z-axis rotation |
Y |
Reverse rotation direction |
A / Z |
Move along X-axis (+/-) |
S / X |
Move along Y-axis (+/-) |
D / C |
Move along Z-axis (+/-) |
🧮 Technical Implementation
Matrix Mathematics
All matrix operations are implemented from scratch without external math libraries:
- Perspective Projection Matrix: Standard OpenGL perspective with configurable FOV
- View Matrix (Look-At): Camera positioning using forward, right, and up vectors
- Rotation Matrices: Separate X, Y, Z rotation with column-major layout
- Matrix Multiplication: Column-major 4x4 matrix multiplication
Shaders
Vertex Shader
#version 140
// Transforms vertices through Model-View-Projection pipeline
// Generates per-face colors from uniform buffer
// Centers object using bounding box calculation
Fragment Shader
#version 140
// Blends between solid color and texture based on lerp value
// Supports smooth transitions between rendering modes
OBJ Parser Features
- Vertex position parsing (
v x y z) - Face parsing with n-gon support (
f v1 v2 v3 ...) - Face indices with vertex/texture/normal format (
f v/vt/vn) - Bounding box calculation for object centering
- Automatic fan triangulation for polygons with 3+ vertices
PPM Texture Loader
- Binary PPM (P6) format support
- Header parsing (magic number, dimensions, max value)
- Raw RGB data loading
📐 Rendering Pipeline
OBJ File → Parser → Vertices/Indices → Vertex Buffer
↓
PPM File → Texture Loader → Texture2D → Fragment Shader
↓
Uniforms (Matrices, Colors) → Shaders → Framebuffer → Display
🎨 Color System
The renderer uses a uniform buffer with 5 colors for per-face coloring:
- Colors are assigned cyclically to triangles (
(gl_VertexID / 3) % 5) - "Lizard mode" creates dynamic animated colors using cosine waves
- Smooth interpolation between color and texture modes
📝 Notes
Constraints (per 42 subject)
- ✅ No external libraries for matrix operations
- ✅ No external libraries for OBJ parsing
- ✅ No external libraries for shader loading
- ✅ Custom perspective projection implementation
- ✅ Classic Makefile provided
File Format Requirements
- OBJ files: Standard Wavefront format with
v(vertices) andf(faces) - Textures: PPM format (P6 binary) required
🔗 Dependencies
[dependencies]
glium = { git = "https://github.com/glium/glium.git" }
Glium provides:
- OpenGL context creation
- Window management (via Winit)
- Safe OpenGL bindings
- Shader compilation and management
📄 License
This project is part of the 42 Network curriculum.
Developed as part of the 42 Network / 1337 Advanced Curriculum