]> git.tdb.fi Git - libs/gl.git/blobdiff - source/animation/animatedobject.cpp
Rearrange soucre files into subdirectories
[libs/gl.git] / source / animation / animatedobject.cpp
diff --git a/source/animation/animatedobject.cpp b/source/animation/animatedobject.cpp
new file mode 100644 (file)
index 0000000..390b90b
--- /dev/null
@@ -0,0 +1,110 @@
+#include <algorithm>
+#include <msp/strings/format.h>
+#include "animatedobject.h"
+#include "error.h"
+#include "object.h"
+#include "programdata.h"
+#include "renderer.h"
+#include "technique.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+AnimatedObject::AnimatedObject(const Object &o):
+       ObjectInstance(o),
+       shdata(0)
+{
+       if(const Technique *tech = object.get_technique())
+               if(tech->has_shaders())
+                       shdata = new ProgramData;
+}
+
+AnimatedObject::~AnimatedObject()
+{
+       delete shdata;
+}
+
+void AnimatedObject::set_pose_matrix(unsigned link, const Matrix &m)
+{
+       if(shdata)
+       {
+               if(link*16>=pose_data.size())
+                       pose_data.resize((link+1)*16);
+               copy(m.data(), m.data()+16, &pose_data[link*16]);
+               shdata->uniform_matrix4_array("pose", pose_data.size()/16, &pose_data[0]);
+       }
+}
+
+ProgramData &AnimatedObject::get_shader_data()
+{
+       if(!shdata)
+               throw invalid_operation("AnimatedObject::get_shader_data");
+       return *shdata;
+}
+
+const ProgramData &AnimatedObject::get_shader_data() const
+{
+       if(!shdata)
+               throw invalid_operation("AnimatedObject::get_shader_data");
+       return *shdata;
+}
+
+void AnimatedObject::set_uniform(const string &name, const KeyFrame::AnimatedUniform &uni)
+{
+       if(!shdata)
+               throw invalid_operation("AnimatedObject::set_uniform");
+
+       if(uni.size==1)
+               shdata->uniform(name, uni.values[0]);
+       else if(uni.size==2)
+               shdata->uniform2(name, uni.values);
+       else if(uni.size==3)
+               shdata->uniform3(name, uni.values);
+       else if(uni.size==4)
+               shdata->uniform4(name, uni.values);
+       else
+               throw invalid_argument("AnimatedObject::set_uniform");
+}
+
+void AnimatedObject::setup_render(Renderer &renderer, const Tag &) const
+{
+       renderer.transform(matrix);
+       if(shdata)
+               renderer.add_shader_data(*shdata);
+}
+
+
+AnimatedObject::Loader::Loader(AnimatedObject &o):
+       DataFile::DerivedObjectLoader<AnimatedObject, ObjectInstance::Loader>(o)
+{
+       // Deprecated; Use the transform statement defined in ObjectInstance instead
+       add("position", &Loader::position);
+       add("rotation", &Loader::rotation);
+       add("scale", &Loader::scale);
+       add("scale", &Loader::scale_uniform);
+}
+
+void AnimatedObject::Loader::position(float x, float y, float z)
+{
+       obj.matrix.translate(x, y, z);
+}
+
+void AnimatedObject::Loader::rotation(float a, float x, float y, float z)
+{
+       obj.matrix.rotate_deg(a, x, y, z);
+}
+
+void AnimatedObject::Loader::scale(float x, float y, float z)
+{
+       obj.matrix.scale(x, y, z);
+}
+
+void AnimatedObject::Loader::scale_uniform(float s)
+{
+       obj.matrix.scale(s);
+}
+
+} // namespace GL
+} // namespace Msp