]> git.tdb.fi Git - r2c2.git/blob - source/3d/vehicle.cpp
50dbcb0d693134d969964ba31249bf71e3acc9c7
[r2c2.git] / source / 3d / vehicle.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include <msp/gl/matrix.h>
10 #include "layout.h"
11 #include "vehicle.h"
12 #include "vehicletype.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 namespace R2C2 {
18
19 Vehicle3D::Vehicle3D(Layout3D &l, Vehicle &v):
20         layout(l),
21         vehicle(v),
22         type(layout.get_catalogue().get_vehicle(vehicle.get_type()))
23 {
24         layout.add_vehicle(*this);
25         layout.get_scene().add(*this);
26 }
27
28 Vehicle3D::~Vehicle3D()
29 {
30         layout.remove_vehicle(*this);
31         layout.get_scene().remove(*this);
32 }
33
34 Point Vehicle3D::get_node() const
35 {
36         Point p = vehicle.get_position();
37         return Point(p.x, p.y, p.z+0.01+vehicle.get_type().get_height());
38 }
39
40 bool Vehicle3D::is_visible() const
41 {
42         return vehicle.get_track();
43 }
44
45 void Vehicle3D::render(const GL::Tag &tag) const
46 {
47         if(!vehicle.get_track())
48                 return;
49
50         if(tag==0)
51         {
52                 GL::PushMatrix push_mat;
53
54                 const Point &pos = vehicle.get_position();
55                 GL::translate(pos.x, pos.y, pos.z);
56                 GL::rotate(vehicle.get_direction()*180/M_PI, 0, 0, 1);
57
58                 if(const GL::Object *obj = type.get_body_object())
59                         obj->render(tag);
60
61                 const vector<VehicleType::Axle> &axles = vehicle.get_type().get_axles();
62                 for(unsigned i=0; i<axles.size(); ++i)
63                         if(const GL::Object *obj = type.get_axle_object(i))
64                         {
65                                 GL::PushMatrix push_mat2;
66                                 GL::translate(axles[i].position, 0, axles[i].wheel_dia/2);
67                                 GL::rotate(vehicle.get_axle_angle(i)*180/M_PI, 0, 1, 0);
68                                 obj->render(tag);
69                         }
70
71                 const vector<VehicleType::Bogie> &bogies = vehicle.get_type().get_bogies();
72                 for(unsigned i=0; i<bogies.size(); ++i)
73                 {
74                         GL::PushMatrix push_mat2;
75                         GL::translate(bogies[i].position, 0, 0);
76                         float angle = vehicle.get_bogie_direction(i)*180/M_PI;
77                         GL::rotate(angle, 0, 0, 1);
78
79                         for(unsigned j=0; j<bogies[i].axles.size(); ++j)
80                                 if(const GL::Object *obj = type.get_bogie_axle_object(i, j))
81                                 {
82                                         GL::PushMatrix push_mat3;
83                                         GL::translate(bogies[i].axles[j].position, 0, bogies[i].axles[j].wheel_dia/2);
84                                         GL::rotate(vehicle.get_bogie_axle_angle(i, j)*180/M_PI, 0, 1, 0);
85                                         obj->render(tag);
86                                 }
87
88                         if(bogies[i].rotate_object)
89                                 GL::rotate(180, 0, 0, 1);
90                         if(const GL::Object *obj = type.get_bogie_object(i))
91                                 obj->render(tag);
92                 }
93
94                 const vector<VehicleType::Rod> &rods = vehicle.get_type().get_rods();
95                 for(unsigned i=0; i<rods.size(); ++i)
96                         if(const GL::Object *obj = type.get_rod_object(i))
97                         {
98                                 GL::PushMatrix push_mat2;
99                                 const Point &rpos = vehicle.get_rod_position(i);
100                                 float angle = vehicle.get_rod_angle(i);
101                                 GL::translate(rpos.x, rpos.y, rpos.z);
102                                 if(rods[i].mirror_object)
103                                         GL::scale(1, -1, 1);
104                                 GL::rotate(angle*180/M_PI, 0, -1, 0);
105                                 obj->render(tag);
106                         }
107         }
108 }
109
110 } // namespace R2C2