]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/layout.cpp
Foundations of using physics simulation for trains
[r2c2.git] / source / 3d / layout.cpp
index 1b54e4be7509dd487d519d31a14d827557bfb801..70c55096cfa91eed259b6be9ebd99ef701dfd88c 100644 (file)
@@ -7,11 +7,15 @@ Distributed under the GPL
 
 #include <algorithm>
 #include <limits>
+#include <msp/gl/clip.h>
+#include <msp/gl/matrix.h>
 #include <msp/gl/rendermode.h>
 #include <msp/gl/select.h>
 #include <msp/gl/texture.h>
 #include <msp/datafile/parser.h>
 #include "layout.h"
+#include "track.h"
+#include "vehicle.h"
 
 using namespace std;
 using namespace Msp;
@@ -20,22 +24,23 @@ namespace Marklin {
 
 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));
+       layout.signal_vehicle_added.connect(sigc::mem_fun(this, &Layout3D::vehicle_added));
+
+       const set<Track *> &ltracks = layout.get_tracks();
+       for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
+               track_added(**i);
 }
 
 Layout3D::~Layout3D()
 {
        while(!tracks.empty())
                delete tracks.front();
-}
-
-void Layout3D::set_quality(unsigned q)
-{
-       quality = q;
+       while(!vehicles.empty())
+               delete vehicles.front();
 }
 
 void Layout3D::add_track(Track3D &t)
@@ -65,47 +70,28 @@ Track3D *Layout3D::pick_track(float x, float y, float size) const
        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);
+       {
+               GL::PushMatrix push_mat;
+               GL::load_identity();
 
-       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();
+               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);
 
-       glDisable(GL_CLIP_PLANE0);
-       glDisable(GL_CLIP_PLANE1);
-       glDisable(GL_CLIP_PLANE2);
-       glDisable(GL_CLIP_PLANE3);
+       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<unsigned>::max();
        for(vector<GL::SelectRecord>::iterator i=select_buf.begin(); i!=select_buf.end(); ++i)
-               if(i->min_depth<track_depth)
+               if(i->min_depth<track_depth && !i->names.empty())
                {
                        track = reinterpret_cast<Track3D *>(i->names.back());
                        track_depth = i->min_depth;
@@ -114,6 +100,27 @@ Track3D *Layout3D::pick_track(float x, float y, float size) const
        return track;
 }
 
+void Layout3D::add_vehicle(Vehicle3D &v)
+{
+       vehicles.push_back(&v);
+}
+
+void Layout3D::remove_vehicle(Vehicle3D &v)
+{
+       list<Vehicle3D *>::iterator i = find(vehicles.begin(), vehicles.end(), &v);
+       if(i!=vehicles.end())
+               vehicles.erase(i);
+}
+
+Vehicle3D &Layout3D::get_vehicle(const Vehicle &v) const
+{
+       for(list<Vehicle3D *>::const_iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
+               if(&(*i)->get_vehicle()==&v)
+                       return **i;
+
+       throw KeyError("Unknown vehicle");
+}
+
 void Layout3D::track_added(Track &t)
 {
        new Track3D(*this, t);
@@ -129,4 +136,9 @@ void Layout3D::track_removed(Track &t)
                }
 }
 
+void Layout3D::vehicle_added(Vehicle &v)
+{
+       new Vehicle3D(*this, v);
+}
+
 } // namespace Marklin