]> git.tdb.fi Git - libs/gl.git/blob - source/keyframe.cpp
Merge branch 'animation-rework'
[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_uniform(const string &n, const AnimatedUniform &u)
28 {
29         uniforms.erase(n);
30         uniforms.insert(UniformMap::value_type(n, u));
31 }
32
33 void KeyFrame::set_pose(const Pose &p)
34 {
35         pose = &p;
36         pose.keep();
37 }
38
39
40 KeyFrame::AnimatedUniform::AnimatedUniform(unsigned s, float v0, float v1, float v2, float v3):
41         size(s)
42 {
43         values[0] = v0;
44         values[1] = v1;
45         values[2] = v2;
46         values[3] = v3;
47 }
48
49
50 KeyFrame::Loader::Loader(KeyFrame &k):
51         DataFile::CollectionObjectLoader<KeyFrame>(k, 0)
52 {
53         init();
54 }
55
56 KeyFrame::Loader::Loader(KeyFrame &k, Collection &c):
57         DataFile::CollectionObjectLoader<KeyFrame>(k, &c)
58 {
59         init();
60 }
61
62 void KeyFrame::Loader::init()
63 {
64         add("pose", &Loader::pose);
65         add("pose", &Loader::pose_inline);
66         add("transform", &Loader::transform);
67         add("uniforms", &Loader::uniforms);
68
69         // Deprecated; use the transform statement instead
70         add("position", &Loader::position);
71         add("rotation", &Loader::rotation);
72         add("scaling", &Loader::scaling_uniform);
73         add("scaling", &Loader::scaling);
74 }
75
76 void KeyFrame::Loader::pose(const string &n)
77 {
78         obj.pose = &get_collection().get<Pose>(n);
79         obj.pose.keep();
80 }
81
82 void KeyFrame::Loader::pose_inline()
83 {
84         RefPtr<Pose> p = new Pose;
85         load_sub(*p, get_collection());
86         obj.pose = p;
87 }
88
89 void KeyFrame::Loader::position(float x, float y, float z)
90 {
91         obj.transform.set_position(Vector3(x, y, z));
92 }
93
94 void KeyFrame::Loader::rotation(float a, float x, float y, float z)
95 {
96         obj.transform.set_rotation(Transform::Angle::from_degrees(a), Vector3(x, y, z));
97 }
98
99 void KeyFrame::Loader::scaling_uniform(float s)
100 {
101         obj.transform.set_scale(s);
102 }
103
104 void KeyFrame::Loader::scaling(float x, float y, float z)
105 {
106         obj.transform.set_scale(Vector3(x, y, z));
107 }
108
109 void KeyFrame::Loader::transform()
110 {
111         load_sub(obj.transform);
112 }
113
114 void KeyFrame::Loader::uniforms()
115 {
116         UniformsLoader ldr(obj);
117         load_sub_with(ldr);
118 }
119
120
121 KeyFrame::UniformsLoader::UniformsLoader(KeyFrame &k):
122         DataFile::ObjectLoader<KeyFrame>(k)
123 {
124         add("uniform1f", &UniformsLoader::uniform1f);
125         add("uniform2f", &UniformsLoader::uniform2f);
126         add("uniform3f", &UniformsLoader::uniform3f);
127         add("uniform4f", &UniformsLoader::uniform4f);
128 }
129
130 void KeyFrame::UniformsLoader::uniform1f(const string &n, float v)
131 {
132         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(1, v)));
133 }
134
135 void KeyFrame::UniformsLoader::uniform2f(const string &n, float v0, float v1)
136 {
137         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(2, v0, v1)));
138 }
139
140 void KeyFrame::UniformsLoader::uniform3f(const string &n, float v0, float v1, float v2)
141 {
142         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(3, v0, v1, v2)));
143 }
144
145 void KeyFrame::UniformsLoader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
146 {
147         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(4, v0, v1, v2, v3)));
148 }
149
150 } // namespace GL
151 } // namespace Msp