]> git.tdb.fi Git - r2c2.git/blobdiff - source/designer/cameracontroller.cpp
Make use of the mspmath library
[r2c2.git] / source / designer / cameracontroller.cpp
index f8d1a53b8b1b6279cfc97441d417eaa0ad877e18..0679cbe2f165934f93824929c6268640100690aa 100644 (file)
@@ -37,7 +37,7 @@ void CameraController::set_look_direction(const GL::Vector3 &look)
        GL::Vector3 focus = get_focus();
        float dist = get_distance();
        camera.set_look_direction(look);
-       camera.set_position(GL::Vector3(focus.x-look.x*dist, focus.y-look.y*dist, focus.z-look.z*dist));
+       camera.set_position(focus-look*dist);
 }
 
 void CameraController::view_all()
@@ -49,10 +49,9 @@ void CameraController::move(float x, float y)
 {
        const GL::Vector3 &pos = camera.get_position();
        const GL::Vector3 &look = camera.get_look_direction();
-       float xy_len = sqrt(look.x*look.x+look.y*look.y);
-       float dx = (look.x*y+look.y*x)/xy_len;
-       float dy = (look.y*y-look.x*x)/xy_len;
-       camera.set_position(GL::Vector3(pos.x+dx, pos.y+dy, pos.z));
+       GL::Vector3 fwd_dir = normalize(GL::Vector3(look.x, look.y, 0));
+       GL::Vector3 right_dir = cross(fwd_dir, GL::Vector3(0, 0, 1));
+       camera.set_position(pos+fwd_dir*y+right_dir*x);
 }
 
 void CameraController::adjust_distance(float delta)
@@ -63,20 +62,18 @@ void CameraController::adjust_distance(float delta)
        float low = view.get_layout().get_layout().get_catalogue().get_gauge()*5;
        if(dist+delta<low)
                delta = low-dist;
-       camera.set_position(GL::Vector3(pos.x-look.x*delta, pos.y-look.y*delta, pos.z-look.z*delta));
+       camera.set_position(pos-look*delta);
        dist += delta;
        camera.set_depth_clip(dist*0.02, dist*50);
 }
 
-void CameraController::rotate(float angle)
+void CameraController::rotate(const Angle &angle)
 {
        GL::Vector3 look = camera.get_look_direction();
-       float c = cos(angle);
-       float s = sin(angle);
-       set_look_direction(GL::Vector3(look.x*c-look.y*s, look.x*s+look.y*c, look.z));
+       set_look_direction(rotated_vector(look, angle));
 }
 
-void CameraController::pitch(float angle)
+void CameraController::pitch(const Angle &angle)
 {
        GL::Vector3 look = camera.get_look_direction();
        float xy_len = sqrt(look.x*look.x+look.y*look.y);
@@ -139,9 +136,9 @@ void CameraController::axis_motion(unsigned axis, float, float change)
        else if(drag_mode==ROTATE)
        {
                if(axis==0)
-                       rotate(-change*M_PI);
+                       rotate(Angle::from_turns(-change/2));
                else if(axis==1)
-                       pitch(change*M_PI/2);
+                       pitch(Angle::from_turns(change/4));
        }
        else if(drag_mode==DISTANCE && axis==1)
                adjust_distance(-change*3*get_distance());