1 | #include "Mesh.h"
|
---|
2 | #include "glInterface.h"
|
---|
3 | #include "OcclusionQuery.h"
|
---|
4 | #include "GlRenderer.h"
|
---|
5 | #include "ViewCellsManager.h"
|
---|
6 | #include "SceneGraph.h"
|
---|
7 | #include "Pvs.h"
|
---|
8 | #include "Viewcell.h"
|
---|
9 | #include "Beam.h"
|
---|
10 | #include "KdTree.h"
|
---|
11 | #include "Environment.h"
|
---|
12 |
|
---|
13 | //#include <Cg/cg.h>
|
---|
14 | //#include <Cg/cgGL.h>
|
---|
15 |
|
---|
16 |
|
---|
17 | namespace GtpVisibilityPreprocessor {
|
---|
18 |
|
---|
19 |
|
---|
20 | static bool arbQuerySupport = false;
|
---|
21 | static bool nvQuerySupport = false;
|
---|
22 |
|
---|
23 |
|
---|
24 | static void InitExtensions()
|
---|
25 | {
|
---|
26 | GLenum err = glewInit();
|
---|
27 |
|
---|
28 | if (GLEW_OK != err)
|
---|
29 | {
|
---|
30 | // problem: glewInit failed, something is seriously wrong
|
---|
31 | cerr << "Error: " << glewGetErrorString(err) << endl;
|
---|
32 | exit(1);
|
---|
33 | }
|
---|
34 |
|
---|
35 | if (GLEW_ARB_occlusion_query)
|
---|
36 | arbQuerySupport = true;
|
---|
37 |
|
---|
38 | if (GLEW_NV_occlusion_query)
|
---|
39 | nvQuerySupport = true;
|
---|
40 |
|
---|
41 |
|
---|
42 | if (!arbQuerySupport && !nvQuerySupport)
|
---|
43 | {
|
---|
44 | cout << "I require the GL_ARB_occlusion_query or the GL_NV_occlusion_query OpenGL extension to work.\n";
|
---|
45 | exit(1);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 |
|
---|
50 | GlRenderer::GlRenderer(SceneGraph *sceneGraph,
|
---|
51 | ViewCellsManager *viewCellsManager,
|
---|
52 | KdTree *tree):
|
---|
53 | Renderer(sceneGraph, viewCellsManager),
|
---|
54 | mKdTree(tree)
|
---|
55 | {
|
---|
56 | mSceneGraph->CollectObjects(&mObjects);
|
---|
57 |
|
---|
58 | // mViewCellsManager->GetViewPoint(mViewPoint);
|
---|
59 |
|
---|
60 | mViewPoint = mSceneGraph->GetBox().Center();
|
---|
61 | mViewDirection = Vector3(0,0,1);
|
---|
62 |
|
---|
63 | // mViewPoint = Vector3(991.7, 187.8, -271);
|
---|
64 | // mViewDirection = Vector3(0.9, 0, -0.4);
|
---|
65 |
|
---|
66 | // timerId = startTimer(10);
|
---|
67 | // debug coords for atlanta
|
---|
68 | // mViewPoint = Vector3(3473, 6.778, -1699);
|
---|
69 | // mViewDirection = Vector3(-0.2432, 0, 0.97);
|
---|
70 |
|
---|
71 | mFrame = 0;
|
---|
72 | mWireFrame = false;
|
---|
73 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
74 | mSnapErrorFrames = true;
|
---|
75 | mSnapPrefix = "snap/";
|
---|
76 | mUseForcedColors = false;
|
---|
77 |
|
---|
78 | mUseGlLists = true;
|
---|
79 | //mUseGlLists = false;
|
---|
80 | }
|
---|
81 |
|
---|
82 | GlRenderer::~GlRenderer()
|
---|
83 | {
|
---|
84 | cerr<<"gl renderer destructor..\n";
|
---|
85 |
|
---|
86 | //CLEAR_CONTAINER(sQueries);
|
---|
87 | CLEAR_CONTAINER(mOcclusionQueries);
|
---|
88 |
|
---|
89 | cerr<<"done."<<endl;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | void
|
---|
94 | GlRenderer::RenderIntersectable(Intersectable *object)
|
---|
95 | {
|
---|
96 |
|
---|
97 | glPushAttrib(GL_CURRENT_BIT);
|
---|
98 | if (mUseFalseColors)
|
---|
99 | SetupFalseColor(object->mId);
|
---|
100 |
|
---|
101 |
|
---|
102 | switch (object->Type()) {
|
---|
103 | case Intersectable::MESH_INSTANCE:
|
---|
104 | RenderMeshInstance((MeshInstance *)object);
|
---|
105 | break;
|
---|
106 | case Intersectable::VIEW_CELL:
|
---|
107 | RenderViewCell(dynamic_cast<ViewCell *>(object));
|
---|
108 | break;
|
---|
109 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
110 | RenderTransformedMeshInstance(dynamic_cast<TransformedMeshInstance *>(object));
|
---|
111 | break;
|
---|
112 | default:
|
---|
113 | cerr<<"Rendering this object not yet implemented\n";
|
---|
114 | break;
|
---|
115 | }
|
---|
116 |
|
---|
117 | glPopAttrib();
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | void
|
---|
122 | GlRenderer::RenderViewCell(ViewCell *vc)
|
---|
123 | {
|
---|
124 | if (vc->GetMesh()) {
|
---|
125 |
|
---|
126 | if (!mUseFalseColors) {
|
---|
127 | if (vc->GetValid())
|
---|
128 | glColor3f(0,1,0);
|
---|
129 | else
|
---|
130 | glColor3f(0,0,1);
|
---|
131 | }
|
---|
132 |
|
---|
133 | RenderMesh(vc->GetMesh());
|
---|
134 | } else {
|
---|
135 | // render viewcells in the subtree
|
---|
136 | if (!vc->IsLeaf()) {
|
---|
137 | ViewCellInterior *vci = (ViewCellInterior *) vc;
|
---|
138 |
|
---|
139 | ViewCellContainer::iterator it = vci->mChildren.begin();
|
---|
140 | for (; it != vci->mChildren.end(); ++it) {
|
---|
141 | RenderViewCell(*it);
|
---|
142 | }
|
---|
143 | } else {
|
---|
144 | // cerr<<"Empty viewcell mesh\n";
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | void
|
---|
151 | GlRenderer::RenderMeshInstance(MeshInstance *mi)
|
---|
152 | {
|
---|
153 | RenderMesh(mi->GetMesh());
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | void
|
---|
158 | GlRenderer::RenderTransformedMeshInstance(TransformedMeshInstance *mi)
|
---|
159 | {
|
---|
160 | // apply world transform before rendering
|
---|
161 | Matrix4x4 m;
|
---|
162 | mi->GetWorldTransform(m);
|
---|
163 |
|
---|
164 | glPushMatrix();
|
---|
165 | /* cout << "\n";
|
---|
166 | for (int i = 0; i < 4; ++ i)
|
---|
167 | for (int j = 0; j < 4; ++ j)
|
---|
168 | cout << m.x[i][j] << " "; cout << "\n"*/
|
---|
169 |
|
---|
170 | glMultMatrixf((float *)m.x);
|
---|
171 |
|
---|
172 | /*GLfloat dummy[16];
|
---|
173 | glGetFloatv(GL_MODELVIEW_MATRIX, dummy);
|
---|
174 | for (int i = 0; i < 16; ++ i)
|
---|
175 | cout << dummy[i] << " ";
|
---|
176 | cout << endl;*/
|
---|
177 | RenderMesh(mi->GetMesh());
|
---|
178 |
|
---|
179 | glPopMatrix();
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | void
|
---|
184 | GlRenderer::SetupFalseColor(const int id)
|
---|
185 | {
|
---|
186 | // swap bits of the color
|
---|
187 | glColor3ub(id&255, (id>>8)&255, (id>>16)&255);
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | int GlRenderer::GetId(int r, int g, int b) const
|
---|
192 | {
|
---|
193 | return r + (g << 8) + (b << 16);
|
---|
194 | }
|
---|
195 |
|
---|
196 | void
|
---|
197 | GlRenderer::SetupMaterial(Material *m)
|
---|
198 | {
|
---|
199 | if (m)
|
---|
200 | glColor3fv(&(m->mDiffuseColor.r));
|
---|
201 | }
|
---|
202 |
|
---|
203 | void
|
---|
204 | GlRenderer::RenderMesh(Mesh *mesh)
|
---|
205 | {
|
---|
206 | int i = 0;
|
---|
207 |
|
---|
208 | if (!mUseFalseColors && !mUseForcedColors)
|
---|
209 | SetupMaterial(mesh->mMaterial);
|
---|
210 |
|
---|
211 | for (i=0; i < mesh->mFaces.size(); i++) {
|
---|
212 | if (mWireFrame)
|
---|
213 | glBegin(GL_LINE_LOOP);
|
---|
214 | else
|
---|
215 | glBegin(GL_POLYGON);
|
---|
216 |
|
---|
217 | Face *face = mesh->mFaces[i];
|
---|
218 | for (int j = 0; j < face->mVertexIndices.size(); j++) {
|
---|
219 | glVertex3fv(&mesh->mVertices[face->mVertexIndices[j]].x);
|
---|
220 | }
|
---|
221 | glEnd();
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | void
|
---|
226 | GlRenderer::InitGL()
|
---|
227 | {
|
---|
228 | glMatrixMode(GL_PROJECTION);
|
---|
229 | glLoadIdentity();
|
---|
230 |
|
---|
231 | glMatrixMode(GL_MODELVIEW);
|
---|
232 | glLoadIdentity();
|
---|
233 |
|
---|
234 | glEnable(GL_CULL_FACE);
|
---|
235 | glShadeModel(GL_FLAT);
|
---|
236 | glEnable(GL_DEPTH_TEST);
|
---|
237 | glEnable(GL_CULL_FACE);
|
---|
238 |
|
---|
239 | InitExtensions();
|
---|
240 |
|
---|
241 | #if 0
|
---|
242 | GLfloat mat_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
|
---|
243 | /* mat_specular and mat_shininess are NOT default values */
|
---|
244 | GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
|
---|
245 | GLfloat mat_specular[] = { 0.3, 0.3, 0.3, 1.0 };
|
---|
246 | GLfloat mat_shininess[] = { 1.0 };
|
---|
247 |
|
---|
248 | GLfloat light_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
|
---|
249 | GLfloat light_diffuse[] = { 0.4, 0.4, 0.4, 1.0 };
|
---|
250 | GLfloat light_specular[] = { 0.3, 0.3, 0.3, 1.0 };
|
---|
251 |
|
---|
252 | GLfloat lmodel_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
|
---|
253 |
|
---|
254 |
|
---|
255 | // default Material
|
---|
256 | glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
|
---|
257 | glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
|
---|
258 | glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
|
---|
259 | glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
|
---|
260 |
|
---|
261 | // a light
|
---|
262 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
|
---|
263 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
|
---|
264 | glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
|
---|
265 |
|
---|
266 | glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
|
---|
267 | glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
|
---|
268 | glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
|
---|
269 |
|
---|
270 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
|
---|
271 |
|
---|
272 | glEnable(GL_LIGHTING);
|
---|
273 | glEnable(GL_LIGHT0);
|
---|
274 | glEnable(GL_LIGHT1);
|
---|
275 |
|
---|
276 |
|
---|
277 | // set position of the light
|
---|
278 | GLfloat infinite_light[] = { 1.0, 0.8, 1.0, 0.0 };
|
---|
279 | glLightfv (GL_LIGHT0, GL_POSITION, infinite_light);
|
---|
280 |
|
---|
281 | // set position of the light2
|
---|
282 | GLfloat infinite_light2[] = { -0.3, 1.5, 1.0, 0.0 };
|
---|
283 | glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
|
---|
284 |
|
---|
285 | glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
|
---|
286 | // glColorMaterial( GL_FRONT_AND_BACK, GL_SPECULAR);
|
---|
287 | glEnable(GL_COLOR_MATERIAL);
|
---|
288 |
|
---|
289 | glShadeModel( GL_FLAT );
|
---|
290 |
|
---|
291 | glDepthFunc( GL_LESS );
|
---|
292 | glEnable( GL_DEPTH_TEST );
|
---|
293 | #endif
|
---|
294 |
|
---|
295 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
|
---|
296 |
|
---|
297 | glEnable( GL_NORMALIZE );
|
---|
298 |
|
---|
299 | glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | void
|
---|
304 | GlRenderer::SetupProjection(const int w, const int h, const float angle)
|
---|
305 | {
|
---|
306 | glViewport(0, 0, w, h);
|
---|
307 | glMatrixMode(GL_PROJECTION);
|
---|
308 | glLoadIdentity();
|
---|
309 | gluPerspective(angle, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
|
---|
310 | glMatrixMode(GL_MODELVIEW);
|
---|
311 | }
|
---|
312 |
|
---|
313 | void
|
---|
314 | GlRenderer::SetupCamera()
|
---|
315 | {
|
---|
316 | Vector3 target = mViewPoint + mViewDirection;
|
---|
317 |
|
---|
318 | Vector3 up(0,1,0);
|
---|
319 |
|
---|
320 | if (abs(DotProd(mViewDirection, up)) > 0.99f)
|
---|
321 | up = Vector3(1, 0, 0);
|
---|
322 |
|
---|
323 | glLoadIdentity();
|
---|
324 | gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
|
---|
325 | target.x, target.y, target.z,
|
---|
326 | up.x, up.y, up.z);
|
---|
327 | }
|
---|
328 |
|
---|
329 | void
|
---|
330 | GlRenderer::_RenderScene()
|
---|
331 | {
|
---|
332 | ObjectContainer::const_iterator oi = mObjects.begin();
|
---|
333 |
|
---|
334 | for (; oi != mObjects.end(); oi++)
|
---|
335 | RenderIntersectable(*oi);
|
---|
336 | }
|
---|
337 |
|
---|
338 | bool
|
---|
339 | GlRenderer::RenderScene()
|
---|
340 | {
|
---|
341 | static int glList = -1;
|
---|
342 | if (mUseGlLists) {
|
---|
343 | if (glList == -1) {
|
---|
344 | glList = glGenLists(1);
|
---|
345 | glNewList(glList, GL_COMPILE);
|
---|
346 | _RenderScene();
|
---|
347 | glEndList();
|
---|
348 | }
|
---|
349 | glCallList(glList);
|
---|
350 | } else
|
---|
351 | _RenderScene();
|
---|
352 |
|
---|
353 |
|
---|
354 | return true;
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | void
|
---|
359 | GlRendererBuffer::EvalQueryWithItemBuffer(
|
---|
360 | //RenderCostSample &sample
|
---|
361 | )
|
---|
362 | {
|
---|
363 | // read back the texture
|
---|
364 | glReadPixels(0, 0,
|
---|
365 | GetWidth(), GetHeight(),
|
---|
366 | GL_RGBA,
|
---|
367 | GL_UNSIGNED_BYTE,
|
---|
368 | mPixelBuffer);
|
---|
369 |
|
---|
370 |
|
---|
371 | unsigned int *p = mPixelBuffer;
|
---|
372 |
|
---|
373 | for (int y = 0; y < GetHeight(); y++)
|
---|
374 | {
|
---|
375 | for (int x = 0; x < GetWidth(); x++, p++)
|
---|
376 | {
|
---|
377 | unsigned int id = (*p) & 0xFFFFFF;
|
---|
378 |
|
---|
379 | if (id != 0xFFFFFF)
|
---|
380 | ++ mObjects[id]->mCounter;
|
---|
381 | }
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 |
|
---|
386 |
|
---|
387 | /****************************************************************/
|
---|
388 | /* GlRendererBuffer implementation */
|
---|
389 | /****************************************************************/
|
---|
390 |
|
---|
391 |
|
---|
392 |
|
---|
393 | GlRendererBuffer::GlRendererBuffer(SceneGraph *sceneGraph,
|
---|
394 | ViewCellsManager *viewcells,
|
---|
395 | KdTree *tree):
|
---|
396 | GlRenderer(sceneGraph, viewcells, tree)
|
---|
397 | {
|
---|
398 | mPixelBuffer = NULL;
|
---|
399 |
|
---|
400 | // implement width and height in subclasses
|
---|
401 | }
|
---|
402 |
|
---|
403 |
|
---|
404 | void
|
---|
405 | GlRendererBuffer::EvalQueryWithOcclusionQueries(
|
---|
406 | //RenderCostSample &sample
|
---|
407 | )
|
---|
408 | {
|
---|
409 | glDepthFunc(GL_LEQUAL);
|
---|
410 |
|
---|
411 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
412 | glDepthMask(GL_FALSE);
|
---|
413 |
|
---|
414 |
|
---|
415 | // simulate detectemptyviewspace using backface culling
|
---|
416 | if (mDetectEmptyViewSpace)
|
---|
417 | {
|
---|
418 | glEnable(GL_CULL_FACE);
|
---|
419 | //cout << "culling" << endl;
|
---|
420 | }
|
---|
421 | else
|
---|
422 | {
|
---|
423 | //cout << "not culling" << endl;
|
---|
424 | glDisable(GL_CULL_FACE);
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | //const int numQ = 1;
|
---|
429 | const int numQ = (int)mOcclusionQueries.size();
|
---|
430 |
|
---|
431 | //glFinish();
|
---|
432 | #if 0
|
---|
433 | //-- now issue queries for all objects
|
---|
434 | for (int j = 0; j < (int)mObjects.size(); ++ j)
|
---|
435 | {
|
---|
436 | mOcclusionQueries[j]->BeginQuery();
|
---|
437 | RenderIntersectable(mObjects[j]);
|
---|
438 | mOcclusionQueries[j]->EndQuery();
|
---|
439 |
|
---|
440 | unsigned int pixelCount;
|
---|
441 |
|
---|
442 | pixelCount = mOcclusionQueries[j]->GetQueryResult();
|
---|
443 | mObjects[j]->mCounter += pixelCount;
|
---|
444 | }
|
---|
445 | #else
|
---|
446 |
|
---|
447 | int q = 0;
|
---|
448 |
|
---|
449 | //-- now issue queries for all objects
|
---|
450 | for (int j = 0; j < (int)mObjects.size(); j += q)
|
---|
451 | {
|
---|
452 | for (q = 0; ((j + q) < (int)mObjects.size()) && (q < numQ); ++ q)
|
---|
453 | {
|
---|
454 | //glFinish();
|
---|
455 | mOcclusionQueries[q]->BeginQuery();
|
---|
456 |
|
---|
457 | RenderIntersectable(mObjects[j + q]);
|
---|
458 |
|
---|
459 | mOcclusionQueries[q]->EndQuery();
|
---|
460 | //glFinish();
|
---|
461 | }
|
---|
462 | //cout << "q: " << q << endl;
|
---|
463 | // collect results of the queries
|
---|
464 | for (int t = 0; t < q; ++ t)
|
---|
465 | {
|
---|
466 | unsigned int pixelCount;
|
---|
467 |
|
---|
468 | //-- reenable other state
|
---|
469 | #if 0
|
---|
470 | bool available;
|
---|
471 |
|
---|
472 | do
|
---|
473 | {
|
---|
474 | available = mOcclusionQueries[t]->ResultAvailable();
|
---|
475 |
|
---|
476 | if (!available) cout << "W";
|
---|
477 | }
|
---|
478 | while (!available);
|
---|
479 | #endif
|
---|
480 |
|
---|
481 | pixelCount = mOcclusionQueries[t]->GetQueryResult();
|
---|
482 |
|
---|
483 | //if (pixelCount > 0)
|
---|
484 | // cout <<"o="<<j+q<<" q="<<mOcclusionQueries[q]->GetQueryId()<<" pc="<<pixelCount<<" ";
|
---|
485 | mObjects[j + t]->mCounter += pixelCount;
|
---|
486 | }
|
---|
487 |
|
---|
488 | //j += q;
|
---|
489 | }
|
---|
490 | #endif
|
---|
491 | //glFinish();
|
---|
492 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
493 | glDepthMask(GL_TRUE);
|
---|
494 |
|
---|
495 | glEnable(GL_CULL_FACE);
|
---|
496 | }
|
---|
497 |
|
---|
498 |
|
---|
499 | void
|
---|
500 | GlRendererBuffer::RandomViewPoint()
|
---|
501 | {
|
---|
502 | // do not use this function since it could return different viewpoints for
|
---|
503 | // different executions of the algorithm
|
---|
504 |
|
---|
505 | // mViewCellsManager->GetViewPoint(mViewPoint);
|
---|
506 |
|
---|
507 | while (1) {
|
---|
508 | Vector3 pVector = Vector3(halton.GetNumber(1),
|
---|
509 | halton.GetNumber(2),
|
---|
510 | halton.GetNumber(3));
|
---|
511 |
|
---|
512 | mViewPoint = mViewCellsManager->GetViewSpaceBox().GetPoint(pVector);
|
---|
513 | ViewCell *v = mViewCellsManager->GetViewCell(mViewPoint);
|
---|
514 | if (v && v->GetValid())
|
---|
515 | break;
|
---|
516 | // generate a new vector
|
---|
517 | halton.GenerateNext();
|
---|
518 | }
|
---|
519 |
|
---|
520 | Vector3 dVector = Vector3(2*M_PI*halton.GetNumber(4),
|
---|
521 | M_PI*halton.GetNumber(5),
|
---|
522 | 0.0f);
|
---|
523 |
|
---|
524 | mViewDirection = Normalize(Vector3(sin(dVector.x),
|
---|
525 | // cos(dVector.y),
|
---|
526 | 0.0f,
|
---|
527 | cos(dVector.x)));
|
---|
528 | halton.GenerateNext();
|
---|
529 | }
|
---|
530 |
|
---|
531 |
|
---|
532 | }
|
---|