added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / gfx / animation.hpp
1 /*
2 This file is part of the graphics core library.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /* fundamental animation system (initally part of 3dgeom.hpp)
22  *
23  * Author: John Tsiombikas 2004
24  * Modified:
25  *              John Tsiombikas 2005
26  */
27
28 #ifndef _ANIMATION_HPP_
29 #define _ANIMATION_HPP_
30
31 #include "3dengfx_config.h"
32
33 #include <iostream>
34 #include <vector>
35 #include "n3dmath2/n3dmath2.hpp"
36 #include "controller.hpp"
37 #include "timeline.hpp"
38
39 class PRS {
40 public:
41         Vector3 position;
42         Quaternion rotation;
43         Vector3 scale;
44         Vector3 pivot;
45         
46         PRS();
47         PRS(const Vector3 &pos, const Quaternion &rot, const Vector3 &scale = Vector3(1,1,1), const Vector3 &pivot = Vector3(0,0,0));
48         
49         Matrix4x4 get_xform_matrix() const;
50
51         friend PRS combine_prs(const PRS &prs1, const PRS &prs2);
52         friend std::ostream &operator <<(std::ostream &out, const PRS &prs);
53 };
54
55 PRS combine_prs(const PRS &prs1, const PRS &prs2);
56 PRS inherit_prs(const PRS &child, const PRS &parent);
57 std::ostream &operator <<(std::ostream &out, const PRS &prs);
58
59 class Keyframe {
60 public:
61         PRS prs;
62         unsigned long time;
63         
64         Keyframe(const PRS &prs, unsigned long time);
65         
66         inline bool operator ==(const Keyframe &key) const;
67         inline bool operator <(const Keyframe &key) const;
68 };
69
70
71 enum ControllerType {CTRL_TRANSLATION, CTRL_ROTATION, CTRL_SCALING};
72 #define XFORM_LOCAL_PRS         0xffffffff
73
74 //////////// Transformable Node Base class /////////////
75 class XFormNode {
76 protected:
77         PRS local_prs;
78
79         // PRS cache
80         mutable struct {
81                 PRS prs;
82                 unsigned long time;
83                 bool valid;
84         } cache;
85
86         int key_count;
87         std::vector<Keyframe> keys;
88         std::vector<MotionController> trans_ctrl, rot_ctrl, scale_ctrl;
89
90         TimelineMode key_time_mode;
91         
92         bool use_ctrl;
93
94         inline Keyframe *get_nearest_key(unsigned long time);
95         inline const Keyframe *get_nearest_key(unsigned long time) const;
96         Keyframe *get_nearest_key(int start, int end, unsigned long time);
97         inline const Keyframe *get_nearest_key(int start, int end, unsigned long time) const;
98         void get_key_interval(unsigned long time, const Keyframe **start, const Keyframe **end) const;
99         
100 public:
101         std::string name;
102         XFormNode *parent;
103         std::vector<XFormNode*> children;
104         
105         XFormNode();
106         virtual ~XFormNode();
107
108         virtual void add_controller(MotionController ctrl, ControllerType ctrl_type);
109         virtual std::vector<MotionController> *get_controllers(ControllerType ctrl_type);
110                 
111         virtual void add_keyframe(const Keyframe &key);
112         virtual Keyframe *get_keyframe(unsigned long time);
113         virtual void delete_keyframe(unsigned long time);
114         virtual std::vector<Keyframe> *get_keyframes();
115
116         virtual void set_timeline_mode(TimelineMode time_mode);
117         
118         virtual void set_position(const Vector3 &pos, unsigned long time = XFORM_LOCAL_PRS);
119         virtual void set_rotation(const Quaternion &rot, unsigned long time = XFORM_LOCAL_PRS);
120         virtual void set_rotation(const Vector3 &euler, unsigned long time = XFORM_LOCAL_PRS);
121         virtual void set_scaling(const Vector3 &scale, unsigned long time = XFORM_LOCAL_PRS);
122         virtual void set_pivot(const Vector3 &pivot);
123         
124         virtual Vector3 get_position(unsigned long time = XFORM_LOCAL_PRS) const;
125         virtual Quaternion get_rotation(unsigned long time = XFORM_LOCAL_PRS) const;
126         virtual Vector3 get_scaling(unsigned long time = XFORM_LOCAL_PRS) const;
127         virtual Vector3 get_pivot() const;
128         
129         virtual void translate(const Vector3 &trans, unsigned long time = XFORM_LOCAL_PRS);
130         virtual void rotate(const Quaternion &rot, unsigned long time = XFORM_LOCAL_PRS);
131         virtual void rotate(const Vector3 &euler, unsigned long time = XFORM_LOCAL_PRS);
132         virtual void rotate(const Matrix3x3 &rmat, unsigned long time = XFORM_LOCAL_PRS);
133         virtual void scale(const Vector3 &scale, unsigned long time = XFORM_LOCAL_PRS); 
134
135         virtual void reset_position(unsigned long time = XFORM_LOCAL_PRS);
136         virtual void reset_rotation(unsigned long time = XFORM_LOCAL_PRS);
137         virtual void reset_scaling(unsigned long time = XFORM_LOCAL_PRS);
138         virtual void reset_xform(unsigned long time = XFORM_LOCAL_PRS);
139         
140         virtual PRS get_prs(unsigned long time = XFORM_LOCAL_PRS) const;
141 };
142
143 #include "animation.inl"
144
145
146 #endif  // _ANIMATION_HPP_