]> git.tdb.fi Git - libs/gl.git/blob - source/animatedobject.cpp
Derive ProgramCompiler::DeclarationCombiner from BlockModifier
[libs/gl.git] / source / 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 void AnimatedObject::set_uniform(const string &name, const KeyFrame::AnimatedUniform &uni)
41 {
42         if(!shdata)
43                 throw invalid_operation("AnimatedObject::set_uniform");
44
45         if(uni.size==1)
46                 shdata->uniform(name, uni.values[0]);
47         else if(uni.size==2)
48                 shdata->uniform2(name, uni.values);
49         else if(uni.size==3)
50                 shdata->uniform3(name, uni.values);
51         else if(uni.size==4)
52                 shdata->uniform4(name, uni.values);
53         else
54                 throw invalid_argument("AnimatedObject::set_uniform");
55 }
56
57 void AnimatedObject::setup_render(Renderer &renderer, const Tag &) const
58 {
59         renderer.transform(matrix);
60         if(shdata)
61                 renderer.add_shader_data(*shdata);
62 }
63
64
65 AnimatedObject::Loader::Loader(AnimatedObject &o):
66         DataFile::ObjectLoader<AnimatedObject>(o)
67 {
68         add("position", &Loader::position);
69         add("rotation", &Loader::rotation);
70         add("scale", &Loader::scale);
71         add("scale", &Loader::scale_uniform);
72 }
73
74 void AnimatedObject::Loader::position(float x, float y, float z)
75 {
76         obj.matrix.translate(x, y, z);
77 }
78
79 void AnimatedObject::Loader::rotation(float a, float x, float y, float z)
80 {
81         obj.matrix.rotate_deg(a, x, y, z);
82 }
83
84 void AnimatedObject::Loader::scale(float x, float y, float z)
85 {
86         obj.matrix.scale(x, y, z);
87 }
88
89 void AnimatedObject::Loader::scale_uniform(float s)
90 {
91         obj.matrix.scale(s);
92 }
93
94 } // namespace GL
95 } // namespace Msp