Code Monkey home page Code Monkey logo

ray's People

Contributors

wadetb avatar

Watchers

 avatar

Forkers

kmastroluca

ray's Issues

I Forked This Because....

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.

  • Kasene

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.