eqemu
diff src/bvol.cc @ 4:3d3656360a82
rendering properly, added picking, almost done...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 17 Jul 2014 08:51:17 +0300 |
parents | |
children | 2656099aff12 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/bvol.cc Thu Jul 17 08:51:17 2014 +0300 1.3 @@ -0,0 +1,62 @@ 1.4 +#include <math.h> 1.5 +#include "bvol.h" 1.6 + 1.7 +BSphere::BSphere() 1.8 +{ 1.9 + radius = 1.0f; 1.10 +} 1.11 + 1.12 +BSphere::BSphere(const Vector3 &c, float rad) 1.13 + : center(c) 1.14 +{ 1.15 + radius = rad; 1.16 +} 1.17 + 1.18 +void BSphere::set_center(const Vector3 ¢er) 1.19 +{ 1.20 + this->center = center; 1.21 +} 1.22 + 1.23 +const Vector3 &BSphere::get_center() const 1.24 +{ 1.25 + return center; 1.26 +} 1.27 + 1.28 +void BSphere::set_radius(float rad) 1.29 +{ 1.30 + radius = rad; 1.31 +} 1.32 + 1.33 +float BSphere::get_radius() const 1.34 +{ 1.35 + return radius; 1.36 +} 1.37 + 1.38 +bool BSphere::intersect(const Ray &ray, HitPoint *hit) const 1.39 +{ 1.40 + float a = dot(ray.dir, ray.dir); 1.41 + float b = 2.0 * dot(ray.dir, ray.origin - center); 1.42 + float c = dot(ray.origin, ray.origin) + dot(center, center) - 1.43 + 2.0 * dot(ray.origin, center) - radius * radius; 1.44 + 1.45 + float disc = b * b - 4.0f * a * c; 1.46 + if(disc < 1e-6) { 1.47 + return false; 1.48 + } 1.49 + 1.50 + float sqrt_disc = sqrt(disc); 1.51 + float x1 = (-b + sqrt_disc) / (2.0f * a); 1.52 + float x2 = (-b - sqrt_disc) / (2.0f * a); 1.53 + 1.54 + if(x1 < 1e-6) x1 = x2; 1.55 + if(x2 < 1e-6) x2 = x1; 1.56 + 1.57 + float t = x1 < x2 ? x1 : x2; 1.58 + if(t < 1e-6) { 1.59 + return false; 1.60 + } 1.61 + 1.62 + hit->t = t; 1.63 + hit->pos = ray.origin + ray.dir * t; 1.64 + return true; 1.65 +}