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