A 3D ray tracer built in C using the miniLibX library, following 42 School norminette standards.
├── includes/
│ └── minirt.h # Main header with all structures and prototypes
├── srcs/minirt/
│ ├── minirt.c # Main program entry point
│ ├── vector/ # Vector mathematics
│ │ ├── vector_ops.c
│ │ └── vector_utils.c
│ ├── color/ # Color operations
│ │ ├── color_ops.c
│ │ └── color_utils.c
│ ├── ray/ # Ray utilities
│ │ └── ray_utils.c
│ ├── camera/ # Camera functionality
│ │ └── camera.c
│ ├── objects/ # Object intersection and management
│ │ ├── intersect_sphere.c
│ │ ├── intersect_plane.c
│ │ ├── intersect_cylinder.c
│ │ ├── intersect_scene.c
│ │ └── object_utils.c
│ ├── lighting/ # Lighting calculations
│ │ └── lighting.c
│ ├── renderer/ # Rendering engine
│ │ └── renderer.c
│ ├── mlx/ # MLX window management
│ │ └── mlx_utils.c
│ ├── parser/ # Scene file parsing
│ │ ├── parser.c
│ │ ├── parse_scene_elements.c
│ │ └── parse_objects.c
│ └── utils/ # General utilities
│ └── utils.c
├── libft/ # Custom C library
├── libmlx/ # MiniLibX for Linux
├── libmlx_macos/ # MiniLibX for macOS
└── scenes/ # Scene files
└── sample.rt # Sample scene
- t_vec3: 3D vector for positions, directions, and normals
- t_color: RGB color representation (0.0-1.0 range)
- t_ray: Ray with origin and direction
- t_hit: Intersection information
- t_material: Surface material properties
- t_camera: Camera with position, direction, and field of view
- t_light: Point light source
- t_ambient: Ambient lighting
- t_scene: Complete scene container
- t_sphere: Sphere with center and radius
- t_plane: Infinite plane with point and normal
- t_cylinder: Finite cylinder with center, axis, radius, and height
- Sphere, plane, and cylinder primitives
- Camera with configurable FOV
- Point light source
- Ambient lighting
- Diffuse and specular lighting (Phong model)
- Hard shadows
- Scene file parsing (.rt format)
- MLX window management
- ESC key and window close handling
- Ray-object intersection
- Phong lighting model
- Shadow ray casting
- Anti-aliasing ready structure
- Configurable materials
The program accepts .rt files with the following elements:
A [ratio] [R,G,B]
A 0.2 255,255,255
C [position] [direction] [FOV]
C -50,0,20 0,0,1 70
L [position] [brightness] [R,G,B]
L -40,0,30 0.7 255,255,255
sp [center] [diameter] [R,G,B]
sp 0,0,20 20 255,0,0
pl [point] [normal] [R,G,B]
pl 0,0,0 0,1,0 255,0,225
cy [center] [axis] [diameter] [height] [R,G,B]
cy 50.0,0.0,20.6 0,0,1.0 14.2 21.42 10,0,255
- GCC compiler
- Make
- X11 development libraries (Linux)
- MiniLibX
make # Build the project
make clean # Remove object files
make fclean # Remove all generated files
make re # Rebuild everything
make run # Build and run with sample scene./miniRT [scene_file.rt]- Camera Setup: Initialize camera with proper viewport calculations
- Ray Generation: Create rays for each pixel through the viewport
- Intersection Testing: Test rays against all scene objects
- Lighting Calculation: Apply Phong lighting model with shadows
- Color Output: Convert final color to RGB and display
- Vector operations (add, subtract, multiply, normalize, dot product, cross product)
- Ray-sphere intersection using quadratic formula
- Ray-plane intersection using plane equation
- Ray-cylinder intersection with caps handling
- Phong reflection model for realistic lighting
- Proper allocation and deallocation of scene objects
- Linked list structure for dynamic object storage
- Clean exit handling for all resources
This structure provides:
- Modularity: Each component is isolated and testable
- Extensibility: Easy to add new object types or features
- Maintainability: Clear separation of concerns
- Performance: Efficient ray-object intersection algorithms
- Compliance: Follows 42 norminette standards
To add new object types:
- Define structure in
minirt.h - Add enum value to
t_object_type - Implement intersection function in
objects/ - Add parsing logic in
parser/ - Update object management functions
To add new lighting models:
- Add material properties to
t_material - Implement lighting function in
lighting/ - Update
calculate_lighting()function
- ESC: Exit the program
- Red X: Close window and exit
This foundation provides a solid base for a complete ray tracing implementation with room for advanced features like reflections, refractions, textures, and more complex geometries.