]> git.tdb.fi Git - libs/gl.git/blob - source/animatedobject.cpp
Miscellaneous fixes
[libs/gl.git] / source / animatedobject.cpp
1 #include <algorithm>
2 #include <msp/strings/format.h>
3 #include "animatedobject.h"
4 #include "object.h"
5 #include "programdata.h"
6 #include "renderer.h"
7 #include "technique.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 AnimatedObject::AnimatedObject(const Object &o):
15         ObjectInstance(o),
16         shdata(0)
17 {
18         if(const Technique *tech = object.get_technique())
19                 if(tech->has_shaders())
20                         shdata = new ProgramData;
21 }
22
23 AnimatedObject::~AnimatedObject()
24 {
25         delete shdata;
26 }
27
28 void AnimatedObject::set_matrix(const Matrix &m)
29 {
30         matrix = m;
31 }
32
33 void AnimatedObject::set_pose_matrix(unsigned link, const Matrix &m)
34 {
35         if(shdata)
36         {
37                 if(link*16>=pose_data.size())
38                         pose_data.resize((link+1)*16);
39                 copy(m.data(), m.data()+16, &pose_data[link*16]);
40                 shdata->uniform_matrix4_array("pose", pose_data.size()/16, &pose_data[0]);
41         }
42 }
43
44 void AnimatedObject::setup_render(Renderer &renderer, const Tag &) const
45 {
46         renderer.transform(matrix);
47         if(shdata)
48                 renderer.add_shader_data(*shdata);
49 }
50
51
52 AnimatedObject::Loader::Loader(AnimatedObject &o):
53         DataFile::ObjectLoader<AnimatedObject>(o)
54 {
55         add("position", &Loader::position);
56         add("rotation", &Loader::rotation);
57         add("scale", &Loader::scale);
58         add("scale", &Loader::scale_uniform);
59 }
60
61 void AnimatedObject::Loader::position(float x, float y, float z)
62 {
63         obj.matrix.translate(x, y, z);
64 }
65
66 void AnimatedObject::Loader::rotation(float a, float x, float y, float z)
67 {
68         obj.matrix.rotate_deg(a, x, y, z);
69 }
70
71 void AnimatedObject::Loader::scale(float x, float y, float z)
72 {
73         obj.matrix.scale(x, y, z);
74 }
75
76 void AnimatedObject::Loader::scale_uniform(float s)
77 {
78         obj.matrix.scale(s);
79 }
80
81 } // namespace GL
82 } // namespace Msp