]> git.tdb.fi Git - r2c2.git/blobdiff - source/designer/designer.cpp
Add View3D class to bundle Layout3D with a camera and a pipeline
[r2c2.git] / source / designer / designer.cpp
index 86fc3494a424508b20ad93de6c685db00cc8ecbd..366a94839dc36ca46c2d97f867e3dd4b28f8bbfe 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
 */
 
@@ -14,8 +14,6 @@ Distributed under the GPL
 #include <msp/gl/matrix.h>
 #include <msp/gl/misc.h>
 #include <msp/gl/projection.h>
-#include <msp/gl/rendermode.h>
-#include <msp/gl/select.h>
 #include <msp/gl/tests.h>
 #include <msp/gl/texture2d.h>
 #include <msp/input/keys.h>
@@ -28,6 +26,7 @@ Distributed under the GPL
 #include <msp/time/utils.h>
 #include "libr2c2/route.h"
 #include "libr2c2/tracktype.h"
+#include "libr2c2/zone.h"
 #include "3d/path.h"
 #include "designer.h"
 #include "input.h"
@@ -39,6 +38,8 @@ Distributed under the GPL
 #include "svgexporter.h"
 #include "trackbar.h"
 #include "trackproperties.h"
+#include "zonebar.h"
+#include "zoneproperties.h"
 
 using namespace std;
 using namespace R2C2;
@@ -52,11 +53,10 @@ Designer::Designer(int argc, char **argv):
        root(ui_res, window),
        base_object(0),
        cur_route(0),
+       cur_zone(0),
        mode(SELECT),
        manipulator(*this, root, selection),
-       measure(*this),
-       camera_ctl(*this, root, camera),
-       track_wrap(*this, selection)
+       measure(*this)
 {
        window.set_title("Railway Designer");
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
@@ -91,35 +91,27 @@ Designer::Designer(int argc, char **argv):
        }
 
        // Setup OpenGL
-       GL::enable(GL_CULL_FACE);
+       cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
 
-       pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
-       pipeline->set_camera(&camera);
-       pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
-       if(base_object)
-               pipeline->add_renderable(*base_object);
-       pipeline->add_renderable_for_pass(track_wrap, "unlit");
-       pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
-       pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
-
-       light.set_position(0, -0.259, 0.966, 0);
-       lighting.attach(0, light);
-
-       GL::PipelinePass *pass = &pipeline->add_pass(0);
-       pass->lighting = &lighting;
-       pass->depth_test = &GL::DepthTest::lequal();
+       main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
+       GL::Pipeline *pipeline = &main_view->get_pipeline();
 
-       pass = &pipeline->add_pass("unlit");
+       GL::PipelinePass *pass = &pipeline->add_pass("unlit");
        pass->depth_test = &GL::DepthTest::lequal();
        pass->blend = &GL::Blend::alpha();
 
        pass = &pipeline->add_pass("blended");
-       pass->lighting = &lighting;
+       pass->lighting = &layout_3d->get_lighting();
        pass->depth_test = &GL::DepthTest::lequal();
        pass->blend = &GL::Blend::alpha();
 
-       camera.set_up_direction(GL::Vector3(0, 0, 1));
-       camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
+       pass = &pipeline->add_pass("overlay");
+       pass->blend = &GL::Blend::alpha();
+
+       if(base_object)
+               pipeline->add_renderable(*base_object);
+       pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
+       pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
 
        // Setup UI
        root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
@@ -130,6 +122,7 @@ Designer::Designer(int argc, char **argv):
        toolbars.push_back(new Layoutbar(*this));
        toolbars.push_back(new Trackbar(*this));
        toolbars.push_back(new Routebar(*this));
+       toolbars.push_back(new Zonebar(*this));
        for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
        {
                root.add(**i);
@@ -148,9 +141,15 @@ Designer::Designer(int argc, char **argv):
        statusbar->add(*lbl_status);
        lbl_status->set_geometry(GLtk::Geometry(20, 2, 300, 16));
 
-       overlay = new Overlay3D(window, camera, ui_res.get_default_font());
+       track_wrap = new TrackWrap(*layout_3d, selection);
+       pipeline->add_renderable_for_pass(*track_wrap, "unlit");
+       overlay = new Overlay3D(ui_res.get_default_font());
+       pipeline->add_renderable_for_pass(*overlay, "overlay");
 
-       camera_ctl.view_all();
+       camera_ctl = new CameraController(*main_view, root);
+       cat_view->get_camera().set_look_direction(GL::Vector3(0, 0.13053, -0.99144));
+       cat_view->view_all(true);
+       main_view->view_all();
 
        const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
        for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
@@ -159,11 +158,16 @@ Designer::Designer(int argc, char **argv):
 
 Designer::~Designer()
 {
+       for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
+               delete *i;
+       delete camera_ctl;
+       delete track_wrap;
        delete overlay;
-       delete pipeline;
+       delete main_view;
        delete base_object;
        delete layout_3d;
        delete layout;
+       delete cat_view;
        delete cat_layout_3d;
 }
 
@@ -249,6 +253,22 @@ void Designer::flatten_tracks()
        manipulator.flatten();
 }
 
+void Designer::svg_export()
+{
+       InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
+       input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
+}
+
+void Designer::edit_route(Route *r)
+{
+       cur_route = r;
+       cur_zone = 0;
+       if(cur_route)
+               show_route(*cur_route);
+       else
+               clear_paths();
+}
+
 void Designer::rename_route()
 {
        if(mode!=SELECT || !cur_route)
@@ -258,43 +278,70 @@ void Designer::rename_route()
        input->signal_accept.connect(sigc::mem_fun(this, &Designer::route_name_accept));
 }
 
-void Designer::svg_export()
+void Designer::add_selection_to_route()
 {
-       InputDialog *input = new InputDialog(*this, "SVG export", FS::basepart(filename)+".svg");
-       input->signal_accept.connect(sigc::mem_fun(this, &Designer::svg_export_accept));
+       if(!cur_route)
+               return;
+
+       try
+       {
+               cur_route->add_tracks(selection.get_tracks());
+       }
+       catch(const Exception &e)
+       {
+               lbl_status->set_text(e.what());
+       }
+
+       show_route(*cur_route);
 }
 
-void Designer::edit_route(Route *r)
+void Designer::edit_zone(Zone *z)
 {
-       cur_route = r;
-       show_route(r);
+       cur_zone = z;
+       cur_route = 0;
+       if(cur_zone)
+               show_zone(*cur_zone);
+       else
+               clear_paths();
 }
 
-void Designer::add_selection_to_route()
+void Designer::zone_properties()
 {
-       if(!cur_route)
+       if(!cur_zone)
+               return;
+
+       ZoneProperties *zone_prop = new ZoneProperties(*cur_zone);
+       root.add(*zone_prop);
+       const GLtk::Geometry &root_geom = root.get_geometry();
+       const GLtk::Geometry &dlg_geom = zone_prop->get_geometry();
+       zone_prop->set_position((root_geom.w-dlg_geom.w)/2, (root_geom.h-dlg_geom.h)/2);
+}
+
+void Designer::add_selection_to_zone()
+{
+       if(!cur_zone)
                return;
 
        try
        {
-               cur_route->add_tracks(selection.get_tracks());
+               cur_zone->add_tracks(selection.get_tracks());
        }
        catch(const Exception &e)
        {
                lbl_status->set_text(e.what());
        }
 
-       show_route(cur_route);
+       show_zone(*cur_zone);
 }
 
-Point Designer::map_pointer_to_ground(int x, int y)
+Vector Designer::map_pointer_to_ground(int x, int y)
 {
        float xf = x*2.0/window.get_width()-1.0;
        float yf = y*2.0/window.get_height()-1.0;
-       GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
-       const GL::Vector3 &pos = camera.get_position();
+       GL::Vector4 vec = main_view->get_camera().unproject(GL::Vector4(xf, yf, 0, 0));
+       const GL::Vector3 &pos = main_view->get_camera().get_position();
 
-       return Point(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
+       return Vector(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
 }
 
 void Designer::tick()
@@ -308,7 +355,7 @@ void Designer::tick()
 
        window.tick();
        root.tick();
-       camera_ctl.tick(dt);
+       camera_ctl->tick(dt);
 
        for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
                layout_3d->get_track(**i).get_path().set_mask(0);
@@ -395,7 +442,12 @@ void Designer::key_press(unsigned key, unsigned mod, wchar_t)
        else if(key==Msp::Input::KEY_E)
                manipulator.even_slope();
        else if(key==Msp::Input::KEY_A)
-               add_selection_to_route();
+       {
+               if(cur_route)
+                       add_selection_to_route();
+               else if(cur_zone)
+                       add_selection_to_zone();
+       }
        else if(key==Msp::Input::KEY_C)
                connect_tracks();
        else if(key==Msp::Input::KEY_V)
@@ -409,16 +461,16 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
        y = window.get_height()-y-1;
        mod = Input::mod_from_sys(mod);
 
-       Point ground = map_pointer_to_ground(x, y);
+       Vector ground = map_pointer_to_ground(x, y);
 
        if(mode==CATALOGUE)
        {
                if(btn==1)
                {
-                       Track3D *ctrack = pick_track(x, y);
+                       Track *ctrack = pick_track(x, y);
                        if(ctrack)
                        {
-                               Track *track = new Track(*layout, ctrack->get_track().get_type());
+                               Track *track = new Track(*layout, ctrack->get_type());
                                track->set_position(ground);
 
                                selection.clear();
@@ -434,12 +486,12 @@ void Designer::button_press(int x, int y, unsigned btn, unsigned mod)
        {
                if(btn==1)
                {
-                       Track3D *track = pick_track(x, y);
+                       Track *track = pick_track(x, y);
                        if(track)
                        {
                                if(!(mod&Input::MOD_SHIFT))
                                        selection.clear();
-                               selection.toggle_track(&track->get_track());
+                               selection.toggle_track(track);
                        }
                }
        }
@@ -453,47 +505,21 @@ void Designer::pointer_motion(int x, int y)
 
        if(!root.get_child_at(x, y))
        {
-               Point ground = map_pointer_to_ground(x, y);
+               Vector ground = map_pointer_to_ground(x, y);
                measure.pointer_motion(x, y, ground.x, ground.y);
        }
 }
 
-void Designer::apply_camera()
-{
-       if(mode==CATALOGUE)
-       {
-               GL::matrix_mode(GL::PROJECTION);
-               GL::load_identity();
-               GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
-               GL::matrix_mode(GL::MODELVIEW);
-               GL::load_identity();
-               GL::translate(0, 0, -1);
-       }
-       else
-               camera.apply();
-}
-
 void Designer::render()
 {
        GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
 
-       if(mode==CATALOGUE)
+       View3D *view = (mode==CATALOGUE ? cat_view : main_view);
+       view->render();
+       if(mode==MEASURE)
        {
-               apply_camera();
-               cat_layout_3d->get_scene().render();
-       }
-       else
-       {
-               pipeline->render_all();
-               GL::enable(GL_CULL_FACE);
-               {
-                       GL::Bind bind_blend(GL::Blend::alpha());
-                       overlay->render(0);
-               }
-
                GL::Bind bind_depth(GL::DepthTest::lequal());
-               if(mode==MEASURE)
-                       measure.render();
+               measure.render();
        }
 
        GL::matrix_mode(GL::PROJECTION);
@@ -502,11 +528,11 @@ void Designer::render()
        GL::matrix_mode(GL::MODELVIEW);
        GL::load_identity();
 
-       GL::disable(GL::DEPTH_TEST);
-
        GL::Bind bind_blend(GL::Blend::alpha());
        root.render();
+       // XXX Should fix GLtk so these would not be needed
        GL::Texture::unbind();
+       glColor3f(1.0, 1.0, 1.0);
 }
 
 void Designer::track_added(Track &trk)
@@ -521,19 +547,16 @@ void Designer::track_removed(Track &trk)
                new_tracks.erase(i);
 }
 
-Track3D *Designer::pick_track(int x, int y)
+Track *Designer::pick_track(int x, int y)
 {
-       Layout3D *l = layout_3d;
-       if(mode==CATALOGUE)
-               l = cat_layout_3d;
-
-       float xx = ((float(x)-window.get_width()/2)/window.get_height())*0.82843;
-       float yy = (float(y)/window.get_height()-0.5)*0.82843;
-       float size = 4.0/window.get_height()*0.82843;
+       float xx = x*2.0/window.get_width()-1.0;
+       float yy = y*2.0/window.get_height()-1.0;
 
-       apply_camera();
+       View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
+       const GL::Vector3 &cpos = view.get_camera().get_position();
+       GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(xx, yy, 0, 0));
 
-       return l->pick_track(xx, yy, size);
+       return view.get_layout().get_layout().pick_track(Vector(cpos.x, cpos.y, cpos.z), Vector(cray.x, cray.y, cray.z));
 }
 
 void Designer::update_track_icon(Track3D &track)
@@ -631,17 +654,16 @@ void Designer::svg_export_accept(const string &text)
 
 string Designer::tooltip(int x, int y)
 {
-       if(Track3D *t3d = pick_track(x, y))
+       if(Track *track = pick_track(x, y))
        {
-               const Track &track = t3d->get_track();
-               const TrackType &ttype = track.get_type();
+               const TrackType &ttype = track->get_type();
                string info = format("%d %s", ttype.get_article_number(), ttype.get_description());
-               if(mode!=CATALOGUE && abs(track.get_slope())>1e-4)
-                       info += format(" (slope %.1f%%)", abs(track.get_slope()/ttype.get_total_length()*100));
-               if(track.get_turnout_id())
-                       info += format(" (turnout %d)", track.get_turnout_id());
-               else if(track.get_sensor_id())
-                       info += format(" (sensor %d)", track.get_sensor_id());
+               if(mode!=CATALOGUE && abs(track->get_slope())>1e-4)
+                       info += format(" (slope %.1f%%)", abs(track->get_slope()/ttype.get_total_length()*100));
+               if(track->get_turnout_id())
+                       info += format(" (turnout %d)", track->get_turnout_id());
+               else if(track->get_sensor_id())
+                       info += format(" (sensor %d)", track->get_sensor_id());
 
                return info;
        }
@@ -649,28 +671,44 @@ string Designer::tooltip(int x, int y)
        return string();
 }
 
-void Designer::show_route(const Route *route)
+void Designer::clear_paths()
 {
        const set<Track *> &ltracks = layout->get_tracks();
        for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
        {
                Track3D &t3d = layout_3d->get_track(**i);
-               if(route && route->has_track(**i))
-               {
-                       t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
-                       if((*i)->get_type().is_turnout())
-                       {
-                               unsigned tid = (*i)->get_turnout_id();
-                               int path = (tid ? route->get_turnout(tid) : -1);
-                               if(path>=0)
-                                       t3d.get_path().set_path(path);
-                               else
-                                       t3d.get_path().set_mask((*i)->get_type().get_paths());
-                       }
-                       else
-                               t3d.get_path().set_path(0);
-               }
+               t3d.get_path().set_mask(0);
+       }
+}
+
+void Designer::show_route(const Route &route)
+{
+       clear_paths();
+
+       const set<Track *> &rtracks = route.get_tracks();
+       for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
+       {
+               Track3D &t3d = layout_3d->get_track(**i);
+               t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0));
+               int path = -1;
+               if(unsigned tid = (*i)->get_turnout_id())
+                       path = route.get_turnout(tid);
+               if(path>=0)
+                       t3d.get_path().set_path(path);
                else
-                       t3d.get_path().set_mask(0);
+                       t3d.get_path().set_mask((*i)->get_type().get_paths());
+       }
+}
+
+void Designer::show_zone(const Zone &zone)
+{
+       clear_paths();
+
+       const Zone::TrackSet &ztracks = zone.get_tracks();
+       for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
+       {
+               Track3D &t3d = layout_3d->get_track(**i);
+               t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
+               t3d.get_path().set_mask((*i)->get_type().get_paths());
        }
 }