1 #include <msp/gl/blend.h>
2 #include <msp/gl/tests.h>
12 View3D::View3D(Layout3D &l, unsigned w, unsigned h):
17 sky(layout.get_catalogue()),
18 shadow(4096, layout.get_scene(), layout.get_sun()),
19 ambient_occlusion(w, h, 100)
21 pipeline.set_hdr(true);
22 pipeline.set_camera(&camera);
23 pipeline.add_renderable(sky);
24 pipeline.add_renderable_for_pass(shadow, 0);
25 pipeline.add_renderable_for_pass(layout.get_scene(), "translucent");
27 GL::Pipeline::Pass *pass = &pipeline.add_pass("sky");
29 pass = &pipeline.add_pass(0);
30 pass->set_lighting(&layout.get_lighting());
31 pass->set_depth_test(&GL::DepthTest::lequal());
33 pass = &pipeline.add_pass("translucent");
34 pass->set_lighting(&layout.get_lighting());
35 pass->set_depth_test(&GL::DepthTest::lequal());
36 pass->set_blend(&GL::Blend::alpha());
38 pipeline.add_postprocessor(ambient_occlusion);
39 colorcurve.set_srgb();
40 pipeline.add_postprocessor(colorcurve);
43 layout.get_layout().signal_object_added.connect(sigc::hide(sigc::mem_fun(this, &View3D::update_shadow_area)));
45 camera.set_up_direction(GL::Vector3(0, 0, 1));
47 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
48 camera.set_aspect(float(width)/height);
53 Ray View3D::create_ray(int x, int y) const
55 return create_ray(x*2.0f/width-1.0f, y*2.0f/height-1.0f);
58 Ray View3D::create_ray(float x, float y) const
60 const GL::Vector3 &start = camera.get_position();
61 GL::Vector4 ray = camera.unproject(GL::Vector4(x, y, 0, 0));
62 return Ray(start, Vector(ray));
65 void View3D::compute_bounds(Vector &minp, Vector &maxp)
67 const set<Object *> &objects = layout.get_layout().get_all<Object>();
68 Geometry::BoundingBox<float, 3> bbox;
69 for(set<Object *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
70 bbox = bbox|(*i)->get_bounding_box();
72 minp = bbox.get_minimum_point();
73 maxp = bbox.get_maximum_point();
76 void View3D::view_all(bool tight)
79 compute_bounds(minp, maxp);
81 float t = tan(camera.get_field_of_view()/2.0f)*2.0f;
82 float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
85 dist += sin(camera.get_field_of_view()/2.0f)*size;
86 GL::Vector3 center = (minp+maxp)/2.0f;
87 const GL::Vector3 &look = camera.get_look_direction();
88 camera.set_position(center-look*dist);
89 camera.set_depth_clip(dist*0.02, dist*50);
97 void View3D::update_shadow_area()
100 compute_bounds(minp, maxp);
101 shadow.set_target((minp+maxp)/2.0f, (maxp-minp).norm()/2.0f);