]> git.tdb.fi Git - libs/gl.git/blob - source/keyframe.cpp
Store a Transform in keyframes instead of a Matrix
[libs/gl.git] / source / keyframe.cpp
1 #include <msp/datafile/collection.h>
2 #include "keyframe.h"
3 #include "pose.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 // Avoid synthesizing RefPtr c'tor and d'tor in files including keyframe.h
11 KeyFrame::KeyFrame()
12 { }
13
14 KeyFrame::~KeyFrame()
15 { }
16
17 void KeyFrame::set_transform(const Transform &t)
18 {
19         transform = t;
20 }
21
22 void KeyFrame::set_matrix(const Matrix &m)
23 {
24         transform = Transform::from_matrix(m);
25 }
26
27 void KeyFrame::set_pose(const Pose &p)
28 {
29         pose = &p;
30         pose.keep();
31 }
32
33
34 KeyFrame::AnimatedUniform::AnimatedUniform(unsigned s, float v0, float v1, float v2, float v3):
35         size(s)
36 {
37         values[0] = v0;
38         values[1] = v1;
39         values[2] = v2;
40         values[3] = v3;
41 }
42
43
44 KeyFrame::Loader::Loader(KeyFrame &k):
45         DataFile::CollectionObjectLoader<KeyFrame>(k, 0)
46 {
47         init();
48 }
49
50 KeyFrame::Loader::Loader(KeyFrame &k, Collection &c):
51         DataFile::CollectionObjectLoader<KeyFrame>(k, &c)
52 {
53         init();
54 }
55
56 void KeyFrame::Loader::init()
57 {
58         add("pose", &Loader::pose);
59         add("pose", &Loader::pose_inline);
60         add("transform", &Loader::transform);
61         add("uniforms", &Loader::uniforms);
62
63         // Deprecated; use the transform statement instead
64         add("position", &Loader::position);
65         add("rotation", &Loader::rotation);
66         add("scaling", &Loader::scaling_uniform);
67         add("scaling", &Loader::scaling);
68 }
69
70 void KeyFrame::Loader::pose(const string &n)
71 {
72         obj.pose = &get_collection().get<Pose>(n);
73         obj.pose.keep();
74 }
75
76 void KeyFrame::Loader::pose_inline()
77 {
78         RefPtr<Pose> p = new Pose;
79         load_sub(*p, get_collection());
80         obj.pose = p;
81 }
82
83 void KeyFrame::Loader::position(float x, float y, float z)
84 {
85         obj.transform.set_position(Vector3(x, y, z));
86 }
87
88 void KeyFrame::Loader::rotation(float a, float x, float y, float z)
89 {
90         obj.transform.set_rotation(Transform::Angle::from_degrees(a), Vector3(x, y, z));
91 }
92
93 void KeyFrame::Loader::scaling_uniform(float s)
94 {
95         obj.transform.set_scale(s);
96 }
97
98 void KeyFrame::Loader::scaling(float x, float y, float z)
99 {
100         obj.transform.set_scale(Vector3(x, y, z));
101 }
102
103 void KeyFrame::Loader::transform()
104 {
105         load_sub(obj.transform);
106 }
107
108 void KeyFrame::Loader::uniforms()
109 {
110         UniformsLoader ldr(obj);
111         load_sub_with(ldr);
112 }
113
114
115 KeyFrame::UniformsLoader::UniformsLoader(KeyFrame &k):
116         DataFile::ObjectLoader<KeyFrame>(k)
117 {
118         add("uniform1f", &UniformsLoader::uniform1f);
119         add("uniform2f", &UniformsLoader::uniform2f);
120         add("uniform3f", &UniformsLoader::uniform3f);
121         add("uniform4f", &UniformsLoader::uniform4f);
122 }
123
124 void KeyFrame::UniformsLoader::uniform1f(const string &n, float v)
125 {
126         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(1, v)));
127 }
128
129 void KeyFrame::UniformsLoader::uniform2f(const string &n, float v0, float v1)
130 {
131         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(2, v0, v1)));
132 }
133
134 void KeyFrame::UniformsLoader::uniform3f(const string &n, float v0, float v1, float v2)
135 {
136         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(3, v0, v1, v2)));
137 }
138
139 void KeyFrame::UniformsLoader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
140 {
141         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(4, v0, v1, v2, v3)));
142 }
143
144 } // namespace GL
145 } // namespace Msp