]> git.tdb.fi Git - libs/gl.git/commitdiff
Add support for viewing animations
authorMikko Rasa <tdb@tdb.fi>
Sun, 5 Aug 2012 12:53:41 +0000 (15:53 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 5 Aug 2012 12:53:41 +0000 (15:53 +0300)
tools/viewer.cpp

index bc024e1f642eab1fb1814895aeb0cf3ee6654f7a..db2b99e1869d22c6a5bb19b642c4697e5c494e66 100644 (file)
@@ -3,6 +3,9 @@
 #include <msp/core/getopt.h>
 #include <msp/fs/utils.h>
 #include <msp/graphics/simplewindow.h>
+#include <msp/gl/animatedobject.h>
+#include <msp/gl/animation.h>
+#include <msp/gl/animationplayer.h>
 #include <msp/gl/blend.h>
 #include <msp/gl/camera.h>
 #include <msp/gl/framebuffer.h>
@@ -13,6 +16,8 @@
 #include <msp/gl/tests.h>
 #include <msp/input/mouse.h>
 #include <msp/io/print.h>
+#include <msp/time/timestamp.h>
+#include <msp/time/utils.h>
 
 using namespace std;
 using namespace Msp;
@@ -24,6 +29,9 @@ private:
        Input::Mouse mouse;
        GL::Object *object;
        GL::Mesh *mesh;
+       GL::Animation *animation;
+       GL::AnimatedObject *anim_object;
+       GL::AnimationPlayer *anim_player;
        GL::Light light;
        GL::Lighting lighting;
        GL::Camera camera;
@@ -33,6 +41,7 @@ private:
        float light_yaw;
        float light_pitch;
        unsigned dragging;
+       Time::TimeStamp last_tick;
 
 public:
        Viewer(int, char **);
@@ -56,6 +65,9 @@ Viewer::Viewer(int argc, char **argv):
        mouse(window),
        object(0),
        mesh(0),
+       animation(0),
+       anim_object(0),
+       anim_player(0),
        yaw(0),
        pitch(0),
        distance(10),
@@ -66,21 +78,41 @@ Viewer::Viewer(int argc, char **argv):
        if(argc<2)
                throw usage_error("Filename must be provided");
 
-       string fn = argv[1];
-       string ext = FS::extpart(fn);
-
-       if(ext==".mesh")
-       {
-               mesh = new GL::Mesh;
-               DataFile::load(*mesh, fn);
-       }
-       else if(ext==".object")
+       for(int i=1; i<argc; ++i)
        {
-               object = new GL::Object;
-               DataFile::load(*object, fn);
+               string fn = argv[i];
+               string ext = FS::extpart(fn);
+
+               if(ext==".mesh")
+               {
+                       if(mesh || object)
+                               throw usage_error("Only one mesh or object may be specified");
+
+                       mesh = new GL::Mesh;
+                       DataFile::load(*mesh, fn);
+               }
+               else if(ext==".object")
+               {
+                       if(mesh || object)
+                               throw usage_error("Only one mesh or object may be specified");
+
+                       object = new GL::Object;
+                       DataFile::load(*object, fn);
+               }
+               else if(ext==".anim")
+               {
+                       if(!object)
+                               throw usage_error("An object must be provided for animation");
+
+                       animation = new GL::Animation;
+                       DataFile::load(*animation, argv[2]);
+                       anim_object = new GL::AnimatedObject(*object);
+                       anim_player = new GL::AnimationPlayer;
+                       anim_player->play(*anim_object, *animation);
+               }
+               else
+                       throw usage_error("Don't know how to view this file");
        }
-       else
-               throw usage_error("Don't know how to view this file");
 
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Viewer::exit), 0));
        mouse.signal_button_press.connect(sigc::mem_fun(this, &Viewer::button_press));
@@ -108,6 +140,17 @@ int Viewer::main()
 
 void Viewer::tick()
 {
+       if(anim_player)
+       {
+               Time::TimeStamp t = Time::now();
+               Time::TimeDelta dt;
+               if(last_tick)
+                       dt = t-last_tick;
+               last_tick = t;
+
+               anim_player->tick(dt);
+       }
+
        window.tick();
 
        GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
@@ -117,7 +160,9 @@ void Viewer::tick()
        GL::Bind bind_lighting(lighting);
        GL::Bind bind_depth(GL::DepthTest::lequal());
        GL::Bind bind_blend(GL::Blend::alpha());
-       if(object)
+       if(anim_object)
+               anim_object->render();
+       else if(object)
                object->render();
        else if(mesh)
                mesh->draw();