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");
28 pass->set_lighting(&layout.get_lighting());
30 pass = &pipeline.add_pass(0);
31 pass->set_lighting(&layout.get_lighting());
32 pass->set_depth_test(&GL::DepthTest::lequal());
34 pass = &pipeline.add_pass("translucent");
35 pass->set_lighting(&layout.get_lighting());
36 pass->set_depth_test(&GL::DepthTest::lequal());
37 pass->set_blend(&GL::Blend::alpha());
39 pipeline.add_postprocessor(ambient_occlusion);
40 colorcurve.set_srgb();
41 pipeline.add_postprocessor(colorcurve);
44 layout.get_layout().signal_object_added.connect(sigc::hide(sigc::mem_fun(this, &View3D::update_shadow_area)));
46 camera.set_up_direction(GL::Vector3(0, 0, 1));
48 camera.set_look_direction(GL::Vector3(0, 0.5, -0.866));
49 camera.set_aspect(float(width)/height);
54 Ray View3D::create_ray(int x, int y) const
56 return create_ray(x*2.0f/width-1.0f, y*2.0f/height-1.0f);
59 Ray View3D::create_ray(float x, float y) const
61 const GL::Vector3 &start = camera.get_position();
62 GL::Vector4 ray = camera.unproject(GL::Vector4(x, y, 0, 0));
63 return Ray(start, ray.slice<3>(0));
66 void View3D::compute_bounds(Vector &minp, Vector &maxp)
68 const set<Object *> &objects = layout.get_layout().get_all<Object>();
69 Geometry::BoundingBox<float, 3> bbox;
70 for(set<Object *>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
71 bbox = bbox|(*i)->get_bounding_box();
73 minp = bbox.get_minimum_point();
74 maxp = bbox.get_maximum_point();
77 void View3D::view_all(bool tight)
80 compute_bounds(minp, maxp);
82 float t = tan(camera.get_field_of_view()/2.0f)*2.0f;
83 float size = max((maxp.y-minp.y+0.1), (maxp.x-minp.x+0.1)/camera.get_aspect());
86 dist += sin(camera.get_field_of_view()/2.0f)*size;
87 GL::Vector3 center = (minp+maxp)/2.0f;
88 const GL::Vector3 &look = camera.get_look_direction();
89 camera.set_position(center-look*dist);
90 camera.set_depth_clip(dist*0.02, dist*50);
98 void View3D::update_shadow_area()
101 compute_bounds(minp, maxp);
102 shadow.set_target((minp+maxp)/2.0f, (maxp-minp).norm()/2.0f);