-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
-#include <algorithm>
-#include <limits>
-#include <msp/gl/rendermode.h>
-#include <msp/gl/select.h>
-#include <msp/gl/texture.h>
-#include <msp/datafile/parser.h>
+#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),
- catalogue(layout.get_catalogue()),
- 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);
+ 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<Object *> &lobjs = layout.get_all<Object>();
+ for(set<Object *>::iterator i=lobjs.begin(); i!=lobjs.end(); ++i)
+ object_added(**i);
}
Layout3D::~Layout3D()
{
- for(list<Track3D *>::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::get_bounds(Vector &minp, Vector &maxp) const
+{
+ Geometry::BoundingBox<float, 3> 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);
}
-void Layout3D::set_quality(unsigned q)
+Object3D &Layout3D::get(Object &o) const
{
- quality = q;
+ 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<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
- (*i)->render();
-
- if(endpoints)
- {
- glDepthMask(false);
- for(list<Track3D *>::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<Track3D *>::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<GL::SelectRecord> 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<unsigned>::max();
- for(vector<GL::SelectRecord>::iterator i=select_buf.begin(); i!=select_buf.end(); ++i)
- if(i->min_depth<track_depth)
- {
- track = reinterpret_cast<Track3D *>(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(*this, t));
+ if(Track *t = dynamic_cast<Track *>(&o))
+ new Track3D(*this, *t);
+ else if(Signal *s = dynamic_cast<Signal *>(&o))
+ new Signal3D(*this, *s);
+ else if(Vehicle *v = dynamic_cast<Vehicle *>(&o))
+ new Vehicle3D(*this, *v);
+ else if(BeamGate *g = dynamic_cast<BeamGate *>(&o))
+ new BeamGate3D(*this, *g);
}
-void Layout3D::track_removed(Track &t)
+void Layout3D::object_removed(Object &o)
{
- for(list<Track3D *>::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