]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/view.cpp
Add View3D class to bundle Layout3D with a camera and a pipeline
[r2c2.git] / source / 3d / view.cpp
diff --git a/source/3d/view.cpp b/source/3d/view.cpp
new file mode 100644 (file)
index 0000000..006abda
--- /dev/null
@@ -0,0 +1,58 @@
+/* $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