# Raytracer - Triangle Intersection + Normals

So i have written a basic raytracer which so far is functional with spheres. However i am extending its functionality to also work with meshes, which means implementing triangle intersections.

I found the general logic of triangle intersections and implemented it easily enough, however i am not quite sure how to calculate the intersection normals.

Here is the code i have written for spheres.

``````

bool sphereIntersection(Ray *ray, Sphere *sphere, Intersection* intersection)
{
double discriminant;
double lambda1, lambda2;
TVector temp;

dirDot = ray->direction | ray->direction;
temp = ray->origin - sphere->center;

rayDot = 2 * (temp | ray->direction);
temp = ray->origin - sphere->center;

discriminant = rayDot * rayDot - 4 * dirDot * radDot;

intersection->valid = false;

if (discriminant >= 0)
{
lambda1 = (-rayDot + sqrt(discriminant)) / (2 * dirDot);
lambda2 = (-rayDot - sqrt(discriminant)) / (2 * dirDot);

// is the object visible from the eye position?
if (lambda1 >= 0 && lambda2 >= 0)
{
if (lambda1 == lambda2)
{
intersection->lambdaIn = intersection->lambdaOut = lambda1;
}
else if (lambda1 < lambda2)
{
intersection->lambdaIn  = lambda1;
intersection->lambdaOut = lambda2;
}
else
{
intersection->lambdaIn  = lambda2;
intersection->lambdaOut = lambda1;
}

intersection->valid = true;
return true;
}
}

return false;
}

void normalOfSphereIntersection(Ray* ray, Sphere *sphere, Intersection* intersection)
{
double lambda, scale;
TVector v1, v2, point, normal;

lambda = intersection->lambdaIn;

v1 = lambda * ray->direction;
point = v1 + ray->origin;

intersection->point = point;
intersection->point = point;
intersection->point = point;

v2 = point - sphere->center;

{
}

normal = scale * v2;

normal.normalize();

intersection->normal = normal;
intersection->normal = normal;
intersection->normal = normal;
}
``````

And here is my triangle intersection code:

``````
bool triangleIntersection(Ray* ray, Triangle* tri, Intersection* intersection)
{
TVector v0 = tri->points;
TVector v1 = tri->points;
TVector v2 = tri->points;

TVector e1 = v1 - v0;
TVector e2 = v2 - v0;

TVector h = ray->direction * e2;

float determinant = e1 | h;

intersection->valid = false;

if (determinant > -0.00001 && determinant < 0.00001)
{
return false;
}

float inverse = 1.0 / determinant;
TVector s = ray->origin - v0;

float u = inverse * (s | h);

if (u < 0.0 || u > 1.0)
{
return false;
}

TVector q = s * e1;

float v = inverse * (ray->direction | q);

if (v < 0.0 || u + v > 1.0)
{
return false;
}

float t = inverse * (e2 | q);

if (t > 0.00001) // ray intersection
{
intersection->lambdaIn = t;
intersection->valid = true;

return true;
}
else // there is a line intersection, but no ray intersection
{
return false;
}
}

void normalOfTriangleIntersection(Ray* ray, Triangle* tri, Intersection* intersection)
{
//todo
}
``````

is the normal of the triangle intersection just the normal of the triangle that is hit? Or would it be the direction in which the ray would bounce if it hit the triangle?