X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2F3d%2Flayout.cpp;h=59a23c935aab6be2f3e41c5e53c4e383e3975f14;hb=05b95b6b6b095821f1e79dabed802b853c296c9d;hp=dcf5544e8dfb8435d793dc7aa98fd300971c9ace;hpb=f42183985c65e1e12f19e9246dee90b8e7e44a34;p=r2c2.git diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index dcf5544..59a23c9 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -1,139 +1,94 @@ -/* $Id$ - -This file is part of the MSP Märklin suite -Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - -#include -#include -#include -#include -#include -#include +#include "beamgate.h" #include "layout.h" +#include "signal.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), - quality(4) + 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)); + // South, 15° from zenith + sun.set_position(0, -0.259, 0.966, 0); + sun.set_diffuse(GL::Color(0.9)); + lighting.set_ambient(GL::Color(0.4)); + 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() { - for(list::iterator i=tracks.begin(); i!=tracks.end(); ++i) - delete *i; + while(!utilities.empty()) + delete *utilities.begin(); + while(!objects.empty()) + delete objects.begin()->second; } -void Layout3D::set_quality(unsigned q) +void Layout3D::get_bounds(Vector &minp, Vector &maxp) const { - quality = q; - for(list::iterator i=tracks.begin(); i!=tracks.end(); ++i) - (*i)->set_quality(quality); + Geometry::BoundingBox bbox; + + for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) + bbox = bbox|i->second->get_object().get_type().get_shape()->get_axis_aligned_bounding_box(); + + minp = bbox.get_minimum_point(); + maxp = bbox.get_maximum_point(); +} + +void Layout3D::add(Object3D &o) +{ + insert_unique(objects, &o.get_object(), &o); +} + +Object3D &Layout3D::get_3d(Object &o) const +{ + return *get_item(objects, &o); } -void Layout3D::render(bool endpoints) const +void Layout3D::remove(Object3D &o) { - GL::Texture::unbind(); - glEnable(GL_DEPTH_TEST); - - for(list::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - (*i)->render(); - - if(endpoints) - { - glDepthMask(false); - for(list::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - (*i)->render_endpoints(); - glDepthMask(true); - } + objects.erase(&o.get_object()); } -Track3D &Layout3D::get_track(const Track &t) const +void Layout3D::add(Utility3D &u) { - for(list::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - if(&(*i)->get_track()==&t) - return **i; - - throw KeyError("Unknown track"); + utilities.insert(&u); } -Track3D *Layout3D::pick_track(float x, float y, float size) const +void Layout3D::remove(Utility3D &u) { - vector select_buf; - GL::select_buffer(select_buf); - GL::render_mode(GL::SELECT); - - glPushMatrix(); - glLoadIdentity(); - - double clip[4]; - clip[0] = 1; - clip[1] = 0; - clip[2] = x-size; - clip[3] = 0; - glClipPlane(GL_CLIP_PLANE0, clip); - glEnable(GL_CLIP_PLANE0); - - clip[0] = -1; - clip[2] = -(x+size); - glClipPlane(GL_CLIP_PLANE1, clip); - glEnable(GL_CLIP_PLANE1); - - clip[0] = 0; - clip[1] = 1; - clip[2] = y-size; - glClipPlane(GL_CLIP_PLANE2, clip); - glEnable(GL_CLIP_PLANE2); - - clip[1] = -1; - clip[2] = -(y+size); - glClipPlane(GL_CLIP_PLANE3, clip); - glEnable(GL_CLIP_PLANE3); - - glPopMatrix(); - - render(); - - glDisable(GL_CLIP_PLANE0); - glDisable(GL_CLIP_PLANE1); - glDisable(GL_CLIP_PLANE2); - glDisable(GL_CLIP_PLANE3); - - 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; + utilities.erase(&u); } -void Layout3D::track_added(Track &t) +void Layout3D::object_added(Object &o) { - tracks.push_back(new Track3D(t, quality)); + 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); } -void Layout3D::track_removed(Track &t) +void Layout3D::object_removed(Object &o) { - for(list::iterator i=tracks.begin(); i!=tracks.end(); ++i) - if(&(*i)->get_track()==&t) - { - delete *i; - tracks.erase(i); - return; - } + ObjectMap::iterator i = objects.find(&o); + if(i!=objects.end()) + delete i->second; } -} // namespace Marklin +} // namespace R2C2