]> git.tdb.fi Git - r2c2.git/blob - source/3d/track.cpp
c3eb388dc8bb4a96b35a4a797319cf53db8dd949
[r2c2.git] / source / 3d / track.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include <GL/gl.h>
10 #include <msp/gl/misc.h>
11 #include "libmarklin/tracktype.h"
12 #include "layout.h"
13 #include "track.h"
14 #include "tracktype.h"
15
16 using namespace std;
17 using namespace Msp;
18
19 namespace Marklin {
20
21 Track3D::Track3D(Layout3D &l, Track &t):
22         layout(l),
23         track(t),
24         type(layout.get_catalogue().get_track(track.get_type())),
25         color(1, 1, 1)
26 { }
27
28 void Track3D::set_color(const Msp::GL::Color &c)
29 {
30         color = c;
31 }
32
33 void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
34 {
35         type.get_bounds(angle-track.get_rotation(), minp, maxp);
36
37         float c = cos(-angle);
38         float s = sin(-angle);
39
40         const Point &pos = track.get_position();
41         minp.x += c*pos.x-s*pos.y;
42         maxp.x += c*pos.x-s*pos.y;
43         minp.y += s*pos.x+c*pos.y;
44         maxp.y += s*pos.x+c*pos.y;
45
46         float slope = track.get_slope();
47         if(slope>0)
48                 maxp.z += slope;
49         else
50                 minp.z += slope;
51 }
52
53 void Track3D::render() const
54 {
55         prepare_render(true);
56
57         glPushName(reinterpret_cast<unsigned>(this));
58
59         type.render();
60
61         glPopName();
62         glPopMatrix();
63 }
64
65 void Track3D::render_endpoints() const
66 {
67         prepare_render(false);
68
69         const vector<Endpoint> &endpoints = track.get_type().get_endpoints();
70         for(unsigned i=0; i<endpoints.size(); ++i)
71         {
72                 const Endpoint &ep = endpoints[i];
73                 GL::set(GL_CULL_FACE, track.get_link(i));
74                 if(track.get_link(i))
75                         glColor4f(0.5, 0, 1, 0.5);
76                 else
77                         glColor4f(1, 0, 0.5, 0.5);
78
79                 float c = cos(ep.dir);
80                 float s = sin(ep.dir);
81                 float z = (i==1 ? track.get_slope() : 0);
82
83                 glBegin(GL_QUADS);
84                 glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z);
85                 glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z);
86                 glVertex3f(ep.pos.x+s*0.025, ep.pos.y-c*0.025, z+0.02);
87                 glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, z+0.02);
88                 glEnd();
89         }
90
91         if(abs(track.get_slope())>1e-4)
92         {
93                 Point p;
94                 if(track.get_slope()>0)
95                 {
96                         p = endpoints[1].pos;
97                         p.z += track.get_slope();
98                 }
99                 else
100                         p = endpoints[0].pos;
101                 glBegin(GL_TRIANGLE_STRIP);
102                 for(unsigned i=0; i<=16; ++i)
103                 {
104                         float c = cos(i*M_PI/8);
105                         float s = sin(i*M_PI/8);
106                         glNormal3f(c, s, 0);
107                         glVertex3f(p.x+c*0.01, p.y+s*0.01, p.z);
108                         glVertex3f(p.x+c*0.01, p.y+s*0.01, -track.get_position().z);
109                 }
110                 glEnd();
111         }
112
113         glPopMatrix();
114 }
115
116 void Track3D::render_path(int path) const
117 {
118         prepare_render(true);
119
120         (void)path;
121         /*varray.apply();
122         if(path>=0 && static_cast<unsigned>(path)<path_seq.size())
123                 glDrawElements(GL_QUADS, path_seq[path].size(), GL_UNSIGNED_INT, &path_seq[path][0]);
124         else
125         {
126                 for(unsigned i=0; i<path_seq.size(); ++i)
127                         glDrawElements(GL_QUADS, path_seq[i].size(), GL_UNSIGNED_INT, &path_seq[i][0]);
128         }*/
129
130         glPopMatrix();
131 }
132
133 void Track3D::prepare_render(bool slope) const
134 {
135         const Point &pos = track.get_position();
136         float rot = track.get_rotation();
137
138         glPushMatrix();
139         glTranslatef(pos.x, pos.y, pos.z);
140         glRotatef(rot*180/M_PI, 0, 0, 1);
141         if(slope)
142                 glRotatef(track.get_slope()/track.get_type().get_total_length()*180/M_PI, 0, -1, 0);
143 }
144
145 } // namespace Marklin