layout(l),
catalogue(layout.get_catalogue())
{
+ // South, 15° from zenith
+ sun.set_position(0, -0.259, 0.966, 0);
+ lighting.attach(0, sun);
+
layout.signal_track_added.connect(sigc::mem_fun(this, &Layout3D::track_added));
layout.signal_track_removed.connect(sigc::mem_fun(this, &Layout3D::track_removed));
layout.signal_vehicle_added.connect(sigc::mem_fun(this, &Layout3D::vehicle_added));
delete vehicles.begin()->second;
}
+void Layout3D::get_bounds(Vector &minp, Vector &maxp) const
+{
+ minp = maxp = Vector();
+
+ for(TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ {
+ Vector tmin;
+ Vector tmax;
+ i->second->get_bounds(0, tmin, tmax);
+ minp.x = min(minp.x, tmin.x);
+ minp.y = min(minp.y, tmin.y);
+ maxp.x = max(maxp.x, tmax.x);
+ maxp.y = max(maxp.y, tmax.y);
+ }
+}
+
void Layout3D::add_track(Track3D &t)
{
if(tracks.count(&t.get_track()))
#include <sigc++/trackable.h>
#include <msp/gl/instancescene.h>
+#include <msp/gl/light.h>
+#include <msp/gl/lighting.h>
#include <msp/gl/simplescene.h>
#include "libr2c2/layout.h"
#include "catalogue.h"
namespace R2C2 {
+class Object3D;
class Track3D;
class Vehicle3D;
Msp::GL::InstanceScene scene;
Msp::GL::SimpleScene ep_scene;
Msp::GL::InstanceScene path_scene;
+ Msp::GL::Light sun;
+ Msp::GL::Lighting lighting;
public:
Layout3D(Layout &);
~Layout3D();
+ Layout &get_layout() const { return layout; }
const Catalogue3D &get_catalogue() const { return catalogue; }
+ void get_bounds(Vector &, Vector &) const;
void add_track(Track3D &);
void remove_track(Track3D &);
Msp::GL::Scene &get_scene() { return scene; }
Msp::GL::Scene &get_endpoint_scene() { return ep_scene; }
Msp::GL::Scene &get_path_scene() { return path_scene; }
+ const Msp::GL::Lighting &get_lighting() const { return lighting; }
private:
void track_added(Track &);
--- /dev/null
+/* $Id$
+
+This file is part of R²C²
+Copyright © 2011 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <msp/gl/tests.h>
+#include "layout.h"
+#include "track.h"
+#include "view.h"
+
+using namespace std;
+using namespace Msp;
+
+namespace R2C2 {
+
+View3D::View3D(Layout3D &l, unsigned w, unsigned h):
+ layout(l),
+ pipeline(w, h)
+{
+ pipeline.set_camera(&camera);
+ pipeline.add_renderable_for_pass(layout.get_scene(), 0);
+
+ GL::PipelinePass *pass = &pipeline.add_pass(0);
+ pass->lighting = &layout.get_lighting();
+ pass->depth_test = &GL::DepthTest::lequal();
+
+ camera.set_up_direction(GL::Vector3(0, 0, 1));
+ // Y+, 60° down
+ camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
+
+ view_all();
+}
+
+void View3D::view_all(bool tight)
+{
+ Vector minp;
+ Vector maxp;
+
+ layout.get_bounds(minp, maxp);
+
+ float t = tan(camera.get_field_of_view()/2)*2;
+ float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
+ float dist = size/t;
+ if(!tight)
+ dist += sin(camera.get_field_of_view()/2)*size;
+ GL::Vector3 center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, 0);
+ const GL::Vector3 &look = camera.get_look_direction();
+ camera.set_position(GL::Vector3(center.x-look.x*dist, center.y-look.y*dist, center.z-look.z*dist));
+}
+
+void View3D::render()
+{
+ pipeline.render_all();
+}
+
+} // namespace R2C2
--- /dev/null
+/* $Id$
+
+This file is part of R²C²
+Copyright © 2011 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#ifndef R2C2_3D_VIEW_H_
+#define R2C2_3D_VIEW_H_
+
+#include <msp/gl/camera.h>
+#include <msp/gl/pipeline.h>
+
+namespace R2C2 {
+
+class Layout3D;
+
+class View3D
+{
+protected:
+ Layout3D &layout;
+ Msp::GL::Camera camera;
+ Msp::GL::Pipeline pipeline;
+
+public:
+ View3D(Layout3D &, unsigned, unsigned);
+
+ Layout3D &get_layout() const { return layout; }
+ Msp::GL::Camera &get_camera() { return camera; }
+ Msp::GL::Pipeline &get_pipeline() { return pipeline; }
+
+ void view_all(bool = false);
+
+ void render();
+};
+
+} // namespace R2C2
+
+#endif
/* $Id$
This file is part of R²C²
-Copyright © 2010 Mikkosoft Productions, Mikko Rasa
+Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
#include <cmath>
#include <msp/input/keys.h>
+#include "3d/layout.h"
#include "cameracontroller.h"
-#include "designer.h"
using namespace std;
using namespace Msp;
using namespace R2C2;
-CameraController::CameraController(Designer &d, Graphics::EventSource &es, GL::Camera &c):
- designer(d),
+CameraController::CameraController(View3D &v, Graphics::EventSource &es):
+ view(v),
event_source(es),
- camera(c),
+ camera(view.get_camera()),
move_x(0),
move_y(0),
drag_mode(NONE)
void CameraController::view_all()
{
- Vector minp;
- Vector maxp;
-
- const Layout3D::TrackMap &tracks = designer.get_layout_3d().get_tracks();
- for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
- {
- Vector tmin;
- Vector tmax;
- i->second->get_bounds(0, tmin, tmax);
- minp.x = min(minp.x, tmin.x);
- minp.y = min(minp.y, tmin.y);
- maxp.x = max(maxp.x, tmax.x);
- maxp.y = max(maxp.y, tmax.y);
- }
-
- float t = tan(camera.get_field_of_view()/2)*2;
- float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
- float dist = size/t+size*0.25;
- GL::Vector3 center((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, 0);
- const GL::Vector3 &look = camera.get_look_direction();
- camera.set_position(GL::Vector3(center.x-look.x*dist, center.y-look.y*dist, center.z-look.z*dist));
+ view.view_all();
}
void CameraController::move(float x, float y)
const GL::Vector3 &pos = camera.get_position();
const GL::Vector3 &look = camera.get_look_direction();
float dist = get_distance();
- float low = designer.get_layout().get_catalogue().get_gauge()*5;
+ float low = view.get_layout().get_layout().get_catalogue().get_gauge()*5;
if(dist+delta<low)
delta = low-dist;
camera.set_position(GL::Vector3(pos.x-look.x*delta, pos.y-look.y*delta, pos.z-look.z*delta));
/* $Id$
This file is part of R²C²
-Copyright © 2010 Mikkosoft Productions, Mikko Rasa
+Copyright © 2010-2011 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
#include <msp/gbase/window.h>
#include <msp/gl/camera.h>
-
-class Designer;
+#include "3d/view.h"
/**
Moves the camera based on keyboard and mouse events. Controls are as follows:
DISTANCE
};
- Designer &designer;
+ R2C2::View3D &view;
Msp::Graphics::EventSource &event_source;
Msp::GL::Camera &camera;
int move_x;
DragMode drag_mode;
public:
- CameraController(Designer &, Msp::Graphics::EventSource &, Msp::GL::Camera &);
+ CameraController(R2C2::View3D &, Msp::Graphics::EventSource &);
void top_down();
void set_look_direction(const Msp::GL::Vector3 &);
cur_zone(0),
mode(SELECT),
manipulator(*this, root, selection),
- measure(*this),
- camera_ctl(*this, root, camera)
+ measure(*this)
{
window.set_title("Railway Designer");
window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
}
// Setup OpenGL
- pipeline = new GL::Pipeline(window.get_width(), window.get_height(), false);
- pipeline->set_camera(&camera);
- pipeline->add_renderable_for_pass(layout_3d->get_scene(), 0);
- if(base_object)
- pipeline->add_renderable(*base_object);
- pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
- pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
+ cat_view = new View3D(*cat_layout_3d, window.get_width(), window.get_height());
- light.set_position(0, -0.259, 0.966, 0);
- lighting.attach(0, light);
+ main_view = new View3D(*layout_3d, window.get_width(), window.get_height());
+ GL::Pipeline *pipeline = &main_view->get_pipeline();
- GL::PipelinePass *pass = &pipeline->add_pass(0);
- pass->lighting = &lighting;
- pass->depth_test = &GL::DepthTest::lequal();
-
- pass = &pipeline->add_pass("unlit");
+ GL::PipelinePass *pass = &pipeline->add_pass("unlit");
pass->depth_test = &GL::DepthTest::lequal();
pass->blend = &GL::Blend::alpha();
pass = &pipeline->add_pass("blended");
- pass->lighting = &lighting;
+ pass->lighting = &layout_3d->get_lighting();
pass->depth_test = &GL::DepthTest::lequal();
pass->blend = &GL::Blend::alpha();
pass = &pipeline->add_pass("overlay");
pass->blend = &GL::Blend::alpha();
- camera.set_up_direction(GL::Vector3(0, 0, 1));
- camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
+ if(base_object)
+ pipeline->add_renderable(*base_object);
+ pipeline->add_renderable_for_pass(layout_3d->get_path_scene(), "unlit");
+ pipeline->add_renderable_for_pass(layout_3d->get_endpoint_scene(), "unlit");
// Setup UI
root.signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
overlay = new Overlay3D(ui_res.get_default_font());
pipeline->add_renderable_for_pass(*overlay, "overlay");
- camera_ctl.view_all();
+ camera_ctl = new CameraController(*main_view, root);
+ cat_view->get_camera().set_look_direction(GL::Vector3(0, 0.13053, -0.99144));
+ cat_view->view_all(true);
+ main_view->view_all();
const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
for(vector<Toolbar *>::iterator i=toolbars.begin(); i!=toolbars.end(); ++i)
delete *i;
+ delete camera_ctl;
+ delete track_wrap;
delete overlay;
- delete pipeline;
+ delete main_view;
delete base_object;
delete layout_3d;
delete layout;
+ delete cat_view;
delete cat_layout_3d;
}
{
float xf = x*2.0/window.get_width()-1.0;
float yf = y*2.0/window.get_height()-1.0;
- GL::Vector4 vec = camera.unproject(GL::Vector4(xf, yf, 0, 0));
- const GL::Vector3 &pos = camera.get_position();
+ GL::Vector4 vec = main_view->get_camera().unproject(GL::Vector4(xf, yf, 0, 0));
+ const GL::Vector3 &pos = main_view->get_camera().get_position();
return Vector(pos.x-vec.x*pos.z/vec.z, pos.y-vec.y*pos.z/vec.z);
}
window.tick();
root.tick();
- camera_ctl.tick(dt);
+ camera_ctl->tick(dt);
for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
layout_3d->get_track(**i).get_path().set_mask(0);
}
}
-void Designer::apply_camera()
-{
- if(mode==CATALOGUE)
- {
- GL::matrix_mode(GL::PROJECTION);
- GL::load_identity();
- GL::frustum_centered(0.11046, 0.082843, 0.1, 10);
- GL::matrix_mode(GL::MODELVIEW);
- GL::load_identity();
- GL::translate(0, 0, -1);
- }
- else
- camera.apply();
-}
-
void Designer::render()
{
GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT);
- if(mode==CATALOGUE)
- {
- apply_camera();
- cat_layout_3d->get_scene().render();
- }
- else
+ View3D *view = (mode==CATALOGUE ? cat_view : main_view);
+ view->render();
+ if(mode==MEASURE)
{
- pipeline->render_all();
-
GL::Bind bind_depth(GL::DepthTest::lequal());
- if(mode==MEASURE)
- measure.render();
+ measure.render();
}
GL::matrix_mode(GL::PROJECTION);
{
float xx = x*2.0/window.get_width()-1.0;
float yy = y*2.0/window.get_height()-1.0;
- if(mode==CATALOGUE)
- return catalogue.get_layout().pick_track(Vector(0, 0, 1), Vector(xx*0.05523, yy*0.042421, -0.1));
- else
- {
- const GL::Vector3 &cpos = camera.get_position();
- GL::Vector4 cray = camera.unproject(GL::Vector4(xx, yy, 0, 0));
- return layout->pick_track(Vector(cpos.x, cpos.y, cpos.z), Vector(cray.x, cray.y, cray.z));
- }
+
+ View3D &view = *(mode==CATALOGUE ? cat_view : main_view);
+ const GL::Vector3 &cpos = view.get_camera().get_position();
+ GL::Vector4 cray = view.get_camera().unproject(GL::Vector4(xx, yy, 0, 0));
+
+ return view.get_layout().get_layout().pick_track(Vector(cpos.x, cpos.y, cpos.z), Vector(cray.x, cray.y, cray.z));
}
void Designer::update_track_icon(Track3D &track)
#include "3d/layout.h"
#include "3d/overlay.h"
#include "3d/track.h"
+#include "3d/view.h"
#include "cameracontroller.h"
#include "manipulator.h"
#include "measure.h"
R2C2::Catalogue catalogue;
R2C2::Layout3D *cat_layout_3d;
+ R2C2::View3D *cat_view;
+
std::string filename;
R2C2::Layout *layout;
R2C2::Layout3D *layout_3d;
+ R2C2::View3D *main_view;
R2C2::Overlay3D *overlay;
Msp::GL::Object *base_object;
R2C2::Route *cur_route;
R2C2::Zone *cur_zone;
std::list<R2C2::Track *> new_tracks;
- Msp::GL::Pipeline *pipeline;
- Msp::GL::Camera camera;
- Msp::GL::Lighting lighting;
- Msp::GL::Light light;
Mode mode;
Selection selection;
Manipulator manipulator;
Measure measure;
- CameraController camera_ctl;
+ CameraController *camera_ctl;
TrackWrap *track_wrap;
Msp::Time::TimeStamp last_tick;
const R2C2::Catalogue &get_catalogue() const { return catalogue; }
R2C2::Layout &get_layout() { return *layout; }
R2C2::Layout3D &get_layout_3d() { return *layout_3d; }
- const CameraController &get_camera_controller() const { return camera_ctl; }
+ const CameraController &get_camera_controller() const { return *camera_ctl; }
const Msp::GLtk::Resources &get_ui_resources() const { return ui_res; }
Msp::GLtk::Root &get_root() { return root; }