]> git.tdb.fi Git - r2c2.git/blob - source/3d/track.cpp
Render tracks through GL::Objects
[r2c2.git] / source / 3d / track.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/misc.h>
11 #include "libr2c2/tracktype.h"
12 #include "endpoint.h"
13 #include "layout.h"
14 #include "path.h"
15 #include "track.h"
16 #include "tracktype.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 namespace R2C2 {
22
23 Track3D::Track3D(Layout3D &l, Track &t):
24         GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()),
25         layout(l),
26         track(t),
27         type(layout.get_catalogue().get_track(track.get_type())),
28         path(new Path3D(*this))
29 {
30         layout.add_track(*this);
31         layout.get_scene().add(*this);
32
33         const vector<TrackType::Endpoint> &type_eps = track.get_type().get_endpoints();
34         for(unsigned i=0; i<type_eps.size(); ++i)
35                 endpoints.push_back(new Endpoint3D(*this, i));
36 }
37
38 Track3D::~Track3D()
39 {
40         delete path;
41
42         layout.remove_track(*this);
43         layout.get_scene().remove(*this);
44
45         for(vector<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
46                 delete *i;
47 }
48
49 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
50 {
51         type.get_bounds(angle-track.get_rotation(), minp, maxp);
52
53         float c = cos(-angle);
54         float s = sin(-angle);
55
56         const Point &pos = track.get_position();
57         minp.x += c*pos.x-s*pos.y;
58         maxp.x += c*pos.x-s*pos.y;
59         minp.y += s*pos.x+c*pos.y;
60         maxp.y += s*pos.x+c*pos.y;
61         minp.z += pos.z;
62         maxp.z += pos.z;
63
64         float slope = track.get_slope();
65         if(slope>0)
66                 maxp.z += slope;
67         else
68                 minp.z += slope;
69 }
70
71 Point Track3D::get_node() const
72 {
73         const Point &pos = track.get_position();
74         Point minp;
75         Point maxp;
76         type.get_bounds(0, minp, maxp);
77         float rot = track.get_rotation();
78         float c = cos(rot);
79         float s = sin(rot);
80
81         Point center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, 0);
82         return Point(pos.x+c*center.x-s*center.y, pos.y+s*center.x+c*center.y, pos.z+0.02);
83 }
84
85 GL::Matrix Track3D::get_matrix() const
86 {
87         const Point &pos = track.get_position();
88         float rot = track.get_rotation();
89
90         GL::Matrix matrix;
91         matrix.translate(pos.x, pos.y, pos.z);
92         matrix.rotate(rot, 0, 0, 1);
93         matrix.rotate(track.get_slope()/track.get_type().get_total_length(), 0, -1, 0);
94
95         return matrix;
96 }
97
98 void Track3D::setup_render(const GL::Tag &) const
99 {
100         GL::MatrixStack::modelview().push();
101         GL::MatrixStack::modelview() *= get_matrix();
102         glPushName(reinterpret_cast<unsigned>(this));
103 }
104
105 void Track3D::finish_render(const GL::Tag &) const
106 {
107         glPopName();
108         GL::MatrixStack::modelview().pop();
109 }
110
111 } // namespace R2C2