]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor viewer to use Geometry::Angle
authorMikko Rasa <tdb@tdb.fi>
Tue, 5 Sep 2023 10:26:31 +0000 (13:26 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 5 Sep 2023 10:26:31 +0000 (13:26 +0300)
tools/viewer.cpp

index 36a5dc9bc13e3a2af70daf93d32e2234e781b04d..97685163cf716eeb10cccd16f3ae9e0821a25143 100644 (file)
@@ -5,6 +5,7 @@
 #include <msp/datafile/packsource.h>
 #include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
+#include <msp/geometry/angle.h>
 #include <msp/graphics/display.h>
 #include <msp/graphics/window.h>
 #include <msp/gl/animatedobject.h>
@@ -75,11 +76,11 @@ private:
        GL::DirectionalLight light;
        GL::Lighting lighting;
        GL::Camera camera;
-       float yaw;
-       float pitch;
+       Geometry::Angle<float> yaw;
+       Geometry::Angle<float> pitch;
        float distance;
-       float light_yaw;
-       float light_pitch;
+       Geometry::Angle<float> light_yaw;
+       Geometry::Angle<float> light_pitch;
        unsigned dragging;
        Time::TimeStamp last_tick;
 
@@ -138,11 +139,7 @@ Viewer::Viewer(int argc, char **argv):
        renderable(0),
        anim_object(0),
        anim_player(0),
-       yaw(0),
-       pitch(0),
        distance(10),
-       light_yaw(0),
-       light_pitch(0),
        dragging(0)
 {
        for(list<string>::const_iterator i=opts.resource_locations.begin(); i!=opts.resource_locations.end(); ++i)
@@ -327,17 +324,11 @@ void Viewer::axis_motion(unsigned axis, float, float delta)
                float dx = (axis==0 ? delta : 0);
                float dy = (axis==1 ? delta : 0);
 
-               yaw -= dx*M_PI*2;
-               while(yaw>M_PI)
-                       yaw -= M_PI*2;
-               while(yaw<-M_PI)
-                       yaw += M_PI*2;
+               yaw -= Geometry::Angle<float>::from_turns(dx);
+               yaw.wrap_balanced();
 
-               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;
+               pitch += Geometry::Angle<float>::from_turns(dy*0.5f);
+               pitch = min(max(pitch, Geometry::Angle<float>::from_turns(-0.24f)), Geometry::Angle<float>::from_turns(0.24f));
 
                update_camera();
        }
@@ -346,8 +337,8 @@ void Viewer::axis_motion(unsigned axis, float, float delta)
                float x = mouse.get_axis_value(0);
                float y = mouse.get_axis_value(1);
 
-               light_yaw = yaw+x*M_PI;
-               light_pitch = pitch-y*M_PI;
+               light_yaw = yaw+Geometry::Angle<float>::from_turns(x*0.5f);
+               light_pitch = pitch-Geometry::Angle<float>::from_turns(y*0.5f);
 
                update_light();
        }