]> git.tdb.fi Git - libs/gl.git/commitdiff
Update viewer for compatibility with library changes
authorMikko Rasa <tdb@tdb.fi>
Tue, 6 Sep 2011 20:58:13 +0000 (23:58 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 6 Sep 2011 20:58:13 +0000 (23:58 +0300)
viewer.cpp

index e6826bf052d41ae9744ac20083e47378263479de..bc024e1f642eab1fb1814895aeb0cf3ee6654f7a 100644 (file)
@@ -1,5 +1,6 @@
 #include <cmath>
 #include <msp/core/application.h>
+#include <msp/core/getopt.h>
 #include <msp/fs/utils.h>
 #include <msp/graphics/simplewindow.h>
 #include <msp/gl/blend.h>
 #include <msp/gl/mesh.h>
 #include <msp/gl/object.h>
 #include <msp/gl/tests.h>
+#include <msp/input/mouse.h>
 #include <msp/io/print.h>
 
 using namespace std;
 using namespace Msp;
 
-class Viewer: public Application
+class Viewer: public RegisteredApplication<Viewer>
 {
 private:
        Graphics::SimpleGLWindow window;
+       Input::Mouse mouse;
        GL::Object *object;
        GL::Mesh *mesh;
        GL::Light light;
@@ -30,10 +33,6 @@ private:
        float light_yaw;
        float light_pitch;
        unsigned dragging;
-       int pointer_x;
-       int pointer_y;
-
-       static Application::RegApp<Viewer> reg;
 
 public:
        Viewer(int, char **);
@@ -43,23 +42,18 @@ public:
 private:
        virtual void tick();
 
-       void button_press(int, int, unsigned, unsigned);
-       void button_release(int, int, unsigned, unsigned);
-       void pointer_motion(int, int);
-       void key_press(unsigned, unsigned, wchar_t);
+       void button_press(unsigned);
+       void button_release(unsigned);
+       void axis_motion(unsigned, float, float);
 
        void update_camera();
        void update_light();
-
-public:
-       static void usage(const char *, const char *, bool);
 };
 
 
-Application::RegApp<Viewer> Viewer::reg;
-
 Viewer::Viewer(int argc, char **argv):
        window(1024, 768, false),
+       mouse(window),
        object(0),
        mesh(0),
        yaw(0),
@@ -67,12 +61,10 @@ Viewer::Viewer(int argc, char **argv):
        distance(10),
        light_yaw(0),
        light_pitch(0),
-       dragging(0),
-       pointer_x(0),
-       pointer_y(0)
+       dragging(0)
 {
        if(argc<2)
-               throw UsageError("Filename must be provided");
+               throw usage_error("Filename must be provided");
 
        string fn = argv[1];
        string ext = FS::extpart(fn);
@@ -88,13 +80,12 @@ Viewer::Viewer(int argc, char **argv):
                DataFile::load(*object, fn);
        }
        else
-               throw UsageError("Don't know how to view this file");
+               throw usage_error("Don't know how to view this file");
 
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Viewer::exit), 0));
-       window.signal_button_press.connect(sigc::mem_fun(this, &Viewer::button_press));
-       window.signal_button_release.connect(sigc::mem_fun(this, &Viewer::button_release));
-       window.signal_pointer_motion.connect(sigc::mem_fun(this, &Viewer::pointer_motion));
-       window.signal_key_press.connect(sigc::mem_fun(this, &Viewer::key_press));
+       mouse.signal_button_press.connect(sigc::mem_fun(this, &Viewer::button_press));
+       mouse.signal_button_release.connect(sigc::mem_fun(this, &Viewer::button_release));
+       mouse.signal_axis_motion.connect(sigc::mem_fun(this, &Viewer::axis_motion));
 
        light.set_position(GL::Vector4(0, 0, 1, 0));
        lighting.attach(0, light);
@@ -134,25 +125,16 @@ void Viewer::tick()
        window.swap_buffers();
 }
 
-void Viewer::usage(const char *err, const char *argv0, bool)
-{
-       if(err)
-               IO::print("%s\n", err);
-       IO::print("Usage: %s <filename>\n", argv0);
-}
-
-void Viewer::button_press(int x, int y, unsigned btn, unsigned)
+void Viewer::button_press(unsigned btn)
 {
        if(btn==1)
        {
                dragging = 1;
-               pointer_x = x;
-               pointer_y = y;
        }
        else if(btn==3)
        {
                dragging = 3;
-               pointer_motion(x, y);
+               axis_motion(0, 0, 0);
        }
        else if(btn==4)
        {
@@ -166,52 +148,45 @@ void Viewer::button_press(int x, int y, unsigned btn, unsigned)
        }
 }
 
-void Viewer::button_release(int, int, unsigned btn, unsigned)
+void Viewer::button_release(unsigned btn)
 {
        if(btn==dragging)
                dragging = 0;
 }
 
-void Viewer::pointer_motion(int x, int y)
+void Viewer::axis_motion(unsigned axis, float, float delta)
 {
        if(dragging==1)
        {
-               int dx = x-pointer_x;
-               int dy = pointer_y-y;
+               float dx = (axis==0 ? delta : 0);
+               float dy = (axis==1 ? delta : 0);
 
-               yaw -= dx*M_PI*2/window.get_width();
+               yaw -= dx*M_PI*2;
                while(yaw>M_PI)
                        yaw -= M_PI*2;
                while(yaw<-M_PI)
                        yaw += M_PI*2;
 
-               pitch += dy*M_PI/window.get_height();
+               pitch += dy*M_PI;
                if(pitch>M_PI*0.49)
                        pitch = M_PI*0.49;
                if(pitch<-M_PI*0.49)
                        pitch = -M_PI*0.49;
 
                update_camera();
-
-               pointer_x = x;
-               pointer_y = y;
        }
        else if(dragging==3)
        {
-               int dx = x-window.get_width()/2;
-               int dy = window.get_height()/2-y;
+               float x = mouse.get_axis_value(0);
+               float y = mouse.get_axis_value(1);
 
-               light_yaw = yaw+dx*M_PI/window.get_width();
-               light_pitch = pitch-dy*M_PI/window.get_height();
+               light_yaw = yaw+x*M_PI;
+               light_pitch = pitch-y*M_PI;
 
                update_light();
        }
 }
 
-void Viewer::key_press(unsigned, unsigned, wchar_t)
-{
-}
-
 void Viewer::update_camera()
 {
        float cy = cos(yaw);