-/* $Id$
-
-This file is part of R²C²
-Copyright © 2006-2011 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
#include <cmath>
#include <msp/gl/matrix.h>
-#include <msp/gl/misc.h>
#include <msp/gl/renderer.h>
#include "libr2c2/tracktype.h"
#include "endpoint.h"
namespace R2C2 {
Track3D::Track3D(Layout3D &l, Track &t):
- GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()),
- layout(l),
+ Object3D(l, t),
+ GL::ObjectInstance(l.get_catalogue().get_3d(t.get_type()).get_object()),
track(t),
- type(layout.get_catalogue().get_track(track.get_type())),
- path(new Path3D(*this))
+ type(layout.get_catalogue().get_3d(track.get_type()))
{
- layout.add_track(*this);
layout.get_scene().add(*this);
const vector<TrackType::Endpoint> &type_eps = track.get_type().get_endpoints();
Track3D::~Track3D()
{
- delete path;
-
- layout.remove_track(*this);
layout.get_scene().remove(*this);
for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
delete *i;
}
-void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
-{
- 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;
-}
-
-Point Track3D::get_node() const
+Vector Track3D::get_node() const
{
- 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);
+ 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();
- 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);
-}
-
-GL::Matrix Track3D::get_matrix() const
-{
- const Point &pos = track.get_position();
- float rot = track.get_rotation();
-
- 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);
-
- return matrix;
+ return matrix*((minp+maxp)/2.0f+Vector(0, 0, 0.02));
}
void Track3D::setup_render(Msp::GL::Renderer &renderer, const GL::Tag &) const
{
- renderer.matrix_stack() *= get_matrix();
- glPushName(reinterpret_cast<unsigned>(this));
-}
-
-void Track3D::finish_render(Msp::GL::Renderer &, const GL::Tag &) const
-{
- glPopName();
+ renderer.matrix_stack() *= matrix;
}
void Track3D::link_changed(unsigned i, Track *trk)