eqemu

annotate src/bvol.cc @ 10:819c7ebb1bec

added libimago to avoid the external dependency
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 18 Jul 2014 05:07:40 +0300
parents
children 2656099aff12
rev   line source
nuclear@4 1 #include <math.h>
nuclear@4 2 #include "bvol.h"
nuclear@4 3
nuclear@4 4 BSphere::BSphere()
nuclear@4 5 {
nuclear@4 6 radius = 1.0f;
nuclear@4 7 }
nuclear@4 8
nuclear@4 9 BSphere::BSphere(const Vector3 &c, float rad)
nuclear@4 10 : center(c)
nuclear@4 11 {
nuclear@4 12 radius = rad;
nuclear@4 13 }
nuclear@4 14
nuclear@4 15 void BSphere::set_center(const Vector3 &center)
nuclear@4 16 {
nuclear@4 17 this->center = center;
nuclear@4 18 }
nuclear@4 19
nuclear@4 20 const Vector3 &BSphere::get_center() const
nuclear@4 21 {
nuclear@4 22 return center;
nuclear@4 23 }
nuclear@4 24
nuclear@4 25 void BSphere::set_radius(float rad)
nuclear@4 26 {
nuclear@4 27 radius = rad;
nuclear@4 28 }
nuclear@4 29
nuclear@4 30 float BSphere::get_radius() const
nuclear@4 31 {
nuclear@4 32 return radius;
nuclear@4 33 }
nuclear@4 34
nuclear@4 35 bool BSphere::intersect(const Ray &ray, HitPoint *hit) const
nuclear@4 36 {
nuclear@4 37 float a = dot(ray.dir, ray.dir);
nuclear@4 38 float b = 2.0 * dot(ray.dir, ray.origin - center);
nuclear@4 39 float c = dot(ray.origin, ray.origin) + dot(center, center) -
nuclear@4 40 2.0 * dot(ray.origin, center) - radius * radius;
nuclear@4 41
nuclear@4 42 float disc = b * b - 4.0f * a * c;
nuclear@4 43 if(disc < 1e-6) {
nuclear@4 44 return false;
nuclear@4 45 }
nuclear@4 46
nuclear@4 47 float sqrt_disc = sqrt(disc);
nuclear@4 48 float x1 = (-b + sqrt_disc) / (2.0f * a);
nuclear@4 49 float x2 = (-b - sqrt_disc) / (2.0f * a);
nuclear@4 50
nuclear@4 51 if(x1 < 1e-6) x1 = x2;
nuclear@4 52 if(x2 < 1e-6) x2 = x1;
nuclear@4 53
nuclear@4 54 float t = x1 < x2 ? x1 : x2;
nuclear@4 55 if(t < 1e-6) {
nuclear@4 56 return false;
nuclear@4 57 }
nuclear@4 58
nuclear@4 59 hit->t = t;
nuclear@4 60 hit->pos = ray.origin + ray.dir * t;
nuclear@4 61 return true;
nuclear@4 62 }