]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/track.cpp
Use GL::Renderables and a Pipeline for rendering
[r2c2.git] / source / 3d / track.cpp
index c3eb388dc8bb4a96b35a4a797319cf53db8dd949..32857961d9ecc54134bb4f352c7136783f3098a6 100644 (file)
@@ -6,9 +6,10 @@ Distributed under the GPL
 */
 
 #include <cmath>
-#include <GL/gl.h>
+#include <msp/gl/matrix.h>
 #include <msp/gl/misc.h>
 #include "libmarklin/tracktype.h"
+#include "endpoint.h"
 #include "layout.h"
 #include "track.h"
 #include "tracktype.h"
@@ -23,7 +24,23 @@ Track3D::Track3D(Layout3D &l, Track &t):
        track(t),
        type(layout.get_catalogue().get_track(track.get_type())),
        color(1, 1, 1)
-{ }
+{
+       layout.add_track(*this);
+       layout.get_scene().add(*this);
+
+       const vector<Endpoint> &type_eps = track.get_type().get_endpoints();
+       for(unsigned i=0; i<type_eps.size(); ++i)
+               endpoints.push_back(new Endpoint3D(*this, i));
+}
+
+Track3D::~Track3D()
+{
+       layout.remove_track(*this);
+       layout.get_scene().remove(*this);
+
+       for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
+               delete *i;
+}
 
 void Track3D::set_color(const Msp::GL::Color &c)
 {
@@ -42,6 +59,8 @@ void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
        maxp.x += c*pos.x-s*pos.y;
        minp.y += s*pos.x+c*pos.y;
        maxp.y += s*pos.x+c*pos.y;
+       minp.z += pos.z;
+       maxp.z += pos.z;
 
        float slope = track.get_slope();
        if(slope>0)
@@ -50,96 +69,22 @@ void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
                minp.z += slope;
 }
 
-void Track3D::render() const
-{
-       prepare_render(true);
-
-       glPushName(reinterpret_cast<unsigned>(this));
-
-       type.render();
-
-       glPopName();
-       glPopMatrix();
-}
-
-void Track3D::render_endpoints() const
-{
-       prepare_render(false);
-
-       const vector<Endpoint> &endpoints = track.get_type().get_endpoints();
-       for(unsigned i=0; i<endpoints.size(); ++i)
-       {
-               const Endpoint &ep = endpoints[i];
-               GL::set(GL_CULL_FACE, track.get_link(i));
-               if(track.get_link(i))
-                       glColor4f(0.5, 0, 1, 0.5);
-               else
-                       glColor4f(1, 0, 0.5, 0.5);
-
-               float c = cos(ep.dir);
-               float s = sin(ep.dir);
-               float z = (i==1 ? track.get_slope() : 0);
-
-               glBegin(GL_QUADS);
-               glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z);
-               glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z);
-               glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z+0.02);
-               glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z+0.02);
-               glEnd();
-       }
-
-       if(abs(track.get_slope())>1e-4)
-       {
-               Point p;
-               if(track.get_slope()>0)
-               {
-                       p = endpoints[1].pos;
-                       p.z += track.get_slope();
-               }
-               else
-                       p = endpoints[0].pos;
-               glBegin(GL_TRIANGLE_STRIP);
-               for(unsigned i=0; i<=16; ++i)
-               {
-                       float c = cos(i*M_PI/8);
-                       float s = sin(i*M_PI/8);
-                       glNormal3f(c, s, 0);
-                       glVertex3f(p.x+c*0.01, p.y+s*0.01, p.z);
-                       glVertex3f(p.x+c*0.01, p.y+s*0.01, -track.get_position().z);
-               }
-               glEnd();
-       }
-
-       glPopMatrix();
-}
-
-void Track3D::render_path(int path) const
+void Track3D::render(const GL::Tag &tag) const
 {
-       prepare_render(true);
-
-       (void)path;
-       /*varray.apply();
-       if(path>=0 && static_cast<unsigned>(path)<path_seq.size())
-               glDrawElements(GL_QUADS, path_seq[path].size(), GL_UNSIGNED_INT, &path_seq[path][0]);
-       else
-       {
-               for(unsigned i=0; i<path_seq.size(); ++i)
-                       glDrawElements(GL_QUADS, path_seq[i].size(), GL_UNSIGNED_INT, &path_seq[i][0]);
-       }*/
-
-       glPopMatrix();
-}
+       GL::PushMatrix push_mat;
 
-void Track3D::prepare_render(bool slope) const
-{
        const Point &pos = track.get_position();
        float rot = track.get_rotation();
 
-       glPushMatrix();
        glTranslatef(pos.x, pos.y, pos.z);
        glRotatef(rot*180/M_PI, 0, 0, 1);
-       if(slope)
-               glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
+       glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
+
+       glPushName(reinterpret_cast<unsigned>(this));
+
+       type.render(tag);
+
+       glPopName();
 }
 
 } // namespace Marklin