1 #ifndef MOTIONSPHERE_HXX
 2 #define MOTIONSPHERE_HXX
 3 
 4 #include "Primitive.hxx"
 5 
 6 /**
 7  * MotionSphere
 8  * A sphere moving in the range from 'center_from' to 'center_to'
 9  */
10 class MotionSphere : public Primitive
11 {
12     private:
13         Vec3f center;
14         Vec3f center_from;
15         Vec3f center_to;
16         float radius;
17 
18         /**
19          * compute bounding box of the sphere with radius 'radius' having its midpoint at 'center_from'
20          * and equivalent for the sphere located at 'center_to' (the maximum translation)
21          */ 
22         Box CalcBounds()
23         {
24             Box b = Box();
25             b.Extend(center_from + Vec3f(-radius));
26             b.Extend(center_from + Vec3f( radius));
27             b.Extend(center_to + Vec3f(-radius));
28             b.Extend(center_to + Vec3f( radius));
29             return b;
30         }
31 
32     public:
33         /**
34          * @parameter translation: in which direction does the sphere moves
35          */     
36         MotionSphere(Vec3f center_from, Vec3f translation, float radius) 
37         : center_from(center_from), radius(radius)
38         {
39             center_to = center_from + translation;
40         };
41         
42         virtual ~MotionSphere() {}
43         
44         /**
45          * Compute ray-triangle intersection
46          */     
47         virtual bool Intersect(Ray &ray)
48         {
49             /** 
50             * compute center: randomly choosen between start center and end center
51             */
52             center = center_from + drand48() * (center_from - center_to);
53 
54             Primitive* sphere = new Sphere( Vec3f(center), radius );
55                 sphere->setShader(this->getShader());
56                 bool hit = sphere->Intersect(ray);
57             delete sphere;
58             
59             if (hit)
60                 ray.hit = this;
61 
62             return hit;
63         };
64         
65         /** 
66          * get motion sphere´s normal
67          */
68         virtual Vec3f GetNormal(Ray &ray)
69         {
70             Vec3f hit = ray.org + ray.t * ray.dir;
71             Vec3f normal = hit - center;
72             Normalize(normal);
73             return normal;
74         };
75         
76         /** 
77          * get the UV value of a motion sphere
78          */
79         virtual Vec2f GetUV(Ray &ray) 
80         {
81             Primitive* sphere = new Sphere( Vec3f(center), radius );
82                 Vec2f UV = sphere->GetUV(ray);
83             delete sphere;
84             
85             return UV;
86         };
87 };
88 
89 #endif


syntax highlighted by Code2HTML, v. 0.9.1