wadetb / ray

Ray Tracing in One Weekend (in Rust)
0 stars 1 forks source link

I Forked This Because.... #1

Open KMastroluca opened 11 months ago

KMastroluca commented 11 months ago

I want to learn how to build that Ray Tracer you used to tell me was required to be considered for an internship. I write a fair amount of rust these days, and I've honestly defeated my impostor syndrome recently so I've been more confident to take on more difficult challenges. But I read main.rs and my brain turned directly into scrambled eggs.

I see that you've written a 3D vector structure and implemented various functions for manipulating and transforming its values and in the case of a ray tracer these vectors represent beams of light (i think?) which is why you have this

    pub fn reflect(self, n: Vec3) -> Vec3 {
        self - 2.0 * self.dot(n) * n
    }

    pub fn refract(self, n: Vec3, etai_over_etat: f32) -> Vec3 {
        let cos_theta = (-1.0 * self).dot(n).min(1.0);
        let r_out_perp = etai_over_etat * (self + cos_theta * n);
        let r_out_parallel = -((1.0 - r_out_perp.length_sqr()).abs().sqrt()) * n;
        r_out_perp + r_out_parallel
    }

Which, im sure is some textbook light refraction and algo, that I don't understand even a small tiny bit. Im sure reflect just bounces it off in whatever direction physics dictates.

Then, you implement traits for Adding, Subtracting, and Multiplying these vectors. Simple enough.

impl Sub for Vec3 {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        Vec3 {
            x: self.x - other.x,
            y: self.y - other.y,
            z: self.z - other.z,
        }
    }
}

impl Mul for Vec3 {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        Vec3 {
            x: self.x * other.x,
            y: self.y * other.y,
            z: self.z * other.z,
        }
    }
}

Then you implement your Sphere structure along with a function that, clearly does math stuff determining if the object is being hit with a ray of light, and then returning some Hit structure containing data about where it was hit, what kind of material the sphere is made out of which I assume somewhere down the line determines how the light is supposed to behave when reflecting or refracting light.

Next you define the attributes of your spheres:

const SPHERES: [Sphere; 5] = [
    // Ground
    Sphere {
        center: Point3 {
            x: 0.0,
            y: -100.5,
            z: -1.0,
        },
        radius: 100.0,
        material: MaterialRef {
            kind: MaterialKind::Lambertian,
            index: 0, // Ground
        },
    },
    // Center

I've got no idea what this is, other than the fact that you defined it under Materials, so....must be a material.

const LAMBERTIANS: [Lambertian; 4] = [

same with this

const DIELECTRICS: [Dielectric; 2] = [

EDIT: You explained these ^^^^ in the Readme which I did not read, So, I'm just an egghead.

You implement the 3D camera and on Camera::new() you setup the camera in some position looking down on your spheres

    pub fn new(
        lookfrom: Point3,
        lookat: Point3,
        vup: Vec3,
        vfov: f32,
        aspect_ratio: f32,
        aperture: f32,
        focus_dist: f32,
    ) -> Camera {
        let theta = vfov * 3.14159 / 180.0;
        let h = (theta / 2.0).tan();
        let viewport_height = 2.0 * h;
        let viewport_width = aspect_ratio * viewport_height;

        let w = (lookfrom - lookat).normalize();
        let u = vup.cross(w).normalize();
        let v = w.cross(u);

        let origin = lookfrom;
        let horizontal = focus_dist * viewport_width * u;
        let vertical = focus_dist * viewport_height * v;
        let lower_left_corner = origin - (0.5 * horizontal) - (0.5 * vertical) - (focus_dist * w);

        let lens_radius = aperture / 2.0;

        Camera {
            origin,
            lower_left_corner,
            horizontal,
            vertical,
            u,
            v,
            lens_radius,
        }
    }

Then you implement the light scattering behaviors for the different materials, and then in main() you setup the camera and loop thru all the pixels of the image and do more complicated algorithm math stuff.

I really wish I could understand this better. btw, I hope you're doing well, I went thru alot of bad stuff and now I'm doing pretty good and trying to get me a programming job somewhere. I've been working on a bunch of cool projects and making Open Source contributions. I'm not allowed to email you rn for some interesting reasons that ill gladly fill you in on later, so the only place I can talk is Github for now. But I'm glad to see that you're still the successful genius you've always been and I owe you a lot for teaching me C the hard way when I was a kid, and I'm eternally grateful for that.

wadetb commented 10 months ago

Hey Kasene, it's really great to hear from you. I saw you pop up on GitHub recently and was really happy to learn that you're back - and focusing on Rust - and it's great to know that you are doing well.

It's cool that you are trying to learn Ray tracing just from reading and following my code, but please know that I totally cheated and despite working on graphics for 25 years I could never have written this code entirely on my own. This repo a straightforward Rust port of the examples from the free book our colleague Pete Shirley, you can find it here: https://raytracing.github.io/. I read the book one weekend a few months ago after Pete joined Activision, because I had seen other people do this and always wanted to try it myself, and because I would feel guilty working with Pete if I hadn't spent one silly weekend on his book. I hope you can meet him sometime, he's a great guy and I think you would hit it off.

Anyway, if you have the time, I'd highly recommend spending a weekend reading the book and building out your own version of the code as you go. You'll end up with something a bit like mine (I made some minor tweaks), and when you are done you've got a launchpad to go a lot further (RT the rest of your life).

If I had more time to play with this I would also be trying my own version of NeRFs and InstantNGP from https://github.com/NVlabs/instant-ngp which I think is probably the future of graphics, but I don't really work on graphics anymore, instead I'm focused on trying to make our artist tools better.

I'll keep following you on GitHub and feel free to leave me a comment anytime. Good luck!

KMastroluca commented 10 months ago

Thats awesome. I totally check that book out and go thru the process. Graphics programming is still extremely daunting and Im still not that great at math. Still, I wont feel like Im a decent programmer until I write a raytracer.

Fun fact: I am currently a resident at the Maine State Prison and Ive shown them that I can write code, so i work building internal tools here for Maine Dept of Corrections. Theres three of us here of a skill level suitable to do this and its been really fun. They give me a laptop, access to the internet and Github and all the tools I need. Its been great and ive been writing code everyday preparing for my release on December 8th. Ive been here for 4 years...I wrote a POS System for cannabis dispensaries because they are forced to operate as cash based businesses. I want to expand it to help manage compliance and stuff like that. Its live right now in one dispensary in maine and we're getting feedback and bug reports. Also, I'm about to start working at UnlockedLabs as a paid developer working on a LMS data aggregation software.

It looks like Im gonna get out with a Job so I pretty happy about that. I can't ever thank you enough for what you did for me when I was younger. Are you still working out of the same office with Mike? If so, tell him I say hi.

It feels good to be back!