Adds normals for sphere and triangle

This commit is contained in:
Maximilian Giller 2022-11-17 16:16:32 +01:00
parent b54fdf4f7c
commit 1531a0f2c7
2 changed files with 5 additions and 2 deletions

View file

@ -36,7 +36,9 @@ bool Sphere::intersect(Ray &ray) const {
return false;
// Calculate the normal
// IMPLEMENT ME
auto intersectPoint = ray.origin + ray.direction * t;
auto normalDirection = intersectPoint - this->center; // Point of intersect - origin of sphere creates a line along the normal, pointing outwards
ray.normal = normalized(normalDirection);
// Calculate the surface position and tangent vector
float const phi = std::acos(ray.normal.y);

View file

@ -35,7 +35,7 @@ bool Triangle::intersectArea(Ray &ray) const {
float const cosine = dotProduct(ray.direction, normal);
if (abs(cosine) < EPSILON)
if (std::abs(cosine) < EPSILON)
return false;
float const t = dotProduct(vertex[0] - ray.origin, normal) / cosine;
@ -57,6 +57,7 @@ bool Triangle::intersectArea(Ray &ray) const {
ray.surface = a * this->surface[1] + b * this->surface[2] + (1 - a - b) * this->surface[0];
// Set the new length and the current primitive
ray.normal = normal;
ray.length = t;
ray.primitive = this;