]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/engineer.cpp
Use raycasting instead of OpenGL selection mode to pick tracks
[r2c2.git] / source / engineer / engineer.cpp
index fad61693aef66161e1822dc18f7e8a209f7d33af..32b23ab900aa16ea6ab7009081d911ec8801a73a 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of R²C²
-Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -213,15 +213,15 @@ void Engineer::tick()
 
                if(picking)
                {
-                       Track3D *track = pick_track(pointer_x, window.get_height()-pointer_y-1);
-                       if(track && &track->get_track()!=picking_track)
+                       Track *track = pick_track(pointer_x, window.get_height()-pointer_y-1);
+                       if(track && track!=picking_track)
                        {
-                               picking_track = &track->get_track();
+                               picking_track = track;
                                if(picking_entry>=0)
                                        picking_entry = 0;
 
                                delete picking_path;
-                               picking_path = new Path3D(*track);
+                               picking_path = new Path3D(layout_3d.get_track(*track));
                                if(picking_entry>=0)
                                        picking_path->set_mask(picking_track->get_type().get_endpoint(picking_entry).paths);
                                else
@@ -280,19 +280,18 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned)
        }
        else
        {
-               Track3D *t3d = pick_track(x, window.get_height()-y-1);
-               if(t3d)
+               Track *track = pick_track(x, window.get_height()-y-1);
+               if(track)
                {
-                       Track &track = t3d->get_track();
-                       if(track.get_turnout_id())
+                       if(track->get_turnout_id())
                        {
-                               Block &block = track.get_block();
+                               Block &block = track->get_block();
                                if(block.get_train() && !block.get_train()->free_block(block))
                                        set_status("Turnout is busy");
                                else
                                {
-                                       unsigned paths = track.get_type().get_paths();
-                                       unsigned i = track.get_active_path()+1;
+                                       unsigned paths = track->get_type().get_paths();
+                                       unsigned i = track->get_active_path()+1;
                                        while(!(paths&(1<<i)))
                                        {
                                                if(!(paths>>i))
@@ -300,11 +299,11 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned)
                                                else
                                                        ++i;
                                        }
-                                       track.set_active_path(i);
-                                       set_status(format("Turnout %d", track.get_turnout_id()));
+                                       track->set_active_path(i);
+                                       set_status(format("Turnout %d", track->get_turnout_id()));
                                }
                        }
-                       if(unsigned sid = track.get_sensor_id())
+                       if(unsigned sid = track->get_sensor_id())
                        {
                                if(options.simulate)
                                        layout.get_driver().set_sensor(sid, !layout.get_driver().get_sensor(sid));
@@ -418,16 +417,14 @@ void Engineer::block_reserved(const Block &block, const Train *)
        reset_block_color(block);
 }
 
-Track3D *Engineer::pick_track(int x, int y)
+Track *Engineer::pick_track(int x, int y)
 {
-       float view_height = tan(camera.get_field_of_view()/2)*2;
-       float xx = ((float(x)-window.get_width()/2)/window.get_height())*view_height;
-       float yy = (float(y)/window.get_height()-0.5)*view_height;
-       float size = 4.0/window.get_height()*view_height;
-
-       camera.apply();
+       const GL::Vector3 &start = camera.get_position();
+       float xx = x*2.0/window.get_width()-1.0;
+       float yy = y*2.0/window.get_height()-1.0;
+       GL::Vector4 ray = camera.unproject(GL::Vector4(xx, yy, 0, 0));
 
-       return layout_3d.pick_track(xx, yy, size);
+       return layout.pick_track(Vector(start.x, start.y, start.z), Vector(ray.x, ray.y, ray.z));
 }
 
 void Engineer::train_added(Train &train)