]> git.tdb.fi Git - libs/gl.git/blob - source/animation/animatedobject.cpp
Move parenthesizing expressions to Formatter
[libs/gl.git] / source / animation / animatedobject.cpp
1 #include <algorithm>
2 #include <msp/strings/format.h>
3 #include "animatedobject.h"
4 #include "error.h"
5 #include "object.h"
6 #include "programdata.h"
7 #include "renderer.h"
8 #include "technique.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 AnimatedObject::AnimatedObject(const Object &o):
16         ObjectInstance(o),
17         shdata(0)
18 {
19         if(const Technique *tech = object.get_technique())
20                 if(tech->has_shaders())
21                         shdata = new ProgramData;
22 }
23
24 AnimatedObject::~AnimatedObject()
25 {
26         delete shdata;
27 }
28
29 void AnimatedObject::set_pose_matrix(unsigned link, const Matrix &m)
30 {
31         if(shdata)
32         {
33                 if(link*16>=pose_data.size())
34                         pose_data.resize((link+1)*16);
35                 copy(m.data(), m.data()+16, &pose_data[link*16]);
36                 shdata->uniform_matrix4_array("pose", pose_data.size()/16, &pose_data[0]);
37         }
38 }
39
40 ProgramData &AnimatedObject::get_shader_data()
41 {
42         if(!shdata)
43                 throw invalid_operation("AnimatedObject::get_shader_data");
44         return *shdata;
45 }
46
47 const ProgramData &AnimatedObject::get_shader_data() const
48 {
49         if(!shdata)
50                 throw invalid_operation("AnimatedObject::get_shader_data");
51         return *shdata;
52 }
53
54 void AnimatedObject::set_uniform(const string &name, const KeyFrame::AnimatedUniform &uni)
55 {
56         if(!shdata)
57                 throw invalid_operation("AnimatedObject::set_uniform");
58
59         if(uni.size==1)
60                 shdata->uniform(name, uni.values[0]);
61         else if(uni.size==2)
62                 shdata->uniform2(name, uni.values);
63         else if(uni.size==3)
64                 shdata->uniform3(name, uni.values);
65         else if(uni.size==4)
66                 shdata->uniform4(name, uni.values);
67         else
68                 throw invalid_argument("AnimatedObject::set_uniform");
69 }
70
71 void AnimatedObject::setup_render(Renderer &renderer, const Tag &) const
72 {
73         renderer.transform(matrix);
74         if(shdata)
75                 renderer.add_shader_data(*shdata);
76 }
77
78
79 AnimatedObject::Loader::Loader(AnimatedObject &o):
80         DataFile::DerivedObjectLoader<AnimatedObject, ObjectInstance::Loader>(o)
81 {
82         // Deprecated; Use the transform statement defined in ObjectInstance instead
83         add("position", &Loader::position);
84         add("rotation", &Loader::rotation);
85         add("scale", &Loader::scale);
86         add("scale", &Loader::scale_uniform);
87 }
88
89 void AnimatedObject::Loader::position(float x, float y, float z)
90 {
91         obj.matrix.translate(x, y, z);
92 }
93
94 void AnimatedObject::Loader::rotation(float a, float x, float y, float z)
95 {
96         obj.matrix.rotate_deg(a, x, y, z);
97 }
98
99 void AnimatedObject::Loader::scale(float x, float y, float z)
100 {
101         obj.matrix.scale(x, y, z);
102 }
103
104 void AnimatedObject::Loader::scale_uniform(float s)
105 {
106         obj.matrix.scale(s);
107 }
108
109 } // namespace GL
110 } // namespace Msp