]> git.tdb.fi Git - libs/gl.git/commitdiff
Rework loading in viewer
authorMikko Rasa <tdb@tdb.fi>
Sat, 16 Aug 2014 09:39:16 +0000 (12:39 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 16 Aug 2014 09:42:34 +0000 (12:42 +0300)
It can now load external resources, and adding new types of renderables
in the future is easier.

tools/viewer.cpp

index 1d533b5deae8fee3d44615066367c10012937fb2..2582b887ffcd51256fcfc0b0bd40be9d6e0a6bef 100644 (file)
@@ -1,6 +1,9 @@
 #include <cmath>
 #include <msp/core/application.h>
 #include <msp/core/getopt.h>
+#include <msp/datafile/directorysource.h>
+#include <msp/datafile/packsource.h>
+#include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
 #include <msp/graphics/simplewindow.h>
 #include <msp/gl/animatedobject.h>
@@ -13,6 +16,8 @@
 #include <msp/gl/lighting.h>
 #include <msp/gl/mesh.h>
 #include <msp/gl/object.h>
+#include <msp/gl/resources.h>
+#include <msp/gl/technique.h>
 #include <msp/gl/tests.h>
 #include <msp/input/mouse.h>
 #include <msp/io/print.h>
@@ -25,11 +30,23 @@ using namespace Msp;
 class Viewer: public RegisteredApplication<Viewer>
 {
 private:
+       class Resources: public GL::Resources
+       {
+       private:
+               DataFile::DirectorySource dir_source;
+               DataFile::PackSource pack_source;
+
+       public:
+               Resources();
+
+               void add_directory(const string &);
+               void add_pack(const string &);
+       };
+
        Graphics::SimpleGLWindow window;
        Input::Mouse mouse;
-       GL::Object *object;
-       GL::Mesh *mesh;
-       GL::Animation *animation;
+       Resources resources;
+       GL::Renderable *renderable;
        GL::AnimatedObject *anim_object;
        GL::AnimationPlayer *anim_player;
        GL::Light light;
@@ -45,6 +62,10 @@ private:
 
 public:
        Viewer(int, char **);
+private:
+       template<typename T>
+       T *load(const string &);
+public:
        ~Viewer();
 
        virtual int main();
@@ -63,9 +84,7 @@ private:
 Viewer::Viewer(int argc, char **argv):
        window(1024, 768, false),
        mouse(window),
-       object(0),
-       mesh(0),
-       animation(0),
+       renderable(0),
        anim_object(0),
        anim_player(0),
        yaw(0),
@@ -75,43 +94,72 @@ Viewer::Viewer(int argc, char **argv):
        light_pitch(0),
        dragging(0)
 {
-       if(argc<2)
-               throw usage_error("Filename must be provided");
-
-       for(int i=1; i<argc; ++i)
+       list<string> resource_locations;
+       string animation_name;
+       string renderable_name;
+       GetOpt getopt;
+       getopt.add_option('r', "resources", resource_locations, GetOpt::REQUIRED_ARG);
+       getopt.add_option('a', "animation", animation_name, GetOpt::REQUIRED_ARG);
+       getopt.add_argument("renderable", renderable_name);
+       getopt(argc, argv);
+
+       for(list<string>::const_iterator i=resource_locations.begin(); i!=resource_locations.end(); ++i)
        {
-               string fn = argv[i];
-               string ext = FS::extpart(fn);
-
-               if(ext==".mesh")
+               if(FS::is_dir(*i))
+                       resources.add_directory(*i);
+               else
                {
-                       if(mesh || object)
-                               throw usage_error("Only one mesh or object may be specified");
-
-                       mesh = new GL::Mesh;
-                       DataFile::load(*mesh, fn);
+                       string ext = FS::extpart(*i);
+                       if(ext==".mdp")
+                               resources.add_pack(*i);
+                       else
+                               DataFile::load(resources, *i);
                }
-               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")
+       GL::Object *object = 0;
+
+       string ext = FS::extpart(renderable_name);
+       if(ext==".mesh")
+       {
+               GL::Mesh *mesh = 0;
+               if(FS::exists(renderable_name))
                {
-                       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);
+                       mesh = new GL::Mesh;
+                       DataFile::load(*mesh, renderable_name);
+                       resources.add("__"+renderable_name, mesh);
                }
                else
-                       throw usage_error("Don't know how to view this file");
+                       mesh = &resources.get<GL::Mesh>(renderable_name);
+
+               object = new GL::Object;
+               GL::Technique *tech = new GL::Technique;
+               tech->add_pass(0);
+               object->set_mesh(mesh);
+               object->set_technique(tech);
+               renderable = object;
+
+               resources.add("__.tech", tech);
+               resources.add("__.object", object);
+       }
+       else if(ext==".object")
+       {
+               object = load<GL::Object>(renderable_name);
+               renderable = object;
+       }
+       else
+               throw usage_error("Unknown renderable type");
+
+       if(!animation_name.empty())
+       {
+               if(!object)
+                       throw usage_error("Must have an object to animate");
+
+               GL::Animation *anim = load<GL::Animation>(animation_name);
+               anim_player = new GL::AnimationPlayer;
+               anim_object = new GL::AnimatedObject(*object);
+               anim_player->play(*anim_object, *anim);
+               renderable = anim_object;
        }
 
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Viewer::exit), 0));
@@ -126,10 +174,24 @@ Viewer::Viewer(int argc, char **argv):
        update_camera();
 }
 
+template<typename T>
+T *Viewer::load(const string &name)
+{
+       if(FS::exists(name))
+       {
+               T *thing = new T;
+               DataFile::load(*thing, name, resources);
+               resources.add("__"+name, thing);
+               return thing;
+       }
+       else
+               return &resources.get<T>(name);
+}
+
 Viewer::~Viewer()
 {
-       delete object;
-       delete mesh;
+       delete anim_player;
+       delete anim_object;
 }
 
 int Viewer::main()
@@ -160,12 +222,7 @@ void Viewer::tick()
        GL::Bind bind_lighting(lighting);
        GL::Bind bind_depth(GL::DepthTest::lequal());
        GL::Bind bind_blend(GL::Blend::alpha());
-       if(anim_object)
-               anim_object->render();
-       else if(object)
-               object->render();
-       else if(mesh)
-               mesh->draw();
+       renderable->render();
 
        window.swap_buffers();
 }
@@ -251,3 +308,20 @@ void Viewer::update_light()
        float sp = sin(light_pitch);
        light.set_position(GL::Vector4(-cy*cp, -sy*cp, -sp, 0));
 }
+
+
+Viewer::Resources::Resources()
+{
+       add_source(dir_source);
+       add_source(pack_source);
+}
+
+void Viewer::Resources::add_directory(const string &dir)
+{
+       dir_source.add_directory(dir);
+}
+
+void Viewer::Resources::add_pack(const string &pack)
+{
+       pack_source.add_pack_file(pack);
+}