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