eqemu

view 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 source
1 #include <math.h>
2 #include "bvol.h"
4 BSphere::BSphere()
5 {
6 radius = 1.0f;
7 }
9 BSphere::BSphere(const Vector3 &c, float rad)
10 : center(c)
11 {
12 radius = rad;
13 }
15 void BSphere::set_center(const Vector3 &center)
16 {
17 this->center = center;
18 }
20 const Vector3 &BSphere::get_center() const
21 {
22 return center;
23 }
25 void BSphere::set_radius(float rad)
26 {
27 radius = rad;
28 }
30 float BSphere::get_radius() const
31 {
32 return radius;
33 }
35 bool BSphere::intersect(const Ray &ray, HitPoint *hit) const
36 {
37 float a = dot(ray.dir, ray.dir);
38 float b = 2.0 * dot(ray.dir, ray.origin - center);
39 float c = dot(ray.origin, ray.origin) + dot(center, center) -
40 2.0 * dot(ray.origin, center) - radius * radius;
42 float disc = b * b - 4.0f * a * c;
43 if(disc < 1e-6) {
44 return false;
45 }
47 float sqrt_disc = sqrt(disc);
48 float x1 = (-b + sqrt_disc) / (2.0f * a);
49 float x2 = (-b - sqrt_disc) / (2.0f * a);
51 if(x1 < 1e-6) x1 = x2;
52 if(x2 < 1e-6) x2 = x1;
54 float t = x1 < x2 ? x1 : x2;
55 if(t < 1e-6) {
56 return false;
57 }
59 hit->t = t;
60 hit->pos = ray.origin + ray.dir * t;
61 return true;
62 }