2 #include <msp/gl/extensions/arb_occlusion_query.h>
4 #include "occludedscene.h"
12 const char vshader[] =
15 " gl_Position = gl_ProjectionMatrix*gl_ModelViewMatrix*gl_Vertex;\n"
18 const char fshader[] =
21 " gl_FragColor = vec4(1.0);\n"
29 OccludedScene::OccludedScene():
30 bounding_mesh((VERTEX3, NORMAL3)),
31 bounding_shader(vshader, fshader),
32 occluder_min_size(0.25f),
35 static Require req(ARB_occlusion_query);
37 /* Use a slightly larger radius to ensure that all parts of the renderable
38 fit inside the icosahedron */
39 IcoSphereBuilder(1.26f, 1).build(bounding_mesh);
40 bounding_mesh.set_winding(&WindingTest::counterclockwise());
43 OccludedScene::~OccludedScene()
45 vector<unsigned> queries;
46 queries.reserve(occluded_cache.size());
47 for(OccludedArray::iterator i=occluded_cache.begin(); i!=occluded_cache.end(); ++i)
48 queries.push_back(i->query);
49 glDeleteQueries(queries.size(), &queries[0]);
52 void OccludedScene::add(const Renderable &r)
54 renderables.insert(&r);
58 void OccludedScene::remove(const Renderable &r)
60 renderables.erase(&r);
64 void OccludedScene::render(Renderer &renderer, const Tag &tag) const
66 if(renderables.empty())
69 const Camera *camera = renderer.get_camera();
72 for(RenderableSet::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
73 renderer.render(**i, tag);
79 if(occluded_cache.size()<renderables.size())
81 unsigned old_size = occluded_cache.size();
82 occluded_cache.resize(renderables.size());
83 vector<unsigned> new_queries(occluded_cache.size()-old_size);
84 glGenQueries(new_queries.size(), &new_queries[0]);
85 for(unsigned i=0; i<new_queries.size(); ++i)
86 occluded_cache[old_size+i].query = new_queries[i];
89 OccludedArray::iterator j = occluded_cache.begin();
90 for(RenderableSet::iterator i=renderables.begin(); i!=renderables.end(); ++i, ++j)
92 for(; j!=occluded_cache.end(); ++j)
95 j->in_frustum = false;
101 const Vector3 &camera_pos = camera->get_position();
102 const Vector3 &look_dir = camera->get_look_direction();
103 float near_clip = camera->get_near_clip();
104 float frustum_h = tan(camera->get_field_of_view()/2.0f)*2.0f;
106 // Perform frustum culling and render any major occluders
107 bool use_frustum = setup_frustum(renderer);
108 for(OccludedArray::iterator i=occluded_cache.begin(); (i!=occluded_cache.end() && i->renderable); ++i)
110 i->in_frustum = (!use_frustum || !frustum_cull(*i->renderable));
114 const Matrix *matrix = i->renderable->get_matrix();
115 i->bounding_sphere = i->renderable->get_bounding_sphere();
116 if(matrix && i->bounding_sphere)
118 float depth = dot(*matrix*i->bounding_sphere->get_center()-camera_pos, look_dir);
119 float size = i->bounding_sphere->get_radius()*2/max(depth, near_clip);
120 i->occluder = (size>frustum_h*occluder_min_size);
123 // If the size can't be calculated, treat it as occluder
127 renderer.render(*i->renderable, tag);
130 // Move all objects within the frustum to the beginning of the array
131 for(OccludedArray::iterator i=occluded_cache.begin(), j=i+renderables.size()-1; i!=j; )
135 else if(j->in_frustum)
142 Renderer::Push push(renderer);
143 renderer.set_shader_program(&bounding_shader);
145 glColorMask(false, false, false, false);
148 // Fire off the occlusion queries
149 for(OccludedArray::const_iterator i=occluded_cache.begin(); (i!=occluded_cache.end() && i->in_frustum); ++i)
152 glBeginQuery(GL_SAMPLES_PASSED, i->query);
153 Renderer::Push push2(renderer);
154 renderer.transform(Matrix(*i->renderable->get_matrix())
155 .translate(i->bounding_sphere->get_center())
156 .scale(i->bounding_sphere->get_radius()));
157 bounding_mesh.draw(renderer);
158 glEndQuery(GL_SAMPLES_PASSED);
161 glColorMask(true, true, true, true);
165 // Render anything that has a chance of being visible
166 for(OccludedArray::const_iterator i=occluded_cache.begin(); (i!=occluded_cache.end() && i->in_frustum); ++i)
170 glGetQueryObjectiv(i->query, GL_QUERY_RESULT, &samples_passed);
172 renderer.render(*i->renderable, tag);
177 OccludedScene::OccludedRenderable::OccludedRenderable():