X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2F3d%2Flayout.cpp;h=29e6bb40daeb4a45dd686871bae173122eebf4dd;hb=6aa6e3a60c31d71e8f8117be1ddf547897d19bd8;hp=1a557153fda308ce56a18066e281ca2e3e1b8830;hpb=651698847d5293cfb15b6fb23a394701388c0151;p=r2c2.git diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index 1a55715..29e6bb4 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -1,142 +1,104 @@ -/* $Id$ - -This file is part of the MSP Märklin suite -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - -#include -#include -#include -#include -#include -#include -#include -#include +#include "beamgate.h" #include "layout.h" +#include "signal.h" +#include "terrain.h" +#include "track.h" +#include "utility.h" +#include "vehicle.h" using namespace std; using namespace Msp; -namespace Marklin { +namespace R2C2 { Layout3D::Layout3D(Layout &l): layout(l), catalogue(layout.get_catalogue()) { - 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_train_added.connect(sigc::mem_fun(this, &Layout3D::train_added)); - - const set <racks = layout.get_tracks(); - for(set::iterator i=ltracks.begin(); i!=ltracks.end(); ++i) - track_added(**i); + // South, 15° from zenith + sun.set_position(GL::Vector4(0, -0.259, 0.966, 0)); + sun.set_diffuse(GL::Color(0.7)); + lighting.set_ambient(GL::Color(0.2)); + lighting.attach(0, sun); + + layout.signal_object_added.connect(sigc::mem_fun(this, &Layout3D::object_added)); + layout.signal_object_removed.connect(sigc::mem_fun(this, &Layout3D::object_removed)); + + const set &lobjs = layout.get_all(); + for(set::iterator i=lobjs.begin(); i!=lobjs.end(); ++i) + object_added(**i); } Layout3D::~Layout3D() { - while(!tracks.empty()) - delete tracks.front(); - while(!trains.empty()) - delete trains.front(); + while(!utilities.empty()) + delete *utilities.begin(); + while(!objects.empty()) + delete objects.begin()->second; } -void Layout3D::add_track(Track3D &t) +void Layout3D::add(Object3D &o) { - tracks.push_back(&t); + insert_unique(objects, &o.get_object(), &o); } -void Layout3D::remove_track(Track3D &t) +Object3D &Layout3D::get_3d(Object &o) const { - list::iterator i = find(tracks.begin(), tracks.end(), &t); - if(i!=tracks.end()) - tracks.erase(i); + return *get_item(objects, &o); } -Track3D &Layout3D::get_track(const Track &t) const +void Layout3D::remove(Object3D &o) { - for(list::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - if(&(*i)->get_track()==&t) - return **i; - - throw KeyError("Unknown track"); + objects.erase(&o.get_object()); } -Track3D *Layout3D::pick_track(float x, float y, float size) const +void Layout3D::add(Utility3D &u) { - vector select_buf; - GL::select_buffer(select_buf); - GL::render_mode(GL::SELECT); - - { - 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); - - GL::ClipPlane::disable(0); - GL::ClipPlane::disable(1); - GL::ClipPlane::disable(2); - GL::ClipPlane::disable(3); - - 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; - } - - return track; -} - -void Layout3D::add_train(Train3D &t) -{ - trains.push_back(&t); + utilities.insert(&u); } -void Layout3D::remove_train(Train3D &t) +void Layout3D::remove(Utility3D &u) { - list::iterator i = find(trains.begin(), trains.end(), &t); - if(i!=trains.end()) - trains.erase(i); -} - -Train3D &Layout3D::get_train(const Train &t) const -{ - for(list::const_iterator i=trains.begin(); i!=trains.end(); ++i) - if(&(*i)->get_train()==&t) - return **i; - - throw KeyError("Unknown train"); + utilities.erase(&u); } -void Layout3D::track_added(Track &t) +void Layout3D::tick() { - new Track3D(*this, t); + Time::TimeDelta t = layout.get_clock().get_current_time(); + Angle time_of_day = Angle::from_turns(t/Time::day); + Angle latitude = Angle::from_degrees(52.5); + Angle axial_tilt = Angle::from_degrees(23.45); + Transform trans = Transform::rotation(Angle::quarter_turn()-latitude, Vector(-1, 0, 0))* + Transform::rotation(Angle::half_turn()-time_of_day, Vector(0, 0, 1))* + Transform::rotation(axial_tilt, Vector(-1, 0, 0)); + Vector sun_dir = trans.transform_linear(Vector(0, -1, 0)); + Vector diff = Vector(sun.get_position())-sun_dir; + if(diff.norm()>0.0025f) + sun.set_position(GL::Vector4(sun_dir, 0.0f)); + // TODO replace these very unscientific guesses with correct formulas + lighting.set_ambient(GL::Color(pow(0.2f*(sun_dir.z+1.0f), 2.0f))); + sun.set_diffuse(GL::Color(0.7f*max(sun_dir.z, 0.0f))); } -void Layout3D::track_removed(Track &t) +void Layout3D::object_added(Object &o) { - for(list::iterator i=tracks.begin(); i!=tracks.end(); ++i) - if(&(*i)->get_track()==&t) - { - delete *i; - return; - } + if(Track *t = dynamic_cast(&o)) + new Track3D(*this, *t); + else if(Signal *s = dynamic_cast(&o)) + new Signal3D(*this, *s); + else if(Vehicle *v = dynamic_cast(&o)) + new Vehicle3D(*this, *v); + else if(BeamGate *g = dynamic_cast(&o)) + new BeamGate3D(*this, *g); + else if(Terrain *r = dynamic_cast(&o)) + new Terrain3D(*this, *r); } -void Layout3D::train_added(Train &t) +void Layout3D::object_removed(Object &o) { - new Train3D(*this, t); + ObjectMap::iterator i = objects.find(&o); + if(i!=objects.end()) + delete i->second; } -} // namespace Marklin +} // namespace R2C2