]> git.tdb.fi Git - r2c2.git/blob - source/3d/vehicle.cpp
41bb26259da3b2a769c7a93e427acfb76ab1fe0b
[r2c2.git] / source / 3d / vehicle.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
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 Marklin {
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 void Vehicle3D::render(const GL::Tag &tag) const
41 {
42         if(tag==0)
43         {
44                 GL::PushMatrix push_mat;
45
46                 const Point &pos = vehicle.get_position();
47                 GL::translate(pos.x, pos.y, pos.z+0.01);
48                 GL::rotate(vehicle.get_direction()*180/M_PI, 0, 0, 1);
49
50                 if(type.get_body_object())
51                         type.get_body_object()->render(tag);
52
53                 const vector<VehicleType::Bogie> &bogies = vehicle.get_type().get_bogies();
54                 for(unsigned i=0; i<bogies.size(); ++i)
55                 {
56                         GL::PushMatrix push_mat2;
57                         GL::translate(bogies[i].position, 0, 0);
58                         float angle = vehicle.get_bogie_direction(i)*180/M_PI;
59                         if(bogies[i].rotate_object)
60                                 angle += 180;
61                         GL::rotate(angle, 0, 0, 1);
62                         if(type.get_bogie_object(i))
63                                 type.get_bogie_object(i)->render(tag);
64                 }
65         }
66 }
67
68 } // namespace Marklin