X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fengineer%2Fengineer.cpp;h=371e82e49e7c4d4fc6b69315be3e0ca1b355d4a6;hb=c87c4200e105a21c1d1f9faec20c4c08ddbb53ee;hp=6a35a06a3317992dae0075dece5eecfe72e43ddd;hpb=e60fdcb9617f539b4d578d06ec46eb955c5e0624;p=r2c2.git diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index 6a35a06..371e82e 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -1,30 +1,31 @@ /* $Id$ This file is part of the MSP Märklin suite -Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa +Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ #include +#include #include #include -#include #include -#include #include #include #include -#include +#include +#include #include +#include #include +#include #include #include #include -#include -#include #include -#include "libmarklin/except.h" +#include "libmarklin/driver.h" #include "libmarklin/tracktype.h" +#include "3d/path.h" #include "engineer.h" #include "mainpanel.h" #include "trainpanel.h" @@ -34,80 +35,70 @@ using namespace std; using namespace Marklin; using namespace Msp; +Application::RegApp Engineer::reg; + Engineer::Engineer(int argc, char **argv): - screen_w(1280), - screen_h(960), - fullscreen(false), - layout(catalogue), + options(argc, argv), + window(options.screen_w, options.screen_h, options.fullscreen), + layout(catalogue, (options.driver.empty() ? 0 : Driver::create(options.driver))), layout_3d(layout), server(0), + pipeline(window.get_width(), window.get_height(), false), placing_train(0), placing_block(0), - placing_entry(0), - no_lighting(false), - simulate(false) + placing_entry(0) { - string res; - bool debug = false; - string device = "/dev/ttyS0"; - unsigned quality = 4; - bool network = false; - - GetOpt getopt; - getopt.add_option('r', "resolution", res, GetOpt::REQUIRED_ARG); - getopt.add_option('f', "fullscreen", fullscreen, GetOpt::NO_ARG); - getopt.add_option('g', "debug", debug, GetOpt::NO_ARG); - getopt.add_option('d', "device", device, GetOpt::REQUIRED_ARG); - getopt.add_option('q', "quality", quality, GetOpt::REQUIRED_ARG); - getopt.add_option('s', "simulate", simulate, GetOpt::NO_ARG); - getopt.add_option('n', "network", network, GetOpt::NO_ARG); - getopt.add_option( "no-lighting", no_lighting, GetOpt::NO_ARG); - getopt(argc, argv); - - if(!res.empty()) - { - if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res)) - { - screen_w = lexical_cast(m[1].str); - screen_h = lexical_cast(m[2].str); - } - else - throw UsageError("Invalid resolution"); - } + // Setup GUI + window.set_title("Railroad Engineer"); + window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0)); - if(device!="none") - control.open(device); + DataFile::load(ui_res, "marklin.res"); + root = new GLtk::Root(ui_res, window); + root->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press)); + root->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion)); + root->set_visible(true); - control.set_debug(debug); + main_panel = new MainPanel(*this, ui_res); + root->add(*main_panel); + main_panel->set_position(0, window.get_height()-main_panel->get_geometry().h); + main_panel->set_visible(true); - layout_3d.set_quality(quality); + overlay = new Overlay3D(window, camera, ui_res.get_default_font()); + // Setup railroad control DataFile::load(catalogue, "tracks.dat"); DataFile::load(catalogue, "locos.dat"); + DataFile::load(layout, options.layout_fn); - const vector &args = getopt.get_args(); - if(args.empty()) - throw UsageError("No layout given"); - DataFile::load(layout, args.front()); - - trfc_mgr = new TrafficManager(control, layout); + layout.signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added)); + layout.signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved)); if(FS::exists("engineer.state")) - DataFile::load(*trfc_mgr, "engineer.state"); - trfc_mgr->signal_train_added.connect(sigc::mem_fun(this, &Engineer::train_added)); - trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved)); + DataFile::load(layout, "engineer.state"); - if(network) + if(options.network) { - server = new Server(*trfc_mgr); + server = new Server(layout); server->use_event_dispatcher(event_disp); } - const map &sensors = control.get_sensors(); - for(map::const_iterator i=sensors.begin(); i!=sensors.end(); ++i) - i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_event), i->second)); + layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Engineer::sensor_event)); + + // Setup 3D view + DataFile::load(arrow_mesh, "arrow.mesh"); + + pipeline.set_camera(&camera); + pipeline.add_renderable(layout_3d.get_scene()); + + light.set_position(0, -0.259, 0.966, 0); + lighting.attach(0, light); + + GL::PipelinePass &pass = pipeline.add_pass(0); + pass.depth_test = &GL::DepthTest::lequal(); + pass.lighting = &lighting; view_all(); + // Catch various signals so we can stop the trains in case we get terminated catch_signal(SIGINT); catch_signal(SIGTERM); catch_signal(SIGSEGV); @@ -118,16 +109,17 @@ Engineer::Engineer(int argc, char **argv): Engineer::~Engineer() { - const list &trains = trfc_mgr->get_trains(); - for(list::const_iterator i=trains.begin(); i!=trains.end(); ++i) - (*i)->set_speed(0); + const map &trains = layout.get_trains(); + for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) + i->second->set_speed(0); + layout.get_driver().flush(); - while(control.get_queue_length()) - control.tick(); + if(!options.simulate) + layout.save_trains("engineer.state"); + + delete overlay; + delete root; - if(!simulate) - trfc_mgr->save("engineer.state"); - delete trfc_mgr; delete server; } @@ -140,124 +132,30 @@ void Engineer::place_train(Train &train) int Engineer::main() { - dpy = new Graphics::Display; - - Graphics::WindowOptions wopt; - wopt.width = screen_w; - wopt.height = screen_h; - wopt.fullscreen = fullscreen; - wnd = new Graphics::Window(*dpy, wopt); - - Graphics::GLOptions glopt; - //glopt.multisample = 4; - glc = new Graphics::GLContext(*wnd, glopt); - - wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0)); - wnd->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press)); - wnd->signal_button_release.connect(sigc::mem_fun(this, &Engineer::button_release)); - wnd->signal_pointer_motion.connect(sigc::mem_fun(this, &Engineer::pointer_motion)); - - glEnableClientState(GL_VERTEX_ARRAY); - glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); - glDepthFunc(GL_LEQUAL); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - DataFile::load(ui_res, "marklin.res"); - root = new GLtk::Root(ui_res, *wnd); - root->set_visible(true); + window.show(); - list texs = ui_res.get_list(); - for(list::iterator i=texs.begin(); i!=texs.end(); ++i) - { - (*i)->set_min_filter(GL::NEAREST); - (*i)->set_mag_filter(GL::NEAREST); - } - - main_panel = new MainPanel(*this, ui_res); - root->add(*main_panel); - main_panel->set_position(0, screen_h-main_panel->get_geometry().h); - main_panel->set_visible(true); - - const list &trains = trfc_mgr->get_trains(); - int y = main_panel->get_geometry().y; - for(list::const_iterator i=trains.begin(); i!=trains.end(); ++i) - { - TrainPanel *tpanel = new TrainPanel(*this, ui_res, **i); - root->add(*tpanel); - tpanel->set_position(0, y-tpanel->get_geometry().h); - train_panels.push_back(tpanel); - tpanel->set_visible(true); - y -= tpanel->get_geometry().h; - } - - const list &blocks = trfc_mgr->get_blocks(); - for(list::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) - reset_block_color(**i); - - wnd->show(); - - Application::main(); - - delete root; - delete glc; - delete wnd; - delete dpy; - - return exit_code; + return Application::main(); } void Engineer::tick() { - dpy->tick(); + window.get_display().tick(); - control.tick(); - trfc_mgr->tick(); + layout.tick(); event_disp.tick(Time::zero); - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); - - project_3d(); - glLoadIdentity(); - glRotatef(-cam_rot*180/M_PI, 0, 0, 1); - glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z); + GL::clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT); - if(!no_lighting) + pipeline.render_all(); + layout_3d.get_path_scene().render(); { - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - float params[4]; - params[0] = 0; - params[1] = -0.2; - params[2] = 1; - params[3] = 0; - glLightfv(GL_LIGHT0, GL_POSITION, params); - } - - //glEnable(GL_DEPTH_TEST); - glEnable(GL_MULTISAMPLE); - - layout_3d.render(); - - glDisable(GL_LIGHTING); - glColor4f(1, 1, 1, 1); - const list <racks = layout_3d.get_tracks(); - for(list::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i) - { - Track &track = (*i)->get_track(); - if(track.get_turnout_id()) - { - Turnout &trnt = control.get_turnout(track.get_turnout_id()); - (*i)->render_path(trnt.get_path()); - } - else - (*i)->render_path(-1); + GL::Bind blend(GL::Blend::alpha()); + overlay->render(0); } if(placing_train && placing_block) { - GL::push_matrix(); + GL::PushMatrix push_mat; const Marklin::Block::Endpoint &bep = placing_block->get_endpoints()[placing_entry]; float rot = bep.track->get_endpoint_direction(bep.track_ep); @@ -265,61 +163,24 @@ void Engineer::tick() GL::translate(pos.x, pos.y, pos.z+0.03); GL::rotate(rot*180/M_PI+180, 0, 0, 1); - GL::Texture::unbind(); - GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2)); - imm.color(1.0f, 1.0f, 1.0f); - imm.begin(GL::TRIANGLE_FAN); - imm.vertex(0.08, 0); - imm.vertex(0.05, 0.03); - imm.vertex(0.05, 0.01); - imm.vertex(0, 0.01); - imm.vertex(0, -0.01); - imm.vertex(0.05, -0.01); - imm.vertex(0.05, -0.03); - imm.end(); - - GL::pop_matrix(); - } - - const list &trains = trfc_mgr->get_trains(); - for(list::const_iterator i=trains.begin(); i!=trains.end(); ++i) - { - if(!(*i)->is_placed()) - continue; - - GL::PushMatrix _push; - - const Point &tp = (*i)->get_position(); - GL::translate(tp.x, tp.y, tp.z+0.02); - GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2)); - imm.color(0.8f, 0.8f, 1.0f); - imm.begin(GL::TRIANGLE_FAN); - imm.vertex(0, 0); - for(unsigned j=0; j<=12; ++j) - imm.vertex(0.02*cos(j*M_PI/6), 0.02*sin(j*M_PI/6)); - imm.end(); - - GL::rotate(cam_rot*180/M_PI, 0, 0, 1); - GL::translate(0.03, -0.02, 0); - GL::scale_uniform(0.04); - ui_res.get_default_font().draw_string((*i)->get_name()); - GL::Texture::unbind(); + arrow_mesh.draw(); } + const GLtk::Geometry &rgeom = root->get_geometry(); GL::matrix_mode(GL::PROJECTION); GL::load_identity(); - GL::ortho_bottomleft(screen_w, screen_h); + GL::ortho_bottomleft(rgeom.w, rgeom.h); GL::matrix_mode(GL::MODELVIEW); GL::load_identity(); - glDisable(GL_DEPTH_TEST); - glDisable(GL_LIGHTING); - glDisable(GL_MULTISAMPLE); - - root->render(); + { + GL::Bind blend(GL::Blend::alpha()); + root->render(); + GL::Texture::unbind(); + } - glc->swap_buffers(); + window.swap_buffers(); } void Engineer::button_press(int x, int y, unsigned btn, unsigned) @@ -328,7 +189,7 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned) { if(btn==1 && placing_block && !placing_block->get_train()) { - set_block_color(*placing_block, GL::Color(1, 1, 1)); + reset_block_color(*placing_block); placing_train->place(*placing_block, placing_entry); placing_train = 0; @@ -342,59 +203,59 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned) } else { - Track3D *track = pick_track(x, screen_h-y-1); - if(track) + Track3D *t3d = pick_track(x, window.get_height()-y-1); + if(t3d) { - if(unsigned tid=track->get_track().get_turnout_id()) + Track &track = t3d->get_track(); + if(track.get_turnout_id()) { - Turnout &turnout = control.get_turnout(tid); - try - { - turnout.set_path((turnout.get_path()+1)%track->get_track().get_type().get_n_paths()); - main_panel->set_status_text(format("Turnout %d switched", turnout.get_address())); - } - catch(const TurnoutBusy &e) + Block &block = layout.get_block_by_track(track); + if(block.get_train() && !block.get_train()->free_block(block)) + main_panel->set_status_text("Turnout busy"); + else { - main_panel->set_status_text(e.what()); + unsigned paths = track.get_type().get_paths(); + unsigned i = track.get_active_path()+1; + while(!(paths&(1<>i)) + i = 0; + else + ++i; + } + track.set_active_path(i); } } - else if(simulate) + /*else if(simulate) { if(unsigned sid=track->get_track().get_sensor_id()) { Sensor &sensor = control.get_sensor(sid); control.signal_sensor_event.emit(sid, !sensor.get_state()); } - } + }*/ } } } -void Engineer::button_release(int, int, unsigned, unsigned) -{ -} - void Engineer::pointer_motion(int x, int y) { if(placing_train) { - Track3D *track = pick_track(x, screen_h-y-1); - if(track && placing_train) + Track3D *track = pick_track(x, window.get_height()-y-1); + if(track) { - Block &block = trfc_mgr->get_block_by_track(track->get_track()); + Block &block = layout.get_block_by_track(track->get_track()); if(&block!=placing_block) { if(placing_block) reset_block_color(*placing_block); placing_block = █ placing_entry = 0; - set_block_color(*placing_block, GL::Color(0.5, 1, 0.7)); + if(!placing_block->get_train()) + set_block_color(*placing_block, GL::Color(0)); } } - else if(track && track->get_track().get_turnout_id()) - main_panel->set_status_text(format("Turnout %d", track->get_track().get_turnout_id())); - else if(!placing_train) - main_panel->set_status_text(string()); } } @@ -402,10 +263,11 @@ void Engineer::view_all() { const list &tracks = layout_3d.get_tracks(); - cam_rot = 0; - float best_height = -1; - float mid_x = 0; - float mid_y = 0; + float view_aspect = float(window.get_width()-200)/window.get_height(); + float view_height = tan(camera.get_field_of_view()/2)*2; + float best_score = 0; + GL::Vector3 pos; + GL::Vector3 up; for(float angle=0; anglebest_score) { - cam_rot = angle; - best_height = height; - mid_x = (min_x+max_x)/2; - mid_y = (min_y+max_y)/2; + best_score = score; + + float size = max(width/view_aspect, height); + float c = cos(angle); + float s = sin(angle); + float x = (min_x+max_x)/2-size*105/window.get_height(); + float y = (min_y+max_y)/2; + float z = max(size*1.05/view_height, 0.15); + + pos = GL::Vector3(c*x-s*y, s*x+c*y, z); + up = GL::Vector3(-s, c, 0); } } - float c = cos(cam_rot); - float s = sin(cam_rot); - cam_pos.x = c*mid_x-s*mid_y; - cam_pos.y = s*mid_x+c*mid_y; - cam_pos.z = max(best_height*1.05/0.82843, 0.15); + camera.set_position(pos); + camera.set_up_direction(up); + camera.set_look_direction(GL::Vector3(0, 0, -1)); } void Engineer::set_block_color(const Block &block, const GL::Color &color) { const set &tracks = block.get_tracks(); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - layout_3d.get_track(**i).set_color(color); + layout_3d.get_track(**i).get_path().set_color(color); } void Engineer::reset_block_color(const Block &block) { + bool active = false; if(unsigned sid=block.get_sensor_id()) - { - Sensor &sensor = control.get_sensor(sid); - if(sensor.get_state()) - { - set_block_color(block, GL::Color(1, 0.5, 0.3)); - return; - } - } + active = layout.get_driver().get_sensor(sid); if(block.get_train()) - set_block_color(block, GL::Color(1, 1, 0.3)); + { + GL::Color color; + map::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, 1, 1)); + set_block_color(block, GL::Color(1)); } -void Engineer::sensor_event(bool, Sensor *sensor) +void Engineer::sensor_event(unsigned addr, bool) { - const list &blocks = trfc_mgr->get_blocks(); - for(list::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) - if((*i)->get_sensor_id()==sensor->get_address()) + const set &blocks = layout.get_blocks(); + for(set::const_iterator i=blocks.begin(); i!=blocks.end(); ++i) + if((*i)->get_sensor_id()==addr) reset_block_color(**i); } @@ -480,25 +354,14 @@ void Engineer::block_reserved(const Block &block, const Train *) reset_block_color(block); } -void Engineer::project_3d() -{ - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - float offset = 200.0/screen_w*0.055228; - glFrustum(-0.055228-offset, 0.055228-offset, -0.041421, 0.041421, 0.1, 10); - glMatrixMode(GL_MODELVIEW); -} - Track3D *Engineer::pick_track(int x, int y) { - float xx = (static_cast(x-static_cast(screen_w)/2-100)/screen_h)*0.82843; - float yy = (static_cast(y)/screen_h-0.5)*0.82843; - float size = 4.0/screen_h*0.82843; + 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; - project_3d(); - glLoadIdentity(); - glRotatef(-cam_rot*180/M_PI, 0, 0, 1); - glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z); + camera.apply(); return layout_3d.pick_track(xx, yy, size); } @@ -514,7 +377,36 @@ void Engineer::train_added(Train &train) train_panels.push_back(tpanel); tpanel->set_visible(true); - place_train(train); + Train3D &t3d = layout_3d.get_train(train); + overlay->set_label(t3d, train.get_name()); + train.signal_name_changed.connect(sigc::bind<0>(sigc::mem_fun(overlay, &Overlay3D::set_label), sigc::ref(t3d))); + + GL::Color best_color; + float best_d_sq = 0; + for(unsigned i=0; i<10; ++i) + { + GL::Color color; + color.r = rand()*1.0/RAND_MAX; + color.g = rand()*1.0/RAND_MAX; + color.b = rand()*1.0/RAND_MAX; + color = color*(1/max(max(color.r, color.g), color.b)); + float min_d_sq = 3; + for(map::const_iterator j=train_colors.begin(); j!=train_colors.end(); ++j) + { + float dr = color.r-j->second.r; + float dg = color.g-j->second.g; + float db = color.b-j->second.b; + float d_sq = dr*dr+dg*dg+db*db; + if(d_sqbest_d_sq) + { + best_color = color; + best_d_sq = min_d_sq; + } + } + train_colors[&train] = best_color; } void Engineer::sighandler(int sig) @@ -523,14 +415,12 @@ void Engineer::sighandler(int sig) { signal(sig, SIG_DFL); IO::print(IO::cerr, "Fatal signal received, terminating\n"); - const map &locos = control.get_locomotives(); - for(map::const_iterator i=locos.begin(); i!=locos.end(); ++i) + const map &trains = layout.get_trains(); + for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) i->second->set_speed(0); - control.flush(); + layout.get_driver().flush(); raise(sig); } else if(sig==SIGTERM || sig==SIGINT) exit(0); } - -Application::RegApp Engineer::reg;