]> git.tdb.fi Git - r2c2.git/blob - source/3d/vehicle.cpp
Update Track3D to use GL::Renderer
[r2c2.git] / source / 3d / vehicle.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010-2011  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/renderer.h>
11 #include "axle.h"
12 #include "bogie.h"
13 #include "layout.h"
14 #include "rod.h"
15 #include "vehicle.h"
16 #include "vehicletype.h"
17
18 using namespace std;
19 using namespace Msp;
20
21 namespace R2C2 {
22
23 Vehicle3D::Vehicle3D(Layout3D &l, Vehicle &v):
24         GL::ObjectInstance(*l.get_catalogue().get_vehicle(v.get_type()).get_body_object()),
25         layout(l),
26         vehicle(v),
27         type(layout.get_catalogue().get_vehicle(vehicle.get_type()))
28 {
29         unsigned n_axles = vehicle.get_type().get_axles().size();
30         for(unsigned i=0; i<n_axles; ++i)
31                 if(type.get_axle_object(i))
32                 {
33                         Axle3D *a = new Axle3D(*this, i);
34                         axles.push_back(a);
35                         layout.get_scene().add(*a);
36                 }
37
38         unsigned n_bogies = vehicle.get_type().get_bogies().size();
39         for(unsigned i=0; i<n_bogies; ++i)
40                 if(type.get_bogie_object(i))
41                 {
42                         Bogie3D *b = new Bogie3D(*this, i);
43                         bogies.push_back(b);
44                         layout.get_scene().add(*b);
45
46                         n_axles = vehicle.get_type().get_bogie(i).axles.size();
47                         for(unsigned j=0; j<n_axles; ++j)
48                                 if(type.get_bogie_axle_object(i, j))
49                                 {
50                                         Axle3D *a = new Axle3D(*this, i, j);
51                                         axles.push_back(a);
52                                         layout.get_scene().add(*a);
53                                 }
54                 }
55
56         unsigned n_rods = vehicle.get_type().get_rods().size();
57         for(unsigned i=0; i<n_rods; ++i)
58                 if(type.get_rod_object(i))
59                 {
60                         Rod3D *r = new Rod3D(*this, i);
61                         rods.push_back(r);
62                         layout.get_scene().add(*r);
63                 }
64
65         layout.add_vehicle(*this);
66         layout.get_scene().add(*this);
67 }
68
69 Vehicle3D::~Vehicle3D()
70 {
71         layout.remove_vehicle(*this);
72         layout.get_scene().remove(*this);
73         for(vector<Axle3D *>::iterator i=axles.begin(); i!=axles.end(); ++i)
74                 delete *i;
75         for(vector<Bogie3D *>::iterator i=bogies.begin(); i!=bogies.end(); ++i)
76                 delete *i;
77         for(vector<Rod3D *>::iterator i=rods.begin(); i!=rods.end(); ++i)
78                 delete *i;
79 }
80
81 Point Vehicle3D::get_node() const
82 {
83         Point p = vehicle.get_position();
84         return Point(p.x, p.y, p.z+0.01+vehicle.get_type().get_height());
85 }
86
87 bool Vehicle3D::is_visible() const
88 {
89         return vehicle.get_track();
90 }
91
92 void Vehicle3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
93 {
94         if(!vehicle.get_track())
95                 return;
96
97         ObjectInstance::render(renderer, tag);
98
99         /*if(tag==0)
100         {
101                 GL::PushMatrix push_mat;
102
103                 const Point &pos = vehicle.get_position();
104                 GL::translate(pos.x, pos.y, pos.z);
105                 GL::rotate(vehicle.get_direction()*180/M_PI, 0, 0, 1);
106
107                 if(const GL::Object *obj = type.get_body_object())
108                         obj->render(tag);*/
109
110                 /*const vector<VehicleType::Axle> &axles = vehicle.get_type().get_axles();
111                 for(unsigned i=0; i<axles.size(); ++i)
112                         if(const GL::Object *obj = type.get_axle_object(i))
113                         {
114                                 GL::PushMatrix push_mat2;
115                                 GL::translate(axles[i].position, 0, axles[i].wheel_dia/2);
116                                 GL::rotate(vehicle.get_axle(i).angle*180/M_PI, 0, 1, 0);
117                                 obj->render(tag);
118                         }
119
120                 const vector<VehicleType::Bogie> &bogies = vehicle.get_type().get_bogies();
121                 for(unsigned i=0; i<bogies.size(); ++i)
122                 {
123                         GL::PushMatrix push_mat2;
124                         GL::translate(bogies[i].position, 0, 0);
125                         float angle = vehicle.get_bogie(i).direction*180/M_PI;
126                         GL::rotate(angle, 0, 0, 1);
127
128                         for(unsigned j=0; j<bogies[i].axles.size(); ++j)
129                                 if(const GL::Object *obj = type.get_bogie_axle_object(i, j))
130                                 {
131                                         GL::PushMatrix push_mat3;
132                                         GL::translate(bogies[i].axles[j].position, 0, bogies[i].axles[j].wheel_dia/2);
133                                         GL::rotate(vehicle.get_bogie_axle(i, j).angle*180/M_PI, 0, 1, 0);
134                                         obj->render(tag);
135                                 }
136
137                         if(bogies[i].rotate_object)
138                                 GL::rotate(180, 0, 0, 1);
139                         if(const GL::Object *obj = type.get_bogie_object(i))
140                                 obj->render(tag);
141                 }*/
142
143                 /*const vector<VehicleType::Rod> &rods = vehicle.get_type().get_rods();
144                 for(unsigned i=0; i<rods.size(); ++i)
145                         if(const GL::Object *obj = type.get_rod_object(i))
146                         {
147                                 GL::PushMatrix push_mat2;
148                                 const Point &rpos = vehicle.get_rod(i).position;
149                                 float angle = vehicle.get_rod(i).angle;
150                                 GL::translate(rpos.x, rpos.y, rpos.z);
151                                 if(rods[i].mirror_object)
152                                         GL::scale(1, -1, 1);
153                                 GL::rotate(angle*180/M_PI, 0, -1, 0);
154                                 obj->render(tag);
155                         }*/
156         //}
157 }
158
159 void Vehicle3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const
160 {
161         GL::Matrix matrix;
162         const Point &pos = vehicle.get_position();
163         matrix.translate(pos.x, pos.y, pos.z);
164         matrix.rotate(vehicle.get_direction(), 0, 0, 1);
165         renderer.matrix_stack() *= matrix;
166 }
167
168 } // namespace R2C2