Comment and formatting tweaks

This commit is contained in:
Eryn Wells 2013-09-08 18:28:53 -07:00
parent 45b71f18db
commit 6e3f92e47c
2 changed files with 9 additions and 9 deletions

View file

@ -57,7 +57,7 @@ vector_mul_scalar(Vector3 v, float f)
/* /*
* vector_sub_vector -- * vector_sub_vector --
* *
* Subtract s from m. Return a new vector. * m - s. Return a new vector.
*/ */
Vector3 Vector3
vector_sub_vector(Vector3 m, Vector3 s) vector_sub_vector(Vector3 m, Vector3 s)

View file

@ -173,17 +173,17 @@ object_sphere_set_radius(Object *obj, float r)
int int
sphere_does_intersect(Object *obj, Ray ray, float **t) sphere_does_intersect(Object *obj, Ray ray, float **t)
{ {
// Distance from vector point to center of sphere. // Location of the vector in object space.
Vector3 dist = vector_sub_vector(ray.location, object_get_location(obj)); Vector3 ray_loc_obj = vector_sub_vector(ray.location, object_get_location(obj));
float r = object_sphere_get_radius(obj); float r = object_sphere_get_radius(obj);
// Coefficients for quadratic equation. // Coefficients for quadratic equation.
float a = vector_dot(ray.direction, ray.direction); float a = vector_dot(ray.direction, ray.direction);
float b = vector_dot(vector_mul_scalar(dist, 2), ray.direction); float b = vector_dot(ray.direction, ray_loc_obj) * 2.0;
float c = vector_dot(dist, dist) - (r * r); float c = vector_dot(ray_loc_obj, ray_loc_obj) - (r * r);
// Discriminant for quadratic equation. // Discriminant for the quadratic equation.
float discrim = b * b - 4.0 * a * c; float discrim = (b * b) - (4.0 * a * c);
// If the discriminant is less than zero, there are no real (as in not imaginary) solutions to this intersection. // If the discriminant is less than zero, there are no real (as in not imaginary) solutions to this intersection.
if (discrim < 0) { if (discrim < 0) {
@ -192,7 +192,7 @@ sphere_does_intersect(Object *obj, Ray ray, float **t)
/* /*
* Compute most of the quadratic equation as q. Doing this first helps avoid precision errors when * Compute most of the quadratic equation as q. Doing this first helps avoid precision errors when
* b =~ * sqrt(b^2 - 4ac). * b =~ sqrt(b^2 - 4ac).
* *
* See: http://wiki.cgsociety.org/index.php/Ray_Sphere_Intersection * See: http://wiki.cgsociety.org/index.php/Ray_Sphere_Intersection
*/ */
@ -205,7 +205,7 @@ sphere_does_intersect(Object *obj, Ray ray, float **t)
q = (-b + sqrt_discrim) / 2.0; q = (-b + sqrt_discrim) / 2.0;
} }
// Compute the intersections. Spheres have at most two intersections. // Compute the intersections, the roots of the quadratic equation. Spheres have at most two intersections.
float t0 = q / a; float t0 = q / a;
float t1 = c / q; float t1 = c / q;