]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/engineer.cpp
Use some template magic to deduce the proper 3D type
[r2c2.git] / source / engineer / engineer.cpp
index 61dd49a1c72d86136baa9fd462e05b2458b398e5..fb21f9917b0e718b4031e0e8a42937aea3375d67 100644 (file)
 #include "libr2c2/driver.h"
 #include "libr2c2/trackcircuit.h"
 #include "libr2c2/tracktype.h"
+#include "3d/allocation.h"
 #include "3d/path.h"
 #include "3d/track.h"
+#include "3d/trackcircuit.h"
 #include "3d/vehicle.h"
 #include "engineer.h"
 #include "mainwindow.h"
@@ -40,12 +42,7 @@ Engineer::Engineer(int argc, char **argv):
        layout(catalogue, (options.driver.empty() ? 0 : Driver::create(options.driver))),
        layout_3d(layout),
        server(0),
-       pipeline(window.get_width(), window.get_height(), false),
-       picking(false),
-       picking_track(0),
-       picking_entry(0),
-       picking_path(0),
-       pointer_moved(false)
+       main_view(layout_3d, window.get_width(), window.get_height())
 {
        // Setup GUI
        window.set_title("Railroad Engineer");
@@ -70,11 +67,16 @@ Engineer::Engineer(int argc, char **argv):
        DataFile::load(layout, options.layout_fn);
 
        layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added));
-       layout.signal_block_reserved.connect(sigc::hide<1>(sigc::mem_fun(this, &Engineer::reset_block_color)));
        layout.signal_emergency.connect(sigc::mem_fun(this, &Engineer::set_status));
        const set<Block *> &blocks = layout.get_all<Block>();
        for(set<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
-               (*i)->get_sensor().signal_state_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &Engineer::reset_block_color), sigc::ref(**i))));
+               if(TrackCircuit *tc = (*i)->get_sensor())
+                       new TrackCircuit3D(layout_3d, *tc);
+
+       const set<Track *> &tracks = layout.get_all<Track>();
+       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               if((*i)->get_type().is_turnout())
+                       new Path3D(layout_3d.get_3d(**i));
 
        if(FS::exists(options.state_fn))
                DataFile::load(layout, options.state_fn);
@@ -86,27 +88,15 @@ Engineer::Engineer(int argc, char **argv):
        }
 
        // Setup 3D view
-       DataFile::load(arrow_mesh, "arrow.mesh");
-
-       pipeline.set_camera(&camera);
-       pipeline.add_renderable_for_pass(layout_3d.get_scene(), 0);
-       pipeline.add_renderable_for_pass(layout_3d.get_path_scene(), "unlit");
-       pipeline.add_renderable_for_pass(*overlay, "overlay");
-
-       light.set_position(GL::Vector4(0, -0.259, 0.966, 0));
-       light.set_diffuse(GL::Color(0.9));
-       lighting.set_ambient(GL::Color(0.4));
-       lighting.attach(0, light);
+       GL::Pipeline &pipeline = main_view.get_pipeline();
 
-       GL::Pipeline::Pass *pass = &pipeline.add_pass(0);
-       pass->set_depth_test(&GL::DepthTest::lequal());
-       pass->set_lighting(&lighting);
-
-       pass = &pipeline.add_pass("unlit");
+       GL::Pipeline::Pass *pass = &pipeline.add_pass("unlit");
        pass->set_depth_test(&GL::DepthTest::lequal());
+       pipeline.add_renderable_for_pass(layout_3d.get_path_scene(), "unlit");
 
        pass = &pipeline.add_pass("overlay");
        pass->set_blend(&GL::Blend::alpha());
+       pipeline.add_renderable_for_pass(*overlay, "overlay");
 
        view_all();
 
@@ -154,13 +144,6 @@ void Engineer::remove_train_view(TrainView &tv)
        train_views.erase(remove(train_views.begin(), train_views.end(), &tv), train_views.end());
 }
 
-void Engineer::pick(bool with_ep)
-{
-       picking = true;
-       picking_track = 0;
-       picking_entry = (with_ep ? 0 : -1);
-}
-
 int Engineer::main()
 {
        window.show();
@@ -190,45 +173,7 @@ void Engineer::tick()
 
        GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
 
-       pipeline.render();
-
-       if(pointer_moved)
-       {
-               pointer_moved = false;
-
-               if(picking)
-               {
-                       Track *track = dynamic_cast<Track *>(pick_object(pointer));
-                       if(track && track!=picking_track)
-                       {
-                               picking_track = track;
-                               if(picking_entry>=0)
-                                       picking_entry = 0;
-
-                               delete picking_path;
-                               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
-                                       picking_path->set_mask(picking_track->get_type().get_paths());
-                               picking_path->set_color(GL::Color(0));
-                               picking_path->set_layer(1);
-                       }
-               }
-       }
-
-       if(picking && picking_track && picking_entry>=0)
-       {
-               camera.apply();
-               GL::MatrixStack::Push push_mat(GL::MatrixStack::modelview());
-
-               Snap sn = picking_track->get_snap_node(picking_entry);
-
-               GL::MatrixStack::modelview() *= GL::Matrix::translation(sn.position+GL::Vector3(0, 0, 0.03));
-               GL::MatrixStack::modelview() *= GL::Matrix::rotation(sn.rotation+Angle::half_turn(), 0, 0, 1);
-
-               arrow_mesh.draw();
-       }
+       main_view.render();
 
        root->render();
 
@@ -237,22 +182,7 @@ void Engineer::tick()
 
 void Engineer::button_press(unsigned btn)
 {
-       if(picking)
-       {
-               if(btn==1 && picking_track)
-               {
-                       picking = false;
-                       delete picking_path;
-                       picking_path = 0;
-                       signal_pick_done.emit(picking_track, picking_entry);
-               }
-               else if(btn==3 && picking_entry>=0)
-               {
-                       picking_entry = (picking_entry+1)%picking_track->get_type().get_endpoints().size();
-                       picking_path->set_mask(picking_track->get_type().get_endpoint(picking_entry).paths);
-               }
-       }
-       else
+       if(btn==1)
        {
                Object *obj = pick_object(pointer);
                if(Track *track = dynamic_cast<Track *>(obj))
@@ -299,15 +229,14 @@ void Engineer::axis_motion(unsigned axis, float value, float)
                pointer.x = value;
        if(axis==1)
                pointer.y = value;
-       pointer_moved = true;
 }
 
 void Engineer::view_all()
 {
-       const Layout3D::TrackMap &tracks = layout_3d.get_tracks();
+       const Layout3D::ObjectMap &objects = layout_3d.get_all();
 
        float view_aspect = float(window.get_width())/window.get_height();
-       float view_height = tan(camera.get_field_of_view()/2.0f)*2.0f;
+       float view_height = tan(main_view.get_camera().get_field_of_view()/2.0f)*2.0f;
        float best_score = 0;
        GL::Vector3 pos;
        GL::Vector3 up;
@@ -315,8 +244,8 @@ void Engineer::view_all()
        {
                Transform trans = Transform::rotation(-angle, Vector(0, 0, 1));
                BoundingBox bbox;
-               for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-                       bbox = bbox|trans.transform(i->second->get_track().get_bounding_box());
+               for(Layout3D::ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
+                       bbox = bbox|trans.transform(i->second->get_object().get_bounding_box());
 
                const Vector &minp = bbox.get_minimum_point();
                const Vector &maxp = bbox.get_maximum_point();
@@ -342,6 +271,7 @@ void Engineer::view_all()
                }
        }
 
+       GL::Camera &camera = main_view.get_camera();
        camera.set_position(pos);
        camera.set_up_direction(up);
        camera.set_look_direction(GL::Vector3(0, 0, -1));
@@ -349,39 +279,10 @@ void Engineer::view_all()
        camera.set_depth_clip(pos.z*0.5, pos.z*1.5);
 }
 
-void Engineer::set_block_color(const Block &block, const GL::Color &color)
-{
-       const set<Track *> &tracks = block.get_tracks();
-       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               layout_3d.get_track(**i).get_path().set_color(color);
-}
-
-void Engineer::reset_block_color(const Block &block)
-{
-       bool active = block.get_sensor().get_state()>Sensor::INACTIVE;
-
-       if(block.get_train())
-       {
-               GL::Color color;
-               map<Train *, GL::Color>::iterator i = train_colors.find(block.get_train());
-               if(i!=train_colors.end())
-                       color = i->second;
-
-               if(active)
-                       set_block_color(block, color*0.6);
-               else
-                       set_block_color(block, color*0.5+0.5);
-       }
-       else if(active)
-               set_block_color(block, GL::Color(0.6));
-       else
-               set_block_color(block, GL::Color(1));
-}
-
 Object *Engineer::pick_object(const Vector &p)
 {
-       const GL::Vector3 &start = camera.get_position();
-       GL::Vector4 ray = camera.unproject(GL::Vector4(p.x, p.y, 0, 0));
+       const GL::Vector3 &start = main_view.get_camera().get_position();
+       GL::Vector4 ray = main_view.get_camera().unproject(GL::Vector4(p.x, p.y, 0, 0));
 
        // XXX Do this better; make this function a template?
        if(Vehicle *veh = layout.pick<Vehicle>(Ray(start, Vector(ray))))
@@ -391,7 +292,7 @@ Object *Engineer::pick_object(const Vector &p)
 
 void Engineer::process_new_train(Train &train)
 {
-       Vehicle3D &loco3d = layout_3d.get_vehicle(train.get_vehicle(0));
+       Vehicle3D &loco3d = layout_3d.get_3d(train.get_vehicle(0));
        overlay->set_label(loco3d, train.get_name());
        train.signal_name_changed.connect(sigc::bind<0>(sigc::mem_fun(overlay, &Overlay3D::set_label), sigc::ref(loco3d)));
 }
@@ -427,6 +328,9 @@ void Engineer::train_added(Train &train)
                }
        }
        train_colors[&train] = best_color;
+
+       Allocation3D *alloc = new Allocation3D(layout_3d, train);
+       alloc->set_color(best_color);
 }
 
 void Engineer::sighandler(int sig)