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