Thanks to visit codestin.com
Credit goes to GitHub.com

Skip to content

ni5arga/raytracer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

raytracer

a small path-tracing renderer written in rust. this project follows the structure of ray tracing in one weekend.

the goal of this project is to learn the fundamentals of rendering: vectors, rays, normals, intersections, light scattering, sampling, and camera math — all implemented from scratch with no graphics libraries.


project structure

raytracer/
  ├── cargo.toml
  ├── src/
  │   ├── main.rs
  │   ├── lib.rs
  │   ├── vec3.rs
  │   ├── ray.rs
  │   ├── hittable.rs
  │   ├── sphere.rs
  │   ├── hittable_list.rs
  │   ├── camera.rs
  │   └── renderer.rs
  └── tests/
      └── vec3_tests.rs

all rendering logic lives in the library (src/lib.rs). src/main.rs is just a tiny entrypoint calling raytracer::run().


installation

make sure you have rust and cargo installed:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

clone the project:

git clone https://github.com/ni5arga/raytracer
cd raytracer

running the renderer

render an image in release mode (recommended):

cargo run --release > image.ppm

this produces a file called image.ppm in the project directory.


viewing or converting the image

macos

open image.ppm

linux

xdg-open image.ppm

convert to png (requires imagemagick)

convert image.ppm image.png

configuration

all render settings live in src/lib.rs inside run():

let image_width = 400;
let samples_per_pixel = 100;
let max_depth = 50;

you can modify:

  • image resolution
  • number of samples per pixel (reduces noise)
  • max recursion depth
  • scene geometry (list of spheres)
  • camera parameters (in camera.rs)

to add new spheres:

world.add(Box::new(Sphere::new(Point3::new(0.0, 1.0, -1.0), 0.5)));

running tests

unit tests live inside the tests/ directory.

run them with:

cargo test

they verify vector math (dot, cross, unit vector, clamp, and random sampling).


how it works (short explanation)

  • a ray is defined as p(t) = origin + t * direction.
  • each pixel fires many rays into the scene.
  • for each ray, we check intersections with hittable objects.
  • if a ray hits something, it scatters into a random direction.
  • each bounce accumulates color while reducing intensity.
  • if a ray misses, it blends into a blue sky gradient.
  • final pixel color = average of all ray samples.

license

mit license


About

raytracer written in rust from scratch

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages