Create obj module for scene objects
This commit is contained in:
29
src/obj.c
Normal file
29
src/obj.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "obj.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
bool intersect_sphere(obj_params_t params, ray_t ray, hit_t *hit_out)
|
||||
{
|
||||
const vec3_t centre = params.sphere.centre;
|
||||
const double r = params.sphere.radius;
|
||||
const vec3_t disp = vec3_sub(centre, ray.orig);
|
||||
|
||||
const double a = vec3_dot(ray.dir, ray.dir);
|
||||
const double b = -2.0 * vec3_dot(ray.dir, disp);
|
||||
const double c = vec3_dot(disp, disp) - r * r;
|
||||
|
||||
const double discriminant = b * b - 4 * a * c;
|
||||
if (discriminant < 0)
|
||||
return false;
|
||||
|
||||
const double t = (-b - sqrt(discriminant)) / (2.0 * a);
|
||||
const vec3_t point = vec3_add(ray.orig, vec3_scale(ray.dir, t));
|
||||
const vec3_t normal = vec3_unit(vec3_sub(point, centre));
|
||||
|
||||
if (hit_out != nullptr) {
|
||||
hit_out->point = point;
|
||||
hit_out->normal = normal;
|
||||
hit_out->t = t;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user