/* $Id$
This file is part of the MSP Märklin suite
-Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
#include <GL/gl.h>
#include <msp/gl/misc.h>
#include "libmarklin/tracktype.h"
+#include "layout.h"
#include "track.h"
+#include "tracktype.h"
using namespace std;
using namespace Msp;
namespace Marklin {
-Track3D::Track3D(Track &t, unsigned q):
+Track3D::Track3D(Layout3D &l, Track &t):
+ layout(l),
track(t),
- color(1, 1, 1),
- varray((GL::NORMAL3, GL::VERTEX3)),
- quality(q)
-{
- build_object();
-}
+ type(layout.get_catalogue().get_track(track.get_type())),
+ color(1, 1, 1)
+{ }
void Track3D::set_color(const Msp::GL::Color &c)
{
color = c;
}
-void Track3D::set_quality(unsigned q)
-{
- 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 slope = track.get_slope();
+ type.get_bounds(angle-track.get_rotation(), minp, maxp);
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;
- minp.z = pos.z+min(slope, 0.0f);
- maxp.z = pos.z+0.01+max(slope, 0.0f);
-
- 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;
+ 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.x = min(minp.x, x);
- minp.y = min(minp.y, y);
- maxp.x = max(maxp.x, x);
- maxp.y = max(maxp.y, y);
- }
+ float slope = track.get_slope();
+ if(slope>0)
+ maxp.z += slope;
+ else
+ minp.z += slope;
}
void Track3D::render() const
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]);
- }
+ type.render();
glPopName();
glPopMatrix();
{
prepare_render(true);
- varray.apply();
+ (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();
}
glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
}
-void Track3D::build_object()
-{
- varray.clear();
- GL::VertexArrayBuilder builder(varray);
-
- base_seq.clear();
- rail_seq.clear();
- path_seq.clear();
- path_seq.resize(track.get_type().get_n_paths());
-
- 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);
-}
-
-void Track3D::build_part(const TrackPart &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();
-
- 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)
- {
- 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<psize; ++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);
- va_builder.vertex(v.x, v.y, v.z);
- if(profile[j].z==0)
- border.push_back(v);
- }
- }
-
- 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);
- }
- path_seq[part.path].push_back(base_index+i*psize+18);
- path_seq[part.path].push_back(base_index+(i+1)*psize+18);
- path_seq[part.path].push_back(base_index+(i+1)*psize+19);
- path_seq[part.path].push_back(base_index+i*psize+19);
- }
-
- base_index += (nsegs+1)*psize;
-}
-
} // namespace Marklin