1 | #include "Mesh.h" |
---|
2 | #include "GlRenderer.h" |
---|
3 | #include "ViewCellsManager.h" |
---|
4 | #include "SceneGraph.h" |
---|
5 | #include "Pvs.h" |
---|
6 | #include "Viewcell.h" |
---|
7 | #include "Beam.h" |
---|
8 |
|
---|
9 | #include <GL/glext.h> |
---|
10 | #include <Cg/cg.h>
|
---|
11 | #include <Cg/cgGL.h> |
---|
12 | |
---|
13 | static CGcontext sCgContext = NULL;
|
---|
14 | static CGprogram sCgFragmentProgram = NULL; |
---|
15 | |
---|
16 | static CGparameter sCgKdParam = NULL;
|
---|
17 | static CGparameter sCgModelViewProjParam = NULL;
|
---|
18 | |
---|
19 | |
---|
20 | static CGprofile sCgFragmentProfile = CG_PROFILE_FP30; |
---|
21 | |
---|
22 | GLuint depthMap; |
---|
23 | const int depthMapSize = 512; |
---|
24 | static vector<int> sQueries; |
---|
25 | |
---|
26 | GlRendererWidget *rendererWidget = NULL; |
---|
27 | |
---|
28 | |
---|
29 | #ifdef _WIN32 |
---|
30 | PFNGLGENOCCLUSIONQUERIESNVPROC glGenOcclusionQueriesNV; |
---|
31 | PFNGLBEGINOCCLUSIONQUERYNVPROC glBeginOcclusionQueryNV; |
---|
32 | PFNGLENDOCCLUSIONQUERYNVPROC glEndOcclusionQueryNV; |
---|
33 | PFNGLGETOCCLUSIONQUERYUIVNVPROC glGetOcclusionQueryuivNV; |
---|
34 | #endif |
---|
35 | |
---|
36 | GlRenderer::GlRenderer(SceneGraph *sceneGraph, |
---|
37 | ViewCellsManager *viewCellsManager): |
---|
38 | Renderer(sceneGraph, viewCellsManager) |
---|
39 | { |
---|
40 | mSceneGraph->CollectObjects(&mObjects); |
---|
41 | mViewPoint = mSceneGraph->GetBox().Center(); |
---|
42 | mViewDirection = Vector3(0,0,1); |
---|
43 | // timerId = startTimer(10); |
---|
44 | mFrame = 0; |
---|
45 | mWireFrame = false; |
---|
46 | } |
---|
47 | |
---|
48 | GlRenderer::~GlRenderer() |
---|
49 | { |
---|
50 | } |
---|
51 | |
---|
52 |
|
---|
53 | static void handleCgError()
|
---|
54 | {
|
---|
55 | Debug << "Cg error: " << cgGetErrorString(cgGetError()) << endl;
|
---|
56 | exit(1);
|
---|
57 | } |
---|
58 | |
---|
59 | void |
---|
60 | GlRenderer::RenderIntersectable(Intersectable *object) |
---|
61 | { |
---|
62 | |
---|
63 | SetupFalseColor(object->mId); |
---|
64 | |
---|
65 | switch (object->Type()) { |
---|
66 | case Intersectable::MESH_INSTANCE: |
---|
67 | RenderMeshInstance((MeshInstance *)object); |
---|
68 | break; |
---|
69 | default: |
---|
70 | cerr<<"Rendering this object not yet implemented\n"; |
---|
71 | break; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | void |
---|
77 | GlRenderer::RenderMeshInstance(MeshInstance *mi) |
---|
78 | { |
---|
79 | RenderMesh(mi->GetMesh()); |
---|
80 | } |
---|
81 | |
---|
82 | void |
---|
83 | GlRenderer::SetupFalseColor(const int id) |
---|
84 | { |
---|
85 | // swap bits of the color |
---|
86 | |
---|
87 | glColor3ub(id&255, (id>>8)&255, (id>>16)&255); |
---|
88 | } |
---|
89 | |
---|
90 | void |
---|
91 | GlRenderer::SetupMaterial(Material *m) |
---|
92 | { |
---|
93 | if (m) |
---|
94 | glColor3fv(&(m->mDiffuseColor.r)); |
---|
95 | else |
---|
96 | glColor3f(1.0f, 1.0f, 1.0f); |
---|
97 | } |
---|
98 | |
---|
99 | void |
---|
100 | GlRenderer::RenderMesh(Mesh *mesh) |
---|
101 | { |
---|
102 | int i = 0; |
---|
103 | |
---|
104 | if (!mUseFalseColors) |
---|
105 | SetupMaterial(mesh->mMaterial); |
---|
106 | |
---|
107 | for (i=0; i < mesh->mFaces.size(); i++) { |
---|
108 | if (mWireFrame) |
---|
109 | glBegin(GL_LINE_LOOP); |
---|
110 | else |
---|
111 | glBegin(GL_POLYGON); |
---|
112 | |
---|
113 | Face *face = mesh->mFaces[i]; |
---|
114 | for (int j = 0; j < face->mVertexIndices.size(); j++) { |
---|
115 | glVertex3fv(&mesh->mVertices[face->mVertexIndices[j]].x); |
---|
116 | } |
---|
117 | glEnd(); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | void |
---|
122 | GlRenderer::InitGL() |
---|
123 | { |
---|
124 | glMatrixMode(GL_PROJECTION); |
---|
125 | glLoadIdentity(); |
---|
126 | |
---|
127 | glMatrixMode(GL_MODELVIEW); |
---|
128 | glLoadIdentity(); |
---|
129 | |
---|
130 | glEnable(GL_CULL_FACE); |
---|
131 | glShadeModel(GL_FLAT); |
---|
132 | glEnable(GL_DEPTH_TEST); |
---|
133 | glEnable(GL_CULL_FACE); |
---|
134 | |
---|
135 | glGenOcclusionQueriesNV = (PFNGLGENOCCLUSIONQUERIESNVPROC) |
---|
136 | wglGetProcAddress("glGenOcclusionQueriesNV"); |
---|
137 | glBeginOcclusionQueryNV = (PFNGLBEGINOCCLUSIONQUERYNVPROC) |
---|
138 | wglGetProcAddress("glBeginOcclusionQueryNV"); |
---|
139 | glEndOcclusionQueryNV = (PFNGLENDOCCLUSIONQUERYNVPROC) |
---|
140 | wglGetProcAddress("glEndOcclusionQueryNV"); |
---|
141 | glGetOcclusionQueryuivNV = (PFNGLGETOCCLUSIONQUERYUIVNVPROC) |
---|
142 | wglGetProcAddress("glGetOcclusionQueryuivNV"); |
---|
143 | |
---|
144 | // initialise second depth buffer texture
|
---|
145 | glGenTextures(1, &depthMap);
|
---|
146 | glBindTexture(GL_TEXTURE_2D, depthMap);
|
---|
147 |
|
---|
148 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,
|
---|
149 | depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
|
---|
150 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
151 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
152 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); |
---|
153 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); |
---|
154 | |
---|
155 | // cg initialization |
---|
156 | cgSetErrorCallback(handleCgError); |
---|
157 | sCgContext = cgCreateContext();
|
---|
158 |
|
---|
159 | /*sCgFragmentProgram = cgCreateProgramFromFile(sCgContext,
|
---|
160 | CG_SOURCE, "../src/dual_depth.cg", |
---|
161 | sCgFragmentProfile, |
---|
162 | NULL, |
---|
163 | NULL);*/
|
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | void |
---|
169 | GlRenderer::SetupProjection(const int w, const int h) |
---|
170 | { |
---|
171 | glViewport(0, 0, w, h); |
---|
172 | glMatrixMode(GL_PROJECTION); |
---|
173 | glLoadIdentity(); |
---|
174 | gluPerspective(70.0, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal())); |
---|
175 | glMatrixMode(GL_MODELVIEW); |
---|
176 | } |
---|
177 | |
---|
178 | void |
---|
179 | GlRenderer::SetupCamera() |
---|
180 | { |
---|
181 | Vector3 target = mViewPoint + mViewDirection; |
---|
182 | Vector3 up(0,1,0); |
---|
183 | |
---|
184 | glLoadIdentity(); |
---|
185 | gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z, |
---|
186 | target.x, target.y, target.z, |
---|
187 | up.x, up.y, up.z); |
---|
188 | } |
---|
189 | |
---|
190 | void |
---|
191 | GlRenderer::RandomViewPoint() |
---|
192 | { |
---|
193 | Vector3 pVector = Vector3(halton.GetNumber(1), |
---|
194 | halton.GetNumber(2), |
---|
195 | halton.GetNumber(3)); |
---|
196 | |
---|
197 | Vector3 dVector = Vector3(2*M_PI*halton.GetNumber(4), |
---|
198 | M_PI*halton.GetNumber(5), |
---|
199 | 0.0f); |
---|
200 | |
---|
201 | mViewPoint = mSceneGraph->GetBox().GetPoint(pVector); |
---|
202 | |
---|
203 | mViewDirection = Normalize(Vector3(sin(dVector.x), |
---|
204 | // cos(dVector.y), |
---|
205 | 0.0f, |
---|
206 | cos(dVector.x))); |
---|
207 | |
---|
208 | halton.GenerateNext(); |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | float |
---|
213 | GlRenderer::GetPixelError() |
---|
214 | { |
---|
215 | float pErrorPixels = -1.0f; |
---|
216 | |
---|
217 | glReadBuffer(GL_BACK); |
---|
218 | |
---|
219 | mUseFalseColors = true; |
---|
220 | |
---|
221 | SetupCamera(); |
---|
222 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
223 | glEnable( GL_CULL_FACE ); |
---|
224 | |
---|
225 | RenderScene(); |
---|
226 | |
---|
227 | // now check whether any backfacing polygon would pass the depth test |
---|
228 | static int query = -1; |
---|
229 | if (query == -1) |
---|
230 | glGenOcclusionQueriesNV(1, (unsigned int *)&query); |
---|
231 | |
---|
232 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
---|
233 | glDepthMask(GL_FALSE); |
---|
234 | glDisable( GL_CULL_FACE ); |
---|
235 | |
---|
236 | glBeginOcclusionQueryNV(query); |
---|
237 | |
---|
238 | RenderScene(); |
---|
239 | |
---|
240 | glEndOcclusionQueryNV(); |
---|
241 | |
---|
242 | // at this point, if possible, go and do some other computation |
---|
243 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
---|
244 | glDepthMask(GL_TRUE); |
---|
245 | glEnable( GL_CULL_FACE ); |
---|
246 | |
---|
247 | unsigned int pixelCount; |
---|
248 | // reenable other state |
---|
249 | glGetOcclusionQueryuivNV(query, |
---|
250 | GL_PIXEL_COUNT_NV, |
---|
251 | &pixelCount); |
---|
252 | |
---|
253 | if (pixelCount > 0) |
---|
254 | return -1.0f; // backfacing polygon found -> not a valid viewspace sample |
---|
255 | |
---|
256 | ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint); |
---|
257 | |
---|
258 | if (viewcell) { |
---|
259 | SetupCamera(); |
---|
260 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
261 | |
---|
262 | // Render PVS |
---|
263 | std::map<Intersectable *, |
---|
264 | PvsData<Intersectable *>, |
---|
265 | LtSample<Intersectable *> >::const_iterator it = viewcell->GetPvs().mEntries.begin(); |
---|
266 | |
---|
267 | for (; it != viewcell->GetPvs().mEntries.end(); ++ it) { |
---|
268 | Intersectable *object = (*it).first; |
---|
269 | RenderIntersectable(object); |
---|
270 | } |
---|
271 | |
---|
272 | glBeginOcclusionQueryNV(query); |
---|
273 | |
---|
274 | SetupCamera(); |
---|
275 | |
---|
276 | RenderScene(); |
---|
277 | |
---|
278 | glEndOcclusionQueryNV(); |
---|
279 | |
---|
280 | |
---|
281 | unsigned int pixelCount; |
---|
282 | // reenable other state |
---|
283 | glGetOcclusionQueryuivNV(query, |
---|
284 | GL_PIXEL_COUNT_NV, |
---|
285 | &pixelCount); |
---|
286 | |
---|
287 | pErrorPixels = (100.f*pixelCount)/(GetWidth()*GetHeight()); |
---|
288 | } |
---|
289 | |
---|
290 | return pErrorPixels; |
---|
291 | } |
---|
292 | |
---|
293 | |
---|
294 | float |
---|
295 | GlRendererWidget::RenderErrors() |
---|
296 | { |
---|
297 | float pErrorPixels = -1.0f; |
---|
298 | |
---|
299 | glReadBuffer(GL_BACK); |
---|
300 | |
---|
301 | mUseFalseColors = true; |
---|
302 | |
---|
303 | SetupCamera(); |
---|
304 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
305 | glEnable( GL_CULL_FACE ); |
---|
306 | |
---|
307 | ObjectContainer::const_iterator oi = mObjects.begin(); |
---|
308 | for (; oi != mObjects.end(); oi++) |
---|
309 | RenderIntersectable(*oi); |
---|
310 | |
---|
311 | ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint); |
---|
312 | |
---|
313 | QImage im1, im2; |
---|
314 | QImage diff; |
---|
315 | |
---|
316 | if (viewcell) { |
---|
317 | // read back the texture |
---|
318 | im1 = grabFrameBuffer(true); |
---|
319 | |
---|
320 | SetupCamera(); |
---|
321 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
322 | |
---|
323 | std::map<Intersectable *, |
---|
324 | PvsData<Intersectable *>, |
---|
325 | LtSample<Intersectable *> >::const_iterator it = viewcell->GetPvs().mEntries.begin(); |
---|
326 | |
---|
327 | for (; it != viewcell->GetPvs().mEntries.end(); ++ it) { |
---|
328 | Intersectable *object = (*it).first; |
---|
329 | RenderIntersectable(object); |
---|
330 | } |
---|
331 | |
---|
332 | // read back the texture |
---|
333 | im2 = grabFrameBuffer(true); |
---|
334 | |
---|
335 | diff = im1; |
---|
336 | int x, y; |
---|
337 | int errorPixels = 0; |
---|
338 | |
---|
339 | for (y = 0; y < im1.height(); y++) |
---|
340 | for (x = 0; x < im1.width(); x++) |
---|
341 | if (im1.pixel(x, y) == im2.pixel(x, y)) |
---|
342 | diff.setPixel(x, y, qRgba(0,0,0,0)); |
---|
343 | else { |
---|
344 | diff.setPixel(x, y, qRgba(255,128,128,255)); |
---|
345 | errorPixels++; |
---|
346 | } |
---|
347 | pErrorPixels = (100.f*errorPixels)/(im1.height()*im1.width()); |
---|
348 | } |
---|
349 | |
---|
350 | // now render the pvs again |
---|
351 | SetupCamera(); |
---|
352 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
353 | mUseFalseColors = false; |
---|
354 | |
---|
355 | oi = mObjects.begin(); |
---|
356 | for (; oi != mObjects.end(); oi++) |
---|
357 | RenderIntersectable(*oi); |
---|
358 | |
---|
359 | // now render im1 |
---|
360 | if (viewcell) { |
---|
361 | if (mTopView) { |
---|
362 | mWireFrame = true; |
---|
363 | RenderMeshInstance(viewcell); |
---|
364 | mWireFrame = false; |
---|
365 | } |
---|
366 | |
---|
367 | // init ortographic projection |
---|
368 | glMatrixMode(GL_PROJECTION); |
---|
369 | glPushMatrix(); |
---|
370 | |
---|
371 | glLoadIdentity(); |
---|
372 | gluOrtho2D(0, 1.0f, 0, 1.0f); |
---|
373 | |
---|
374 | glMatrixMode(GL_MODELVIEW); |
---|
375 | glLoadIdentity(); |
---|
376 | |
---|
377 | bindTexture(diff); |
---|
378 | |
---|
379 | glPushAttrib(GL_ENABLE_BIT); |
---|
380 | glEnable( GL_ALPHA_TEST ); |
---|
381 | glDisable( GL_CULL_FACE ); |
---|
382 | glAlphaFunc( GL_GREATER, 0.5 ); |
---|
383 | |
---|
384 | glEnable( GL_TEXTURE_2D ); |
---|
385 | glBegin(GL_QUADS); |
---|
386 | |
---|
387 | glTexCoord2f(0,0); |
---|
388 | glVertex3f(0,0,0); |
---|
389 | |
---|
390 | glTexCoord2f(1,0); |
---|
391 | glVertex3f( 1, 0, 0); |
---|
392 | |
---|
393 | glTexCoord2f(1,1); |
---|
394 | glVertex3f( 1, 1, 0); |
---|
395 | |
---|
396 | glTexCoord2f(0,1); |
---|
397 | glVertex3f(0, 1, 0); |
---|
398 | glEnd(); |
---|
399 | |
---|
400 | glPopAttrib(); |
---|
401 | |
---|
402 | // restore the projection matrix |
---|
403 | glMatrixMode(GL_PROJECTION); |
---|
404 | glPopMatrix(); |
---|
405 | glMatrixMode(GL_MODELVIEW); |
---|
406 | } |
---|
407 | |
---|
408 | return pErrorPixels; |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | bool |
---|
413 | GlRenderer::RenderScene() |
---|
414 | { |
---|
415 | static int glList = -1; |
---|
416 | if (glList != -1) { |
---|
417 | glCallList(glList); |
---|
418 | } else { |
---|
419 | glList = glGenLists(1); |
---|
420 | glNewList(glList, GL_COMPILE_AND_EXECUTE); |
---|
421 | ObjectContainer::const_iterator oi = mObjects.begin(); |
---|
422 | for (; oi != mObjects.end(); oi++) |
---|
423 | RenderIntersectable(*oi); |
---|
424 | glEndList(); |
---|
425 | } |
---|
426 | return true; |
---|
427 | } |
---|
428 | |
---|
429 | |
---|
430 | void |
---|
431 | GlRendererBuffer::ClearErrorBuffer() |
---|
432 | { |
---|
433 | for (int i=0; i < mPvsStatFrames; i++) { |
---|
434 | mPvsErrorBuffer[i] = 1.0f; |
---|
435 | } |
---|
436 | } |
---|
437 | |
---|
438 | |
---|
439 | void |
---|
440 | GlRendererBuffer::EvalPvsStat() |
---|
441 | { |
---|
442 | mPvsStat.Reset(); |
---|
443 | halton.Reset(); |
---|
444 | |
---|
445 | makeCurrent(); |
---|
446 | SetupProjection(GetWidth(), GetHeight()); |
---|
447 | |
---|
448 | for (int i=0; i < mPvsStatFrames; i++) { |
---|
449 | float err; |
---|
450 | RandomViewPoint(); |
---|
451 | if (mPvsErrorBuffer[i] > 0.0f) { |
---|
452 | mPvsErrorBuffer[i] = GetPixelError(); |
---|
453 | cout<<"("<<i<<","<<mPvsErrorBuffer[i]<<")"; |
---|
454 | // swapBuffers(); |
---|
455 | } |
---|
456 | |
---|
457 | err = mPvsErrorBuffer[i]; |
---|
458 | |
---|
459 | if (err >= 0.0f) { |
---|
460 | if (err > mPvsStat.maxError) |
---|
461 | mPvsStat.maxError = err; |
---|
462 | mPvsStat.sumError += err; |
---|
463 | if (err == 0.0f) |
---|
464 | mPvsStat.errorFreeFrames++; |
---|
465 | mPvsStat.frames++; |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | doneCurrent(); |
---|
470 | |
---|
471 | cout<<endl<<flush; |
---|
472 | mRenderingFinished.wakeAll(); |
---|
473 | } |
---|
474 | |
---|
475 | |
---|
476 | |
---|
477 | |
---|
478 | |
---|
479 | void |
---|
480 | GlRendererWidget::mousePressEvent(QMouseEvent *e) |
---|
481 | { |
---|
482 | int x = e->pos().x(); |
---|
483 | int y = e->pos().y(); |
---|
484 | |
---|
485 | mousePoint.x = x; |
---|
486 | mousePoint.y = y; |
---|
487 | |
---|
488 | } |
---|
489 | |
---|
490 | void |
---|
491 | GlRendererWidget::mouseMoveEvent(QMouseEvent *e) |
---|
492 | { |
---|
493 | float MOVE_SENSITIVITY = Magnitude(mSceneGraph->GetBox().Diagonal())*1e-3; |
---|
494 | float TURN_SENSITIVITY=0.1f; |
---|
495 | float TILT_SENSITIVITY=32.0 ; |
---|
496 | float TURN_ANGLE= M_PI/36.0 ; |
---|
497 | |
---|
498 | int x = e->pos().x(); |
---|
499 | int y = e->pos().y(); |
---|
500 | |
---|
501 | if (e->modifiers() & Qt::ControlModifier) { |
---|
502 | mViewPoint.y += (y-mousePoint.y)*MOVE_SENSITIVITY/2.0; |
---|
503 | mViewPoint.x += (x-mousePoint.x)*MOVE_SENSITIVITY/2.0; |
---|
504 | } else { |
---|
505 | mViewPoint += mViewDirection*((mousePoint.y - y)*MOVE_SENSITIVITY); |
---|
506 | float adiff = TURN_ANGLE*(x - mousePoint.x)*-TURN_SENSITIVITY; |
---|
507 | float angle = atan2(mViewDirection.x, mViewDirection.z); |
---|
508 | mViewDirection.x = sin(angle+adiff); |
---|
509 | mViewDirection.z = cos(angle+adiff); |
---|
510 | } |
---|
511 | |
---|
512 | mousePoint.x = x; |
---|
513 | mousePoint.y = y; |
---|
514 | |
---|
515 | updateGL(); |
---|
516 | } |
---|
517 | |
---|
518 | void |
---|
519 | GlRendererWidget::mouseReleaseEvent(QMouseEvent *) |
---|
520 | { |
---|
521 | |
---|
522 | |
---|
523 | } |
---|
524 | |
---|
525 | void |
---|
526 | GlRendererWidget::resizeGL(int w, int h) |
---|
527 | { |
---|
528 | SetupProjection(w, h); |
---|
529 | updateGL(); |
---|
530 | } |
---|
531 | |
---|
532 | void |
---|
533 | GlRendererWidget::paintGL() |
---|
534 | { |
---|
535 | RenderErrors(); |
---|
536 | mFrame++; |
---|
537 | } |
---|
538 | |
---|
539 | |
---|
540 | void |
---|
541 | GlRendererWidget::SetupCamera() |
---|
542 | { |
---|
543 | if (!mTopView) |
---|
544 | GlRenderer::SetupCamera(); |
---|
545 | else { |
---|
546 | float dist = Magnitude(mSceneGraph->GetBox().Diagonal())*0.05; |
---|
547 | Vector3 pos = mViewPoint - dist*Vector3(mViewDirection.x, |
---|
548 | -1, |
---|
549 | mViewDirection.y); |
---|
550 | |
---|
551 | Vector3 target = mViewPoint + dist*mViewDirection; |
---|
552 | Vector3 up(0,1,0); |
---|
553 | |
---|
554 | glLoadIdentity(); |
---|
555 | gluLookAt(pos.x, pos.y, pos.z, |
---|
556 | target.x, target.y, target.z, |
---|
557 | up.x, up.y, up.z); |
---|
558 | } |
---|
559 | |
---|
560 | } |
---|
561 | |
---|
562 | void |
---|
563 | GlRendererWidget::keyPressEvent ( QKeyEvent * e ) |
---|
564 | { |
---|
565 | switch (e->key()) { |
---|
566 | case Qt::Key_T: |
---|
567 | mTopView = !mTopView; |
---|
568 | updateGL(); |
---|
569 | break; |
---|
570 | default: |
---|
571 | e->ignore(); |
---|
572 | break; |
---|
573 | } |
---|
574 | |
---|
575 | } |
---|
576 | |
---|
577 | |
---|
578 | void GlRendererBuffer::SampleBeamContributions(Intersectable *sourceObject, |
---|
579 | Beam &beam, |
---|
580 | const int desiredSamples, |
---|
581 | BeamSampleStatistics &stat)
|
---|
582 | { |
---|
583 | // TODO: should not be done every time here |
---|
584 | // only back faces are interesting for the depth pass
|
---|
585 | glShadeModel(GL_FLAT); |
---|
586 | glDisable(GL_LIGHTING); |
---|
587 | |
---|
588 | // assumes that the beam is constructed and contains kd-tree nodes |
---|
589 | // and viewcells which it intersects |
---|
590 | |
---|
591 | |
---|
592 | // Get the number of viewpoints to be sampled |
---|
593 | // Now it is a sqrt but in general a wiser decision could be made. |
---|
594 | // The less viewpoints the better for rendering performance, since less passes |
---|
595 | // over the beam is needed. |
---|
596 | // The viewpoints could actually be generated outside of the bounding box which |
---|
597 | // would distribute the 'efective viewpoints' of the object surface and thus |
---|
598 | // with a few viewpoints better sample the vipoint space.... |
---|
599 | |
---|
600 | int viewPointSamples = sqrt((float)desiredSamples); |
---|
601 | |
---|
602 | // the number of direction samples per pass is given by the number of viewpoints |
---|
603 | int directionalSamples = desiredSamples/viewPointSamples; |
---|
604 | |
---|
605 | for (int i = 0; i < viewPointSamples; ++ i) |
---|
606 | { |
---|
607 | Vector3 viewPoint = beam.mBox.GetRandomPoint(); |
---|
608 | |
---|
609 | // perhaps the viewpoint should be shifted back a little bit so that it always lies |
---|
610 | // inside the source object |
---|
611 | // 'ideally' the viewpoints would be distributed on the soureObject surface, but this |
---|
612 | // would require more complicated sampling (perhaps hierarchical rejection sampling of |
---|
613 | // the object surface is an option here - only the mesh faces which are inside the box |
---|
614 | // are considered as candidates) |
---|
615 | |
---|
616 | SampleViewpointContributions(sourceObject, |
---|
617 | viewPoint, |
---|
618 | beam, |
---|
619 | directionalSamples, |
---|
620 | stat); |
---|
621 | } |
---|
622 | |
---|
623 | |
---|
624 | // note: |
---|
625 | // this routine would be called only if the number of desired samples is sufficiently |
---|
626 | // large - for other rss tree cells the cpu based sampling is perhaps more efficient |
---|
627 | // distributing the work between cpu and gpu would also allow us to place more sophisticated |
---|
628 | // sample distributions (silhouette ones) using the cpu and the jittered once on the GPU |
---|
629 | // in order that thios scheme is working well the gpu render buffer should run in a separate |
---|
630 | // thread than the cpu sampler, which would not be such a big problem.... |
---|
631 | } |
---|
632 | |
---|
633 | |
---|
634 | void GlRendererBuffer::SampleViewpointContributions(Intersectable *sourceObject, |
---|
635 | const Vector3 viewPoint, |
---|
636 | Beam &beam, |
---|
637 | const int desiredSamples, |
---|
638 | BeamSampleStatistics &stat)
|
---|
639 | { |
---|
640 | // 1. setup the view port to match the desired samples |
---|
641 | glViewport(0, 0, desiredSamples, desiredSamples); |
---|
642 | |
---|
643 | // 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox |
---|
644 | SetupProjectionForViewPoint(viewPoint, beam, sourceObject); |
---|
645 | |
---|
646 | // 3. reset z-buffer to 0 and render the source object for the beam |
---|
647 | // with glCullFace(Enabled) and glFrontFace(GL_CW) |
---|
648 | // save result to depth map
|
---|
649 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
---|
650 | glEnable(GL_CULL_FACE); |
---|
651 | glEnable(GL_STENCIL_TEST); |
---|
652 | glCullFace(GL_FRONT); |
---|
653 | glColorMask(0, 0, 0, 0); |
---|
654 | |
---|
655 | // stencil is increased where the source object is located |
---|
656 | glStencilFunc(GL_ALWAYS, 0x1, 0x1); |
---|
657 | glStencilOp(GL_INCR, GL_INCR, GL_INCR); |
---|
658 | |
---|
659 | #if 0
|
---|
660 | static int glSourceObjList = -1; |
---|
661 | if (glSourceObjList != -1) |
---|
662 | { |
---|
663 | glSourceObjList = glGenLists(1); |
---|
664 | glNewList(glSourceObjList, GL_COMPILE); |
---|
665 | |
---|
666 | RenderIntersectable(sourceObject); |
---|
667 |
|
---|
668 | glEndList();
|
---|
669 | } |
---|
670 | glCallList(glSourceObjList); |
---|
671 | |
---|
672 | #else |
---|
673 | RenderIntersectable(sourceObject); |
---|
674 | |
---|
675 | #endif |
---|
676 | |
---|
677 | // 4. set back to normal rendering and clear the ray termination depth buffer |
---|
678 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
679 | glColorMask(1, 1, 1, 1); |
---|
680 | |
---|
681 | // 5. render all objects inside the beam using id based false color |
---|
682 | // (objects can be compiled to a gl list now so that subsequent rendering for |
---|
683 | // this beam is fast - the same hold for step 3) |
---|
684 |
|
---|
685 | // list of objects intersected by the frustum
|
---|
686 | #if 0
|
---|
687 | static int glObjList = -1; |
---|
688 | if (glObjList != -1) |
---|
689 | { |
---|
690 | glObjList = glGenLists(1); |
---|
691 | glNewList(glObjList, GL_COMPILE); |
---|
692 | |
---|
693 | ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); |
---|
694 | for (it = beam.mObjects.begin(); it != it_end; ++ it) |
---|
695 | RenderIntersectable(*it); |
---|
696 |
|
---|
697 | glEndList();
|
---|
698 | } |
---|
699 | glCallList(glObjList); |
---|
700 | #else |
---|
701 | ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); |
---|
702 | for (it = beam.mObjects.begin(); it != it_end; ++ it) |
---|
703 | RenderIntersectable(*it); |
---|
704 | #endif |
---|
705 | |
---|
706 | // bind ray origin depth buffer |
---|
707 | glBindTexture(GL_TEXTURE_2D, depthMap);
|
---|
708 | glEnable(GL_TEXTURE_2D);
|
---|
709 |
|
---|
710 |
|
---|
711 | //Read the depth buffer into the shadow map texture
|
---|
712 | glBindTexture(GL_TEXTURE_2D, depthMap);
|
---|
713 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize);
|
---|
714 |
|
---|
715 | |
---|
716 | // 6. Now we have a two depth buffers defining the ray origin and termination |
---|
717 | // only rays which have non-zero entry in the origin buffer are valid since |
---|
718 | // they realy start on the object surface (this can also be tagged by setting a |
---|
719 | // stencil buffer bit at step 3)
|
---|
720 | glStencilFunc(GL_EQUAL, 0x1, 0x1);
|
---|
721 |
|
---|
722 | // compare with second depth buffer: done in pixel shader
|
---|
723 | cgGLBindProgram(sCgFragmentProgram);
|
---|
724 | cgGLEnableProfile(sCgFragmentProfile); |
---|
725 | |
---|
726 | // 7. Use occlusion queries for all viewcell meshes associated with the beam -> |
---|
727 | // a fragment passes if the corresponding stencil fragment is set and its depth is |
---|
728 | // between origin and termination buffer |
---|
729 | |
---|
730 | // create new queries if necessary |
---|
731 | GenQueries((int)beam.mViewCells.size()); |
---|
732 | |
---|
733 | // now check whether any backfacing polygon would pass the depth test |
---|
734 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
---|
735 | glDepthMask(GL_FALSE); |
---|
736 | glDisable(GL_CULL_FACE); |
---|
737 | |
---|
738 | ViewCellContainer::const_iterator vit, vit_end = beam.mViewCells.end(); |
---|
739 | |
---|
740 | int queryIdx = 0; |
---|
741 | |
---|
742 | for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit) |
---|
743 | { |
---|
744 | glBeginOcclusionQueryNV(sQueries[queryIdx ++]); |
---|
745 | |
---|
746 | RenderIntersectable(*vit); |
---|
747 | |
---|
748 | glEndOcclusionQueryNV(); |
---|
749 | } |
---|
750 | |
---|
751 | // at this point, if possible, go and do some other computation |
---|
752 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
---|
753 | glDepthMask(GL_TRUE); |
---|
754 | glEnable(GL_CULL_FACE); |
---|
755 | |
---|
756 | // 8. The number of visible pixels is the number of sample rays which see the source |
---|
757 | // object from the corresponding viewcell -> rember these values for later update |
---|
758 | // of the viewcell pvs - or update immediatelly? |
---|
759 | |
---|
760 | // In general it is not neccessary to rember to extract all the rays cast. I hope it |
---|
761 | // would be sufficient to gain only the intergral statistics about the new contributions |
---|
762 | // and so the rss tree would actually store no new rays (only the initial ones) |
---|
763 | // the subdivision of the tree would only be driven by the statistics (the glrender could |
---|
764 | // evaluate the contribution entropy for example) |
---|
765 | // However might be an option to extract/store only those the rays which made a contribution |
---|
766 | // (new viewcell has been discovered) or relative contribution greater than a threashold ... |
---|
767 | |
---|
768 | cgGLDisableProfile(sCgFragmentProfile); |
---|
769 | glDisable(GL_TEXTURE_2D); |
---|
770 | } |
---|
771 | |
---|
772 | |
---|
773 | void GlRendererBuffer::GenQueries(const int numQueries) |
---|
774 | { |
---|
775 | if ((int)sQueries.size() < numQueries) |
---|
776 | { |
---|
777 | const int n = numQueries - (int)sQueries.size(); |
---|
778 | int *newQueries = new int[n]; |
---|
779 | |
---|
780 | glGenOcclusionQueriesNV(n, (unsigned int *)&newQueries); |
---|
781 | |
---|
782 | for (int i = 0; i < n; ++ i) |
---|
783 | { |
---|
784 | sQueries.push_back(newQueries[i]); |
---|
785 | } |
---|
786 | |
---|
787 | delete newQueries; |
---|
788 | } |
---|
789 | } |
---|
790 | |
---|
791 | void GlRendererBuffer::SetupProjectionForViewPoint(const Vector3 &viewPoint, |
---|
792 | const Beam &beam, |
---|
793 | Intersectable *sourceObject) |
---|
794 | { |
---|
795 | float left, right, bottom, top, znear, zfar; |
---|
796 | |
---|
797 | beam.ComputeFrustum(left, right, bottom, top, znear, zfar, |
---|
798 | mSceneGraph->GetBox()); |
---|
799 | |
---|
800 | glFrustum(left, right, bottom, top, znear, zfar); |
---|
801 | |
---|
802 | const Vector3 eye = viewPoint + beam.GetMainDirection(); |
---|
803 | const Vector3 up;//AbitraryNormal(eye); |
---|
804 | |
---|
805 | gluLookAt(eye.x, eye.y, eye.z, |
---|
806 | viewPoint.x, viewPoint.y, viewPoint.z, |
---|
807 | up.x, up.y, up.z); |
---|
808 | } |
---|