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