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