1 #include <msp/core/maputils.h>
2 #include <msp/gl/tests.h>
3 #include <msp/gltk/button.h>
4 #include <msp/gltk/image.h>
5 #include <msp/gltk/label.h>
6 #include <msp/strings/format.h>
7 #include "libr2c2/vehicle.h"
8 #include "libr2c2/vehicletype.h"
10 #include "trainview.h"
15 TrainView::TrainView(Engineer &e, const Train &t):
20 pipeline(400, 300, false)
22 Loader::WidgetMap widgets;
23 DataFile::load(*this, "data/trainview.ui", widgets);
25 dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_title"))->set_text(format("View of %s", train.get_name()));
26 dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_forward"))->signal_toggled.connect(sigc::mem_fun(this, &TrainView::set_forward));
27 dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_roof"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainView::ui_mode_toggled), ROOF));
28 dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_side"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainView::ui_mode_toggled), SIDE));
29 dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_head"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainView::ui_mode_toggled), HEAD));
31 tex.set_min_filter(GL::LINEAR);
32 tex.storage(GL::RGB, 400, 300);
33 tex.image(0, GL::RGB, GL::UNSIGNED_BYTE, 0);
34 fbo.attach(GL::COLOR_ATTACHMENT0, tex, 0);
35 depth.storage(GL::DEPTH_COMPONENT, 400, 300);
36 fbo.attach(GL::DEPTH_ATTACHMENT, depth);
38 camera.set_up_direction(GL::Vector3(0, 0, 1));
39 camera.set_depth_clip(0.01, 10);
41 pipeline.set_camera(&camera);
43 pipeline.add_renderable(engineer.get_layout_3d().get_scene());
45 GL::Pipeline::Pass *pass = &pipeline.add_pass(0);
46 pass->set_depth_test(&GL::DepthTest::lequal());
47 pass->set_lighting(&engineer.get_lighting());
49 dynamic_cast<GLtk::Image *>(get_item(widgets, "img_view"))->set_image(&tex);
51 engineer.add_train_view(*this);
54 TrainView::~TrainView()
56 engineer.remove_train_view(*this);
59 void TrainView::set_mode(Mode m)
64 void TrainView::ui_mode_toggled(bool v, Mode m)
70 void TrainView::set_forward(bool f)
75 void TrainView::prepare()
77 const Vehicle &veh = train.get_vehicle(0);
78 const Vector &pos = veh.get_position();
79 Angle angle = veh.get_rotation();
81 angle += Angle::half_turn();
82 Vector fwd_vec = rotated_vector(Vector(1, 0, 0), angle);
83 Vector side_vec = rotated_vector(Vector(0, -1, 0), angle);
84 float l = veh.get_type().get_length();
89 camera.set_position(pos-l*fwd_vec+Vector(0, 0, 0.07));
90 camera.set_look_direction(fwd_vec-Vector(0, 0, -0.2));
93 camera.set_position(pos-0.8f*fwd_vec+0.05f*side_vec+Vector(0, 0, 0.03));
94 camera.set_look_direction(fwd_vec-side_vec*0.2f);
97 camera.set_position(pos+fwd_vec*(l*0.55f)+Vector(0, 0, 0.03));
98 camera.set_look_direction(fwd_vec);
102 GL::Bind _bind_fbo(fbo);
103 fbo.clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);