]> git.tdb.fi Git - r2c2.git/blobdiff - source/3d/view.cpp
Only use postprocessors that can be supported
[r2c2.git] / source / 3d / view.cpp
index 8b63553d6fc348f294cd9a5e245c399301de6e16..40a168f86fc7e5986dd14db78de668b0680c3900 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/gl/blend.h>
 #include <msp/gl/tests.h>
 #include "layout.h"
 #include "track.h"
@@ -10,37 +11,107 @@ namespace R2C2 {
 
 View3D::View3D(Layout3D &l, unsigned w, unsigned h):
        layout(l),
-       pipeline(w, h)
+       width(w),
+       height(h),
+       pipeline(w, h),
+       sky(layout.get_catalogue()),
+       shadow(4096, layout.get_scene(), layout.get_sun())
 {
+       pipeline.set_hdr(true);
        pipeline.set_camera(&camera);
-       pipeline.add_renderable_for_pass(layout.get_scene(), 0);
+       pipeline.add_renderable(sky);
+       pipeline.add_renderable_for_pass(shadow, 0);
+       pipeline.add_renderable_for_pass(layout.get_scene(), "translucent");
 
-       GL::Pipeline::Pass *pass = &pipeline.add_pass(0);
+       GL::Pipeline::Pass *pass = &pipeline.add_pass("sky");
+       pass->set_lighting(&layout.get_lighting());
+
+       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());
+
+       try
+       {
+               ambient_occlusion = new GL::AmbientOcclusion(w, h, 100);
+               pipeline.add_postprocessor(*ambient_occlusion);
+       }
+       catch(...)
+       {
+               delete ambient_occlusion;
+               ambient_occlusion = 0;
+       }
+
+       try
+       {
+               colorcurve = new GL::ColorCurve;
+               colorcurve->set_srgb();
+               pipeline.add_postprocessor(*colorcurve);
+       }
+       catch(...)
+       {
+               delete colorcurve;
+               colorcurve = 0;
+       }
+
+       update_shadow_area();
+       layout.get_layout().signal_object_added.connect(sigc::hide(sigc::mem_fun(this, &View3D::update_shadow_area)));
+
        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();
 }
 
-void View3D::view_all(bool tight)
+View3D::~View3D()
 {
-       Vector minp;
-       Vector maxp;
+       delete colorcurve;
+       delete ambient_occlusion;
+}
 
-       layout.get_bounds(minp, maxp);
+Ray View3D::create_ray(int x, int y) const
+{
+       return create_ray(x*2.0f/width-1.0f, y*2.0f/height-1.0f);
+}
 
-       float t = tan(camera.get_field_of_view()/2)*2;
+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, ray.slice<3>(0));
+}
+
+void View3D::compute_bounds(Vector &minp, Vector &maxp)
+{
+       const set<Object *> &objects = layout.get_layout().get_all<Object>();
+       Geometry::BoundingBox<float, 3> bbox;
+       for(set<Object *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
+               bbox = bbox|(*i)->get_bounding_box();
+
+       minp = bbox.get_minimum_point();
+       maxp = bbox.get_maximum_point();
+}
+
+void View3D::view_all(bool tight)
+{
+       Vector minp, maxp;
+       compute_bounds(minp, maxp);
+
+       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)*size;
+               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()
@@ -48,4 +119,11 @@ void View3D::render()
        pipeline.render();
 }
 
+void View3D::update_shadow_area()
+{
+       Vector minp, maxp;
+       compute_bounds(minp, maxp);
+       shadow.set_target((minp+maxp)/2.0f, max((maxp-minp).norm()/2.0f, 0.1f));
+}
+
 } // namespace R2C2