]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/view.cpp.orig
Improve graphics quality with some shaders and effects
[r2c2.git] / source / 3d / view.cpp.orig
diff --git a/source/3d/view.cpp.orig b/source/3d/view.cpp.orig
new file mode 100644 (file)
index 0000000..ef03c4b
--- /dev/null
@@ -0,0 +1,81 @@
+#include <msp/gl/blend.h>
+#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),
+       width(w),
+       height(h),
+       pipeline(w, h),
+       sky(layout.get_catalogue())
+{
+       pipeline.set_camera(&camera);
+       pipeline.add_renderable(sky);
+       pipeline.add_renderable_for_pass(layout.get_scene(), 0);
+       pipeline.add_renderable_for_pass(layout.get_scene(), "translucent");
+
+       GL::Pipeline::Pass *pass = &pipeline.add_pass("sky");
+
+       pass = &pipeline.add_pass(0);
+       pass->set_lighting(&layout.get_lighting());
+       pass->set_depth_test(&GL::DepthTest::lequal());
+
+       pass = &pipeline.add_pass("translucent");
+       pass->set_lighting(&layout.get_lighting());
+       pass->set_depth_test(&GL::DepthTest::lequal());
+       pass->set_blend(&GL::Blend::alpha());
+
+       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();
+}
+
+Ray View3D::create_ray(int x, int y) const
+{
+       return create_ray(x*2.0f/width-1.0f, y*2.0f/height-1.0f);
+}
+
+Ray View3D::create_ray(float x, float y) const
+{
+       const GL::Vector3 &start = camera.get_position();
+       GL::Vector4 ray = camera.unproject(GL::Vector4(x, y, 0, 0));
+       return Ray(start, Vector(ray));
+}
+
+void View3D::view_all(bool tight)
+{
+       const set<Track *> &tracks = layout.get_layout().get_all<Track>();
+       Geometry::BoundingBox<float, 3> bbox;
+       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               bbox = bbox|(*i)->get_bounding_box();
+
+       const Vector &minp = bbox.get_minimum_point();
+       const Vector &maxp = bbox.get_maximum_point();
+
+       float t = tan(camera.get_field_of_view()/2.0f)*2.0f;
+       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.0f)*size;
+       GL::Vector3 center = (minp+maxp)/2.0f;
+       const GL::Vector3 &look = camera.get_look_direction();
+       camera.set_position(center-look*dist);
+       camera.set_depth_clip(dist*0.02, dist*50);
+}
+
+void View3D::render()
+{
+       pipeline.render();
+}
+
+} // namespace R2C2