Skip to content

MajidAbdelilah/scop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

15 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

SCOP - 3D Object Viewer with OpenGL

Rust OpenGL 42

๐Ÿ“‹ 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 .obj files
  • 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) and f (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

About

1337 scop project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors