]> git.tdb.fi Git - r2c2.git/blob - source/3d/axle.cpp
5123106e5c805f8736292d7996ec47d7f97080c6
[r2c2.git] / source / 3d / axle.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2011  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <msp/gl/matrix.h>
9 #include <msp/gl/renderer.h>
10 #include "axle.h"
11 #include "vehicle.h"
12 #include "vehicletype.h"
13
14 using namespace Msp;
15
16 namespace R2C2 {
17
18 Axle3D::Axle3D(const Vehicle3D &v, unsigned a):
19         GL::ObjectInstance(*v.get_type().get_axle_object(a)),
20         vehicle(v.get_vehicle()),
21         bogie(0),
22         axle(vehicle.get_axle(a))
23 { }
24
25 Axle3D::Axle3D(const Vehicle3D &v, unsigned b, unsigned a):
26         GL::ObjectInstance(*v.get_type().get_bogie_axle_object(b, a)),
27         vehicle(v.get_vehicle()),
28         bogie(&vehicle.get_bogie(b)),
29         axle(bogie->axles[a])
30 { }
31
32 void Axle3D::render(GL::Renderer &renderer, const GL::Tag &tag) const
33 {
34         if(!vehicle.get_track())
35                 return;
36
37         ObjectInstance::render(renderer, tag);
38 }
39
40 void Axle3D::setup_render(GL::Renderer &renderer, const GL::Tag &) const
41 {
42         GL::Matrix matrix;
43
44         const Point &pos = vehicle.get_position();
45         matrix.translate(pos.x, pos.y, pos.z);
46         matrix.rotate(vehicle.get_direction(), 0, 0, 1);
47
48         if(bogie)
49         {
50                 matrix.translate(bogie->type->position, 0, 0);
51                 matrix.rotate(bogie->direction, 0, 0, 1);
52         }
53
54         matrix.translate(axle.type->position, 0, axle.type->wheel_dia/2);
55         matrix.rotate(axle.angle, 0, 1, 0);
56
57         renderer.matrix_stack() *= matrix;
58 }
59
60 } // namespace R2C2