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 "ViewCell.h"
|
---|
8 | #include "Beam.h"
|
---|
9 | #include "KdTree.h"
|
---|
10 | #include "Environment.h"
|
---|
11 | #include "Triangle3.h"
|
---|
12 | #include "IntersectableWrapper.h"
|
---|
13 | #include "BvHierarchy.h"
|
---|
14 | #include "KdTree.h"
|
---|
15 | #include "SamplingStrategy.h"
|
---|
16 | #include "Preprocessor.h"
|
---|
17 | #include "SceneGraph.h"
|
---|
18 |
|
---|
19 |
|
---|
20 | #ifdef USE_CG
|
---|
21 |
|
---|
22 | #include <Cg/cg.h>
|
---|
23 | #include <Cg/cgGL.h>
|
---|
24 |
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | // if 1 = SAFE RENDERING OF PVS primitives without VBOs for Pvs error estimation
|
---|
28 | #define EVAL_ERROR 0
|
---|
29 |
|
---|
30 | namespace GtpVisibilityPreprocessor {
|
---|
31 |
|
---|
32 |
|
---|
33 | static bool arbQuerySupport = false;
|
---|
34 | static bool nvQuerySupport = false;
|
---|
35 |
|
---|
36 | static GLuint frontDepthMap;
|
---|
37 | static GLuint backDepthMap;
|
---|
38 |
|
---|
39 | const int depthMapSize = 512;
|
---|
40 |
|
---|
41 |
|
---|
42 | static void InitExtensions()
|
---|
43 | {
|
---|
44 | GLenum err = glewInit();
|
---|
45 |
|
---|
46 | if (GLEW_OK != err)
|
---|
47 | {
|
---|
48 | // problem: glewInit failed, something is seriously wrong
|
---|
49 | cerr << "Error: " << glewGetErrorString(err) << endl;
|
---|
50 | exit(1);
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (GLEW_ARB_occlusion_query)
|
---|
54 | arbQuerySupport = true;
|
---|
55 |
|
---|
56 | if (GLEW_NV_occlusion_query)
|
---|
57 | nvQuerySupport = true;
|
---|
58 |
|
---|
59 | if (!arbQuerySupport && !nvQuerySupport)
|
---|
60 | {
|
---|
61 | cout << "I require the GL_ARB_occlusion_query or the GL_NV_occlusion_query OpenGL extension to work.\n";
|
---|
62 | exit(1);
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (!GLEW_ARB_vertex_buffer_object)
|
---|
66 | {
|
---|
67 | cout << "vbos not supported" << endl;
|
---|
68 | }
|
---|
69 | else
|
---|
70 | {
|
---|
71 | cout << "vbos supported" << endl;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | GlRenderer::GlRenderer(SceneGraph *sceneGraph,
|
---|
77 | ViewCellsManager *viewCellsManager,
|
---|
78 | KdTree *tree):
|
---|
79 | Renderer(sceneGraph, viewCellsManager),
|
---|
80 | mKdTree(tree),
|
---|
81 | mUseFalseColors(false),
|
---|
82 | mVboId(-1),
|
---|
83 | mData(NULL),
|
---|
84 | mIndices(NULL),
|
---|
85 | mComputeGVS(false),
|
---|
86 | mCurrentFrame(100)
|
---|
87 | {
|
---|
88 | mSceneGraph->CollectObjects(mObjects);
|
---|
89 |
|
---|
90 | if (0)
|
---|
91 | {
|
---|
92 | viewCellsManager->GetViewPoint(mViewPoint);
|
---|
93 | mViewDirection = Vector3(0,0,1);
|
---|
94 | }
|
---|
95 | else
|
---|
96 | {
|
---|
97 |
|
---|
98 | // for sg snapshot
|
---|
99 | mViewPoint = Vector3(32.8596, 9.86079, -1023.79);
|
---|
100 | mViewDirection = Vector3(-0.92196, 0, 0.387286);
|
---|
101 |
|
---|
102 | // inside
|
---|
103 | mViewPoint = Vector3(14.1254, 10.9818, -1032.75);
|
---|
104 | mViewDirection = Vector3(-0.604798, 0, 0.796379);
|
---|
105 |
|
---|
106 | // outside
|
---|
107 | mViewPoint = Vector3(35.092, 17.7078, -857.966);
|
---|
108 | mViewDirection = Vector3(-0.411287, 0, -0.911506);
|
---|
109 |
|
---|
110 | // strange viewcell for error measurements (id 534)
|
---|
111 | mViewPoint = Vector3(1405.9, 218.284, -736.785);
|
---|
112 | mViewDirection = Vector3(0.989155, 0, 0.146877);
|
---|
113 |
|
---|
114 | // high error for city_full
|
---|
115 | mViewPoint = Vector3(842.521, 194.708, -136.708);
|
---|
116 | mViewDirection = Vector3(0.730514, 0, -0.682897);
|
---|
117 |
|
---|
118 | // also high error for city_full
|
---|
119 | mViewPoint = Vector3(1038.7f, 192.4f, -471.0f);
|
---|
120 | mViewDirection = Vector3(-0.8f, 0.0f, -0.6f);
|
---|
121 |
|
---|
122 | mViewPoint = Vector3(440.295, 196.959, -781.302);
|
---|
123 | mViewDirection = Vector3(-0.0566328, 0, -0.998395);
|
---|
124 |
|
---|
125 | mViewPoint = Vector3(680.682, 189.552, -278.177);
|
---|
126 | mViewDirection = Vector3(0.942709, -0, -0.333584);
|
---|
127 | }
|
---|
128 |
|
---|
129 | mFrame = 0;
|
---|
130 | mWireFrame = false;
|
---|
131 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace",
|
---|
132 | mDetectEmptyViewSpace);
|
---|
133 |
|
---|
134 | //mSnapErrorFrames = false;
|
---|
135 | mSnapErrorFrames = true;
|
---|
136 |
|
---|
137 | mSnapPrefix = "snap/";
|
---|
138 | mUseForcedColors = false;
|
---|
139 | mRenderBoxes = false;
|
---|
140 | //mUseGlLists = true;
|
---|
141 | mUseGlLists = false;
|
---|
142 |
|
---|
143 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useVbos", mUseVbos);
|
---|
144 |
|
---|
145 | if (mViewCellsManager->GetViewCellPointsList()->size())
|
---|
146 | mPvsStatFrames = (int)mViewCellsManager->GetViewCellPointsList()->size();
|
---|
147 | else
|
---|
148 | Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples",
|
---|
149 | mPvsStatFrames);
|
---|
150 |
|
---|
151 | Debug<<"Info: will evaluate pixel error with "<<mPvsStatFrames<<" samples"<<endl;
|
---|
152 |
|
---|
153 | mPvsErrorBuffer.resize(mPvsStatFrames);
|
---|
154 | ClearErrorBuffer();
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | GlRenderer::~GlRenderer()
|
---|
159 | {
|
---|
160 | cerr<<"gl renderer destructor..\n";
|
---|
161 |
|
---|
162 | CLEAR_CONTAINER(mOcclusionQueries);
|
---|
163 |
|
---|
164 | DeleteVbos();
|
---|
165 |
|
---|
166 | if (mData) delete [] mData;
|
---|
167 | if (mIndices) delete [] mIndices;
|
---|
168 |
|
---|
169 | glDeleteBuffersARB(1, &mVboId);
|
---|
170 | cerr<<"done."<<endl;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | void GlRenderer::RenderTriangle(TriangleIntersectable *object)
|
---|
175 | {
|
---|
176 | Triangle3 *t = &(object->GetItem());
|
---|
177 | glBegin(GL_TRIANGLES);
|
---|
178 | Vector3 normal = t->GetNormal();
|
---|
179 | glNormal3f(normal.x, normal.y, normal.z);
|
---|
180 | glVertex3f(t->mVertices[0].x, t->mVertices[0].y, t->mVertices[0].z);
|
---|
181 | glVertex3f(t->mVertices[1].x, t->mVertices[1].y, t->mVertices[1].z);
|
---|
182 | glVertex3f(t->mVertices[2].x, t->mVertices[2].y, t->mVertices[2].z);
|
---|
183 | glEnd();
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | void GlRenderer::RenderIntersectable(Intersectable *object)
|
---|
188 | {
|
---|
189 | if (!object || (object->mRenderedFrame == mCurrentFrame))
|
---|
190 | return;
|
---|
191 |
|
---|
192 | object->mRenderedFrame = mCurrentFrame;
|
---|
193 |
|
---|
194 | glPushAttrib(GL_CURRENT_BIT);
|
---|
195 |
|
---|
196 | if (mUseFalseColors)
|
---|
197 | SetupFalseColor(object->mId);
|
---|
198 |
|
---|
199 | switch (object->Type())
|
---|
200 | {
|
---|
201 | case Intersectable::MESH_INSTANCE:
|
---|
202 | RenderMeshInstance((MeshInstance *)object);
|
---|
203 | break;
|
---|
204 | case Intersectable::VIEW_CELL:
|
---|
205 | RenderViewCell(static_cast<ViewCell *>(object));
|
---|
206 | break;
|
---|
207 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
208 | RenderTransformedMeshInstance(static_cast<TransformedMeshInstance *>(object));
|
---|
209 | break;
|
---|
210 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
211 | RenderTriangle(static_cast<TriangleIntersectable *>(object));
|
---|
212 | break;
|
---|
213 | case Intersectable::BVH_INTERSECTABLE:
|
---|
214 | {
|
---|
215 | BvhNode *node = static_cast<BvhNode *>(object);
|
---|
216 |
|
---|
217 | if (mRenderBoxes)
|
---|
218 | RenderBox(node->GetBoundingBox());
|
---|
219 | else
|
---|
220 | RenderBvhNode(node);
|
---|
221 | break;
|
---|
222 | }
|
---|
223 | case Intersectable::KD_INTERSECTABLE:
|
---|
224 | {
|
---|
225 | KdNode *node = (static_cast<KdIntersectable *>(object))->GetItem();
|
---|
226 |
|
---|
227 | if (mRenderBoxes)
|
---|
228 | RenderBox(mKdTree->GetBox(node));
|
---|
229 | else
|
---|
230 | RenderKdNode(node);
|
---|
231 | break;
|
---|
232 | }
|
---|
233 |
|
---|
234 | default:
|
---|
235 | cerr<<"Rendering this object not yet implemented\n";
|
---|
236 | break;
|
---|
237 | }
|
---|
238 |
|
---|
239 | glPopAttrib();
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | void GlRenderer::RenderRays(const VssRayContainer &rays, int colorType, int showDistribution, int maxAge)
|
---|
244 | {
|
---|
245 | float importance;
|
---|
246 |
|
---|
247 | glBegin(GL_LINES);
|
---|
248 |
|
---|
249 | VssRayContainer::const_iterator it = rays.begin(), it_end = rays.end();
|
---|
250 |
|
---|
251 | for (; it != it_end; ++it)
|
---|
252 | {
|
---|
253 | VssRay *ray = *it;
|
---|
254 |
|
---|
255 | // only show distributions that were checked
|
---|
256 | if (((ray->mDistribution == SamplingStrategy::DIRECTION_BASED_DISTRIBUTION) && ((showDistribution & 1) == 0)) ||
|
---|
257 | ((ray->mDistribution == SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION) && ((showDistribution & 2) == 0)) ||
|
---|
258 | ((ray->mDistribution == SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION) && ((showDistribution & 4) == 0)) ||
|
---|
259 | ((ray->mDistribution == SamplingStrategy::MUTATION_BASED_DISTRIBUTION) && ((showDistribution & 8) == 0)) ||
|
---|
260 | ((mViewCellsManager->GetPreprocessor()->mPass - ray->mPass) >= maxAge))
|
---|
261 | {
|
---|
262 | continue;
|
---|
263 | }
|
---|
264 |
|
---|
265 | switch (colorType)
|
---|
266 | {
|
---|
267 | case 0:
|
---|
268 | glColor3f(1.0f, 0.0f, 0.0f);
|
---|
269 | break;
|
---|
270 |
|
---|
271 | case 1:
|
---|
272 | importance = 1.0f * ray->Length() / Magnitude(mViewCellsManager->GetViewSpaceBox().Diagonal());
|
---|
273 | glColor3f(importance, importance, importance);
|
---|
274 | break;
|
---|
275 |
|
---|
276 | case 2:
|
---|
277 | importance = log10(1e3 * ray->mPvsContribution) / 3.0f;
|
---|
278 | glColor3f(importance, importance, importance);
|
---|
279 | break;
|
---|
280 |
|
---|
281 | case 3:
|
---|
282 | {
|
---|
283 | // nested switches ok?
|
---|
284 | switch (ray->mDistribution)
|
---|
285 | {
|
---|
286 | case SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
287 | glColor3f(1, 0, 0);
|
---|
288 | break;
|
---|
289 | case SamplingStrategy::MUTATION_BASED_DISTRIBUTION:
|
---|
290 | glColor3f(0, 1, 0);
|
---|
291 | break;
|
---|
292 | case SamplingStrategy::DIRECTION_BASED_DISTRIBUTION:
|
---|
293 | glColor3f(0, 1, 1);
|
---|
294 | break;
|
---|
295 | case SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION:
|
---|
296 | glColor3f(1, 1, 0);
|
---|
297 | break;
|
---|
298 | }
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | glVertex3fv(&ray->mOrigin.x);
|
---|
303 | glVertex3fv(&ray->mTermination.x);
|
---|
304 | }
|
---|
305 |
|
---|
306 | glEnd();
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | void GlRenderer::RenderViewCell(ViewCell *vc)
|
---|
311 | {
|
---|
312 | if (vc->GetMesh())
|
---|
313 | {
|
---|
314 | if (!mUseFalseColors)
|
---|
315 | {
|
---|
316 | if (vc->GetValid())
|
---|
317 | glColor3f(0,1,0);
|
---|
318 | else
|
---|
319 | glColor3f(0,0,1);
|
---|
320 | }
|
---|
321 |
|
---|
322 | RenderMesh(vc->GetMesh());
|
---|
323 | }
|
---|
324 | else
|
---|
325 | {
|
---|
326 | // render viewcells in the subtree
|
---|
327 | if (!vc->IsLeaf())
|
---|
328 | {
|
---|
329 | ViewCellInterior *vci = (ViewCellInterior *) vc;
|
---|
330 |
|
---|
331 | ViewCellContainer::iterator it = vci->mChildren.begin();
|
---|
332 | for (; it != vci->mChildren.end(); ++it)
|
---|
333 | {
|
---|
334 | RenderViewCell(*it);
|
---|
335 | }
|
---|
336 | }
|
---|
337 | else
|
---|
338 | {
|
---|
339 | // cerr<<"Empty viewcell mesh\n";
|
---|
340 | }
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 |
|
---|
345 | void GlRenderer::RenderMeshInstance(MeshInstance *mi)
|
---|
346 | {
|
---|
347 | RenderMesh(mi->GetMesh());
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | void
|
---|
352 | GlRenderer::RenderTransformedMeshInstance(TransformedMeshInstance *mi)
|
---|
353 | {
|
---|
354 | // apply world transform before rendering
|
---|
355 | Matrix4x4 m;
|
---|
356 | mi->GetWorldTransform(m);
|
---|
357 |
|
---|
358 | glPushMatrix();
|
---|
359 | glMultMatrixf((float *)m.x);
|
---|
360 |
|
---|
361 | RenderMesh(mi->GetMesh());
|
---|
362 |
|
---|
363 | glPopMatrix();
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | void
|
---|
368 | GlRenderer::SetupFalseColor(const unsigned int id)
|
---|
369 | {
|
---|
370 | // swap bits of the color
|
---|
371 | glColor3ub(id&255, (id>>8)&255, (id>>16)&255);
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | unsigned int GlRenderer::GetId(const unsigned char r,
|
---|
376 | const unsigned char g,
|
---|
377 | const unsigned char b) const
|
---|
378 | {
|
---|
379 | return r + (g << 8) + (b << 16);
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | void
|
---|
384 | GlRenderer::SetupMaterial(Material *m)
|
---|
385 | {
|
---|
386 | if (m)
|
---|
387 | glColor3fv(&(m->mDiffuseColor.r));
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | void GlRenderer::RenderMesh(Mesh *mesh)
|
---|
392 | {
|
---|
393 | int i = 0;
|
---|
394 |
|
---|
395 | if (!mUseFalseColors && !mUseForcedColors)
|
---|
396 | SetupMaterial(mesh->mMaterial);
|
---|
397 |
|
---|
398 | for (i = 0; i < mesh->mFaces.size(); i++)
|
---|
399 | {
|
---|
400 | if (mWireFrame)
|
---|
401 | glBegin(GL_LINE_LOOP);
|
---|
402 | else
|
---|
403 | glBegin(GL_POLYGON);
|
---|
404 |
|
---|
405 | Face *face = mesh->mFaces[i];
|
---|
406 | Vector3 normal = mesh->GetNormal(i);
|
---|
407 |
|
---|
408 | glNormal3f(normal.x, normal.y, normal.z);
|
---|
409 | for (int j = 0; j < face->mVertexIndices.size(); j++) {
|
---|
410 | glVertex3fv(&mesh->mVertices[face->mVertexIndices[j]].x);
|
---|
411 | }
|
---|
412 | glEnd();
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | void GlRenderer::InitGL()
|
---|
417 | {
|
---|
418 | mSphere = (GLUquadric *)gluNewQuadric();
|
---|
419 |
|
---|
420 | glMatrixMode(GL_PROJECTION);
|
---|
421 | glLoadIdentity();
|
---|
422 |
|
---|
423 | glMatrixMode(GL_MODELVIEW);
|
---|
424 | glLoadIdentity();
|
---|
425 |
|
---|
426 | glFrontFace(GL_CCW);
|
---|
427 | glCullFace(GL_BACK);
|
---|
428 |
|
---|
429 | glShadeModel(GL_FLAT);
|
---|
430 | glDepthFunc(GL_LESS );
|
---|
431 | glEnable(GL_DEPTH_TEST);
|
---|
432 | glEnable(GL_CULL_FACE);
|
---|
433 |
|
---|
434 | InitExtensions();
|
---|
435 |
|
---|
436 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
---|
437 |
|
---|
438 | glEnable(GL_NORMALIZE);
|
---|
439 |
|
---|
440 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
---|
441 |
|
---|
442 | // create some occlusion queries
|
---|
443 | OcclusionQuery::GenQueries(mOcclusionQueries, 100);
|
---|
444 |
|
---|
445 | SceneGraphInterior *interior = mSceneGraph->GetRoot();
|
---|
446 |
|
---|
447 | SceneGraphNodeContainer::iterator ni = interior->mChildren.begin();
|
---|
448 |
|
---|
449 | for (; ni != interior->mChildren.end(); ni++)
|
---|
450 | {
|
---|
451 | CreateVertexArrays(static_cast<SceneGraphLeaf *>(*ni));
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 |
|
---|
456 | void
|
---|
457 | GlRenderer::SetupProjection(const int w, const int h, const float angle)
|
---|
458 | {
|
---|
459 | glViewport(0, 0, w, h);
|
---|
460 | glMatrixMode(GL_PROJECTION);
|
---|
461 | glLoadIdentity();
|
---|
462 | gluPerspective(angle, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
|
---|
463 | glMatrixMode(GL_MODELVIEW);
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 |
|
---|
468 | void
|
---|
469 | GlRenderer::SetupCamera()
|
---|
470 | {
|
---|
471 | Vector3 target = mViewPoint + mViewDirection;
|
---|
472 |
|
---|
473 | Vector3 up(0,1,0);
|
---|
474 |
|
---|
475 | if (fabs(DotProd(mViewDirection, up)) > 0.99f)
|
---|
476 | up = Vector3(1, 0, 0);
|
---|
477 |
|
---|
478 | glLoadIdentity();
|
---|
479 | gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
|
---|
480 | target.x, target.y, target.z,
|
---|
481 | up.x, up.y, up.z);
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | void GlRenderer::_RenderScene()
|
---|
486 | {
|
---|
487 | ObjectContainer::const_iterator oi = mObjects.begin();
|
---|
488 |
|
---|
489 | for (; oi != mObjects.end(); oi++)
|
---|
490 | RenderIntersectable(*oi);
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | void GlRenderer::_RenderSceneTrianglesWithDrawArrays()
|
---|
495 | {
|
---|
496 | EnableDrawArrays();
|
---|
497 |
|
---|
498 | if (mUseVbos)
|
---|
499 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
500 |
|
---|
501 | const int offset = (int)mObjects.size() * 3;
|
---|
502 | char *arrayPtr = mUseVbos ? NULL : (char *)mData;
|
---|
503 |
|
---|
504 | glVertexPointer(3, GL_FLOAT, 0, (char *)arrayPtr);
|
---|
505 | glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
|
---|
506 |
|
---|
507 | glDrawArrays(GL_TRIANGLES, 0, offset);
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 | void GlRenderer::_RenderDynamicObject(SceneGraphLeaf *leaf)
|
---|
512 | {
|
---|
513 | // apply world transform before rendering
|
---|
514 | Matrix4x4 m;
|
---|
515 | leaf->GetTransform(m);
|
---|
516 |
|
---|
517 | glPushMatrix();
|
---|
518 | glMultMatrixf((float *)m.x);
|
---|
519 |
|
---|
520 | glBegin(GL_TRIANGLES);
|
---|
521 |
|
---|
522 | ObjectContainer::const_iterator oi = leaf->mGeometry.begin();
|
---|
523 | for (; oi != leaf->mGeometry.end(); oi++)
|
---|
524 | {
|
---|
525 | TriangleIntersectable *object = (TriangleIntersectable *)*oi;
|
---|
526 | Triangle3 *t = &(object->GetItem());
|
---|
527 |
|
---|
528 | Vector3 normal = t->GetNormal();
|
---|
529 | glNormal3f(normal.x, normal.y, normal.z);
|
---|
530 |
|
---|
531 | glVertex3f(t->mVertices[0].x, t->mVertices[0].y, t->mVertices[0].z);
|
---|
532 | glVertex3f(t->mVertices[1].x, t->mVertices[1].y, t->mVertices[1].z);
|
---|
533 | glVertex3f(t->mVertices[2].x, t->mVertices[2].y, t->mVertices[2].z);
|
---|
534 |
|
---|
535 | }
|
---|
536 |
|
---|
537 | glEnd();
|
---|
538 |
|
---|
539 | glPopMatrix();
|
---|
540 |
|
---|
541 | if (1)
|
---|
542 | {
|
---|
543 | // test the box of the object
|
---|
544 | AxisAlignedBox3 box = leaf->GetBox();
|
---|
545 | RenderBox(box);
|
---|
546 | }
|
---|
547 | }
|
---|
548 |
|
---|
549 |
|
---|
550 |
|
---|
551 | void GlRenderer::_RenderSceneTriangles()
|
---|
552 | {
|
---|
553 | glBegin(GL_TRIANGLES);
|
---|
554 |
|
---|
555 | ObjectContainer::const_iterator oi = mObjects.begin();
|
---|
556 | for (; oi != mObjects.end(); oi++) {
|
---|
557 |
|
---|
558 | if ((*oi)->Type() == Intersectable::TRIANGLE_INTERSECTABLE) {
|
---|
559 | TriangleIntersectable *object = (TriangleIntersectable *)*oi;
|
---|
560 | Triangle3 *t = &(object->GetItem());
|
---|
561 |
|
---|
562 | Vector3 normal = t->GetNormal();
|
---|
563 | glNormal3f(normal.x, normal.y, normal.z);
|
---|
564 | glVertex3f(t->mVertices[0].x, t->mVertices[0].y, t->mVertices[0].z);
|
---|
565 | glVertex3f(t->mVertices[1].x, t->mVertices[1].y, t->mVertices[1].z);
|
---|
566 | glVertex3f(t->mVertices[2].x, t->mVertices[2].y, t->mVertices[2].z);
|
---|
567 | }
|
---|
568 | }
|
---|
569 |
|
---|
570 | glEnd();
|
---|
571 | }
|
---|
572 |
|
---|
573 |
|
---|
574 | bool GlRenderer::RenderScene()
|
---|
575 | {
|
---|
576 | ++ mCurrentFrame;
|
---|
577 |
|
---|
578 | Intersectable::NewMail();
|
---|
579 |
|
---|
580 | #if DYNAMIC_OBJECTS_HACK
|
---|
581 | Preprocessor *p = mViewCellsManager->GetPreprocessor();
|
---|
582 | // handle dynamic objects
|
---|
583 | DynamicObjectsContainer::const_iterator dit, dit_end = p->mDynamicObjects.end();
|
---|
584 |
|
---|
585 | for (dit = p->mDynamicObjects.begin(); dit != dit_end; ++ dit)
|
---|
586 | {
|
---|
587 | _RenderDynamicObject(*dit);
|
---|
588 | }
|
---|
589 | #endif
|
---|
590 | _RenderSceneTrianglesWithDrawArrays();
|
---|
591 |
|
---|
592 | return true;
|
---|
593 | }
|
---|
594 |
|
---|
595 |
|
---|
596 | void
|
---|
597 | GlRendererBuffer::EvalQueryWithItemBuffer()
|
---|
598 | {
|
---|
599 | // read back the texture
|
---|
600 | glReadPixels(0, 0,
|
---|
601 | GetWidth(), GetHeight(),
|
---|
602 | GL_RGBA,
|
---|
603 | GL_UNSIGNED_BYTE,
|
---|
604 | mPixelBuffer);
|
---|
605 |
|
---|
606 |
|
---|
607 | unsigned int *p = mPixelBuffer;
|
---|
608 |
|
---|
609 | for (int y = 0; y < GetHeight(); y++)
|
---|
610 | {
|
---|
611 | for (int x = 0; x < GetWidth(); x++, p++)
|
---|
612 | {
|
---|
613 | unsigned int id = (*p) & 0xFFFFFF;
|
---|
614 |
|
---|
615 | if (id != 0xFFFFFF)
|
---|
616 | {
|
---|
617 | ++ mObjects[id]->mCounter;
|
---|
618 | }
|
---|
619 | }
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 |
|
---|
624 |
|
---|
625 | /****************************************************************/
|
---|
626 | /* GlRendererBuffer implementation */
|
---|
627 | /****************************************************************/
|
---|
628 |
|
---|
629 |
|
---|
630 |
|
---|
631 | GlRendererBuffer::GlRendererBuffer(SceneGraph *sceneGraph,
|
---|
632 | ViewCellsManager *viewcells,
|
---|
633 | KdTree *tree):
|
---|
634 | GlRenderer(sceneGraph, viewcells, tree)
|
---|
635 | {
|
---|
636 | mPixelBuffer = NULL;
|
---|
637 | // implement width and height in subclasses
|
---|
638 | }
|
---|
639 |
|
---|
640 |
|
---|
641 | void
|
---|
642 | GlRendererBuffer::EvalQueryWithOcclusionQueries(
|
---|
643 | //RenderCostSample &sample
|
---|
644 | )
|
---|
645 | {
|
---|
646 | glDepthFunc(GL_LEQUAL);
|
---|
647 |
|
---|
648 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
649 | glDepthMask(GL_FALSE);
|
---|
650 |
|
---|
651 |
|
---|
652 | // simulate detectemptyviewspace using backface culling
|
---|
653 | if (mDetectEmptyViewSpace)
|
---|
654 | {
|
---|
655 | glEnable(GL_CULL_FACE);
|
---|
656 | //cout << "culling" << endl;
|
---|
657 | }
|
---|
658 | else
|
---|
659 | {
|
---|
660 | //cout << "not culling" << endl;
|
---|
661 | glDisable(GL_CULL_FACE);
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|
665 | //const int numQ = 1;
|
---|
666 | const int numQ = (int)mOcclusionQueries.size();
|
---|
667 |
|
---|
668 | //glFinish();
|
---|
669 | #if 0
|
---|
670 | //-- now issue queries for all objects
|
---|
671 | for (int j = 0; j < (int)mObjects.size(); ++ j)
|
---|
672 | {
|
---|
673 | mOcclusionQueries[j]->BeginQuery();
|
---|
674 | RenderIntersectable(mObjects[j]);
|
---|
675 | mOcclusionQueries[j]->EndQuery();
|
---|
676 |
|
---|
677 | unsigned int pixelCount;
|
---|
678 |
|
---|
679 | pixelCount = mOcclusionQueries[j]->GetQueryResult();
|
---|
680 |
|
---|
681 | mObjects[j]->mCounter += pixelCount;
|
---|
682 | }
|
---|
683 | #else
|
---|
684 |
|
---|
685 | int q = 0;
|
---|
686 |
|
---|
687 | //-- now issue queries for all objects
|
---|
688 | for (int j = 0; j < (int)mObjects.size(); j += q)
|
---|
689 | {
|
---|
690 | for (q = 0; ((j + q) < (int)mObjects.size()) && (q < numQ); ++ q)
|
---|
691 | {
|
---|
692 | mOcclusionQueries[q]->BeginQuery();
|
---|
693 |
|
---|
694 | RenderIntersectable(mObjects[j + q]);
|
---|
695 |
|
---|
696 | mOcclusionQueries[q]->EndQuery();
|
---|
697 | }
|
---|
698 | //cout << "q: " << q << endl;
|
---|
699 | // collect results of the queries
|
---|
700 | for (int t = 0; t < q; ++ t)
|
---|
701 | {
|
---|
702 | unsigned int pixelCount;
|
---|
703 |
|
---|
704 | //-- reenable other state
|
---|
705 | #if 0
|
---|
706 | bool available;
|
---|
707 |
|
---|
708 | do
|
---|
709 | {
|
---|
710 | available = mOcclusionQueries[t]->ResultAvailable();
|
---|
711 |
|
---|
712 | if (!available) cout << "W";
|
---|
713 | }
|
---|
714 | while (!available);
|
---|
715 | #endif
|
---|
716 |
|
---|
717 | pixelCount = mOcclusionQueries[t]->GetQueryResult();
|
---|
718 |
|
---|
719 | //if (pixelCount > 0)
|
---|
720 | // cout <<"o="<<j+q<<" q="<<mOcclusionQueries[q]->GetQueryId()<<" pc="<<pixelCount<<" ";
|
---|
721 | mObjects[j + t]->mCounter += pixelCount;
|
---|
722 |
|
---|
723 | }
|
---|
724 |
|
---|
725 | //j += q;
|
---|
726 | }
|
---|
727 | #endif
|
---|
728 | //glFinish();
|
---|
729 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
730 | glDepthMask(GL_TRUE);
|
---|
731 |
|
---|
732 | glEnable(GL_CULL_FACE);
|
---|
733 | }
|
---|
734 |
|
---|
735 |
|
---|
736 | void
|
---|
737 | GlRenderer::RandomViewPoint()
|
---|
738 | {
|
---|
739 | // do not use this function since it could return different viewpoints for
|
---|
740 | // different executions of the algorithm
|
---|
741 |
|
---|
742 | // mViewCellsManager->GetViewPoint(mViewPoint);
|
---|
743 |
|
---|
744 | while (1) {
|
---|
745 | Vector3 pVector = Vector3(halton.GetNumber(1),
|
---|
746 | halton.GetNumber(2),
|
---|
747 | halton.GetNumber(3));
|
---|
748 |
|
---|
749 | mViewPoint = mViewCellsManager->GetViewSpaceBox().GetPoint(pVector);
|
---|
750 | ViewCell *v = mViewCellsManager->GetViewCell(mViewPoint);
|
---|
751 | if (v && v->GetValid())
|
---|
752 | break;
|
---|
753 | // generate a new vector
|
---|
754 | halton.GenerateNext();
|
---|
755 | }
|
---|
756 |
|
---|
757 | Vector3 dVector = Vector3(2*M_PI*halton.GetNumber(4),
|
---|
758 | M_PI*halton.GetNumber(5),
|
---|
759 | 0.0f);
|
---|
760 |
|
---|
761 | mViewDirection = Normalize(Vector3(sin(dVector.x),
|
---|
762 | // cos(dVector.y),
|
---|
763 | 0.0f,
|
---|
764 | cos(dVector.x)));
|
---|
765 | halton.GenerateNext();
|
---|
766 | }
|
---|
767 |
|
---|
768 |
|
---|
769 | void
|
---|
770 | GlRenderer::RenderBox(const AxisAlignedBox3 &box)
|
---|
771 | {
|
---|
772 |
|
---|
773 | glBegin(GL_LINE_LOOP);
|
---|
774 | glVertex3d(box.Min().x, box.Max().y, box.Min().z );
|
---|
775 | glVertex3d(box.Max().x, box.Max().y, box.Min().z );
|
---|
776 | glVertex3d(box.Max().x, box.Min().y, box.Min().z );
|
---|
777 | glVertex3d(box.Min().x, box.Min().y, box.Min().z );
|
---|
778 | glEnd();
|
---|
779 |
|
---|
780 | glBegin(GL_LINE_LOOP);
|
---|
781 | glVertex3d(box.Min().x, box.Min().y, box.Max().z );
|
---|
782 | glVertex3d(box.Max().x, box.Min().y, box.Max().z );
|
---|
783 | glVertex3d(box.Max().x, box.Max().y, box.Max().z );
|
---|
784 | glVertex3d(box.Min().x, box.Max().y, box.Max().z );
|
---|
785 | glEnd();
|
---|
786 |
|
---|
787 | glBegin(GL_LINE_LOOP);
|
---|
788 | glVertex3d(box.Max().x, box.Min().y, box.Min().z );
|
---|
789 | glVertex3d(box.Max().x, box.Min().y, box.Max().z );
|
---|
790 | glVertex3d(box.Max().x, box.Max().y, box.Max().z );
|
---|
791 | glVertex3d(box.Max().x, box.Max().y, box.Min().z );
|
---|
792 | glEnd();
|
---|
793 |
|
---|
794 | glBegin(GL_LINE_LOOP);
|
---|
795 | glVertex3d(box.Min().x, box.Min().y, box.Min().z );
|
---|
796 | glVertex3d(box.Min().x, box.Min().y, box.Max().z );
|
---|
797 | glVertex3d(box.Min().x, box.Max().y, box.Max().z );
|
---|
798 | glVertex3d(box.Min().x, box.Max().y, box.Min().z );
|
---|
799 | glEnd();
|
---|
800 |
|
---|
801 | glBegin(GL_LINE_LOOP);
|
---|
802 | glVertex3d(box.Min().x, box.Min().y, box.Min().z );
|
---|
803 | glVertex3d(box.Max().x, box.Min().y, box.Min().z );
|
---|
804 | glVertex3d(box.Max().x, box.Min().y, box.Max().z );
|
---|
805 | glVertex3d(box.Min().x, box.Min().y, box.Max().z );
|
---|
806 | glEnd();
|
---|
807 |
|
---|
808 | glBegin(GL_LINE_LOOP);
|
---|
809 | glVertex3d(box.Min().x, box.Max().y, box.Min().z );
|
---|
810 | glVertex3d(box.Max().x, box.Max().y, box.Min().z );
|
---|
811 | glVertex3d(box.Max().x, box.Max().y, box.Max().z );
|
---|
812 | glVertex3d(box.Min().x, box.Max().y, box.Max().z );
|
---|
813 |
|
---|
814 | glEnd();
|
---|
815 |
|
---|
816 | }
|
---|
817 |
|
---|
818 | void
|
---|
819 | GlRenderer::RenderBvhNode(BvhNode *node)
|
---|
820 | {
|
---|
821 | if (node->IsLeaf()) {
|
---|
822 | BvhLeaf *leaf = (BvhLeaf *) node;
|
---|
823 |
|
---|
824 | #if 0
|
---|
825 | if (leaf->mGlList == 0) {
|
---|
826 | leaf->mGlList = glGenLists(1);
|
---|
827 | if (leaf->mGlList != 0)
|
---|
828 | glNewList(leaf->mGlList, GL_COMPILE);
|
---|
829 |
|
---|
830 | for (int i=0; i < leaf->mObjects.size(); i++)
|
---|
831 | RenderIntersectable(leaf->mObjects[i]);
|
---|
832 |
|
---|
833 | if (leaf->mGlList != 0)
|
---|
834 | glEndList();
|
---|
835 | }
|
---|
836 |
|
---|
837 | if (leaf->mGlList != 0)
|
---|
838 | glCallList(leaf->mGlList);
|
---|
839 | #else
|
---|
840 | for (int i=0; i < leaf->mObjects.size(); i++)
|
---|
841 | RenderIntersectable(leaf->mObjects[i]);
|
---|
842 | #endif
|
---|
843 | } else {
|
---|
844 | BvhInterior *in = (BvhInterior *)node;
|
---|
845 | RenderBvhNode(in->GetBack());
|
---|
846 | RenderBvhNode(in->GetFront());
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | void
|
---|
851 | GlRenderer::RenderKdNode(KdNode *node)
|
---|
852 | {
|
---|
853 | if (node->IsLeaf())
|
---|
854 | {
|
---|
855 | #if !EVAL_ERROR
|
---|
856 | RenderKdLeaf(static_cast<KdLeaf *>(node));
|
---|
857 | #else
|
---|
858 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
859 | for (int i=0; i < leaf->mObjects.size(); i++)
|
---|
860 | {
|
---|
861 | RenderIntersectable(leaf->mObjects[i]);
|
---|
862 |
|
---|
863 | }
|
---|
864 | #endif
|
---|
865 | }
|
---|
866 | else
|
---|
867 | {
|
---|
868 | KdInterior *inter = static_cast<KdInterior *>(node);
|
---|
869 | RenderKdNode(inter->mBack);
|
---|
870 | RenderKdNode(inter->mFront);
|
---|
871 | }
|
---|
872 | }
|
---|
873 |
|
---|
874 |
|
---|
875 |
|
---|
876 |
|
---|
877 | void
|
---|
878 | GlRendererBuffer::EvalRenderCostSample(RenderCostSample &sample,
|
---|
879 | const bool useOcclusionQueries,
|
---|
880 | const int threshold
|
---|
881 | )
|
---|
882 | {
|
---|
883 | // choose a random view point
|
---|
884 | mViewCellsManager->GetViewPoint(mViewPoint);
|
---|
885 | sample.mPosition = mViewPoint;
|
---|
886 | //cout << "viewpoint: " << mViewPoint << endl;
|
---|
887 |
|
---|
888 | // take a render cost sample by rendering a cube
|
---|
889 | Vector3 directions[6];
|
---|
890 |
|
---|
891 | directions[0] = Vector3(1,0,0);
|
---|
892 | directions[1] = Vector3(0,1,0);
|
---|
893 | directions[2] = Vector3(0,0,1);
|
---|
894 | directions[3] = Vector3(-1,0,0);
|
---|
895 | directions[4] = Vector3(0,-1,0);
|
---|
896 | directions[5] = Vector3(0,0,-1);
|
---|
897 |
|
---|
898 | sample.mVisibleObjects = 0;
|
---|
899 |
|
---|
900 | // reset object counters
|
---|
901 | ObjectContainer::const_iterator it, it_end = mObjects.end();
|
---|
902 |
|
---|
903 | for (it = mObjects.begin(); it != it_end; ++ it)
|
---|
904 | {
|
---|
905 | (*it)->mCounter = 0;
|
---|
906 |
|
---|
907 | }
|
---|
908 |
|
---|
909 | ++ mFrame;
|
---|
910 |
|
---|
911 | //glCullFace(GL_FRONT);
|
---|
912 | glCullFace(GL_BACK);
|
---|
913 | glDisable(GL_CULL_FACE);
|
---|
914 |
|
---|
915 |
|
---|
916 | // query all 6 directions for a full point sample
|
---|
917 | for (int i = 0; i < 6; ++ i)
|
---|
918 | {
|
---|
919 | mViewDirection = directions[i];
|
---|
920 | SetupCamera();
|
---|
921 |
|
---|
922 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
923 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
924 | //glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE);
|
---|
925 | glDepthFunc(GL_LESS);
|
---|
926 |
|
---|
927 | mUseFalseColors = true;
|
---|
928 |
|
---|
929 | // the actual scene rendering fills the depth (for occlusion queries)
|
---|
930 | // and the frame buffer (for item buffer)
|
---|
931 | RenderScene();
|
---|
932 |
|
---|
933 |
|
---|
934 | if (0)
|
---|
935 | {
|
---|
936 | char filename[256];
|
---|
937 | sprintf(filename, "snap/frame-%04d-%d.png", mFrame, i);
|
---|
938 | // QImage im = toImage();
|
---|
939 | // im.save(filename, "PNG");
|
---|
940 | }
|
---|
941 |
|
---|
942 | // evaluate the sample
|
---|
943 | if (useOcclusionQueries)
|
---|
944 | {
|
---|
945 | EvalQueryWithOcclusionQueries();
|
---|
946 | }
|
---|
947 | else
|
---|
948 | {
|
---|
949 | EvalQueryWithItemBuffer();
|
---|
950 | }
|
---|
951 | }
|
---|
952 |
|
---|
953 | // now evaluate the statistics over that sample
|
---|
954 | // currently only the number of visible objects is taken into account
|
---|
955 | sample.Reset();
|
---|
956 |
|
---|
957 | for (it = mObjects.begin(); it != it_end; ++ it)
|
---|
958 | {
|
---|
959 | Intersectable *obj = *it;
|
---|
960 | if (obj->mCounter >= threshold)
|
---|
961 | {
|
---|
962 | ++ sample.mVisibleObjects;
|
---|
963 | sample.mVisiblePixels += obj->mCounter;
|
---|
964 | }
|
---|
965 | }
|
---|
966 |
|
---|
967 | //cout << "RS=" << sample.mVisibleObjects << " ";
|
---|
968 | }
|
---|
969 |
|
---|
970 |
|
---|
971 | GlRendererBuffer::~GlRendererBuffer()
|
---|
972 | {
|
---|
973 | #if 0
|
---|
974 | #ifdef USE_CG
|
---|
975 | if (sCgFragmentProgram)
|
---|
976 | cgDestroyProgram(sCgFragmentProgram);
|
---|
977 | if (sCgContext)
|
---|
978 | cgDestroyContext(sCgContext);
|
---|
979 | #endif
|
---|
980 | #endif
|
---|
981 |
|
---|
982 | }
|
---|
983 |
|
---|
984 |
|
---|
985 | void
|
---|
986 | GlRendererBuffer::SampleRenderCost(const int numSamples,
|
---|
987 | vector<RenderCostSample> &samples,
|
---|
988 | const bool useOcclusionQueries,
|
---|
989 | const int threshold
|
---|
990 | )
|
---|
991 | {
|
---|
992 | MakeLive();
|
---|
993 |
|
---|
994 | if (mPixelBuffer == NULL)
|
---|
995 | mPixelBuffer = new unsigned int[GetWidth()*GetHeight()];
|
---|
996 |
|
---|
997 | // using 90 degree projection to capture 360 view with 6 samples
|
---|
998 | SetupProjection(GetHeight(), GetHeight(), 90.0f);
|
---|
999 |
|
---|
1000 | //samples.resize(numSamples);
|
---|
1001 | halton.Reset();
|
---|
1002 |
|
---|
1003 | // the number of queries queried in batch mode
|
---|
1004 | const int numQ = 500;
|
---|
1005 |
|
---|
1006 | //const int numQ = (int)mObjects.size();
|
---|
1007 | if (useOcclusionQueries)
|
---|
1008 | {
|
---|
1009 | cout << "\ngenerating " << numQ << " queries ... ";
|
---|
1010 | OcclusionQuery::GenQueries(mOcclusionQueries, numQ);
|
---|
1011 | cout << "finished" << endl;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | // sampling queries
|
---|
1015 | for (int i = 0; i < numSamples; ++ i)
|
---|
1016 | {
|
---|
1017 | cout << ".";
|
---|
1018 | EvalRenderCostSample(samples[i], useOcclusionQueries, threshold);
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | DoneLive();
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 |
|
---|
1025 |
|
---|
1026 |
|
---|
1027 |
|
---|
1028 | void
|
---|
1029 | GlRenderer::ClearErrorBuffer()
|
---|
1030 | {
|
---|
1031 | for (int i=0; i < mPvsStatFrames; i++) {
|
---|
1032 | mPvsErrorBuffer[i].mError = 1.0f;
|
---|
1033 | }
|
---|
1034 | mPvsStat.maxError = 0.0f;
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 |
|
---|
1038 | void
|
---|
1039 | GlRendererBuffer::EvalPvsStat()
|
---|
1040 | {
|
---|
1041 | //MakeLive();
|
---|
1042 |
|
---|
1043 | GlRenderer::EvalPvsStat();
|
---|
1044 |
|
---|
1045 | //DoneLive();
|
---|
1046 | // mRenderingFinished.wakeAll();
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 |
|
---|
1050 | void GlRendererBuffer::EvalPvsStat(const SimpleRayContainer &viewPoints)
|
---|
1051 | {
|
---|
1052 | //MakeLive();
|
---|
1053 |
|
---|
1054 | GlRenderer::EvalPvsStat(viewPoints);
|
---|
1055 |
|
---|
1056 | //DoneLive();
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 |
|
---|
1060 | void GlRendererBuffer::SampleBeamContributions(Intersectable *sourceObject,
|
---|
1061 | Beam &beam,
|
---|
1062 | const int desiredSamples,
|
---|
1063 | BeamSampleStatistics &stat)
|
---|
1064 | {
|
---|
1065 | // TODO: should be moved out of here (not to be done every time)
|
---|
1066 | // only back faces are interesting for the depth pass
|
---|
1067 | glShadeModel(GL_FLAT);
|
---|
1068 | glDisable(GL_LIGHTING);
|
---|
1069 |
|
---|
1070 | // needed to kill the fragments for the front buffer
|
---|
1071 | glEnable(GL_ALPHA_TEST);
|
---|
1072 | glAlphaFunc(GL_GREATER, 0);
|
---|
1073 |
|
---|
1074 | // assumes that the beam is constructed and contains kd-tree nodes
|
---|
1075 | // and viewcells which it intersects
|
---|
1076 |
|
---|
1077 |
|
---|
1078 | // Get the number of viewpoints to be sampled
|
---|
1079 | // Now it is a sqrt but in general a wiser decision could be made.
|
---|
1080 | // The less viewpoints the better for rendering performance, since less passes
|
---|
1081 | // over the beam is needed.
|
---|
1082 | // The viewpoints could actually be generated outside of the bounding box which
|
---|
1083 | // would distribute the 'efective viewpoints' of the object surface and thus
|
---|
1084 | // with a few viewpoints better sample the viewpoint space....
|
---|
1085 |
|
---|
1086 | //TODO: comment in
|
---|
1087 | //int viewPointSamples = sqrt((float)desiredSamples);
|
---|
1088 | int viewPointSamples = max(desiredSamples / (GetWidth() * GetHeight()), 1);
|
---|
1089 |
|
---|
1090 | // the number of direction samples per pass is given by the number of viewpoints
|
---|
1091 | int directionalSamples = desiredSamples / viewPointSamples;
|
---|
1092 |
|
---|
1093 | Debug << "directional samples: " << directionalSamples << endl;
|
---|
1094 | for (int i = 0; i < viewPointSamples; ++ i)
|
---|
1095 | {
|
---|
1096 | Vector3 viewPoint = beam.mBox.GetRandomPoint();
|
---|
1097 |
|
---|
1098 | // perhaps the viewpoint should be shifted back a little bit so that it always lies
|
---|
1099 | // inside the source object
|
---|
1100 | // 'ideally' the viewpoints would be distributed on the soureObject surface, but this
|
---|
1101 | // would require more complicated sampling (perhaps hierarchical rejection sampling of
|
---|
1102 | // the object surface is an option here - only the mesh faces which are inside the box
|
---|
1103 | // are considered as candidates)
|
---|
1104 |
|
---|
1105 | SampleViewpointContributions(sourceObject,
|
---|
1106 | viewPoint,
|
---|
1107 | beam,
|
---|
1108 | directionalSamples,
|
---|
1109 | stat);
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 |
|
---|
1113 | // note:
|
---|
1114 | // this routine would be called only if the number of desired samples is sufficiently
|
---|
1115 | // large - for other rss tree cells the cpu based sampling is perhaps more efficient
|
---|
1116 | // distributing the work between cpu and gpu would also allow us to place more sophisticated
|
---|
1117 | // sample distributions (silhouette ones) using the cpu and the jittered once on the GPU
|
---|
1118 | // in order that thios scheme is working well the gpu render buffer should run in a separate
|
---|
1119 | // thread than the cpu sampler, which would not be such a big problem....
|
---|
1120 |
|
---|
1121 | // disable alpha test again
|
---|
1122 | glDisable(GL_ALPHA_TEST);
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 |
|
---|
1126 |
|
---|
1127 | void GlRendererBuffer::SampleViewpointContributions(Intersectable *sourceObject,
|
---|
1128 | const Vector3 viewPoint,
|
---|
1129 | Beam &beam,
|
---|
1130 | const int samples,
|
---|
1131 | BeamSampleStatistics &stat)
|
---|
1132 | {
|
---|
1133 | // 1. setup the view port to match the desired samples
|
---|
1134 | glViewport(0, 0, samples, samples);
|
---|
1135 |
|
---|
1136 | // 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox
|
---|
1137 | SetupProjectionForViewPoint(viewPoint, beam, sourceObject);
|
---|
1138 |
|
---|
1139 |
|
---|
1140 | // 3. reset z-buffer to 0 and render the source object for the beam
|
---|
1141 | // with glCullFace(Enabled) and glFrontFace(GL_CW)
|
---|
1142 | // save result to the front depth map
|
---|
1143 | // the front depth map holds ray origins
|
---|
1144 |
|
---|
1145 |
|
---|
1146 | // front depth buffer must be initialised to 0
|
---|
1147 | float clearDepth;
|
---|
1148 |
|
---|
1149 | glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepth);
|
---|
1150 | glClearDepth(0.0f);
|
---|
1151 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
---|
1152 |
|
---|
1153 |
|
---|
1154 | // glFrontFace(GL_CCW);
|
---|
1155 | glEnable(GL_CULL_FACE);
|
---|
1156 | glCullFace(GL_FRONT);
|
---|
1157 | glColorMask(0, 0, 0, 0);
|
---|
1158 |
|
---|
1159 |
|
---|
1160 | // stencil is increased where the source object is located
|
---|
1161 | glEnable(GL_STENCIL_TEST);
|
---|
1162 | glStencilFunc(GL_ALWAYS, 0x1, 0x1);
|
---|
1163 | glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
|
---|
1164 |
|
---|
1165 |
|
---|
1166 | #if 0
|
---|
1167 | static int glSourceObjList = -1;
|
---|
1168 | if (glSourceObjList != -1)
|
---|
1169 | {
|
---|
1170 | glSourceObjList = glGenLists(1);
|
---|
1171 | glNewList(glSourceObjList, GL_COMPILE);
|
---|
1172 |
|
---|
1173 | RenderIntersectable(sourceObject);
|
---|
1174 |
|
---|
1175 | glEndList();
|
---|
1176 | }
|
---|
1177 | glCallList(glSourceObjList);
|
---|
1178 |
|
---|
1179 | #else
|
---|
1180 | RenderIntersectable(sourceObject);
|
---|
1181 |
|
---|
1182 | #endif
|
---|
1183 |
|
---|
1184 | // copy contents of the front depth buffer into depth texture
|
---|
1185 | glBindTexture(GL_TEXTURE_2D, frontDepthMap);
|
---|
1186 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize);
|
---|
1187 |
|
---|
1188 | // reset clear function
|
---|
1189 | glClearDepth(clearDepth);
|
---|
1190 |
|
---|
1191 |
|
---|
1192 | // 4. set up the termination depth buffer (= standard depth buffer)
|
---|
1193 | // only rays which have non-zero entry in the origin buffer are valid since
|
---|
1194 | // they realy start on the object surface (this is tagged by setting a
|
---|
1195 | // stencil buffer bit at step 3).
|
---|
1196 |
|
---|
1197 | glStencilFunc(GL_EQUAL, 0x1, 0x1);
|
---|
1198 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
|
---|
1199 |
|
---|
1200 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
1201 | glDepthMask(1);
|
---|
1202 |
|
---|
1203 | glEnable(GL_DEPTH_TEST);
|
---|
1204 |
|
---|
1205 | glEnable(GL_CULL_FACE);
|
---|
1206 | glCullFace(GL_BACK);
|
---|
1207 |
|
---|
1208 | // setup front depth buffer
|
---|
1209 | glEnable(GL_TEXTURE_2D);
|
---|
1210 |
|
---|
1211 | #if 0
|
---|
1212 | #ifdef USE_CG
|
---|
1213 | // bind pixel shader implementing the front depth buffer functionality
|
---|
1214 | cgGLBindProgram(sCgFragmentProgram);
|
---|
1215 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
1216 | #endif
|
---|
1217 | #endif
|
---|
1218 | // 5. render all objects inside the beam
|
---|
1219 | // we can use id based false color to read them back for gaining the pvs
|
---|
1220 |
|
---|
1221 | glColorMask(1, 1, 1, 1);
|
---|
1222 |
|
---|
1223 |
|
---|
1224 | // if objects not stored in beam => extract objects
|
---|
1225 | if (beam.mFlags & !Beam::STORE_OBJECTS)
|
---|
1226 | {
|
---|
1227 | vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
1228 |
|
---|
1229 | Intersectable::NewMail();
|
---|
1230 | for (it = beam.mKdNodes.begin(); it != it_end; ++ it)
|
---|
1231 | {
|
---|
1232 | mKdTree->CollectObjects(*it, beam.mObjects);
|
---|
1233 | }
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 |
|
---|
1237 | // (objects can be compiled to a gl list now so that subsequent rendering for
|
---|
1238 | // this beam is fast - the same hold for step 3)
|
---|
1239 | // Afterwards we have two depth buffers defining the ray origin and termination
|
---|
1240 |
|
---|
1241 |
|
---|
1242 | #if 0
|
---|
1243 | static int glObjList = -1;
|
---|
1244 | if (glObjList != -1)
|
---|
1245 | {
|
---|
1246 | glObjList = glGenLists(1);
|
---|
1247 | glNewList(glObjList, GL_COMPILE);
|
---|
1248 |
|
---|
1249 | ObjectContainer::const_iterator it, it_end = beam.mObjects.end();
|
---|
1250 | for (it = beam.mObjects.begin(); it != it_end; ++ it)
|
---|
1251 | {
|
---|
1252 | // render all objects except the source object
|
---|
1253 | if (*it != sourceObject)
|
---|
1254 | RenderIntersectable(*it);
|
---|
1255 | }
|
---|
1256 |
|
---|
1257 | glEndList();
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | glCallList(glObjList);
|
---|
1261 | #else
|
---|
1262 | ObjectContainer::const_iterator it, it_end = beam.mObjects.end();
|
---|
1263 | for (it = beam.mObjects.begin(); it != it_end; ++ it)
|
---|
1264 | {
|
---|
1265 | // render all objects except the source object
|
---|
1266 | if (*it != sourceObject)
|
---|
1267 | RenderIntersectable(*it);
|
---|
1268 | }
|
---|
1269 | #endif
|
---|
1270 |
|
---|
1271 | // 6. Use occlusion queries for all viewcell meshes associated with the beam ->
|
---|
1272 | // a fragment passes if the corresponding stencil fragment is set and its depth is
|
---|
1273 | // between origin and termination buffer
|
---|
1274 |
|
---|
1275 | // create new queries if necessary
|
---|
1276 | OcclusionQuery::GenQueries(mOcclusionQueries, (int)beam.mViewCells.size());
|
---|
1277 |
|
---|
1278 | // check whether any backfacing polygon would pass the depth test?
|
---|
1279 | // matt: should check both back /front facing because of dual depth buffer
|
---|
1280 | // and danger of cutting the near plane with front facing polys.
|
---|
1281 |
|
---|
1282 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
1283 | glDepthMask(GL_FALSE);
|
---|
1284 | glDisable(GL_CULL_FACE);
|
---|
1285 |
|
---|
1286 |
|
---|
1287 | ViewCellContainer::const_iterator vit, vit_end = beam.mViewCells.end();
|
---|
1288 |
|
---|
1289 | int queryIdx = 0;
|
---|
1290 |
|
---|
1291 | for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit)
|
---|
1292 | {
|
---|
1293 | mOcclusionQueries[queryIdx ++]->BeginQuery();
|
---|
1294 | RenderIntersectable(*vit);
|
---|
1295 | mOcclusionQueries[queryIdx]->EndQuery();
|
---|
1296 |
|
---|
1297 | ++ queryIdx;
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | // at this point, if possible, go and do some other computation
|
---|
1301 |
|
---|
1302 | // 7. The number of visible pixels is the number of sample rays which see the source
|
---|
1303 | // object from the corresponding viewcell -> remember these values for later update
|
---|
1304 | // of the viewcell pvs - or update immediately?
|
---|
1305 |
|
---|
1306 | queryIdx = 0;
|
---|
1307 |
|
---|
1308 | for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit)
|
---|
1309 | {
|
---|
1310 | // fetch queries
|
---|
1311 | unsigned int pixelCount = mOcclusionQueries[queryIdx ++]->GetQueryResult();
|
---|
1312 |
|
---|
1313 | if (pixelCount)
|
---|
1314 | Debug << "view cell " << (*vit)->GetId() << " visible pixels: " << pixelCount << endl;
|
---|
1315 | }
|
---|
1316 |
|
---|
1317 |
|
---|
1318 | // 8. Copmpute rendering statistics
|
---|
1319 | // In general it is not neccessary to remember to extract all the rays cast. I hope it
|
---|
1320 | // would be sufficient to gain only the intergral statistics about the new contributions
|
---|
1321 | // and so the rss tree would actually store no new rays (only the initial ones)
|
---|
1322 | // the subdivision of the tree would only be driven by the statistics (the glrender could
|
---|
1323 | // evaluate the contribution entropy for example)
|
---|
1324 | // However might be an option to extract/store only those the rays which made a contribution
|
---|
1325 | // (new viewcell has been discovered) or relative contribution greater than a threshold ...
|
---|
1326 |
|
---|
1327 | ObjectContainer pvsObj;
|
---|
1328 | stat.pvsSize = ComputePvs(beam.mObjects, pvsObj);
|
---|
1329 |
|
---|
1330 | // to gain ray source and termination
|
---|
1331 | // copy contents of ray termination buffer into depth texture
|
---|
1332 | // and compare with ray source buffer
|
---|
1333 | #if 0
|
---|
1334 | VssRayContainer rays;
|
---|
1335 |
|
---|
1336 | glBindTexture(GL_TEXTURE_2D, backDepthMap);
|
---|
1337 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize);
|
---|
1338 |
|
---|
1339 | ComputeRays(Intersectable *sourceObj, rays);
|
---|
1340 |
|
---|
1341 | #endif
|
---|
1342 |
|
---|
1343 | ////////
|
---|
1344 | //-- cleanup
|
---|
1345 |
|
---|
1346 | // reset gl state
|
---|
1347 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
1348 | glDepthMask(GL_TRUE);
|
---|
1349 | glEnable(GL_CULL_FACE);
|
---|
1350 | glDisable(GL_STENCIL_TEST);
|
---|
1351 |
|
---|
1352 | #if 0
|
---|
1353 | #ifdef USE_CG
|
---|
1354 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
1355 | #endif
|
---|
1356 | #endif
|
---|
1357 |
|
---|
1358 | glDisable(GL_TEXTURE_2D);
|
---|
1359 |
|
---|
1360 | // remove objects from beam
|
---|
1361 | if (beam.mFlags & !Beam::STORE_OBJECTS)
|
---|
1362 | beam.mObjects.clear();
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 |
|
---|
1366 | void GlRendererBuffer::SetupProjectionForViewPoint(const Vector3 &viewPoint,
|
---|
1367 | const Beam &beam,
|
---|
1368 | Intersectable *sourceObject)
|
---|
1369 | {
|
---|
1370 | float left, right, bottom, top, znear, zfar;
|
---|
1371 |
|
---|
1372 | beam.ComputePerspectiveFrustum(left, right, bottom, top, znear, zfar,
|
---|
1373 | mSceneGraph->GetBox());
|
---|
1374 |
|
---|
1375 | //Debug << left << " " << right << " " << bottom << " " << top << " " << znear << " " << zfar << endl;
|
---|
1376 | glMatrixMode(GL_PROJECTION);
|
---|
1377 | glLoadIdentity();
|
---|
1378 | glFrustum(left, right, bottom, top, znear, zfar);
|
---|
1379 | //glFrustum(-1, 1, -1, 1, 1, 20000);
|
---|
1380 |
|
---|
1381 | const Vector3 center = viewPoint + beam.GetMainDirection() * (zfar - znear) * 0.3f;
|
---|
1382 | const Vector3 up =
|
---|
1383 | Normalize(CrossProd(beam.mPlanes[0].mNormal, beam.mPlanes[4].mNormal));
|
---|
1384 |
|
---|
1385 | #ifdef GTP_DEBUG
|
---|
1386 | Debug << "view point: " << viewPoint << endl;
|
---|
1387 | Debug << "eye: " << center << endl;
|
---|
1388 | Debug << "up: " << up << endl;
|
---|
1389 | #endif
|
---|
1390 |
|
---|
1391 | glMatrixMode(GL_MODELVIEW);
|
---|
1392 | glLoadIdentity();
|
---|
1393 | gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
|
---|
1394 | center.x, center.y, center.z,
|
---|
1395 | up.x, up.y, up.z);
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 |
|
---|
1399 | void GlRendererBuffer::InitGL()
|
---|
1400 | {
|
---|
1401 | //MakeCurrent();
|
---|
1402 | GlRenderer::InitGL();
|
---|
1403 |
|
---|
1404 | #if 0
|
---|
1405 | // initialise dual depth buffer textures
|
---|
1406 | glGenTextures(1, &frontDepthMap);
|
---|
1407 | glBindTexture(GL_TEXTURE_2D, frontDepthMap);
|
---|
1408 |
|
---|
1409 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,
|
---|
1410 | depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
|
---|
1411 |
|
---|
1412 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
1413 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
1414 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
---|
1415 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
---|
1416 |
|
---|
1417 | glGenTextures(1, &backDepthMap);
|
---|
1418 | glBindTexture(GL_TEXTURE_2D, backDepthMap);
|
---|
1419 |
|
---|
1420 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,
|
---|
1421 | depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
|
---|
1422 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
1423 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
1424 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
---|
1425 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
---|
1426 |
|
---|
1427 |
|
---|
1428 | #ifdef USE_CG
|
---|
1429 |
|
---|
1430 | // cg initialization
|
---|
1431 | cgSetErrorCallback(handleCgError);
|
---|
1432 | sCgContext = cgCreateContext();
|
---|
1433 |
|
---|
1434 | if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1))
|
---|
1435 | sCgFragmentProfile = CG_PROFILE_ARBFP1;
|
---|
1436 | else
|
---|
1437 | {
|
---|
1438 | // try FP30
|
---|
1439 | if (cgGLIsProfileSupported(CG_PROFILE_FP30))
|
---|
1440 | sCgFragmentProfile = CG_PROFILE_FP30;
|
---|
1441 | else
|
---|
1442 | {
|
---|
1443 | Debug << "Neither arbfp1 or fp30 fragment profiles supported on this system" << endl;
|
---|
1444 | exit(1);
|
---|
1445 | }
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | sCgFragmentProgram = cgCreateProgramFromFile(sCgContext,
|
---|
1449 | CG_SOURCE, "../src/dual_depth.cg",
|
---|
1450 | sCgFragmentProfile,
|
---|
1451 | NULL,
|
---|
1452 | NULL);
|
---|
1453 |
|
---|
1454 | if (!cgIsProgramCompiled(sCgFragmentProgram))
|
---|
1455 | cgCompileProgram(sCgFragmentProgram);
|
---|
1456 |
|
---|
1457 | cgGLLoadProgram(sCgFragmentProgram);
|
---|
1458 | cgGLBindProgram(sCgFragmentProgram);
|
---|
1459 |
|
---|
1460 | Debug << "---- PROGRAM BEGIN ----\n" <<
|
---|
1461 | cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n";
|
---|
1462 |
|
---|
1463 | #endif
|
---|
1464 | #endif
|
---|
1465 | //DoneCurrent();
|
---|
1466 | }
|
---|
1467 |
|
---|
1468 |
|
---|
1469 | void GlRendererBuffer::ComputeRays(Intersectable *sourceObj, VssRayContainer &rays)
|
---|
1470 | {
|
---|
1471 | for (int i = 0; i < depthMapSize * depthMapSize; ++ i)
|
---|
1472 | {
|
---|
1473 | //todo glGetTexImage()
|
---|
1474 | }
|
---|
1475 | }
|
---|
1476 |
|
---|
1477 |
|
---|
1478 | bool GlRendererBuffer::ValidViewPoint()
|
---|
1479 | {
|
---|
1480 | MakeLive();
|
---|
1481 |
|
---|
1482 | SetupProjection(GetWidth(), GetHeight());
|
---|
1483 |
|
---|
1484 | bool result = GlRenderer::ValidViewPoint();
|
---|
1485 |
|
---|
1486 | DoneLive();
|
---|
1487 |
|
---|
1488 | return result;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 |
|
---|
1492 | void
|
---|
1493 | GlRenderer::EvalPvsStat()
|
---|
1494 | {
|
---|
1495 | mPvsStat.Reset();
|
---|
1496 | halton.Reset();
|
---|
1497 |
|
---|
1498 | SetupProjection(GetWidth(), GetHeight());
|
---|
1499 |
|
---|
1500 | cout << "Random Pvs STATS, mPvsStatFrames=" << mPvsStatFrames << endl;
|
---|
1501 |
|
---|
1502 | for (int i=0; i < mPvsStatFrames; i++) {
|
---|
1503 | float err;
|
---|
1504 | // set frame id for saving the error buffer
|
---|
1505 | mFrame = i;
|
---|
1506 |
|
---|
1507 | // cerr<<"RV"<<endl;
|
---|
1508 | RandomViewPoint();
|
---|
1509 | // cerr<<"RV2"<<endl;
|
---|
1510 |
|
---|
1511 | if (mPvsErrorBuffer[i].mError == 1.0f) {
|
---|
1512 | // check if the view point is valid
|
---|
1513 | if (!ValidViewPoint()) {
|
---|
1514 | mPvsErrorBuffer[i].mError = -1.0f;
|
---|
1515 | }
|
---|
1516 |
|
---|
1517 | // manualy corrected view point
|
---|
1518 | if (mFrame == 5105)
|
---|
1519 | mPvsErrorBuffer[i].mError = -1.0f;
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 |
|
---|
1523 | if (mPvsErrorBuffer[i].mError > 0.0f)
|
---|
1524 | {
|
---|
1525 | int pvsSize;
|
---|
1526 |
|
---|
1527 | mPvsErrorBuffer[i].mError = GetPixelError(pvsSize);
|
---|
1528 | mPvsErrorBuffer[i].mPvsSize = pvsSize;
|
---|
1529 |
|
---|
1530 | //cout<<"("<<i<<" ["<<mViewPoint<<"]["<<mViewDirection<<"] "<<mPvsErrorBuffer[i].mError<<")";
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | err = mPvsErrorBuffer[i].mError;
|
---|
1534 |
|
---|
1535 | if (err >= 0.0f) {
|
---|
1536 | if (err > mPvsStat.maxError)
|
---|
1537 | mPvsStat.maxError = err;
|
---|
1538 | mPvsStat.sumError += err;
|
---|
1539 | mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
|
---|
1540 |
|
---|
1541 | if (err == 0.0f)
|
---|
1542 | mPvsStat.errorFreeFrames++;
|
---|
1543 | mPvsStat.frames++;
|
---|
1544 | }
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | glFinish();
|
---|
1548 |
|
---|
1549 | static bool first = true;
|
---|
1550 |
|
---|
1551 | if (first) {
|
---|
1552 |
|
---|
1553 | bool exportRandomViewCells;
|
---|
1554 | Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
|
---|
1555 | exportRandomViewCells);
|
---|
1556 |
|
---|
1557 | if (0 && exportRandomViewCells)
|
---|
1558 | {
|
---|
1559 | char buff[512];
|
---|
1560 | Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
|
---|
1561 | string filename(buff);
|
---|
1562 |
|
---|
1563 | string viewCellPointsFile;
|
---|
1564 |
|
---|
1565 | if (strstr(filename.c_str(), ".obj"))
|
---|
1566 | viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
|
---|
1567 | else if (strstr(filename.c_str(), ".dat"))
|
---|
1568 | viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
|
---|
1569 | else if (strstr(filename.c_str(), ".x3d"))
|
---|
1570 | viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
|
---|
1571 |
|
---|
1572 |
|
---|
1573 | cout << "exporting random view cells" << endl;
|
---|
1574 | preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
|
---|
1575 | cout << "finished" << endl;
|
---|
1576 | }
|
---|
1577 | first = false;
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | cout<<endl<<flush;
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 |
|
---|
1584 | void GlRenderer::EvalPvsStat(const SimpleRayContainer &viewPoints)
|
---|
1585 | {
|
---|
1586 | mPvsStat.Reset();
|
---|
1587 |
|
---|
1588 | SetupProjection(GetWidth(), GetHeight());
|
---|
1589 |
|
---|
1590 | cout << "mPvsStatFrames=" << viewPoints.size() << endl;
|
---|
1591 |
|
---|
1592 | SimpleRayContainer::const_iterator sit, sit_end = viewPoints.end();
|
---|
1593 |
|
---|
1594 | int i = 0;
|
---|
1595 |
|
---|
1596 | for (sit = viewPoints.begin(); sit != sit_end; ++ sit, ++ i)
|
---|
1597 | {
|
---|
1598 | SimpleRay sray = *sit;
|
---|
1599 |
|
---|
1600 | int pvsSize = -1;
|
---|
1601 |
|
---|
1602 | // set frame id for saving the error buffer
|
---|
1603 | mFrame = i;
|
---|
1604 | mViewPoint = sray.mOrigin;
|
---|
1605 | mViewDirection = sray.mDirection;
|
---|
1606 |
|
---|
1607 | // skip all frames which have already 0 pixel error
|
---|
1608 | // $$ Reverted back by JB for efficiency
|
---|
1609 | if (mPvsErrorBuffer[i].mError > 0.0f)
|
---|
1610 | {
|
---|
1611 | // compute the pixel error
|
---|
1612 | mPvsErrorBuffer[i].mError = GetPixelError(pvsSize);
|
---|
1613 | mPvsErrorBuffer[i].mPvsSize = pvsSize;
|
---|
1614 |
|
---|
1615 | if (0 && (mPvsErrorBuffer[i].mError * GetWidth() * GetHeight() > 0) && (pvsSize > 0))
|
---|
1616 | {
|
---|
1617 | cout << "error in frame " << i << "," << mViewPoint << "," << mViewDirection << " "
|
---|
1618 | << mPvsErrorBuffer[i].mError * GetWidth() * GetHeight() << endl;
|
---|
1619 | }
|
---|
1620 | }
|
---|
1621 |
|
---|
1622 | const float err = mPvsErrorBuffer[i].mError;
|
---|
1623 |
|
---|
1624 | // hack:
|
---|
1625 | // do not account for the case that no PVS is rendered - hack for 08 rebuttal GVS evaluation
|
---|
1626 | // drop the first frame (for some reason, the first frame yields wrong pixel error)
|
---|
1627 |
|
---|
1628 | if (pvsSize != 0)
|
---|
1629 | {
|
---|
1630 | // hack: test if error is suspiciously large
|
---|
1631 | if ((err >= -1e-6f) && (err < (1.0f - 1e-6f)))
|
---|
1632 | {
|
---|
1633 | if (err > mPvsStat.maxError)
|
---|
1634 | {
|
---|
1635 | mPvsStat.maxError = err;
|
---|
1636 | cout << "new max error: " << mPvsStat.maxError * GetWidth() * GetHeight() << endl;
|
---|
1637 | }
|
---|
1638 |
|
---|
1639 | mPvsStat.sumError += err;
|
---|
1640 | mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
|
---|
1641 |
|
---|
1642 | if (err == 0.0f)
|
---|
1643 | ++ mPvsStat.errorFreeFrames;
|
---|
1644 |
|
---|
1645 | // $$ JB
|
---|
1646 | // moved it back here not to count frames with negative err (backfacing triangle)
|
---|
1647 | ++ mPvsStat.frames;
|
---|
1648 |
|
---|
1649 | if ((mPvsStat.frames % 100) == 0)
|
---|
1650 | cout << "processed " << mPvsStat.frames << " valid view points " << endl;
|
---|
1651 | }
|
---|
1652 | else
|
---|
1653 | {
|
---|
1654 | cerr << "warning: strange error (" << err << "), pvs size " << pvsSize << endl;
|
---|
1655 | }
|
---|
1656 | }
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | glFinish();
|
---|
1660 |
|
---|
1661 |
|
---|
1662 | cout << endl << flush;
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 |
|
---|
1666 |
|
---|
1667 | bool
|
---|
1668 | GlRenderer::ValidViewPoint()
|
---|
1669 | {
|
---|
1670 | //cout<<"VV4 ";
|
---|
1671 | if (!mDetectEmptyViewSpace)
|
---|
1672 | return true;
|
---|
1673 | //cout << "vp: " << mViewPoint << " dir: " << mViewDirection << endl;
|
---|
1674 |
|
---|
1675 | OcclusionQuery *query = mOcclusionQueries[0];
|
---|
1676 |
|
---|
1677 | // now check whether any backfacing polygon would pass the depth test
|
---|
1678 | SetupCamera();
|
---|
1679 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
1680 | glEnable( GL_CULL_FACE );
|
---|
1681 | glCullFace(GL_BACK);
|
---|
1682 |
|
---|
1683 | //cout<<"VV1 ";
|
---|
1684 | RenderScene();
|
---|
1685 |
|
---|
1686 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
1687 | glDepthMask(GL_FALSE);
|
---|
1688 | glDisable( GL_CULL_FACE );
|
---|
1689 |
|
---|
1690 | query->BeginQuery();
|
---|
1691 |
|
---|
1692 | // cout<<"VV2 ";
|
---|
1693 | RenderScene();
|
---|
1694 | // cout<<"VV3 ";
|
---|
1695 |
|
---|
1696 | query->EndQuery();
|
---|
1697 |
|
---|
1698 | // at this point, if possible, go and do some other computation
|
---|
1699 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
1700 | glDepthMask(GL_TRUE);
|
---|
1701 | glEnable( GL_CULL_FACE );
|
---|
1702 |
|
---|
1703 | // int wait = 0;
|
---|
1704 | // while (!query->ResultAvailable()) {
|
---|
1705 | // wait++;
|
---|
1706 | // }
|
---|
1707 |
|
---|
1708 | // reenable other state
|
---|
1709 | unsigned int pixelCount = query->GetQueryResult();
|
---|
1710 | // cout<<"VV4 ";
|
---|
1711 |
|
---|
1712 |
|
---|
1713 | // backfacing polygon found -> not a valid viewspace sample
|
---|
1714 | if (pixelCount > 0)
|
---|
1715 | return false;
|
---|
1716 |
|
---|
1717 | return true;
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 |
|
---|
1721 | float GlRenderer::GetPixelError(int &pvsSize)
|
---|
1722 | {
|
---|
1723 | return -1.0f;
|
---|
1724 | }
|
---|
1725 |
|
---|
1726 |
|
---|
1727 | void GlRenderer::RenderViewPoint()
|
---|
1728 | {
|
---|
1729 | mWireFrame = true;
|
---|
1730 | glPushMatrix();
|
---|
1731 | glTranslatef(mViewPoint.x, mViewPoint.y, mViewPoint.z);
|
---|
1732 | glScalef(5.0f, 5.0f, 5.0f);
|
---|
1733 | glPushAttrib(GL_CURRENT_BIT);
|
---|
1734 | glColor3f(1.0f, 0.0f, 0.0f);
|
---|
1735 | gluSphere((::GLUquadric *)mSphere,
|
---|
1736 | 1e-3*Magnitude(mViewCellsManager->GetViewSpaceBox().Size()), 6, 6);
|
---|
1737 | glPopAttrib();
|
---|
1738 | glPopMatrix();
|
---|
1739 | mWireFrame = false;
|
---|
1740 | }
|
---|
1741 |
|
---|
1742 |
|
---|
1743 | void GlRenderer::EnableDrawArrays()
|
---|
1744 | {
|
---|
1745 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
1746 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
1747 | }
|
---|
1748 |
|
---|
1749 |
|
---|
1750 | void GlRenderer::DisableDrawArrays()
|
---|
1751 | {
|
---|
1752 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
1753 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
1754 | }
|
---|
1755 |
|
---|
1756 |
|
---|
1757 | #if 0
|
---|
1758 |
|
---|
1759 | void GlRenderer::RenderKdLeaf(KdLeaf *leaf)
|
---|
1760 | {
|
---|
1761 | int bufferSize = 0;
|
---|
1762 |
|
---|
1763 | // count #new triangles
|
---|
1764 | for (size_t i = 0; i < leaf->mObjects.size(); ++ i)
|
---|
1765 | {
|
---|
1766 | TriangleIntersectable *obj = static_cast<TriangleIntersectable *>(leaf->mObjects[i]);
|
---|
1767 |
|
---|
1768 | // check if already rendered
|
---|
1769 | if (!obj->Mailed2())
|
---|
1770 | bufferSize += 3;
|
---|
1771 | //else cout << obj->mMailbox << " " << obj->sMailId << " ";
|
---|
1772 | }
|
---|
1773 |
|
---|
1774 | Vector3 *vertices = new Vector3[bufferSize];
|
---|
1775 | Vector3 *normals = new Vector3[bufferSize];
|
---|
1776 |
|
---|
1777 | int j = 0;
|
---|
1778 |
|
---|
1779 | for (size_t i = 0; i < leaf->mObjects.size(); ++ i)
|
---|
1780 | {
|
---|
1781 | TriangleIntersectable *obj = static_cast<TriangleIntersectable *>(leaf->mObjects[i]);
|
---|
1782 |
|
---|
1783 | // check if already rendered
|
---|
1784 | if (obj->Mailed2())
|
---|
1785 | continue;
|
---|
1786 |
|
---|
1787 | obj->Mail2();
|
---|
1788 |
|
---|
1789 | Triangle3 tri = obj->GetItem();
|
---|
1790 |
|
---|
1791 | vertices[j * 3 + 0] = tri.mVertices[0];
|
---|
1792 | vertices[j * 3 + 1] = tri.mVertices[1];
|
---|
1793 | vertices[j * 3 + 2] = tri.mVertices[2];
|
---|
1794 |
|
---|
1795 | Vector3 n = tri.GetNormal();
|
---|
1796 |
|
---|
1797 | normals[j * 3 + 0] = n;
|
---|
1798 | normals[j * 3 + 1] = n;
|
---|
1799 | normals[j * 3 + 2] = n;
|
---|
1800 |
|
---|
1801 | ++ j;
|
---|
1802 | }
|
---|
1803 |
|
---|
1804 | glVertexPointer(3, GL_FLOAT, 0, vertices);
|
---|
1805 | glNormalPointer(GL_FLOAT, 0, normals);
|
---|
1806 |
|
---|
1807 | glDrawArrays(GL_TRIANGLES, 0, bufferSize);
|
---|
1808 |
|
---|
1809 | DEL_PTR(vertices);
|
---|
1810 | DEL_PTR(normals);
|
---|
1811 | }
|
---|
1812 |
|
---|
1813 | #else
|
---|
1814 |
|
---|
1815 | void GlRenderer::RenderKdLeaf(KdLeaf *leaf)
|
---|
1816 | {
|
---|
1817 | if (!leaf->mIndexBufferSize)
|
---|
1818 | return;
|
---|
1819 |
|
---|
1820 | size_t offset = mObjects.size() * 3;
|
---|
1821 | char *arrayPtr = mUseVbos ? NULL : (char *)mData;
|
---|
1822 |
|
---|
1823 | glVertexPointer(3, GL_FLOAT, 0, (char *)arrayPtr);
|
---|
1824 | glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
|
---|
1825 |
|
---|
1826 | glDrawElements(GL_TRIANGLES, leaf->mIndexBufferSize, GL_UNSIGNED_INT, mIndices + leaf->mIndexBufferStart);
|
---|
1827 | }
|
---|
1828 |
|
---|
1829 | #endif
|
---|
1830 |
|
---|
1831 |
|
---|
1832 |
|
---|
1833 | void GlRenderer::PreparePvs(const ObjectPvs &pvs)
|
---|
1834 | {
|
---|
1835 | int indexBufferSize = 0;
|
---|
1836 | mRenderedNodes = 0;
|
---|
1837 |
|
---|
1838 | KdNode::NewMail2();
|
---|
1839 |
|
---|
1840 | ObjectPvsIterator it = pvs.GetIterator();
|
---|
1841 |
|
---|
1842 | while (it.HasMoreEntries())
|
---|
1843 | {
|
---|
1844 | Intersectable *obj = it.Next();
|
---|
1845 | switch (obj->Type())
|
---|
1846 | {
|
---|
1847 | case Intersectable::KD_INTERSECTABLE:
|
---|
1848 | {
|
---|
1849 | KdNode *node = static_cast<KdIntersectable *>(obj)->GetItem();
|
---|
1850 | _UpdatePvsIndices(node, indexBufferSize);
|
---|
1851 | }
|
---|
1852 | break;
|
---|
1853 |
|
---|
1854 | }
|
---|
1855 | }
|
---|
1856 |
|
---|
1857 | mIndexBufferSize = indexBufferSize;
|
---|
1858 | }
|
---|
1859 |
|
---|
1860 |
|
---|
1861 | void GlRenderer::_UpdatePvsIndices(KdNode *node, int &indexBufferSize)
|
---|
1862 | {
|
---|
1863 | if (node->Mailed2())
|
---|
1864 | return;
|
---|
1865 |
|
---|
1866 | node->Mail2();
|
---|
1867 |
|
---|
1868 | // if (mObjects.size() * 3 < indexBufferSize) cerr << "problem: " << mObjects.size() * 3 << " < " << indexBufferSize << endl;
|
---|
1869 | if (!node->IsLeaf())
|
---|
1870 | {
|
---|
1871 | KdInterior *kdInterior = static_cast<KdInterior *>(node);
|
---|
1872 |
|
---|
1873 | _UpdatePvsIndices(kdInterior->mBack, indexBufferSize);
|
---|
1874 | _UpdatePvsIndices(kdInterior->mFront, indexBufferSize);
|
---|
1875 | }
|
---|
1876 | else
|
---|
1877 | {
|
---|
1878 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
1879 |
|
---|
1880 | leaf->mIndexBufferStart = indexBufferSize;
|
---|
1881 |
|
---|
1882 | for (size_t i = 0; i < leaf->mObjects.size(); ++ i)
|
---|
1883 | {
|
---|
1884 | TriangleIntersectable *obj =
|
---|
1885 | static_cast<TriangleIntersectable *>(leaf->mObjects[i]);
|
---|
1886 |
|
---|
1887 | if (obj->mRenderedFrame != mCurrentFrame)
|
---|
1888 | {
|
---|
1889 | obj->mRenderedFrame = mCurrentFrame;
|
---|
1890 |
|
---|
1891 | const int id = obj->GetId() * 3;
|
---|
1892 |
|
---|
1893 | mIndices[indexBufferSize + 0] = id + 0;
|
---|
1894 | mIndices[indexBufferSize + 1] = id + 1;
|
---|
1895 | mIndices[indexBufferSize + 2] = id + 2;
|
---|
1896 |
|
---|
1897 | indexBufferSize += 3;
|
---|
1898 | }
|
---|
1899 | }
|
---|
1900 |
|
---|
1901 | ++ mRenderedNodes;
|
---|
1902 |
|
---|
1903 | leaf->mIndexBufferSize = indexBufferSize - leaf->mIndexBufferStart;
|
---|
1904 | }
|
---|
1905 | }
|
---|
1906 |
|
---|
1907 |
|
---|
1908 | void GlRenderer::CreateVertexArrays(SceneGraphLeaf *leaf)
|
---|
1909 | {
|
---|
1910 | size_t offset = leaf->mGeometry.size() * 3;
|
---|
1911 |
|
---|
1912 | mData = new Vector3[offset * 2];
|
---|
1913 | mIndices = new unsigned int[offset];
|
---|
1914 |
|
---|
1915 |
|
---|
1916 | for (size_t i = 0; i < leaf->mGeometry.size(); ++ i)
|
---|
1917 | {
|
---|
1918 | TriangleIntersectable *obj = static_cast<TriangleIntersectable *>(leaf->mGeometry[i]);
|
---|
1919 |
|
---|
1920 | if (obj->GetId() != i)
|
---|
1921 | cerr << "****\n****riesenfehler" << endl;
|
---|
1922 | Triangle3 tri = obj->GetItem();
|
---|
1923 | const Vector3 n = tri.GetNormal();
|
---|
1924 |
|
---|
1925 | mData[i * 3 + 0] = tri.mVertices[0];
|
---|
1926 | mData[i * 3 + 1] = tri.mVertices[1];
|
---|
1927 | mData[i * 3 + 2] = tri.mVertices[2];
|
---|
1928 |
|
---|
1929 | mData[offset + i * 3 + 0] = n;
|
---|
1930 | mData[offset + i * 3 + 1] = n;
|
---|
1931 | mData[offset + i * 3 + 2] = n;
|
---|
1932 |
|
---|
1933 | mIndices[i * 3 + 0] = 0;
|
---|
1934 | mIndices[i * 3 + 1] = 0;
|
---|
1935 | mIndices[i * 3 + 2] = 0;
|
---|
1936 | }
|
---|
1937 |
|
---|
1938 | cout << "\n******** created vertex arrays **********" << endl;
|
---|
1939 |
|
---|
1940 | if (mUseVbos)
|
---|
1941 | {
|
---|
1942 | EnableDrawArrays();
|
---|
1943 |
|
---|
1944 | glGenBuffersARB(1, &mVboId);
|
---|
1945 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
1946 |
|
---|
1947 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
1948 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + offset * sizeof(Vector3));
|
---|
1949 |
|
---|
1950 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, offset * 2 * sizeof(Vector3), mData, GL_STATIC_DRAW_ARB);
|
---|
1951 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
|
---|
1952 |
|
---|
1953 | DisableDrawArrays();
|
---|
1954 |
|
---|
1955 | delete [] mData;
|
---|
1956 | mData = NULL;
|
---|
1957 |
|
---|
1958 | cout << "\n******** created vertex buffer objects **********" << endl;
|
---|
1959 | }
|
---|
1960 | }
|
---|
1961 |
|
---|
1962 |
|
---|
1963 | void GlRenderer::DeleteVbos()
|
---|
1964 | {
|
---|
1965 | glDeleteBuffersARB(1, &mVboId);
|
---|
1966 | }
|
---|
1967 |
|
---|
1968 |
|
---|
1969 |
|
---|
1970 | }
|
---|