]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/track.cpp
Don't crash if a train has no router
[r2c2.git] / source / 3d / track.cpp
index 5857e52d8813b8c29053e7dd107d882d7bf0f35c..549bc156ba6dce2de6babcb6db4889441ce4b221 100644 (file)
-/* $Id$
-
-This file is part of the MSP Märklin suite
-Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
 #include <cmath>
-#include <GL/gl.h>
-#include <msp/gl/misc.h>
-#include "libmarklin/tracktype.h"
+#include <msp/gl/matrix.h>
+#include <msp/gl/renderer.h>
+#include "libr2c2/tracktype.h"
+#include "endpoint.h"
+#include "layout.h"
+#include "path.h"
 #include "track.h"
+#include "tracktype.h"
 
 using namespace std;
 using namespace Msp;
 
-namespace Marklin {
+namespace R2C2 {
 
-Track3D::Track3D(Track &t, unsigned q):
+Track3D::Track3D(Layout3D &l, Track &t):
+       Object3D(l, t),
+       GL::ObjectInstance(l.get_catalogue().get_3d(t.get_type()).get_object()),
        track(t),
-       color(1, 1, 1),
-       varray((GL::NORMAL3, GL::VERTEX3)),
-       quality(q)
-{
-       build_object();
-}
-
-void Track3D::set_quality(unsigned q)
+       type(layout.get_catalogue().get_3d(track.get_type()))
 {
-       quality=q;
-       build_object();
-}
-
-void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
-{
-       const Point &pos=track.get_position();
-       float rot=track.get_rotation();
-
-       float c=cos(-angle);
-       float s=sin(-angle);
-
-       minp.x=maxp.x=c*pos.x-s*pos.y;
-       minp.y=maxp.y=s*pos.x+c*pos.y;
-
-       float c2=cos(rot-angle);
-       float s2=sin(rot-angle);
+       layout.get_scene().add(*this);
 
-       for(vector<Point>::const_iterator i=border.begin(); i!=border.end(); ++i)
+       const vector<TrackType::Endpoint> &type_eps = track.get_type().get_endpoints();
+       const vector<Track *> &links = track.get_links();
+       for(unsigned i=0; i<type_eps.size(); ++i)
        {
-               float x=c*pos.x-s*pos.y + c2*i->x-s2*i->y;
-               float y=s*pos.x+c*pos.y + s2*i->x+c2*i->y;
-
-               minp.x=min(minp.x, x);
-               minp.y=min(minp.y, y);
-               maxp.x=max(maxp.x, x);
-               maxp.y=max(maxp.y, y);
-       }
-}
-
-void Track3D::render() const
-{
-       prepare_render();
-
-       glPushName(reinterpret_cast<unsigned>(this));
-
-       varray.apply();
-       glColor4f(0.25*color.r, 0.25*color.g, 0.25*color.b, 1);
-       glDrawElements(GL_QUADS, base_seq.size(), GL_UNSIGNED_INT, &base_seq[0]);
-       if(quality>1)
-       {
-               glColor4f(0.85*color.r, 0.85*color.g, 0.85*color.b, 1);
-               glDrawElements(GL_QUADS, rail_seq.size(), GL_UNSIGNED_INT, &rail_seq[0]);
-       }
-
-       glPopName();
-       glPopMatrix();
-}
-
-void Track3D::render_endpoints() const
-{
-       prepare_render();
-
-       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);
+               if(!links[i] || links[i]>&track)
+                       endpoints.push_back(new Endpoint3D(*this, i));
                else
-                       glColor4f(1, 0, 0.5, 0.5);
-
-               float c=cos(ep.dir);
-               float s=sin(ep.dir);
-
-               glBegin(GL_QUADS);
-               glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, 0);
-               glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, 0);
-               glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, 0.02);
-               glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, 0.02);
-               glEnd();
+                       endpoints.push_back(0);
        }
 
-       glPopMatrix();
+       track.signal_link_changed.connect(sigc::mem_fun(this, &Track3D::link_changed));
 }
 
-void Track3D::render_route(int route) const
+Track3D::~Track3D()
 {
-       prepare_render();
-
-       varray.apply();
-       if(route>=0 && static_cast<unsigned>(route)<route_seq.size())
-               glDrawElements(GL_QUADS, route_seq[route].size(), GL_UNSIGNED_INT, &route_seq[route][0]);
-       else
-       {
-               for(unsigned i=0; i<route_seq.size(); ++i)
-                       glDrawElements(GL_QUADS, route_seq[i].size(), GL_UNSIGNED_INT, &route_seq[i][0]);
-       }
+       layout.get_scene().remove(*this);
 
-       glPopMatrix();
+       for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
+               delete *i;
 }
 
-void Track3D::prepare_render() const
+Vector Track3D::get_node() const
 {
-       const Point &pos=track.get_position();
-       float rot=track.get_rotation();
+       Geometry::BoundingBox<float, 3> bbox = track.get_type().get_shape()->get_axis_aligned_bounding_box();
+       const Vector &minp = bbox.get_minimum_point();
+       const Vector &maxp = bbox.get_maximum_point();
 
-       glPushMatrix();
-       glTranslatef(pos.x, pos.y, pos.z);
-       glRotatef(rot*180/M_PI, 0, 0, 1);
+       return matrix*((minp+maxp)/2.0f+Vector(0, 0, 0.02));
 }
 
-void Track3D::build_object()
+void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const
 {
-       varray.clear();
-       GL::VertexArrayBuilder builder(varray);
-
-       base_seq.clear();
-       rail_seq.clear();
-       route_seq.clear();
-       route_seq.resize(track.get_type().get_n_routes());
-
-       const vector<TrackPart> &parts=track.get_type().get_parts();
-       unsigned index=0;
-       for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
-               build_part(*i, builder, index);
+       renderer.matrix_stack() *= matrix;
 }
 
-void Track3D::build_part(const TrackPart &part, GL::VertexArrayBuilder &va_builder, unsigned &base_index)
+void Track3D::link_changed(unsigned i, Track *trk)
 {
-       static vector<Point> profile;
-       if(profile.empty())
-       {
-               profile.push_back(Point(0, -0.02, 0));
-               profile.push_back(Point(0, -0.014, 0.008));
-               profile.push_back(Point(0, -0.014, 0.008));
-               profile.push_back(Point(0, 0.014, 0.008));
-               profile.push_back(Point(0, 0.014, 0.008));
-               profile.push_back(Point(0, 0.02, 0));
-               for(unsigned i=0; i<2; ++i)
-               {
-                       profile.push_back(Point(0, -0.009+i*0.017, 0.008));
-                       profile.push_back(Point(0, -0.009+i*0.017, 0.0103));
-                       profile.push_back(Point(0, -0.009+i*0.017, 0.0103));
-                       profile.push_back(Point(0, -0.008+i*0.017, 0.0103));
-                       profile.push_back(Point(0, -0.008+i*0.017, 0.0103));
-                       profile.push_back(Point(0, -0.008+i*0.017, 0.008));
-               }
-               profile.push_back(Point(0, -0.002, 0.012));
-               profile.push_back(Point(0, 0.002, 0.012));
-       }
-       static unsigned psize=profile.size();
-
-       unsigned nsegs=(part.radius ? static_cast<unsigned>(part.length*(1<<quality))+1 : 1);
-       float plen=part.length;
-       if(part.radius)
-               plen*=abs(part.radius);
-
-       for(unsigned i=0; i<=nsegs; ++i)
+       if(!trk || trk>&track)
        {
-               float a=part.dir+(part.radius ? i*plen/nsegs/part.radius : 0);
-               float c=cos(a);
-               float s=sin(a);
-               Point p=part.get_point(i*plen/nsegs);
-
-               for(unsigned j=0; j<profile.size(); ++j)
-               {
-                       unsigned k=j&~1;
-                       float dy=profile[k+1].y-profile[k].y;
-                       float dz=profile[k+1].z-profile[k].z;
-                       float d=sqrt(dy*dy+dz*dz);
-                       va_builder.normal(s*dz/d, -c*dz/d, dy/d);
-
-                       Point v(p.x+c*profile[j].x-s*profile[j].y, p.y+c*profile[j].y+s*profile[j].x, profile[j].z+i*track.get_slope()/nsegs);
-                       va_builder.vertex(v.x, v.y, v.z);
-                       if(profile[j].z==0)
-                               border.push_back(v);
-               }
+               if(!endpoints[i])
+                       endpoints[i] = new Endpoint3D(*this, i);
        }
-
-       for(unsigned i=0; i<nsegs; ++i)
+       else if(endpoints[i])
        {
-               for(unsigned j=0; j<3; ++j)
-               {
-                       base_seq.push_back(base_index+i*psize+j*2);
-                       base_seq.push_back(base_index+(i+1)*psize+j*2);
-                       base_seq.push_back(base_index+(i+1)*psize+1+j*2);
-                       base_seq.push_back(base_index+i*psize+1+j*2);
-               }
-               for(unsigned j=3; j<9; ++j)
-               {
-                       rail_seq.push_back(base_index+i*psize+j*2);
-                       rail_seq.push_back(base_index+(i+1)*psize+j*2);
-                       rail_seq.push_back(base_index+(i+1)*psize+1+j*2);
-                       rail_seq.push_back(base_index+i*psize+1+j*2);
-               }
-               route_seq[part.route].push_back(base_index+i*psize+18);
-               route_seq[part.route].push_back(base_index+(i+1)*psize+18);
-               route_seq[part.route].push_back(base_index+(i+1)*psize+19);
-               route_seq[part.route].push_back(base_index+i*psize+19);
+               delete endpoints[i];
+               endpoints[i] = 0;
        }
-
-       base_index+=(nsegs+1)*psize;
 }
 
-} // namespace Marklin
+} // namespace R2C2