Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with shadows when rendering duck #31

Closed
ema2159 opened this issue Jan 3, 2023 · 1 comment
Closed

Issues with shadows when rendering duck #31

ema2159 opened this issue Jan 3, 2023 · 1 comment

Comments

@ema2159
Copy link

ema2159 commented Jan 3, 2023

Hello professor. I was just finishing my version of tinyraytracer written in Rust. In order to render the duck, I just implemented a triangle primitive, defining its intersection function (intersection with the plane containing the triangle and then checking through barycentric coordinates if the point lies inside the triangle), and its normal (cross product between two of its sides). If I render the duck using the ivory material, I get the following:
image

However, if I remove the shadow rendering code, I get a proper image:
image

Do you have any idea why this might be?

@ema2159
Copy link
Author

ema2159 commented Jan 4, 2023

Answering my own question, the issue was that I was calculating the intersection between the ray and the planes without considering whether the plane was behind the ray or not. Originally, I was calculating the plane-ray intersection as follows:

        let d = -normal.dot(&self.a.coords); // Parameter of plane equation

        let n_dot_raydir = -normal.dot(&ray.direction);
        if n_dot_raydir <= 0. {
            return None;
        }

        // If it exists, calculate the intersection point
        let t = (normal.dot(&ray.origin.coords) + d) / n_dot_raydir;
        let intersection_point = ray.origin + t * ray.direction;

without taking into consideration the fact that if t is negative, the intersection point is behind the ray, so it should be discarded.
It is sufficient to check for that condition and discard the point accordingly as follows:

        let d = -normal.dot(&self.a.coords); // Parameter of plane equation

        let n_dot_raydir = -normal.dot(&ray.direction);
        if n_dot_raydir <= 0. {
            return None;
        }

        // If it exists, calculate the intersection point
        let t = (normal.dot(&ray.origin.coords) + d) / n_dot_raydir;
        if t < 0. {
            return None;
        } 
        let intersection_point = ray.origin + t * ray.direction;

Without this, all the object properties (shadows, reflections, refractions) were being affected.

@ema2159 ema2159 closed this as completed Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant