From: Mikko Rasa Date: Sun, 21 Jul 2013 15:25:59 +0000 (+0300) Subject: Make use of View3D in engineer X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=5fa009f4af8d0a2e4f52929e1ee29f5c93986ab6;p=r2c2.git Make use of View3D in engineer --- diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index 72e5052..fe3514f 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -16,6 +16,8 @@ Layout3D::Layout3D(Layout &l): { // South, 15° from zenith sun.set_position(0, -0.259, 0.966, 0); + sun.set_diffuse(GL::Color(0.9)); + lighting.set_ambient(GL::Color(0.4)); lighting.attach(0, sun); layout.signal_object_added.connect(sigc::mem_fun(this, &Layout3D::object_added)); diff --git a/source/3d/view.cpp b/source/3d/view.cpp index 017a526..8943abc 100644 --- a/source/3d/view.cpp +++ b/source/3d/view.cpp @@ -10,6 +10,8 @@ namespace R2C2 { View3D::View3D(Layout3D &l, unsigned w, unsigned h): layout(l), + width(w), + height(h), pipeline(w, h) { pipeline.set_camera(&camera); @@ -22,6 +24,7 @@ View3D::View3D(Layout3D &l, unsigned w, unsigned h): camera.set_up_direction(GL::Vector3(0, 0, 1)); // Y+, 60° down camera.set_look_direction(GL::Vector3(0, 0.5, -0.866)); + camera.set_aspect(float(width)/height); view_all(); } diff --git a/source/3d/view.h b/source/3d/view.h index f3e359c..ffe608e 100644 --- a/source/3d/view.h +++ b/source/3d/view.h @@ -12,6 +12,8 @@ class View3D { protected: Layout3D &layout; + unsigned width; + unsigned height; Msp::GL::Camera camera; Msp::GL::Pipeline pipeline; diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index f0999d9..f89fbd1 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -42,7 +42,7 @@ Engineer::Engineer(int argc, char **argv): layout(catalogue, (options.driver.empty() ? 0 : Driver::create(options.driver))), layout_3d(layout), server(0), - pipeline(window.get_width(), window.get_height(), false), + main_view(layout_3d, window.get_width(), window.get_height()), picking(false), picking_track(0), picking_entry(0), @@ -95,25 +95,15 @@ Engineer::Engineer(int argc, char **argv): // Setup 3D view DataFile::load(arrow_mesh, "arrow.mesh"); - pipeline.set_camera(&camera); - pipeline.add_renderable_for_pass(layout_3d.get_scene(), 0); - pipeline.add_renderable_for_pass(layout_3d.get_path_scene(), "unlit"); - pipeline.add_renderable_for_pass(*overlay, "overlay"); + GL::Pipeline &pipeline = main_view.get_pipeline(); - light.set_position(GL::Vector4(0, -0.259, 0.966, 0)); - light.set_diffuse(GL::Color(0.9)); - lighting.set_ambient(GL::Color(0.4)); - lighting.attach(0, light); - - GL::Pipeline::Pass *pass = &pipeline.add_pass(0); - pass->set_depth_test(&GL::DepthTest::lequal()); - pass->set_lighting(&lighting); - - pass = &pipeline.add_pass("unlit"); + GL::Pipeline::Pass *pass = &pipeline.add_pass("unlit"); pass->set_depth_test(&GL::DepthTest::lequal()); + pipeline.add_renderable_for_pass(layout_3d.get_path_scene(), "unlit"); pass = &pipeline.add_pass("overlay"); pass->set_blend(&GL::Blend::alpha()); + pipeline.add_renderable_for_pass(*overlay, "overlay"); view_all(); @@ -197,7 +187,7 @@ void Engineer::tick() GL::Framebuffer::system().clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT); - pipeline.render(); + main_view.render(); if(pointer_moved) { @@ -222,7 +212,7 @@ void Engineer::tick() if(picking && picking_track && picking_entry>=0) { - camera.apply(); + main_view.get_camera().apply(); GL::MatrixStack::Push push_mat(GL::MatrixStack::modelview()); Snap sn = picking_track->get_snap_node(picking_entry); @@ -309,7 +299,7 @@ void Engineer::view_all() const Layout3D::ObjectMap &objects = layout_3d.get_all(); float view_aspect = float(window.get_width())/window.get_height(); - float view_height = tan(camera.get_field_of_view()/2.0f)*2.0f; + float view_height = tan(main_view.get_camera().get_field_of_view()/2.0f)*2.0f; float best_score = 0; GL::Vector3 pos; GL::Vector3 up; @@ -344,6 +334,7 @@ void Engineer::view_all() } } + GL::Camera &camera = main_view.get_camera(); camera.set_position(pos); camera.set_up_direction(up); camera.set_look_direction(GL::Vector3(0, 0, -1)); @@ -353,8 +344,8 @@ void Engineer::view_all() Object *Engineer::pick_object(const Vector &p) { - const GL::Vector3 &start = camera.get_position(); - GL::Vector4 ray = camera.unproject(GL::Vector4(p.x, p.y, 0, 0)); + const GL::Vector3 &start = main_view.get_camera().get_position(); + GL::Vector4 ray = main_view.get_camera().unproject(GL::Vector4(p.x, p.y, 0, 0)); // XXX Do this better; make this function a template? if(Vehicle *veh = layout.pick(Ray(start, Vector(ray)))) diff --git a/source/engineer/engineer.h b/source/engineer/engineer.h index d7fb2b8..612fe66 100644 --- a/source/engineer/engineer.h +++ b/source/engineer/engineer.h @@ -14,6 +14,7 @@ #include "3d/layout.h" #include "3d/overlay.h" #include "3d/path.h" +#include "3d/view.h" #include "network/server.h" #include "options.h" @@ -42,10 +43,7 @@ private: R2C2::Overlay3D *overlay; std::map train_colors; - Msp::GL::Camera camera; - Msp::GL::Lighting lighting; - Msp::GL::Light light; - Msp::GL::Pipeline pipeline; + R2C2::View3D main_view; Msp::GL::Mesh arrow_mesh; MainWindow *main_wnd; @@ -68,7 +66,7 @@ public: const R2C2::Catalogue &get_catalogue() const { return catalogue; } R2C2::Layout &get_layout() { return layout; } R2C2::Layout3D &get_layout_3d() { return layout_3d; } - const Msp::GL::Lighting &get_lighting() const { return lighting; } + R2C2::View3D &get_main_view() { return main_view; } void set_status(const std::string &); void add_train_view(TrainView &); void remove_train_view(TrainView &); diff --git a/source/engineer/trainview.cpp b/source/engineer/trainview.cpp index ad534ac..1078009 100644 --- a/source/engineer/trainview.cpp +++ b/source/engineer/trainview.cpp @@ -17,7 +17,7 @@ TrainView::TrainView(Engineer &e, const Train &t): train(t), mode(SIDE), forward(true), - pipeline(400, 300, false) + view(engineer.get_layout_3d(), 400, 300) { Loader::WidgetMap widgets; DataFile::load(*this, "data/trainview.ui", widgets); @@ -35,16 +35,7 @@ TrainView::TrainView(Engineer &e, const Train &t): depth.storage(GL::DEPTH_COMPONENT, 400, 300); fbo.attach(GL::DEPTH_ATTACHMENT, depth); - camera.set_up_direction(GL::Vector3(0, 0, 1)); - camera.set_depth_clip(0.01, 10); - camera.set_aspect(1); - pipeline.set_camera(&camera); - - pipeline.add_renderable(engineer.get_layout_3d().get_scene()); - - GL::Pipeline::Pass *pass = &pipeline.add_pass(0); - pass->set_depth_test(&GL::DepthTest::lequal()); - pass->set_lighting(&engineer.get_lighting()); + view.get_camera().set_depth_clip(0.01, 10); dynamic_cast(get_item(widgets, "img_view"))->set_image(&tex); @@ -83,6 +74,7 @@ void TrainView::prepare() Vector side_vec = rotated_vector(Vector(0, -1, 0), angle); float l = veh.get_type().get_length(); + GL::Camera &camera = view.get_camera(); switch(mode) { case ROOF: @@ -101,5 +93,5 @@ void TrainView::prepare() GL::Bind _bind_fbo(fbo); fbo.clear(GL::COLOR_BUFFER_BIT|GL::DEPTH_BUFFER_BIT); - pipeline.render(); + view.render(); } diff --git a/source/engineer/trainview.h b/source/engineer/trainview.h index 6ee9b4e..598b43f 100644 --- a/source/engineer/trainview.h +++ b/source/engineer/trainview.h @@ -29,8 +29,7 @@ private: Msp::GL::Framebuffer fbo; Msp::GL::Texture2D tex; Msp::GL::Renderbuffer depth; - Msp::GL::Camera camera; - Msp::GL::Pipeline pipeline; + R2C2::View3D view; public: TrainView(Engineer &, const R2C2::Train &);