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