#include #include #include "constants.h" #include "typedefs.h" #include "globalvar.h" int maksph(int surf, double r, double x, double y, double z) { int size; o_sphere *sphere; size = sizeof(o_sphere); sphere = ((o_sphere *)malloc(size)); object[nobject].id = nobject; object[nobject].objtyp = OTYPSPHERE; object[nobject].surfnum = surf; object[nobject].objptr.p_sphere = sphere; sphere->r = r; sphere->x = x; sphere->y = y; sphere->z = z; return 0; } double intsph(t_3d *pos, t_3d *ray, t_object *obj) { double b, c, D, s; double xadj, yadj, zadj; o_sphere *sph; sph = obj->objptr.p_sphere; /* translate ray origin to object's space */ xadj = pos->x - sph->x; yadj = pos->y - sph->y; zadj = pos->z - sph->z; /* solve quadratic equation */ b = xadj * ray->x + yadj * ray->y + zadj * ray->z; c = xadj * xadj + yadj * yadj + zadj * zadj - sph->r * sph->r; /* not sure why xadj is calculated, this should work: */ /* b = pos->x * ray->x + pos->y * ray->y + pos->z * ray->z; */ /* c = pos->x * pos->x + pos->y * pos->y + pos->z * pos->z - sph->r * sph->r; */ D = b * b - c; if (D < 0) return 0.0; s = -b - sqrt(D); if (s > 0) return s; s = -b + sqrt(D); if (s > 0) return s; return 0; } int nrmsph(t_3d *pos, t_object *obj, t_3d *nrm) { o_sphere *sph; sph = obj->objptr.p_sphere; nrm->x = (pos->x - sph->x) / sph->r; nrm->y = (pos->y - sph->y) / sph->r; nrm->z = (pos->z - sph->z) / sph->r; return 0; }