[517] | 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" |
---|
[532] | 8 | #include "KdTree.h"
|
---|
[563] | 9 | #include "Environment.h" |
---|
[513] | 10 |
|
---|
[517] | 11 | #include <GL/glext.h> |
---|
[512] | 12 | #include <Cg/cg.h>
|
---|
[517] | 13 | #include <Cg/cgGL.h> |
---|
| 14 | |
---|
[512] | 15 | static CGcontext sCgContext = NULL;
|
---|
[517] | 16 | static CGprogram sCgFragmentProgram = NULL; |
---|
[530] | 17 | static CGprofile sCgFragmentProfile; |
---|
[517] | 18 | |
---|
[540] | 19 | GLuint frontDepthMap; |
---|
| 20 | GLuint backDepthMap; |
---|
| 21 | |
---|
[517] | 22 | const int depthMapSize = 512; |
---|
| 23 | static vector<int> sQueries; |
---|
| 24 | |
---|
| 25 | GlRendererWidget *rendererWidget = NULL; |
---|
[538] | 26 | GlDebuggerWidget *debuggerWidget = NULL; |
---|
[517] | 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, |
---|
[532] | 37 | ViewCellsManager *viewCellsManager, |
---|
| 38 | KdTree *tree): |
---|
| 39 | Renderer(sceneGraph, viewCellsManager), |
---|
| 40 | mKdTree(tree) |
---|
[517] | 41 | { |
---|
| 42 | mSceneGraph->CollectObjects(&mObjects); |
---|
| 43 | mViewPoint = mSceneGraph->GetBox().Center(); |
---|
| 44 | mViewDirection = Vector3(0,0,1); |
---|
| 45 | // timerId = startTimer(10); |
---|
| 46 | mFrame = 0; |
---|
| 47 | mWireFrame = false; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | GlRenderer::~GlRenderer() |
---|
| 51 | { |
---|
[556] | 52 | cerr<<"gl renderer destructor..\n"; |
---|
| 53 | if (sCgFragmentProgram) |
---|
[532] | 54 | cgDestroyProgram(sCgFragmentProgram); |
---|
[556] | 55 | if (sCgContext) |
---|
[532] | 56 | cgDestroyContext(sCgContext); |
---|
[556] | 57 | cerr<<"done."<<endl; |
---|
[517] | 58 | } |
---|
| 59 | |
---|
[516] | 60 |
|
---|
[517] | 61 | static void handleCgError()
|
---|
[516] | 62 | {
|
---|
[525] | 63 | Debug << "Cg error: " << cgGetErrorString(cgGetError()) << endl;
|
---|
[517] | 64 | exit(1);
|
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | void |
---|
| 68 | GlRenderer::RenderIntersectable(Intersectable *object) |
---|
| 69 | { |
---|
| 70 | |
---|
| 71 | SetupFalseColor(object->mId); |
---|
| 72 | |
---|
| 73 | switch (object->Type()) { |
---|
| 74 | case Intersectable::MESH_INSTANCE: |
---|
| 75 | RenderMeshInstance((MeshInstance *)object); |
---|
| 76 | break; |
---|
[535] | 77 | case Intersectable::VIEW_CELL: |
---|
| 78 | RenderViewCell(dynamic_cast<ViewCell *>(object)); |
---|
| 79 | break; |
---|
[517] | 80 | default: |
---|
| 81 | cerr<<"Rendering this object not yet implemented\n"; |
---|
| 82 | break; |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | void |
---|
[535] | 88 | GlRenderer::RenderViewCell(ViewCell *vc) |
---|
| 89 | { |
---|
| 90 | if (vc->GetMesh()) |
---|
| 91 | RenderMesh(vc->GetMesh()); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void |
---|
[517] | 95 | GlRenderer::RenderMeshInstance(MeshInstance *mi) |
---|
| 96 | { |
---|
| 97 | RenderMesh(mi->GetMesh()); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void |
---|
| 101 | GlRenderer::SetupFalseColor(const int id) |
---|
| 102 | { |
---|
| 103 | // swap bits of the color |
---|
| 104 | |
---|
| 105 | glColor3ub(id&255, (id>>8)&255, (id>>16)&255); |
---|
| 106 | } |
---|
| 107 | |
---|
[540] | 108 | |
---|
| 109 | int GlRenderer::GetId(int r, int g, int b) const |
---|
| 110 | { |
---|
| 111 | return r + (g << 8) + (b << 16); |
---|
| 112 | } |
---|
| 113 | |
---|
[517] | 114 | void |
---|
| 115 | GlRenderer::SetupMaterial(Material *m) |
---|
| 116 | { |
---|
| 117 | if (m) |
---|
| 118 | glColor3fv(&(m->mDiffuseColor.r)); |
---|
| 119 | else |
---|
| 120 | glColor3f(1.0f, 1.0f, 1.0f); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | void |
---|
| 124 | GlRenderer::RenderMesh(Mesh *mesh) |
---|
| 125 | { |
---|
| 126 | int i = 0; |
---|
| 127 | |
---|
| 128 | if (!mUseFalseColors) |
---|
| 129 | SetupMaterial(mesh->mMaterial); |
---|
| 130 | |
---|
| 131 | for (i=0; i < mesh->mFaces.size(); i++) { |
---|
| 132 | if (mWireFrame) |
---|
| 133 | glBegin(GL_LINE_LOOP); |
---|
| 134 | else |
---|
| 135 | glBegin(GL_POLYGON); |
---|
| 136 | |
---|
| 137 | Face *face = mesh->mFaces[i]; |
---|
| 138 | for (int j = 0; j < face->mVertexIndices.size(); j++) { |
---|
| 139 | glVertex3fv(&mesh->mVertices[face->mVertexIndices[j]].x); |
---|
| 140 | } |
---|
| 141 | glEnd(); |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void |
---|
| 146 | GlRenderer::InitGL() |
---|
| 147 | { |
---|
| 148 | glMatrixMode(GL_PROJECTION); |
---|
| 149 | glLoadIdentity(); |
---|
| 150 | |
---|
| 151 | glMatrixMode(GL_MODELVIEW); |
---|
| 152 | glLoadIdentity(); |
---|
| 153 | |
---|
| 154 | glEnable(GL_CULL_FACE); |
---|
| 155 | glShadeModel(GL_FLAT); |
---|
| 156 | glEnable(GL_DEPTH_TEST); |
---|
| 157 | glEnable(GL_CULL_FACE); |
---|
| 158 | |
---|
| 159 | glGenOcclusionQueriesNV = (PFNGLGENOCCLUSIONQUERIESNVPROC) |
---|
| 160 | wglGetProcAddress("glGenOcclusionQueriesNV"); |
---|
| 161 | glBeginOcclusionQueryNV = (PFNGLBEGINOCCLUSIONQUERYNVPROC) |
---|
| 162 | wglGetProcAddress("glBeginOcclusionQueryNV"); |
---|
| 163 | glEndOcclusionQueryNV = (PFNGLENDOCCLUSIONQUERYNVPROC) |
---|
| 164 | wglGetProcAddress("glEndOcclusionQueryNV"); |
---|
| 165 | glGetOcclusionQueryuivNV = (PFNGLGETOCCLUSIONQUERYUIVNVPROC) |
---|
[540] | 166 | wglGetProcAddress("glGetOcclusionQueryuivNV");
|
---|
[517] | 167 | } |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | void |
---|
| 172 | GlRenderer::SetupProjection(const int w, const int h) |
---|
| 173 | { |
---|
| 174 | glViewport(0, 0, w, h); |
---|
| 175 | glMatrixMode(GL_PROJECTION); |
---|
| 176 | glLoadIdentity(); |
---|
| 177 | gluPerspective(70.0, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal())); |
---|
| 178 | glMatrixMode(GL_MODELVIEW); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | void |
---|
| 182 | GlRenderer::SetupCamera() |
---|
| 183 | { |
---|
| 184 | Vector3 target = mViewPoint + mViewDirection; |
---|
| 185 | Vector3 up(0,1,0); |
---|
| 186 | |
---|
| 187 | glLoadIdentity(); |
---|
| 188 | gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z, |
---|
| 189 | target.x, target.y, target.z, |
---|
| 190 | up.x, up.y, up.z); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | void |
---|
[563] | 194 | GlRendererBuffer::RandomViewPoint() |
---|
[517] | 195 | { |
---|
| 196 | Vector3 pVector = Vector3(halton.GetNumber(1), |
---|
| 197 | halton.GetNumber(2), |
---|
| 198 | halton.GetNumber(3)); |
---|
| 199 | |
---|
| 200 | Vector3 dVector = Vector3(2*M_PI*halton.GetNumber(4), |
---|
| 201 | M_PI*halton.GetNumber(5), |
---|
| 202 | 0.0f); |
---|
| 203 | |
---|
[563] | 204 | // viewcells->GetViewPoint(mViewPoint); |
---|
| 205 | |
---|
| 206 | mViewPoint = mSceneGraph->GetBox().GetPoint(pVector); |
---|
[517] | 207 | |
---|
| 208 | mViewDirection = Normalize(Vector3(sin(dVector.x), |
---|
| 209 | // cos(dVector.y), |
---|
| 210 | 0.0f, |
---|
| 211 | cos(dVector.x))); |
---|
| 212 | |
---|
| 213 | halton.GenerateNext(); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | |
---|
[563] | 217 | GlRendererBuffer::GlRendererBuffer(const int w,
|
---|
| 218 | const int h,
|
---|
| 219 | SceneGraph *sceneGraph,
|
---|
| 220 | ViewCellsManager *viewcells,
|
---|
| 221 | KdTree *tree):
|
---|
| 222 | QGLPixelBuffer(QSize(w, h)), GlRenderer(sceneGraph, viewcells, tree) {
|
---|
| 223 |
|
---|
| 224 | environment->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames); |
---|
| 225 | mPvsErrorBuffer.resize(mPvsStatFrames);
|
---|
| 226 | ClearErrorBuffer();
|
---|
| 227 |
|
---|
| 228 | InitGL();
|
---|
| 229 |
|
---|
| 230 | }
|
---|
| 231 | |
---|
[517] | 232 | float |
---|
| 233 | GlRenderer::GetPixelError() |
---|
| 234 | { |
---|
| 235 | float pErrorPixels = -1.0f; |
---|
| 236 | |
---|
| 237 | glReadBuffer(GL_BACK); |
---|
| 238 | |
---|
| 239 | mUseFalseColors = true; |
---|
| 240 | |
---|
| 241 | SetupCamera(); |
---|
| 242 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 243 | glEnable( GL_CULL_FACE ); |
---|
| 244 | |
---|
| 245 | RenderScene(); |
---|
| 246 | |
---|
| 247 | // now check whether any backfacing polygon would pass the depth test |
---|
| 248 | static int query = -1; |
---|
| 249 | if (query == -1) |
---|
| 250 | glGenOcclusionQueriesNV(1, (unsigned int *)&query); |
---|
| 251 | |
---|
| 252 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
---|
| 253 | glDepthMask(GL_FALSE); |
---|
| 254 | glDisable( GL_CULL_FACE ); |
---|
| 255 | |
---|
| 256 | glBeginOcclusionQueryNV(query); |
---|
| 257 | |
---|
| 258 | RenderScene(); |
---|
| 259 | |
---|
| 260 | glEndOcclusionQueryNV(); |
---|
| 261 | |
---|
| 262 | // at this point, if possible, go and do some other computation |
---|
| 263 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
---|
| 264 | glDepthMask(GL_TRUE); |
---|
| 265 | glEnable( GL_CULL_FACE ); |
---|
| 266 | |
---|
| 267 | unsigned int pixelCount; |
---|
| 268 | // reenable other state |
---|
| 269 | glGetOcclusionQueryuivNV(query, |
---|
| 270 | GL_PIXEL_COUNT_NV, |
---|
| 271 | &pixelCount); |
---|
| 272 | |
---|
| 273 | if (pixelCount > 0) |
---|
| 274 | return -1.0f; // backfacing polygon found -> not a valid viewspace sample |
---|
| 275 | |
---|
| 276 | ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint); |
---|
| 277 | |
---|
| 278 | if (viewcell) { |
---|
| 279 | SetupCamera(); |
---|
| 280 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 281 | |
---|
| 282 | // Render PVS |
---|
| 283 | std::map<Intersectable *, |
---|
| 284 | PvsData<Intersectable *>, |
---|
| 285 | LtSample<Intersectable *> >::const_iterator it = viewcell->GetPvs().mEntries.begin(); |
---|
| 286 | |
---|
| 287 | for (; it != viewcell->GetPvs().mEntries.end(); ++ it) { |
---|
| 288 | Intersectable *object = (*it).first; |
---|
| 289 | RenderIntersectable(object); |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | glBeginOcclusionQueryNV(query); |
---|
| 293 | |
---|
| 294 | SetupCamera(); |
---|
| 295 | |
---|
| 296 | RenderScene(); |
---|
| 297 | |
---|
| 298 | glEndOcclusionQueryNV(); |
---|
| 299 | |
---|
| 300 | |
---|
| 301 | unsigned int pixelCount; |
---|
| 302 | // reenable other state |
---|
| 303 | glGetOcclusionQueryuivNV(query, |
---|
| 304 | GL_PIXEL_COUNT_NV, |
---|
| 305 | &pixelCount); |
---|
| 306 | |
---|
[556] | 307 | pErrorPixels = ((float)pixelCount)/(GetWidth()*GetHeight()); |
---|
[517] | 308 | } |
---|
| 309 | |
---|
| 310 | return pErrorPixels; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | |
---|
| 314 | float |
---|
| 315 | GlRendererWidget::RenderErrors() |
---|
| 316 | { |
---|
| 317 | float pErrorPixels = -1.0f; |
---|
| 318 | |
---|
| 319 | glReadBuffer(GL_BACK); |
---|
| 320 | |
---|
| 321 | mUseFalseColors = true; |
---|
| 322 | |
---|
| 323 | SetupCamera(); |
---|
| 324 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 325 | glEnable( GL_CULL_FACE ); |
---|
| 326 | |
---|
| 327 | ObjectContainer::const_iterator oi = mObjects.begin(); |
---|
| 328 | for (; oi != mObjects.end(); oi++) |
---|
| 329 | RenderIntersectable(*oi); |
---|
| 330 | |
---|
| 331 | ViewCell *viewcell = mViewCellsManager->GetViewCell(mViewPoint); |
---|
| 332 | |
---|
| 333 | QImage im1, im2; |
---|
| 334 | QImage diff; |
---|
| 335 | |
---|
| 336 | if (viewcell) { |
---|
| 337 | // read back the texture |
---|
| 338 | im1 = grabFrameBuffer(true); |
---|
| 339 | |
---|
| 340 | SetupCamera(); |
---|
| 341 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 342 | |
---|
| 343 | std::map<Intersectable *, |
---|
| 344 | PvsData<Intersectable *>, |
---|
| 345 | LtSample<Intersectable *> >::const_iterator it = viewcell->GetPvs().mEntries.begin(); |
---|
| 346 | |
---|
| 347 | for (; it != viewcell->GetPvs().mEntries.end(); ++ it) { |
---|
| 348 | Intersectable *object = (*it).first; |
---|
| 349 | RenderIntersectable(object); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | // read back the texture |
---|
| 353 | im2 = grabFrameBuffer(true); |
---|
| 354 | |
---|
| 355 | diff = im1; |
---|
| 356 | int x, y; |
---|
| 357 | int errorPixels = 0; |
---|
| 358 | |
---|
| 359 | for (y = 0; y < im1.height(); y++) |
---|
| 360 | for (x = 0; x < im1.width(); x++) |
---|
| 361 | if (im1.pixel(x, y) == im2.pixel(x, y)) |
---|
| 362 | diff.setPixel(x, y, qRgba(0,0,0,0)); |
---|
| 363 | else { |
---|
| 364 | diff.setPixel(x, y, qRgba(255,128,128,255)); |
---|
| 365 | errorPixels++; |
---|
| 366 | } |
---|
[556] | 367 | pErrorPixels = ((float)errorPixels)/(im1.height()*im1.width()); |
---|
[517] | 368 | } |
---|
| 369 | |
---|
| 370 | // now render the pvs again |
---|
| 371 | SetupCamera(); |
---|
| 372 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 373 | mUseFalseColors = false; |
---|
| 374 | |
---|
| 375 | oi = mObjects.begin(); |
---|
| 376 | for (; oi != mObjects.end(); oi++) |
---|
| 377 | RenderIntersectable(*oi); |
---|
| 378 | |
---|
| 379 | // now render im1 |
---|
| 380 | if (viewcell) { |
---|
| 381 | if (mTopView) { |
---|
| 382 | mWireFrame = true; |
---|
| 383 | RenderMeshInstance(viewcell); |
---|
| 384 | mWireFrame = false; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | // init ortographic projection |
---|
| 388 | glMatrixMode(GL_PROJECTION); |
---|
| 389 | glPushMatrix(); |
---|
| 390 | |
---|
| 391 | glLoadIdentity(); |
---|
| 392 | gluOrtho2D(0, 1.0f, 0, 1.0f); |
---|
| 393 | |
---|
| 394 | glMatrixMode(GL_MODELVIEW); |
---|
| 395 | glLoadIdentity(); |
---|
| 396 | |
---|
| 397 | bindTexture(diff); |
---|
| 398 | |
---|
| 399 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 400 | glEnable( GL_ALPHA_TEST ); |
---|
| 401 | glDisable( GL_CULL_FACE ); |
---|
| 402 | glAlphaFunc( GL_GREATER, 0.5 ); |
---|
| 403 | |
---|
| 404 | glEnable( GL_TEXTURE_2D ); |
---|
| 405 | glBegin(GL_QUADS); |
---|
| 406 | |
---|
| 407 | glTexCoord2f(0,0); |
---|
| 408 | glVertex3f(0,0,0); |
---|
| 409 | |
---|
| 410 | glTexCoord2f(1,0); |
---|
| 411 | glVertex3f( 1, 0, 0); |
---|
| 412 | |
---|
| 413 | glTexCoord2f(1,1); |
---|
| 414 | glVertex3f( 1, 1, 0); |
---|
| 415 | |
---|
| 416 | glTexCoord2f(0,1); |
---|
| 417 | glVertex3f(0, 1, 0); |
---|
| 418 | glEnd(); |
---|
| 419 | |
---|
| 420 | glPopAttrib(); |
---|
| 421 | |
---|
| 422 | // restore the projection matrix |
---|
| 423 | glMatrixMode(GL_PROJECTION); |
---|
| 424 | glPopMatrix(); |
---|
| 425 | glMatrixMode(GL_MODELVIEW); |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | return pErrorPixels; |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | |
---|
| 432 | bool |
---|
| 433 | GlRenderer::RenderScene() |
---|
| 434 | { |
---|
| 435 | static int glList = -1; |
---|
[538] | 436 | if (glList == -1) |
---|
| 437 | { |
---|
[517] | 438 | glList = glGenLists(1); |
---|
[538] | 439 | glNewList(glList, GL_COMPILE); |
---|
[517] | 440 | ObjectContainer::const_iterator oi = mObjects.begin(); |
---|
| 441 | for (; oi != mObjects.end(); oi++) |
---|
| 442 | RenderIntersectable(*oi); |
---|
| 443 | glEndList(); |
---|
| 444 | } |
---|
[538] | 445 | |
---|
| 446 | glCallList(glList); |
---|
| 447 | |
---|
[517] | 448 | return true; |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | |
---|
| 452 | void |
---|
| 453 | GlRendererBuffer::ClearErrorBuffer() |
---|
| 454 | { |
---|
| 455 | for (int i=0; i < mPvsStatFrames; i++) { |
---|
| 456 | mPvsErrorBuffer[i] = 1.0f; |
---|
| 457 | } |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | |
---|
| 461 | void |
---|
| 462 | GlRendererBuffer::EvalPvsStat() |
---|
| 463 | { |
---|
| 464 | mPvsStat.Reset(); |
---|
| 465 | halton.Reset(); |
---|
| 466 | |
---|
| 467 | makeCurrent(); |
---|
| 468 | SetupProjection(GetWidth(), GetHeight()); |
---|
| 469 | |
---|
| 470 | for (int i=0; i < mPvsStatFrames; i++) { |
---|
| 471 | float err; |
---|
| 472 | RandomViewPoint(); |
---|
| 473 | if (mPvsErrorBuffer[i] > 0.0f) { |
---|
| 474 | mPvsErrorBuffer[i] = GetPixelError(); |
---|
| 475 | cout<<"("<<i<<","<<mPvsErrorBuffer[i]<<")"; |
---|
| 476 | // swapBuffers(); |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | err = mPvsErrorBuffer[i]; |
---|
| 480 | |
---|
| 481 | if (err >= 0.0f) { |
---|
| 482 | if (err > mPvsStat.maxError) |
---|
| 483 | mPvsStat.maxError = err; |
---|
| 484 | mPvsStat.sumError += err; |
---|
| 485 | if (err == 0.0f) |
---|
| 486 | mPvsStat.errorFreeFrames++; |
---|
| 487 | mPvsStat.frames++; |
---|
| 488 | } |
---|
| 489 | } |
---|
[556] | 490 | |
---|
| 491 | glFinish(); |
---|
[517] | 492 | doneCurrent(); |
---|
| 493 | |
---|
| 494 | cout<<endl<<flush; |
---|
[556] | 495 | // mRenderingFinished.wakeAll(); |
---|
[517] | 496 | } |
---|
| 497 | |
---|
| 498 | |
---|
| 499 | |
---|
| 500 | |
---|
| 501 | |
---|
| 502 | void |
---|
| 503 | GlRendererWidget::mousePressEvent(QMouseEvent *e) |
---|
| 504 | { |
---|
| 505 | int x = e->pos().x(); |
---|
| 506 | int y = e->pos().y(); |
---|
| 507 | |
---|
| 508 | mousePoint.x = x; |
---|
| 509 | mousePoint.y = y; |
---|
| 510 | |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | void |
---|
| 514 | GlRendererWidget::mouseMoveEvent(QMouseEvent *e) |
---|
| 515 | { |
---|
| 516 | float MOVE_SENSITIVITY = Magnitude(mSceneGraph->GetBox().Diagonal())*1e-3; |
---|
| 517 | float TURN_SENSITIVITY=0.1f; |
---|
| 518 | float TILT_SENSITIVITY=32.0 ; |
---|
| 519 | float TURN_ANGLE= M_PI/36.0 ; |
---|
| 520 | |
---|
| 521 | int x = e->pos().x(); |
---|
| 522 | int y = e->pos().y(); |
---|
| 523 | |
---|
| 524 | if (e->modifiers() & Qt::ControlModifier) { |
---|
| 525 | mViewPoint.y += (y-mousePoint.y)*MOVE_SENSITIVITY/2.0; |
---|
| 526 | mViewPoint.x += (x-mousePoint.x)*MOVE_SENSITIVITY/2.0; |
---|
| 527 | } else { |
---|
| 528 | mViewPoint += mViewDirection*((mousePoint.y - y)*MOVE_SENSITIVITY); |
---|
| 529 | float adiff = TURN_ANGLE*(x - mousePoint.x)*-TURN_SENSITIVITY; |
---|
| 530 | float angle = atan2(mViewDirection.x, mViewDirection.z); |
---|
| 531 | mViewDirection.x = sin(angle+adiff); |
---|
| 532 | mViewDirection.z = cos(angle+adiff); |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | mousePoint.x = x; |
---|
| 536 | mousePoint.y = y; |
---|
| 537 | |
---|
| 538 | updateGL(); |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | void |
---|
| 542 | GlRendererWidget::mouseReleaseEvent(QMouseEvent *) |
---|
| 543 | { |
---|
| 544 | |
---|
| 545 | |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | void |
---|
| 549 | GlRendererWidget::resizeGL(int w, int h) |
---|
| 550 | { |
---|
| 551 | SetupProjection(w, h); |
---|
| 552 | updateGL(); |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | void |
---|
| 556 | GlRendererWidget::paintGL() |
---|
| 557 | { |
---|
[538] | 558 | RenderErrors(); |
---|
| 559 | |
---|
| 560 | mFrame++; |
---|
[517] | 561 | } |
---|
| 562 | |
---|
| 563 | |
---|
| 564 | void |
---|
| 565 | GlRendererWidget::SetupCamera() |
---|
| 566 | { |
---|
| 567 | if (!mTopView) |
---|
| 568 | GlRenderer::SetupCamera(); |
---|
| 569 | else { |
---|
| 570 | float dist = Magnitude(mSceneGraph->GetBox().Diagonal())*0.05; |
---|
| 571 | Vector3 pos = mViewPoint - dist*Vector3(mViewDirection.x, |
---|
| 572 | -1, |
---|
| 573 | mViewDirection.y); |
---|
| 574 | |
---|
| 575 | Vector3 target = mViewPoint + dist*mViewDirection; |
---|
| 576 | Vector3 up(0,1,0); |
---|
| 577 | |
---|
| 578 | glLoadIdentity(); |
---|
| 579 | gluLookAt(pos.x, pos.y, pos.z, |
---|
| 580 | target.x, target.y, target.z, |
---|
| 581 | up.x, up.y, up.z); |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | void |
---|
| 587 | GlRendererWidget::keyPressEvent ( QKeyEvent * e ) |
---|
| 588 | { |
---|
| 589 | switch (e->key()) { |
---|
| 590 | case Qt::Key_T: |
---|
| 591 | mTopView = !mTopView; |
---|
| 592 | updateGL(); |
---|
| 593 | break; |
---|
| 594 | default: |
---|
| 595 | e->ignore(); |
---|
| 596 | break; |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | |
---|
| 602 | void GlRendererBuffer::SampleBeamContributions(Intersectable *sourceObject, |
---|
| 603 | Beam &beam, |
---|
| 604 | const int desiredSamples, |
---|
| 605 | BeamSampleStatistics &stat)
|
---|
[540] | 606 | {
|
---|
[544] | 607 | // TODO: should be moved out of here (not to be done every time) |
---|
[513] | 608 | // only back faces are interesting for the depth pass
|
---|
[517] | 609 | glShadeModel(GL_FLAT); |
---|
| 610 | glDisable(GL_LIGHTING); |
---|
[544] | 611 | |
---|
[530] | 612 | // needed to kill the fragments for the front buffer |
---|
[527] | 613 | glEnable(GL_ALPHA_TEST); |
---|
| 614 | glAlphaFunc(GL_GREATER, 0); |
---|
[517] | 615 | |
---|
| 616 | // assumes that the beam is constructed and contains kd-tree nodes |
---|
| 617 | // and viewcells which it intersects |
---|
| 618 | |
---|
| 619 | |
---|
| 620 | // Get the number of viewpoints to be sampled |
---|
| 621 | // Now it is a sqrt but in general a wiser decision could be made. |
---|
| 622 | // The less viewpoints the better for rendering performance, since less passes |
---|
| 623 | // over the beam is needed. |
---|
| 624 | // The viewpoints could actually be generated outside of the bounding box which |
---|
| 625 | // would distribute the 'efective viewpoints' of the object surface and thus |
---|
[544] | 626 | // with a few viewpoints better sample the viewpoint space.... |
---|
| 627 | |
---|
| 628 | //TODO: comment in |
---|
[540] | 629 | //int viewPointSamples = sqrt((float)desiredSamples); |
---|
| 630 | int viewPointSamples = max(desiredSamples / (GetWidth() * GetHeight()), 1); |
---|
| 631 | |
---|
[517] | 632 | // the number of direction samples per pass is given by the number of viewpoints |
---|
[540] | 633 | int directionalSamples = desiredSamples / viewPointSamples; |
---|
[517] | 634 | |
---|
[540] | 635 | Debug << "directional samples: " << directionalSamples << endl; |
---|
[517] | 636 | for (int i = 0; i < viewPointSamples; ++ i) |
---|
| 637 | { |
---|
| 638 | Vector3 viewPoint = beam.mBox.GetRandomPoint(); |
---|
| 639 | |
---|
| 640 | // perhaps the viewpoint should be shifted back a little bit so that it always lies |
---|
| 641 | // inside the source object |
---|
| 642 | // 'ideally' the viewpoints would be distributed on the soureObject surface, but this |
---|
| 643 | // would require more complicated sampling (perhaps hierarchical rejection sampling of |
---|
| 644 | // the object surface is an option here - only the mesh faces which are inside the box |
---|
| 645 | // are considered as candidates) |
---|
| 646 | |
---|
| 647 | SampleViewpointContributions(sourceObject, |
---|
| 648 | viewPoint, |
---|
| 649 | beam, |
---|
| 650 | directionalSamples, |
---|
| 651 | stat); |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | |
---|
| 655 | // note: |
---|
| 656 | // this routine would be called only if the number of desired samples is sufficiently |
---|
| 657 | // large - for other rss tree cells the cpu based sampling is perhaps more efficient |
---|
| 658 | // distributing the work between cpu and gpu would also allow us to place more sophisticated |
---|
| 659 | // sample distributions (silhouette ones) using the cpu and the jittered once on the GPU |
---|
| 660 | // in order that thios scheme is working well the gpu render buffer should run in a separate |
---|
| 661 | // thread than the cpu sampler, which would not be such a big problem.... |
---|
[544] | 662 | |
---|
| 663 | // disable alpha test again |
---|
| 664 | glDisable(GL_ALPHA_TEST); |
---|
[517] | 665 | } |
---|
| 666 | |
---|
| 667 | |
---|
| 668 | void GlRendererBuffer::SampleViewpointContributions(Intersectable *sourceObject, |
---|
| 669 | const Vector3 viewPoint, |
---|
| 670 | Beam &beam, |
---|
[540] | 671 | const int samples, |
---|
[513] | 672 | BeamSampleStatistics &stat)
|
---|
[517] | 673 | { |
---|
[540] | 674 | // 1. setup the view port to match the desired samples |
---|
[544] | 675 | glViewport(0, 0, samples, samples); |
---|
[525] | 676 | |
---|
[517] | 677 | // 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox |
---|
[525] | 678 | SetupProjectionForViewPoint(viewPoint, beam, sourceObject); |
---|
[540] | 679 | |
---|
[544] | 680 | |
---|
[517] | 681 | // 3. reset z-buffer to 0 and render the source object for the beam |
---|
| 682 | // with glCullFace(Enabled) and glFrontFace(GL_CW) |
---|
[544] | 683 | // save result to the front depth map
|
---|
| 684 | // the front depth map holds ray origins
|
---|
[540] | 685 | |
---|
[544] | 686 | |
---|
| 687 | // front depth buffer must be initialised to 0 |
---|
[540] | 688 | float clearDepth; |
---|
[544] | 689 | |
---|
[540] | 690 | glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepth); |
---|
| 691 | glClearDepth(0.0f); |
---|
[544] | 692 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
---|
[540] | 693 | |
---|
[544] | 694 | |
---|
[540] | 695 | //glFrontFace(GL_CW); |
---|
[517] | 696 | glEnable(GL_CULL_FACE); |
---|
| 697 | glCullFace(GL_FRONT); |
---|
| 698 | glColorMask(0, 0, 0, 0); |
---|
| 699 | |
---|
[540] | 700 | |
---|
[517] | 701 | // stencil is increased where the source object is located |
---|
[540] | 702 | glEnable(GL_STENCIL_TEST); |
---|
[517] | 703 | glStencilFunc(GL_ALWAYS, 0x1, 0x1); |
---|
[540] | 704 | glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); |
---|
[517] | 705 | |
---|
[544] | 706 | |
---|
[513] | 707 | #if 0
|
---|
[517] | 708 | static int glSourceObjList = -1; |
---|
| 709 | if (glSourceObjList != -1) |
---|
| 710 | { |
---|
| 711 | glSourceObjList = glGenLists(1); |
---|
| 712 | glNewList(glSourceObjList, GL_COMPILE); |
---|
| 713 | |
---|
| 714 | RenderIntersectable(sourceObject); |
---|
[513] | 715 |
|
---|
| 716 | glEndList();
|
---|
[517] | 717 | } |
---|
| 718 | glCallList(glSourceObjList); |
---|
| 719 | |
---|
| 720 | #else |
---|
| 721 | RenderIntersectable(sourceObject); |
---|
| 722 | |
---|
| 723 | #endif |
---|
| 724 | |
---|
[544] | 725 | // copy contents of the front depth buffer into depth texture |
---|
[540] | 726 | glBindTexture(GL_TEXTURE_2D, frontDepthMap);
|
---|
[544] | 727 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize);
|
---|
[540] | 728 | |
---|
| 729 | |
---|
[544] | 730 | // reset clear function |
---|
| 731 | glClearDepth(clearDepth); |
---|
[540] | 732 | |
---|
[544] | 733 | |
---|
| 734 | |
---|
| 735 | // 4. set up the termination depth buffer (= standard depth buffer) |
---|
[541] | 736 | // only rays which have non-zero entry in the origin buffer are valid since |
---|
[544] | 737 | // they realy start on the object surface (this is tagged by setting a |
---|
| 738 | // stencil buffer bit at step 3). |
---|
| 739 | |
---|
[541] | 740 | glStencilFunc(GL_EQUAL, 0x1, 0x1);
|
---|
| 741 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
---|
| 742 | |
---|
[517] | 743 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
[540] | 744 | glDepthMask(1); |
---|
[544] | 745 | |
---|
[540] | 746 | glEnable(GL_DEPTH_TEST); |
---|
[544] | 747 | |
---|
[540] | 748 | glEnable(GL_CULL_FACE); |
---|
| 749 | glCullFace(GL_BACK); |
---|
| 750 | |
---|
[544] | 751 | // setup front depth buffer |
---|
| 752 | glEnable(GL_TEXTURE_2D);
|
---|
| 753 |
|
---|
| 754 | // bind pixel shader implementing the front depth buffer functionality
|
---|
| 755 | cgGLBindProgram(sCgFragmentProgram);
|
---|
| 756 | cgGLEnableProfile(sCgFragmentProfile); |
---|
| 757 | |
---|
| 758 | |
---|
| 759 | // 5. render all objects inside the beam |
---|
| 760 | // we can use id based false color to read them back for gaining the pvs |
---|
| 761 | |
---|
| 762 | glColorMask(1, 1, 1, 1); |
---|
| 763 | |
---|
| 764 |
|
---|
| 765 | // if objects not stored in beam => extract objects
|
---|
[532] | 766 | if (beam.mFlags & !Beam::STORE_OBJECTS)
|
---|
| 767 | {
|
---|
| 768 | vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
| 769 |
|
---|
| 770 | Intersectable::NewMail();
|
---|
| 771 | for (it = beam.mKdNodes.begin(); it != it_end; ++ it)
|
---|
| 772 | {
|
---|
| 773 | mKdTree->CollectObjects(*it, beam.mObjects);
|
---|
| 774 | }
|
---|
| 775 | }
|
---|
| 776 |
|
---|
[544] | 777 |
|
---|
| 778 | // (objects can be compiled to a gl list now so that subsequent rendering for |
---|
| 779 | // this beam is fast - the same hold for step 3) |
---|
| 780 | // Afterwards we have two depth buffers defining the ray origin and termination
|
---|
| 781 |
|
---|
| 782 |
|
---|
[513] | 783 | #if 0
|
---|
[517] | 784 | static int glObjList = -1; |
---|
| 785 | if (glObjList != -1) |
---|
| 786 | { |
---|
| 787 | glObjList = glGenLists(1); |
---|
| 788 | glNewList(glObjList, GL_COMPILE); |
---|
| 789 | |
---|
| 790 | ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); |
---|
| 791 | for (it = beam.mObjects.begin(); it != it_end; ++ it) |
---|
[540] | 792 | { |
---|
[544] | 793 | // render all objects except the source object |
---|
[540] | 794 | if (*it != sourceObject) |
---|
| 795 | RenderIntersectable(*it); |
---|
| 796 | } |
---|
[513] | 797 |
|
---|
| 798 | glEndList();
|
---|
[517] | 799 | } |
---|
[544] | 800 | |
---|
[517] | 801 | glCallList(glObjList); |
---|
| 802 | #else |
---|
| 803 | ObjectContainer::const_iterator it, it_end = beam.mObjects.end(); |
---|
[540] | 804 | for (it = beam.mObjects.begin(); it != it_end; ++ it) |
---|
| 805 | { |
---|
[544] | 806 | // render all objects except the source object |
---|
[540] | 807 | if (*it != sourceObject) |
---|
[517] | 808 | RenderIntersectable(*it); |
---|
[540] | 809 | } |
---|
[517] | 810 | #endif |
---|
[513] | 811 |
|
---|
[544] | 812 | |
---|
[517] | 813 | |
---|
[544] | 814 | // 6. Use occlusion queries for all viewcell meshes associated with the beam -> |
---|
| 815 | // a fragment passes if the corresponding stencil fragment is set and its depth is |
---|
| 816 | // between origin and termination buffer |
---|
[517] | 817 | |
---|
| 818 | // create new queries if necessary |
---|
| 819 | GenQueries((int)beam.mViewCells.size()); |
---|
| 820 | |
---|
[544] | 821 | // check whether any backfacing polygon would pass the depth test? |
---|
| 822 | // matt: should check both back /front facing because of dual depth buffer |
---|
| 823 | // and danger of cutting the near plane with front facing polys. |
---|
| 824 | |
---|
[517] | 825 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
---|
| 826 | glDepthMask(GL_FALSE); |
---|
| 827 | glDisable(GL_CULL_FACE); |
---|
[544] | 828 | |
---|
[517] | 829 | |
---|
| 830 | ViewCellContainer::const_iterator vit, vit_end = beam.mViewCells.end(); |
---|
| 831 | |
---|
| 832 | int queryIdx = 0; |
---|
| 833 | |
---|
| 834 | for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit) |
---|
| 835 | { |
---|
| 836 | glBeginOcclusionQueryNV(sQueries[queryIdx ++]); |
---|
[525] | 837 | |
---|
[517] | 838 | RenderIntersectable(*vit); |
---|
[525] | 839 | |
---|
[517] | 840 | glEndOcclusionQueryNV(); |
---|
| 841 | } |
---|
| 842 | |
---|
[544] | 843 | |
---|
| 844 | |
---|
[517] | 845 | // at this point, if possible, go and do some other computation |
---|
| 846 | |
---|
[538] | 847 | |
---|
[544] | 848 | |
---|
| 849 | // 7. The number of visible pixels is the number of sample rays which see the source |
---|
[538] | 850 | // object from the corresponding viewcell -> remember these values for later update |
---|
| 851 | // of the viewcell pvs - or update immediately? |
---|
[517] | 852 | |
---|
[544] | 853 | queryIdx = 0; |
---|
| 854 | unsigned int pixelCount; |
---|
| 855 | |
---|
[538] | 856 | for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit) |
---|
| 857 | { |
---|
| 858 | // fetch queries |
---|
| 859 | glGetOcclusionQueryuivNV(sQueries[queryIdx ++], |
---|
| 860 | GL_PIXEL_COUNT_NV, |
---|
| 861 | &pixelCount); |
---|
[540] | 862 | if (pixelCount) |
---|
| 863 | Debug << "view cell " << (*vit)->GetId() << " visible pixels: " << pixelCount << endl; |
---|
[538] | 864 | } |
---|
| 865 | |
---|
| 866 | |
---|
[544] | 867 | // 8. Copmpute rendering statistics |
---|
| 868 | // In general it is not neccessary to remember to extract all the rays cast. I hope it |
---|
[517] | 869 | // would be sufficient to gain only the intergral statistics about the new contributions |
---|
| 870 | // and so the rss tree would actually store no new rays (only the initial ones) |
---|
| 871 | // the subdivision of the tree would only be driven by the statistics (the glrender could |
---|
| 872 | // evaluate the contribution entropy for example) |
---|
| 873 | // However might be an option to extract/store only those the rays which made a contribution |
---|
[540] | 874 | // (new viewcell has been discovered) or relative contribution greater than a threshold ... |
---|
[517] | 875 | |
---|
[540] | 876 | ObjectContainer pvsObj; |
---|
| 877 | stat.pvsSize = ComputePvs(beam.mObjects, pvsObj); |
---|
| 878 | |
---|
[544] | 879 | // to gain ray source and termination |
---|
| 880 | // copy contents of ray termination buffer into depth texture |
---|
| 881 | // and compare with ray source buffer |
---|
[540] | 882 | #if 0 |
---|
[544] | 883 | VssRayContainer rays;
|
---|
| 884 |
|
---|
[540] | 885 | glBindTexture(GL_TEXTURE_2D, backDepthMap);
|
---|
| 886 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize); |
---|
[544] | 887 | |
---|
| 888 | ComputeRays(Intersectable *sourceObj, rays); |
---|
| 889 | |
---|
[540] | 890 | #endif |
---|
[544] | 891 | |
---|
| 892 | |
---|
| 893 | |
---|
| 894 | //-- cleanup |
---|
| 895 | |
---|
| 896 | |
---|
| 897 | // reset gl state |
---|
[538] | 898 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
---|
| 899 | glDepthMask(GL_TRUE); |
---|
| 900 | glEnable(GL_CULL_FACE); |
---|
[540] | 901 | glDisable(GL_STENCIL_TEST); |
---|
[517] | 902 | cgGLDisableProfile(sCgFragmentProfile); |
---|
| 903 | glDisable(GL_TEXTURE_2D); |
---|
[540] | 904 | |
---|
[544] | 905 | // remove objects from beam |
---|
[540] | 906 | if (beam.mFlags & !Beam::STORE_OBJECTS) |
---|
| 907 | beam.mObjects.clear(); |
---|
[517] | 908 | } |
---|
| 909 | |
---|
| 910 | |
---|
| 911 | void GlRendererBuffer::GenQueries(const int numQueries) |
---|
| 912 | { |
---|
| 913 | if ((int)sQueries.size() < numQueries) |
---|
| 914 | { |
---|
| 915 | const int n = numQueries - (int)sQueries.size(); |
---|
[540] | 916 | unsigned int *newQueries = new unsigned int[n]; |
---|
[517] | 917 | |
---|
[540] | 918 | glGenOcclusionQueriesNV(n, (unsigned int *)newQueries); |
---|
[517] | 919 | |
---|
| 920 | for (int i = 0; i < n; ++ i) |
---|
| 921 | { |
---|
| 922 | sQueries.push_back(newQueries[i]); |
---|
| 923 | } |
---|
| 924 | |
---|
[540] | 925 | delete [] newQueries; |
---|
[517] | 926 | } |
---|
[525] | 927 | } |
---|
| 928 | |
---|
[538] | 929 | |
---|
[525] | 930 | void GlRendererBuffer::SetupProjectionForViewPoint(const Vector3 &viewPoint, |
---|
[540] | 931 | const Beam &beam, |
---|
| 932 | Intersectable *sourceObject) |
---|
[525] | 933 | { |
---|
| 934 | float left, right, bottom, top, znear, zfar; |
---|
| 935 | |
---|
[540] | 936 | beam.ComputePerspectiveFrustum(left, right, bottom, top, znear, zfar, |
---|
| 937 | mSceneGraph->GetBox()); |
---|
[525] | 938 | |
---|
[540] | 939 | //Debug << left << " " << right << " " << bottom << " " << top << " " << znear << " " << zfar << endl; |
---|
| 940 | glMatrixMode(GL_PROJECTION); |
---|
| 941 | glLoadIdentity(); |
---|
[525] | 942 | glFrustum(left, right, bottom, top, znear, zfar); |
---|
[540] | 943 | //glFrustum(-1, 1, -1, 1, 1, 20000); |
---|
[525] | 944 | |
---|
[561] | 945 | const Vector3 center = viewPoint + beam.GetMainDirection() * (zfar - znear) * 0.3f; |
---|
[540] | 946 | const Vector3 up = |
---|
| 947 | Normalize(CrossProd(beam.mPlanes[0].mNormal, beam.mPlanes[4].mNormal)); |
---|
[525] | 948 | |
---|
[540] | 949 | #ifdef _DEBUG |
---|
| 950 | Debug << "view point: " << viewPoint << endl; |
---|
| 951 | Debug << "eye: " << center << endl; |
---|
| 952 | Debug << "up: " << up << endl; |
---|
| 953 | #endif |
---|
| 954 | |
---|
| 955 | glMatrixMode(GL_MODELVIEW); |
---|
| 956 | glLoadIdentity(); |
---|
| 957 | gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z, |
---|
| 958 | center.x, center.y, center.z, |
---|
[526] | 959 | up.x, up.y, up.z); |
---|
[538] | 960 | } |
---|
| 961 | |
---|
| 962 | |
---|
[540] | 963 | void GlRendererBuffer::InitGL() |
---|
| 964 | {
|
---|
[556] | 965 | makeCurrent(); |
---|
| 966 | GlRenderer::InitGL();
|
---|
[563] | 967 | #if 1 |
---|
[540] | 968 | // initialise dual depth buffer textures
|
---|
| 969 | glGenTextures(1, &frontDepthMap);
|
---|
| 970 | glBindTexture(GL_TEXTURE_2D, frontDepthMap);
|
---|
| 971 |
|
---|
| 972 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,
|
---|
| 973 | depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
|
---|
| 974 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 975 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
| 976 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); |
---|
| 977 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); |
---|
[538] | 978 | |
---|
[540] | 979 | glGenTextures(1, &backDepthMap);
|
---|
| 980 | glBindTexture(GL_TEXTURE_2D, backDepthMap);
|
---|
| 981 |
|
---|
| 982 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,
|
---|
| 983 | depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
|
---|
| 984 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 985 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
| 986 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); |
---|
| 987 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); |
---|
[538] | 988 | |
---|
[540] | 989 | // cg initialization |
---|
| 990 | cgSetErrorCallback(handleCgError);
|
---|
| 991 | sCgContext = cgCreateContext();
|
---|
| 992 |
|
---|
| 993 | if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1))
|
---|
| 994 | sCgFragmentProfile = CG_PROFILE_ARBFP1;
|
---|
| 995 | else
|
---|
| 996 | {
|
---|
| 997 | // try FP30
|
---|
| 998 | if (cgGLIsProfileSupported(CG_PROFILE_FP30))
|
---|
| 999 | sCgFragmentProfile = CG_PROFILE_FP30;
|
---|
| 1000 | else
|
---|
| 1001 | {
|
---|
| 1002 | Debug << "Neither arbfp1 or fp30 fragment profiles supported on this system" << endl;
|
---|
| 1003 | exit(1);
|
---|
| 1004 | }
|
---|
| 1005 | }
|
---|
| 1006 |
|
---|
[556] | 1007 | |
---|
| 1008 | sCgFragmentProgram = cgCreateProgramFromFile(sCgContext, |
---|
[540] | 1009 | CG_SOURCE, "../src/dual_depth.cg", |
---|
| 1010 | sCgFragmentProfile, |
---|
| 1011 | NULL, |
---|
[556] | 1012 | NULL); |
---|
| 1013 | |
---|
| 1014 | if (!cgIsProgramCompiled(sCgFragmentProgram)) |
---|
| 1015 | cgCompileProgram(sCgFragmentProgram); |
---|
| 1016 | |
---|
| 1017 | cgGLLoadProgram(sCgFragmentProgram); |
---|
| 1018 | cgGLBindProgram(sCgFragmentProgram); |
---|
| 1019 | |
---|
| 1020 | Debug << "---- PROGRAM BEGIN ----\n" << |
---|
[540] | 1021 | cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n"; |
---|
[556] | 1022 | |
---|
| 1023 | #endif |
---|
| 1024 | doneCurrent(); |
---|
[540] | 1025 | } |
---|
[538] | 1026 | |
---|
[542] | 1027 | |
---|
[540] | 1028 | void GlRendererBuffer::ComputeRays(Intersectable *sourceObj, VssRayContainer &rays) |
---|
| 1029 | { |
---|
| 1030 | for (int i = 0; i < depthMapSize * depthMapSize; ++ i) |
---|
| 1031 | { |
---|
| 1032 | //todo glGetTexImage() |
---|
| 1033 | } |
---|
| 1034 | } |
---|
| 1035 | |
---|
| 1036 | |
---|
| 1037 |
|
---|
| 1038 | inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
| 1039 | {
|
---|
| 1040 | return obj1->mId < obj2->mId;
|
---|
| 1041 | }
|
---|
| 1042 | |
---|
| 1043 | |
---|
| 1044 | int GlRendererBuffer::ComputePvs(ObjectContainer &objects, |
---|
| 1045 | ObjectContainer &pvs) const |
---|
| 1046 | { |
---|
| 1047 | int pvsSize = 0; |
---|
| 1048 | QImage image = toImage(); |
---|
| 1049 | Intersectable::NewMail(); |
---|
| 1050 | |
---|
| 1051 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
| 1052 |
|
---|
| 1053 | MeshInstance dummy(NULL);
|
---|
| 1054 |
|
---|
| 1055 | Intersectable *obj = NULL;
|
---|
| 1056 | |
---|
| 1057 | for (int x = 0; x < image.width(); ++ x) |
---|
| 1058 | { |
---|
| 1059 | for (int y = 0; y < image.height(); ++ y) |
---|
| 1060 | { |
---|
| 1061 | QRgb pix = image.pixel(x, y); |
---|
| 1062 | const int id = GetId(qRed(pix), qGreen(pix), qBlue(pix)); |
---|
| 1063 | |
---|
| 1064 | dummy.SetId(id);
|
---|
| 1065 |
|
---|
| 1066 | ObjectContainer::iterator oit =
|
---|
| 1067 | lower_bound(objects.begin(), objects.end(), &dummy, ilt);
|
---|
[542] | 1068 | |
---|
| 1069 | |
---|
| 1070 | if (//(oit != oit.end()) && |
---|
| 1071 | ((*oit)->GetId() == id) && |
---|
| 1072 | !obj->Mailed()) |
---|
[540] | 1073 | { |
---|
[542] | 1074 | obj = *oit; |
---|
[540] | 1075 | obj->Mail(); |
---|
| 1076 | ++ pvsSize; |
---|
| 1077 | pvs.push_back(obj); |
---|
| 1078 | } |
---|
| 1079 | } |
---|
| 1080 | } |
---|
| 1081 | |
---|
| 1082 | return pvsSize; |
---|
| 1083 | } |
---|
| 1084 | |
---|
| 1085 | /***********************************************************************/ |
---|
| 1086 | /* GlDebuggerWidget implementation */ |
---|
| 1087 | /***********************************************************************/ |
---|
| 1088 | |
---|
| 1089 | |
---|
| 1090 | GlDebuggerWidget::GlDebuggerWidget(GlRendererBuffer *buf, QWidget *parent)
|
---|
[538] | 1091 | : QGLWidget(QGLFormat(QGL::SampleBuffers), parent), mRenderBuffer(buf)
|
---|
| 1092 | {
|
---|
| 1093 | // create the pbuffer
|
---|
| 1094 | //pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
|
---|
| 1095 | timerId = startTimer(20);
|
---|
| 1096 | setWindowTitle(("OpenGL pbuffers"));
|
---|
| 1097 | }
|
---|
| 1098 |
|
---|
| 1099 |
|
---|
| 1100 | GlDebuggerWidget::~GlDebuggerWidget()
|
---|
| 1101 | {
|
---|
[556] | 1102 | mRenderBuffer->releaseFromDynamicTexture();
|
---|
| 1103 | glDeleteTextures(1, &dynamicTexture);
|
---|
| 1104 |
|
---|
| 1105 | DEL_PTR(mRenderBuffer);
|
---|
[538] | 1106 | }
|
---|
| 1107 |
|
---|
| 1108 |
|
---|
| 1109 | void GlDebuggerWidget::initializeGL()
|
---|
| 1110 | {
|
---|
| 1111 | glMatrixMode(GL_PROJECTION);
|
---|
| 1112 | glLoadIdentity();
|
---|
| 1113 |
|
---|
| 1114 | glFrustum(-1, 1, -1, 1, 10, 100);
|
---|
| 1115 | glTranslatef(-0.5f, -0.5f, -0.5f);
|
---|
| 1116 | glTranslatef(0.0f, 0.0f, -15.0f);
|
---|
| 1117 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1118 |
|
---|
| 1119 | glEnable(GL_CULL_FACE);
|
---|
| 1120 | initCommon();
|
---|
| 1121 | initPbuffer();
|
---|
| 1122 |
|
---|
| 1123 | }
|
---|
| 1124 |
|
---|
| 1125 |
|
---|
| 1126 | void GlDebuggerWidget::resizeGL(int w, int h)
|
---|
| 1127 | {
|
---|
| 1128 | glViewport(0, 0, w, h);
|
---|
| 1129 | }
|
---|
| 1130 |
|
---|
| 1131 |
|
---|
| 1132 | void GlDebuggerWidget::paintGL()
|
---|
| 1133 | {
|
---|
| 1134 | // draw a spinning cube into the pbuffer..
|
---|
| 1135 | mRenderBuffer->makeCurrent();
|
---|
[540] | 1136 |
|
---|
| 1137 | BeamSampleStatistics stats;
|
---|
| 1138 | mRenderBuffer->SampleBeamContributions(mSourceObject, mBeam, mSamples, stats);
|
---|
| 1139 |
|
---|
[538] | 1140 | glFlush();
|
---|
| 1141 |
|
---|
[540] | 1142 | // rendering directly to a texture is not supported on X11, unfortunately
|
---|
[538] | 1143 | mRenderBuffer->updateDynamicTexture(dynamicTexture);
|
---|
| 1144 |
|
---|
[540] | 1145 | // and use the pbuffer contents as a texture when rendering the
|
---|
[538] | 1146 | // background and the bouncing cubes
|
---|
| 1147 | makeCurrent();
|
---|
| 1148 | glBindTexture(GL_TEXTURE_2D, dynamicTexture);
|
---|
| 1149 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1150 |
|
---|
| 1151 | // draw the background
|
---|
| 1152 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1153 | glPushMatrix();
|
---|
| 1154 | glLoadIdentity();
|
---|
| 1155 | glMatrixMode(GL_PROJECTION);
|
---|
| 1156 | glPushMatrix();
|
---|
| 1157 | glLoadIdentity();
|
---|
| 1158 |
|
---|
| 1159 | glPopMatrix();
|
---|
| 1160 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1161 | glPopMatrix();
|
---|
| 1162 | }
|
---|
| 1163 |
|
---|
| 1164 |
|
---|
| 1165 | void GlDebuggerWidget::initPbuffer()
|
---|
| 1166 | {
|
---|
| 1167 | // set up the pbuffer context
|
---|
| 1168 | mRenderBuffer->makeCurrent();
|
---|
[540] | 1169 | /*mRenderBuffer->InitGL();
|
---|
[538] | 1170 |
|
---|
| 1171 | glViewport(0, 0, mRenderBuffer->size().width(), mRenderBuffer->size().height());
|
---|
| 1172 | glMatrixMode(GL_PROJECTION);
|
---|
| 1173 | glLoadIdentity();
|
---|
| 1174 | glOrtho(-1, 1, -1, 1, -99, 99);
|
---|
| 1175 | glTranslatef(-0.5f, -0.5f, 0.0f);
|
---|
| 1176 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1177 | glLoadIdentity();
|
---|
| 1178 |
|
---|
[540] | 1179 | glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);*/
|
---|
| 1180 |
|
---|
[538] | 1181 | // generate a texture that has the same size/format as the pbuffer
|
---|
| 1182 | dynamicTexture = mRenderBuffer->generateDynamicTexture();
|
---|
| 1183 |
|
---|
| 1184 | // bind the dynamic texture to the pbuffer - this is a no-op under X11
|
---|
| 1185 | mRenderBuffer->bindToDynamicTexture(dynamicTexture);
|
---|
| 1186 | makeCurrent();
|
---|
| 1187 | }
|
---|
| 1188 |
|
---|
| 1189 | void GlDebuggerWidget::initCommon()
|
---|
| 1190 | {
|
---|
| 1191 | glEnable(GL_TEXTURE_2D);
|
---|
| 1192 | glEnable(GL_DEPTH_TEST);
|
---|
| 1193 |
|
---|
| 1194 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
[556] | 1195 | } |
---|