X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2F3d%2Flayout.cpp;h=2881e8d33c82ed23d8c5c6edcffa09e684b8f8fa;hb=495c54e187ddbb7514c983e1829a69d82ccad5f3;hp=b95d4e59c835798d2df81530d060e6e7303bff7d;hpb=c0c5a34d1056eabdebd350da3534e24c902c0dac;p=r2c2.git diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index b95d4e5..2881e8d 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -1,32 +1,31 @@ /* $Id$ -This file is part of the MSP Märklin suite -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa +This file is part of R²C² +Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa Distributed under the GPL */ -#include -#include -#include -#include -#include -#include -#include -#include #include "layout.h" +#include "track.h" +#include "vehicle.h" using namespace std; using namespace Msp; -namespace Marklin { +namespace R2C2 { Layout3D::Layout3D(Layout &l): layout(l), - catalogue(layout.get_catalogue()), - quality(4) + catalogue(layout.get_catalogue()) { + // South, 15° from zenith + sun.set_position(0, -0.259, 0.966, 0); + lighting.attach(0, sun); + layout.signal_track_added.connect(sigc::mem_fun(this, &Layout3D::track_added)); layout.signal_track_removed.connect(sigc::mem_fun(this, &Layout3D::track_removed)); + layout.signal_vehicle_added.connect(sigc::mem_fun(this, &Layout3D::vehicle_added)); + layout.signal_vehicle_removed.connect(sigc::mem_fun(this, &Layout3D::vehicle_removed)); const set <racks = layout.get_tracks(); for(set::iterator i=ltracks.begin(); i!=ltracks.end(); ++i) @@ -36,69 +35,69 @@ Layout3D::Layout3D(Layout &l): Layout3D::~Layout3D() { while(!tracks.empty()) - delete tracks.front(); + delete tracks.begin()->second; + while(!vehicles.empty()) + delete vehicles.begin()->second; } -void Layout3D::set_quality(unsigned q) +void Layout3D::get_bounds(Vector &minp, Vector &maxp) const { - quality = q; + minp = maxp = Vector(); + + for(TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + { + Vector tmin; + Vector tmax; + i->second->get_bounds(0, tmin, tmax); + minp.x = min(minp.x, tmin.x); + minp.y = min(minp.y, tmin.y); + maxp.x = max(maxp.x, tmax.x); + maxp.y = max(maxp.y, tmax.y); + } } void Layout3D::add_track(Track3D &t) { - tracks.push_back(&t); + if(tracks.count(&t.get_track())) + throw KeyError("Duplicate track"); + + tracks[&t.get_track()] = &t; } void Layout3D::remove_track(Track3D &t) { - list::iterator i = find(tracks.begin(), tracks.end(), &t); - if(i!=tracks.end()) - tracks.erase(i); + tracks.erase(&t.get_track()); } -Track3D &Layout3D::get_track(const Track &t) const +Track3D &Layout3D::get_track(Track &t) const { - for(list::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - if(&(*i)->get_track()==&t) - return **i; + TrackMap::const_iterator i = tracks.find(&t); + if(i==tracks.end()) + throw KeyError("Unknown track"); - throw KeyError("Unknown track"); + return *i->second; } -Track3D *Layout3D::pick_track(float x, float y, float size) const +void Layout3D::add_vehicle(Vehicle3D &v) { - vector select_buf; - GL::select_buffer(select_buf); - GL::render_mode(GL::SELECT); + if(vehicles.count(&v.get_vehicle())) + throw KeyError("Duplicate vehicle"); - { - GL::PushMatrix push_mat; - GL::load_identity(); - - GL::ClipPlane(1, 0, x-size, 0).apply_to(0); - GL::ClipPlane(-1, 0, -x-size, 0).apply_to(1); - GL::ClipPlane(0, 1, y-size, 0).apply_to(2); - GL::ClipPlane(0, -1, -y-size, 0).apply_to(3); - } - - scene.render(0); + vehicles[&v.get_vehicle()] = &v; +} - GL::ClipPlane::disable(0); - GL::ClipPlane::disable(1); - GL::ClipPlane::disable(2); - GL::ClipPlane::disable(3); +void Layout3D::remove_vehicle(Vehicle3D &v) +{ + vehicles.erase(&v.get_vehicle()); +} - GL::render_mode(GL::RENDER); - Track3D *track = 0; - unsigned track_depth = numeric_limits::max(); - for(vector::iterator i=select_buf.begin(); i!=select_buf.end(); ++i) - if(i->min_depth(i->names.back()); - track_depth = i->min_depth; - } +Vehicle3D &Layout3D::get_vehicle(Vehicle &v) const +{ + VehicleMap::const_iterator i = vehicles.find(&v); + if(i==vehicles.end()) + throw KeyError("Unknown vehicle"); - return track; + return *i->second; } void Layout3D::track_added(Track &t) @@ -108,12 +107,21 @@ void Layout3D::track_added(Track &t) void Layout3D::track_removed(Track &t) { - for(list::iterator i=tracks.begin(); i!=tracks.end(); ++i) - if(&(*i)->get_track()==&t) - { - delete *i; - return; - } + TrackMap::iterator i = tracks.find(&t); + if(i!=tracks.end()) + delete i->second; +} + +void Layout3D::vehicle_added(Vehicle &v) +{ + new Vehicle3D(*this, v); +} + +void Layout3D::vehicle_removed(Vehicle &v) +{ + VehicleMap::iterator i = vehicles.find(&v); + if(i!=vehicles.end()) + delete i->second; } -} // namespace Marklin +} // namespace R2C2