]> git.tdb.fi Git - r2c2.git/blob - source/engineer/trainview.cpp
Add TrainView for viewing the layout from the train's perspective
[r2c2.git] / source / engineer / trainview.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 <msp/gl/tests.h>
9 #include <msp/gltk/button.h>
10 #include <msp/gltk/image.h>
11 #include "libmarklin/vehicle.h"
12 #include "libmarklin/vehicletype.h"
13 #include "engineer.h"
14 #include "trainview.h"
15
16 using namespace Msp;
17 using namespace Marklin;
18
19 TrainView::TrainView(Engineer &e, const Train &t):
20         GLtk::Widget(e.get_ui_resources()),
21         GLtk::Panel(e.get_ui_resources()),
22         engineer(e),
23         train(t),
24         mode(SIDE),
25         pipeline(280, 280, false),
26         stale(false)
27 {
28         set_size(300, 330);
29
30         tex.set_min_filter(GL::LINEAR);
31         tex.storage(GL::RGB, 280, 280, 0);
32         tex.image(0, GL::RGB, GL::UNSIGNED_BYTE, 0);
33         fbo.attach(GL::COLOR_ATTACHMENT0, tex, 0);
34         depth.storage(GL::DEPTH_COMPONENT, 280, 280);
35         fbo.attach(GL::DEPTH_ATTACHMENT, depth);
36
37         camera.set_up_direction(GL::Vector3(0, 0, 1));
38         camera.set_depth_clip(0.01, 10);
39         camera.set_aspect(1);
40         pipeline.set_camera(&camera);
41
42         pipeline.add_renderable(engineer.get_layout_3d().get_scene());
43
44         GL::PipelinePass *pass = &pipeline.add_pass(0);
45         pass->depth_test = &GL::DepthTest::lequal();
46         pass->lighting = &engineer.get_lighting();
47
48         GLtk::Image *image;
49         add(*(image = new GLtk::Image(res, &tex)));
50         image->set_geometry(GLtk::Geometry(10, 40, geom.w-20, geom.h-50));
51
52         GLtk::Button *btn;
53
54         add(*(btn = new GLtk::Button(res, "Roof")));
55         btn->set_geometry(GLtk::Geometry(10, 10, 36, 25));
56         btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &TrainView::set_mode), ROOF));
57
58         add(*(btn = new GLtk::Button(res, "Side")));
59         btn->set_geometry(GLtk::Geometry(46, 10, 36, 25));
60         btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &TrainView::set_mode), SIDE));
61
62         add(*(btn = new GLtk::Button(res, "Head")));
63         btn->set_geometry(GLtk::Geometry(82, 10, 36, 25));
64         btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &TrainView::set_mode), HEAD));
65
66         add(*(btn = new GLtk::Button(res, "Close")));
67         btn->set_geometry(GLtk::Geometry(geom.w-46, 10, 36, 25));
68         btn->signal_clicked.connect(sigc::mem_fun(this, &TrainView::close_clicked));
69
70         engineer.add_train_view(*this);
71 }
72
73 TrainView::~TrainView()
74 {
75         engineer.remove_train_view(*this);
76 }
77
78 void TrainView::set_mode(Mode m)
79 {
80         mode = m;
81 }
82
83 void TrainView::prepare()
84 {
85         const Vehicle &veh = train.get_vehicle(0);
86         const Point &pos = veh.get_position();
87         float angle = veh.get_direction();
88         float c = cos(angle);
89         float s = sin(angle);
90         float l = veh.get_type().get_length();
91
92         switch(mode)
93         {
94         case ROOF:
95                 camera.set_position(GL::Vector3(pos.x-l*c, pos.y-l*s, pos.z+0.07));
96                 camera.set_look_direction(GL::Vector3(c, s, -0.2));
97                 break;
98         case SIDE:
99                 camera.set_position(GL::Vector3(pos.x-l*0.8*c+0.05*s, pos.y-l*0.8*s-0.05*c, pos.z+0.03));
100                 camera.set_look_direction(GL::Vector3(c-0.2*s, s+0.2*c, 0));
101                 break;
102         case HEAD:
103                 camera.set_position(GL::Vector3(pos.x+l*0.55*c, pos.y+l*0.55*s, pos.z+0.03));
104                 camera.set_look_direction(GL::Vector3(c, s, 0));
105                 break;
106         }
107
108         GL::Bind _bind_fbo(fbo);
109         fbo.clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
110         pipeline.render_all();
111 }
112
113 void TrainView::button_release(int x, int y, unsigned btn)
114 {
115         GLtk::Panel::button_release(x, y, btn);
116         if(stale)
117                 delete this;
118 }
119
120 void TrainView::close_clicked()
121 {
122         stale = true;
123 }