3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
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"
14 #include "trainview.h"
17 using namespace Marklin;
19 TrainView::TrainView(Engineer &e, const Train &t):
20 GLtk::Widget(e.get_ui_resources()),
21 GLtk::Panel(e.get_ui_resources()),
26 pipeline(280, 280, false),
31 tex.set_min_filter(GL::LINEAR);
32 tex.storage(GL::RGB, 280, 280, 0);
33 tex.image(0, GL::RGB, GL::UNSIGNED_BYTE, 0);
34 fbo.attach(GL::COLOR_ATTACHMENT0, tex, 0);
35 depth.storage(GL::DEPTH_COMPONENT, 280, 280);
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::PipelinePass *pass = &pipeline.add_pass(0);
46 pass->depth_test = &GL::DepthTest::lequal();
47 pass->lighting = &engineer.get_lighting();
50 add(*(image = new GLtk::Image(res, &tex)));
51 image->set_geometry(GLtk::Geometry(10, 40, geom.w-20, geom.h-50));
55 add(*(btn = new GLtk::Button(res, "Roof")));
56 btn->set_geometry(GLtk::Geometry(10, 10, 36, 25));
57 btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &TrainView::set_mode), ROOF));
59 add(*(btn = new GLtk::Button(res, "Side")));
60 btn->set_geometry(GLtk::Geometry(46, 10, 36, 25));
61 btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &TrainView::set_mode), SIDE));
63 add(*(btn = new GLtk::Button(res, "Head")));
64 btn->set_geometry(GLtk::Geometry(82, 10, 36, 25));
65 btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &TrainView::set_mode), HEAD));
67 add(*(tgl_forward = new GLtk::Toggle(res, "Fwd")));
68 tgl_forward->set_geometry(GLtk::Geometry(118, 8, 36, 27));
69 tgl_forward->set_value(true);
70 tgl_forward->signal_toggled.connect(sigc::mem_fun(this, &TrainView::set_forward));
72 add(*(btn = new GLtk::Button(res, "Close")));
73 btn->set_geometry(GLtk::Geometry(geom.w-46, 10, 36, 25));
74 btn->signal_clicked.connect(sigc::mem_fun(this, &TrainView::close_clicked));
76 engineer.add_train_view(*this);
79 TrainView::~TrainView()
81 engineer.remove_train_view(*this);
84 void TrainView::set_mode(Mode m)
89 void TrainView::set_forward(bool f)
94 void TrainView::prepare()
96 const Vehicle &veh = train.get_vehicle(0);
97 const Point &pos = veh.get_position();
98 float angle = veh.get_direction();
101 float c = cos(angle);
102 float s = sin(angle);
103 float l = veh.get_type().get_length();
108 camera.set_position(GL::Vector3(pos.x-l*c, pos.y-l*s, pos.z+0.07));
109 camera.set_look_direction(GL::Vector3(c, s, -0.2));
112 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));
113 camera.set_look_direction(GL::Vector3(c-0.2*s, s+0.2*c, 0));
116 camera.set_position(GL::Vector3(pos.x+l*0.55*c, pos.y+l*0.55*s, pos.z+0.03));
117 camera.set_look_direction(GL::Vector3(c, s, 0));
121 GL::Bind _bind_fbo(fbo);
122 fbo.clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
123 pipeline.render_all();
126 void TrainView::button_release(int x, int y, unsigned btn)
128 GLtk::Panel::button_release(x, y, btn);
133 void TrainView::close_clicked()