namespace R2C2 {
Path3D::Path3D(const Track3D &t):
+ Utility3D(t.get_layout()),
track(t),
- paths(0),
+ path(t.get_track().get_active_path()),
+ side(0),
automatic(true),
+ mesh(0),
z_offs(0)
{
- track.get_layout().get_path_scene().add(*this);
+ update_mesh();
+
+ layout.get_path_scene().add(*this);
+ if(track.get_track().get_type().is_turnout())
+ track.get_track().signal_path_changed.connect(sigc::mem_fun(this, &Path3D::path_changed));
}
Path3D::~Path3D()
{
- track.get_layout().get_path_scene().remove(*this);
+ layout.get_path_scene().remove(*this);
}
void Path3D::set_automatic()
automatic = true;
}
-void Path3D::set_path(unsigned p)
+void Path3D::set_path(int p)
{
- if(!(track.get_track().get_type().get_paths()&(1<<p)))
+ if(p>=0 && !(track.get_track().get_type().get_paths()&(1<<p)))
throw invalid_argument("Path3D::set_path");
automatic = false;
- paths = 1<<p;
+ path = p;
+ update_mesh();
}
-void Path3D::set_mask(unsigned p)
+void Path3D::set_side(int s)
{
- if(p&~track.get_track().get_type().get_paths())
- throw invalid_argument("Path3D::set_mask");
- automatic = false;
- paths = p;
+ side = (s<0 ? -1 : s>0 ? 1 : 0);
+ update_mesh();
}
void Path3D::set_color(const GL::Color &c)
void Path3D::set_layer(float l)
{
- z_offs = l*track.get_track().get_layout().get_catalogue().get_gauge()*0.01;
+ z_offs = l*layout.get_layout().get_catalogue().get_gauge()*0.01;
+}
+
+void Path3D::path_changed(unsigned p)
+{
+ if(automatic)
+ {
+ path = p;
+ update_mesh();
+ }
+}
+
+void Path3D::update_mesh()
+{
+ mesh = &track.get_type().get_path_mesh(path, side);
}
long Path3D::get_instance_key() const
{
if(tag=="unlit")
{
- unsigned mask = (automatic ? 1<<track.get_track().get_active_path() : paths);
- mask &= track.get_track().get_type().get_paths();
- if(!mask)
- return;
-
GL::MatrixStack::Push push_mtx(renderer.matrix_stack());
- GL::Matrix matrix = track.get_matrix();
- matrix.translate(0, 0, z_offs);
- renderer.matrix_stack() *= matrix;
+ renderer.matrix_stack() *= track.Object3D::get_matrix();
+ renderer.matrix_stack() *= GL::Matrix::translation(0, 0, z_offs);
glColor4f(color.r, color.g, color.b, color.a);
- for(unsigned i=0; mask; ++i, mask>>=1)
- if(mask&1)
- track.get_type().get_path_mesh(i).draw(renderer);
+ mesh->draw(renderer);
}
}