]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/track.cpp
Render tracks through GL::Objects
[r2c2.git] / source / 3d / track.cpp
index e64f42ed3dde6394fde1749979f24e163f57ba5a..c8bf0e6309211c1308bd6a56bbba803934e37ac5 100644 (file)
+/* $Id$
+
+This file is part of R²C²
+Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
 #include <cmath>
-#include <GL/gl.h>
+#include <msp/gl/matrix.h>
 #include <msp/gl/misc.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;
 
-#include <iostream>
-
-namespace Marklin {
+namespace R2C2 {
 
-Track3D::Track3D(Track &t, unsigned q):
+Track3D::Track3D(Layout3D &l, Track &t):
+       GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()),
+       layout(l),
        track(t),
-       color(1, 1, 1),
-       varray((GL::NORMAL3, GL::VERTEX3)),
-       quality(q)
+       type(layout.get_catalogue().get_track(track.get_type())),
+       path(new Path3D(*this))
 {
-       build_object();
-}
+       layout.add_track(*this);
+       layout.get_scene().add(*this);
 
-void Track3D::set_quality(unsigned q)
-{
-       quality=q;
-       build_object();
+       const vector<TrackType::Endpoint> &type_eps = track.get_type().get_endpoints();
+       for(unsigned i=0; i<type_eps.size(); ++i)
+               endpoints.push_back(new Endpoint3D(*this, i));
 }
 
-void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
+Track3D::~Track3D()
 {
-       const Point &pos=track.get_position();
-       float rot=track.get_rotation();
+       delete path;
 
-       float c=cos(-angle);
-       float s=sin(-angle);
+       layout.remove_track(*this);
+       layout.get_scene().remove(*this);
 
-       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);
-
-       for(vector<Point>::const_iterator i=border.begin(); i!=border.end(); ++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);
-       }
+       for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
+               delete *i;
 }
 
-void Track3D::render()
+void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
 {
-       prepare_render();
-
-       glPushName((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();
+       type.get_bounds(angle-track.get_rotation(), minp, maxp);
+
+       float c = cos(-angle);
+       float s = sin(-angle);
+
+       const Point &pos = track.get_position();
+       minp.x += c*pos.x-s*pos.y;
+       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)
+               maxp.z += slope;
+       else
+               minp.z += slope;
 }
 
-void Track3D::render_endpoints()
+Point Track3D::get_node() const
 {
-       prepare_render();
-
-       const Point &pos=track.get_position();
-       const Track::EndpointSeq &endpoints=track.get_endpoints();
-       for(Track::EndpointSeq::const_iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
-       {
-               GL::set(GL_CULL_FACE, i->link);
-               if(i->link)
-                       glColor4f(0.5, 0, 1, 0.5);
-               else
-                       glColor4f(1, 0, 0.5, 0.5);
-
-               float c=cos(i->rot);
-               float s=sin(i->rot);
-
-               glBegin(GL_QUADS);
-               glVertex3f(i->pos.x-s*0.025, i->pos.y+c*0.025, 0);
-               glVertex3f(i->pos.x+s*0.025, i->pos.y-c*0.025, 0);
-               glVertex3f(i->pos.x+s*0.025, i->pos.y-c*0.025, pos.z+i->pos.z+0.02);
-               glVertex3f(i->pos.x-s*0.025, i->pos.y+c*0.025, pos.z+i->pos.z+0.02);
-               glEnd();
-       }
-
-       glPopMatrix();
+       const Point &pos = track.get_position();
+       Point minp;
+       Point maxp;
+       type.get_bounds(0, minp, maxp);
+       float rot = track.get_rotation();
+       float c = cos(rot);
+       float s = sin(rot);
+
+       Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, 0);
+       return Point(pos.x+c*center.x-s*center.y, pos.y+s*center.x+c*center.y, pos.z+0.02);
 }
 
-void Track3D::render_route(int route)
+GL::Matrix Track3D::get_matrix() const
 {
-       prepare_render();
+       const Point &pos = track.get_position();
+       float rot = track.get_rotation();
 
-       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]);
-       }
+       GL::Matrix matrix;
+       matrix.translate(pos.x, pos.y, pos.z);
+       matrix.rotate(rot, 0, 0, 1);
+       matrix.rotate(track.get_slope()/track.get_type().get_total_length(), 0, -1, 0);
 
-       glPopMatrix();
+       return matrix;
 }
 
-void Track3D::prepare_render()
+void Track3D::setup_render(const GL::Tag &) 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);
+       GL::MatrixStack::modelview().push();
+       GL::MatrixStack::modelview() *= get_matrix();
+       glPushName(reinterpret_cast<unsigned>(this));
 }
 
-void Track3D::build_object()
+void Track3D::finish_render(const GL::Tag &) const
 {
-       varray.clear();
-       RefPtr<GL::VertexArrayBuilder> builder=varray.modify();
-
-       base_seq.clear();
-       rail_seq.clear();
-       route_seq.clear();
-       route_seq.resize(track.get_n_routes());
-
-       const Track::PartSeq &parts=track.get_parts();
-       unsigned index=0;
-       for(Track::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
-               build_part(*i, *builder, index);
-}
-
-void Track3D::build_part(const Track::Part &part, GL::VertexArrayBuilder &va_builder, unsigned &base_index)
-{
-       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();
-
-       const float &radius=part.radius;
-       const float &x=part.x;
-       const float &y=part.y;
-       const float &length=part.length;
-       const float &dir=part.dir;
-
-       unsigned nsegs;
-       if(radius)
-       {
-               nsegs=(unsigned)(part.length*(1<<quality))+1;
-               Point center(x-sin(dir)*radius, y+cos(dir)*radius, 0);
-               float r=fabs(radius);
-               float start=((radius<0)?M_PI:0)+dir;
-               float angle=(radius<0)?-length:length;
-               int inv=(radius<0)?-1:1;
-               for(unsigned i=0; i<=nsegs; ++i)
-               {
-                       float a=start+i*angle/nsegs;
-                       float c=cos(a);
-                       float s=sin(a);
-
-                       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 p(center.x+s*(r-profile[j].y*inv), center.y-c*(r-profile[j].y*inv), profile[j].z+i*track.get_slope()/nsegs);
-                               va_builder.vertex(p.x, p.y, p.z);
-                               if(profile[j].z==0)
-                                       border.push_back(p);
-                       }
-               }
-       }
-       else
-       {
-               nsegs=1;
-               float c=cos(dir);
-               float s=sin(dir);
-               for(unsigned i=0; i<2; ++i)
-               {
-                       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);
-
-                               float len=(part.dead_end && i==1 && j>=6) ? length/2 : length;
-                               Point p(x+c*len*i-s*profile[j].y, y+s*len*i+c*profile[j].y, profile[j].z+i*track.get_slope());
-                               va_builder.vertex(p.x, p.y, p.z);
-                               if(profile[j].z==0)
-                                       border.push_back(p);
-                       }
-               }
-       }
-
-       for(unsigned i=0; i<nsegs; ++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);
-       }
-
-       base_index+=(nsegs+1)*psize;
+       glPopName();
+       GL::MatrixStack::modelview().pop();
 }
 
-} // namespace Marklin
+} // namespace R2C2