[589] | 1 | #include "Mesh.h"
|
---|
[1001] | 2 | #include "glInterface.h"
|
---|
| 3 | #include "OcclusionQuery.h"
|
---|
[589] | 4 | #include "GlRenderer.h"
|
---|
| 5 | #include "ViewCellsManager.h"
|
---|
| 6 | #include "SceneGraph.h"
|
---|
| 7 | #include "Viewcell.h"
|
---|
| 8 | #include "Beam.h"
|
---|
[532] | 9 | #include "KdTree.h"
|
---|
[589] | 10 | #include "Environment.h"
|
---|
[1581] | 11 | #include "Triangle3.h"
|
---|
| 12 | #include "IntersectableWrapper.h"
|
---|
[1585] | 13 | #include "BvHierarchy.h"
|
---|
[1594] | 14 | #include "KdTree.h"
|
---|
[513] | 15 |
|
---|
[1990] | 16 | #ifdef USE_CG
|
---|
[1001] | 17 |
|
---|
[1990] | 18 | #include <Cg/cg.h>
|
---|
| 19 | #include <Cg/cgGL.h>
|
---|
[1001] | 20 |
|
---|
[1990] | 21 | #endif
|
---|
| 22 |
|
---|
[863] | 23 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 24 |
|
---|
[516] | 25 |
|
---|
[1112] | 26 | static bool arbQuerySupport = false;
|
---|
| 27 | static bool nvQuerySupport = false;
|
---|
| 28 |
|
---|
[1925] | 29 | static GLuint frontDepthMap;
|
---|
| 30 | static GLuint backDepthMap;
|
---|
[1112] | 31 |
|
---|
[1785] | 32 | const int depthMapSize = 512;
|
---|
| 33 |
|
---|
[2538] | 34 |
|
---|
[1112] | 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 | }
|
---|
[2538] | 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 | }
|
---|
[1001] | 66 | }
|
---|
[589] | 67 |
|
---|
[811] | 68 |
|
---|
[589] | 69 | GlRenderer::GlRenderer(SceneGraph *sceneGraph,
|
---|
| 70 | ViewCellsManager *viewCellsManager,
|
---|
| 71 | KdTree *tree):
|
---|
[2543] | 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)
|
---|
[589] | 80 | {
|
---|
[2543] | 81 | mSceneGraph->CollectObjects(&mObjects);
|
---|
[1112] | 82 |
|
---|
[2543] | 83 | viewCellsManager->GetViewPoint(mViewPoint);
|
---|
[1112] | 84 |
|
---|
[2543] | 85 | mViewDirection = Vector3(0,0,1);
|
---|
[608] | 86 |
|
---|
[2543] | 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;
|
---|
[878] | 97 |
|
---|
[2543] | 98 | if (mViewCellsManager->GetViewCellPoints()->size())
|
---|
| 99 | mPvsStatFrames = (int)mViewCellsManager->GetViewCellPoints()->size();
|
---|
| 100 | else
|
---|
| 101 | Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", mPvsStatFrames);
|
---|
[1931] | 102 |
|
---|
[2049] | 103 |
|
---|
[2543] | 104 | mPvsErrorBuffer.resize(mPvsStatFrames);
|
---|
| 105 | ClearErrorBuffer();
|
---|
[589] | 106 | }
|
---|
| 107 |
|
---|
[2538] | 108 |
|
---|
[589] | 109 | GlRenderer::~GlRenderer()
|
---|
| 110 | {
|
---|
[2543] | 111 | cerr<<"gl renderer destructor..\n";
|
---|
[589] | 112 |
|
---|
[2543] | 113 | CLEAR_CONTAINER(mOcclusionQueries);
|
---|
[2538] | 114 |
|
---|
[2543] | 115 | DeleteVbos();
|
---|
[2538] | 116 |
|
---|
[2543] | 117 | if (mData) delete [] mData;
|
---|
| 118 | if (mIndices) delete [] mIndices;
|
---|
| 119 |
|
---|
| 120 | glDeleteBuffersARB(1, &mVboId);
|
---|
| 121 | cerr<<"done."<<endl;
|
---|
[589] | 122 | }
|
---|
| 123 |
|
---|
[1145] | 124 |
|
---|
[2543] | 125 | void GlRenderer::RenderTriangle(TriangleIntersectable *object)
|
---|
[1581] | 126 | {
|
---|
[2543] | 127 | Triangle3 &t = object->GetItem();
|
---|
[2050] | 128 |
|
---|
[2543] | 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();
|
---|
[1581] | 136 | }
|
---|
| 137 |
|
---|
[2543] | 138 |
|
---|
| 139 | void GlRenderer::RenderIntersectable(Intersectable *object)
|
---|
[589] | 140 | {
|
---|
[2543] | 141 | if (!object)
|
---|
| 142 | return;
|
---|
[2050] | 143 |
|
---|
[2543] | 144 | if (object->mRenderedFrame == mCurrentFrame)
|
---|
| 145 | return;
|
---|
[1585] | 146 |
|
---|
[2543] | 147 | object->mRenderedFrame = mCurrentFrame;
|
---|
[589] | 148 |
|
---|
[2543] | 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();
|
---|
[589] | 198 | }
|
---|
| 199 |
|
---|
[2543] | 200 |
|
---|
| 201 | void GlRenderer::RenderRays(const VssRayContainer &rays)
|
---|
[1581] | 202 | {
|
---|
[2543] | 203 | VssRayContainer::const_iterator it = rays.begin(), it_end = rays.end();
|
---|
[589] | 204 |
|
---|
[2543] | 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();
|
---|
[1581] | 218 | }
|
---|
| 219 |
|
---|
[1737] | 220 |
|
---|
[2543] | 221 | void GlRenderer::RenderViewCell(ViewCell *vc)
|
---|
[589] | 222 | {
|
---|
[2543] | 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 | }
|
---|
[589] | 232 |
|
---|
[2543] | 233 | RenderMesh(vc->GetMesh());
|
---|
| 234 | }
|
---|
| 235 | else
|
---|
| 236 | {
|
---|
| 237 | // render viewcells in the subtree
|
---|
| 238 | if (!vc->IsLeaf())
|
---|
| 239 | {
|
---|
| 240 | ViewCellInterior *vci = (ViewCellInterior *) vc;
|
---|
[599] | 241 |
|
---|
[2543] | 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 | }
|
---|
[599] | 252 | }
|
---|
[589] | 253 | }
|
---|
| 254 |
|
---|
[599] | 255 |
|
---|
[2530] | 256 | void GlRenderer::RenderMeshInstance(MeshInstance *mi)
|
---|
[589] | 257 | {
|
---|
[2530] | 258 | RenderMesh(mi->GetMesh());
|
---|
[589] | 259 | }
|
---|
| 260 |
|
---|
[1001] | 261 |
|
---|
[589] | 262 | void
|
---|
[1001] | 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
|
---|
[1960] | 280 | GlRenderer::SetupFalseColor(const unsigned int id)
|
---|
[589] | 281 | {
|
---|
[1944] | 282 | // swap bits of the color
|
---|
| 283 | glColor3ub(id&255, (id>>8)&255, (id>>16)&255);
|
---|
[589] | 284 | }
|
---|
| 285 |
|
---|
| 286 |
|
---|
[1960] | 287 | unsigned int GlRenderer::GetId(const unsigned char r,
|
---|
| 288 | const unsigned char g,
|
---|
| 289 | const unsigned char b) const
|
---|
[589] | 290 | {
|
---|
| 291 | return r + (g << 8) + (b << 16);
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[1960] | 294 |
|
---|
[589] | 295 | void
|
---|
| 296 | GlRenderer::SetupMaterial(Material *m)
|
---|
| 297 | {
|
---|
| 298 | if (m)
|
---|
| 299 | glColor3fv(&(m->mDiffuseColor.r));
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[1960] | 302 |
|
---|
[2543] | 303 | void GlRenderer::RenderMesh(Mesh *mesh)
|
---|
[589] | 304 | {
|
---|
[2543] | 305 | int i = 0;
|
---|
[589] | 306 |
|
---|
[2543] | 307 | if (!mUseFalseColors && !mUseForcedColors)
|
---|
| 308 | SetupMaterial(mesh->mMaterial);
|
---|
[589] | 309 |
|
---|
[2543] | 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();
|
---|
[589] | 322 | }
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | void
|
---|
| 326 | GlRenderer::InitGL()
|
---|
| 327 | {
|
---|
[1608] | 328 | mSphere = (GLUquadric *)gluNewQuadric();
|
---|
| 329 |
|
---|
[589] | 330 | glMatrixMode(GL_PROJECTION);
|
---|
| 331 | glLoadIdentity();
|
---|
| 332 |
|
---|
| 333 | glMatrixMode(GL_MODELVIEW);
|
---|
| 334 | glLoadIdentity();
|
---|
| 335 |
|
---|
[1785] | 336 | glFrontFace(GL_CCW);
|
---|
| 337 | glCullFace(GL_BACK);
|
---|
[589] | 338 | glEnable(GL_CULL_FACE);
|
---|
| 339 | glShadeModel(GL_FLAT);
|
---|
[1997] | 340 | glDepthFunc( GL_LESS );
|
---|
[589] | 341 | glEnable(GL_DEPTH_TEST);
|
---|
| 342 | glEnable(GL_CULL_FACE);
|
---|
| 343 |
|
---|
[1001] | 344 | InitExtensions();
|
---|
[746] | 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);
|
---|
[2002] | 351 |
|
---|
| 352 | OcclusionQuery::GenQueries(mOcclusionQueries, 10);
|
---|
[2538] | 353 |
|
---|
| 354 | CreateVertexArrays();
|
---|
[589] | 355 | }
|
---|
| 356 |
|
---|
[746] | 357 |
|
---|
[589] | 358 | void
|
---|
[811] | 359 | GlRenderer::SetupProjection(const int w, const int h, const float angle)
|
---|
[589] | 360 | {
|
---|
| 361 | glViewport(0, 0, w, h);
|
---|
| 362 | glMatrixMode(GL_PROJECTION);
|
---|
| 363 | glLoadIdentity();
|
---|
[811] | 364 | gluPerspective(angle, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
|
---|
[589] | 365 | glMatrixMode(GL_MODELVIEW);
|
---|
| 366 | }
|
---|
| 367 |
|
---|
[1581] | 368 |
|
---|
| 369 |
|
---|
[589] | 370 | void
|
---|
| 371 | GlRenderer::SetupCamera()
|
---|
| 372 | {
|
---|
[2544] | 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);
|
---|
[589] | 384 | }
|
---|
| 385 |
|
---|
[1145] | 386 | void
|
---|
| 387 | GlRenderer::_RenderScene()
|
---|
| 388 | {
|
---|
[2538] | 389 | ObjectContainer::const_iterator oi = mObjects.begin();
|
---|
| 390 | for (; oi != mObjects.end(); oi++)
|
---|
| 391 | RenderIntersectable(*oi);
|
---|
[1145] | 392 | }
|
---|
| 393 |
|
---|
[2538] | 394 | void GlRenderer::_RenderSceneTrianglesWithDrawArrays()
|
---|
[2002] | 395 | {
|
---|
[2538] | 396 | EnableDrawArrays();
|
---|
| 397 |
|
---|
| 398 | if (mUseVbos)
|
---|
| 399 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
[2002] | 400 |
|
---|
[2539] | 401 | int offset = (int)mObjects.size() * 3;
|
---|
[2538] | 402 | char *arrayPtr = mUseVbos ? NULL : (char *)mData;
|
---|
[2002] | 403 |
|
---|
[2538] | 404 | glVertexPointer(3, GL_FLOAT, 0, (char *)arrayPtr);
|
---|
| 405 | glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
|
---|
| 406 |
|
---|
[2539] | 407 | glDrawArrays(GL_TRIANGLES, 0, (int)mObjects.size() * 3);
|
---|
[2538] | 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 | }
|
---|
[2002] | 430 | }
|
---|
| 431 |
|
---|
[2538] | 432 | glEnd();
|
---|
[2002] | 433 | }
|
---|
| 434 |
|
---|
[2538] | 435 |
|
---|
[1145] | 436 | bool
|
---|
| 437 | GlRenderer::RenderScene()
|
---|
| 438 | {
|
---|
[1785] | 439 | Intersectable::NewMail();
|
---|
[2538] | 440 |
|
---|
| 441 | #if 1
|
---|
| 442 |
|
---|
| 443 | _RenderSceneTrianglesWithDrawArrays();
|
---|
| 444 |
|
---|
| 445 | #else
|
---|
[1145] | 446 | static int glList = -1;
|
---|
| 447 | if (mUseGlLists) {
|
---|
| 448 | if (glList == -1) {
|
---|
| 449 | glList = glGenLists(1);
|
---|
| 450 | glNewList(glList, GL_COMPILE);
|
---|
[2002] | 451 | _RenderSceneTriangles();
|
---|
[1145] | 452 | glEndList();
|
---|
| 453 | }
|
---|
[1969] | 454 |
|
---|
[1145] | 455 | glCallList(glList);
|
---|
| 456 | } else
|
---|
[2002] | 457 | _RenderSceneTriangles();
|
---|
[1145] | 458 |
|
---|
[2538] | 459 | #endif
|
---|
[1145] | 460 | return true;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 |
|
---|
[589] | 464 | void
|
---|
[2538] | 465 | GlRendererBuffer::EvalQueryWithItemBuffer()
|
---|
[997] | 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)
|
---|
[2053] | 484 | {
|
---|
[997] | 485 | ++ mObjects[id]->mCounter;
|
---|
[2053] | 486 | }
|
---|
[997] | 487 | }
|
---|
| 488 | }
|
---|
| 489 | }
|
---|
| 490 |
|
---|
[1145] | 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)
|
---|
[2538] | 503 | {
|
---|
| 504 | mPixelBuffer = NULL;
|
---|
[1785] | 505 | // implement width and height in subclasses
|
---|
[1145] | 506 | }
|
---|
| 507 |
|
---|
| 508 |
|
---|
[997] | 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);
|
---|
[1000] | 518 |
|
---|
| 519 |
|
---|
| 520 | // simulate detectemptyviewspace using backface culling
|
---|
[1001] | 521 | if (mDetectEmptyViewSpace)
|
---|
[997] | 522 | {
|
---|
[1000] | 523 | glEnable(GL_CULL_FACE);
|
---|
[1001] | 524 | //cout << "culling" << endl;
|
---|
[1000] | 525 | }
|
---|
| 526 | else
|
---|
| 527 | {
|
---|
[1001] | 528 | //cout << "not culling" << endl;
|
---|
[1000] | 529 | glDisable(GL_CULL_FACE);
|
---|
[1001] | 530 | }
|
---|
[1000] | 531 |
|
---|
| 532 |
|
---|
[1001] | 533 | //const int numQ = 1;
|
---|
| 534 | const int numQ = (int)mOcclusionQueries.size();
|
---|
[1000] | 535 |
|
---|
[1001] | 536 | //glFinish();
|
---|
| 537 | #if 0
|
---|
[1000] | 538 | //-- now issue queries for all objects
|
---|
| 539 | for (int j = 0; j < (int)mObjects.size(); ++ j)
|
---|
[1001] | 540 | {
|
---|
| 541 | mOcclusionQueries[j]->BeginQuery();
|
---|
| 542 | RenderIntersectable(mObjects[j]);
|
---|
| 543 | mOcclusionQueries[j]->EndQuery();
|
---|
| 544 |
|
---|
| 545 | unsigned int pixelCount;
|
---|
| 546 |
|
---|
| 547 | pixelCount = mOcclusionQueries[j]->GetQueryResult();
|
---|
[2053] | 548 |
|
---|
[1001] | 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)
|
---|
[1000] | 557 | {
|
---|
[1001] | 558 | for (q = 0; ((j + q) < (int)mObjects.size()) && (q < numQ); ++ q)
|
---|
[997] | 559 | {
|
---|
[1000] | 560 | //glFinish();
|
---|
[1001] | 561 | mOcclusionQueries[q]->BeginQuery();
|
---|
[1000] | 562 |
|
---|
[997] | 563 | RenderIntersectable(mObjects[j + q]);
|
---|
[1000] | 564 |
|
---|
[1001] | 565 | mOcclusionQueries[q]->EndQuery();
|
---|
[1000] | 566 | //glFinish();
|
---|
[997] | 567 | }
|
---|
[1001] | 568 | //cout << "q: " << q << endl;
|
---|
[997] | 569 | // collect results of the queries
|
---|
[1001] | 570 | for (int t = 0; t < q; ++ t)
|
---|
[997] | 571 | {
|
---|
| 572 | unsigned int pixelCount;
|
---|
[1001] | 573 |
|
---|
[997] | 574 | //-- reenable other state
|
---|
[1001] | 575 | #if 0
|
---|
| 576 | bool available;
|
---|
| 577 |
|
---|
[997] | 578 | do
|
---|
| 579 | {
|
---|
[1001] | 580 | available = mOcclusionQueries[t]->ResultAvailable();
|
---|
| 581 |
|
---|
| 582 | if (!available) cout << "W";
|
---|
[997] | 583 | }
|
---|
[1001] | 584 | while (!available);
|
---|
[997] | 585 | #endif
|
---|
| 586 |
|
---|
[1001] | 587 | pixelCount = mOcclusionQueries[t]->GetQueryResult();
|
---|
[997] | 588 |
|
---|
[1001] | 589 | //if (pixelCount > 0)
|
---|
| 590 | // cout <<"o="<<j+q<<" q="<<mOcclusionQueries[q]->GetQueryId()<<" pc="<<pixelCount<<" ";
|
---|
| 591 | mObjects[j + t]->mCounter += pixelCount;
|
---|
[2053] | 592 |
|
---|
[997] | 593 | }
|
---|
| 594 |
|
---|
[1001] | 595 | //j += q;
|
---|
[997] | 596 | }
|
---|
[1001] | 597 | #endif
|
---|
| 598 | //glFinish();
|
---|
[997] | 599 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
| 600 | glDepthMask(GL_TRUE);
|
---|
[1000] | 601 |
|
---|
| 602 | glEnable(GL_CULL_FACE);
|
---|
[997] | 603 | }
|
---|
| 604 |
|
---|
| 605 |
|
---|
| 606 | void
|
---|
[1931] | 607 | GlRenderer::RandomViewPoint()
|
---|
[1001] | 608 | {
|
---|
[589] | 609 | // do not use this function since it could return different viewpoints for
|
---|
| 610 | // different executions of the algorithm
|
---|
| 611 |
|
---|
[1112] | 612 | // mViewCellsManager->GetViewPoint(mViewPoint);
|
---|
[589] | 613 |
|
---|
| 614 | while (1) {
|
---|
| 615 | Vector3 pVector = Vector3(halton.GetNumber(1),
|
---|
| 616 | halton.GetNumber(2),
|
---|
| 617 | halton.GetNumber(3));
|
---|
| 618 |
|
---|
[1112] | 619 | mViewPoint = mViewCellsManager->GetViewSpaceBox().GetPoint(pVector);
|
---|
[589] | 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 |
|
---|
[1585] | 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 |
|
---|
[811] | 686 | }
|
---|
[1585] | 687 |
|
---|
| 688 | void
|
---|
| 689 | GlRenderer::RenderBvhNode(BvhNode *node)
|
---|
| 690 | {
|
---|
| 691 | if (node->IsLeaf()) {
|
---|
| 692 | BvhLeaf *leaf = (BvhLeaf *) node;
|
---|
[1785] | 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
|
---|
[1983] | 710 | for (int i=0; i < leaf->mObjects.size(); i++)
|
---|
| 711 | RenderIntersectable(leaf->mObjects[i]);
|
---|
[1785] | 712 | #endif
|
---|
[1585] | 713 | } else {
|
---|
| 714 | BvhInterior *in = (BvhInterior *)node;
|
---|
| 715 | RenderBvhNode(in->GetBack());
|
---|
| 716 | RenderBvhNode(in->GetFront());
|
---|
| 717 | }
|
---|
[1594] | 718 | }
|
---|
| 719 |
|
---|
| 720 | void
|
---|
| 721 | GlRenderer::RenderKdNode(KdNode *node)
|
---|
| 722 | {
|
---|
[2538] | 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);
|
---|
[1594] | 740 | }
|
---|
[1585] | 741 | }
|
---|
| 742 |
|
---|
[1785] | 743 |
|
---|
| 744 |
|
---|
| 745 |
|
---|
| 746 | void
|
---|
| 747 | GlRendererBuffer::EvalRenderCostSample(RenderCostSample &sample,
|
---|
| 748 | const bool useOcclusionQueries,
|
---|
| 749 | const int threshold
|
---|
| 750 | )
|
---|
| 751 | {
|
---|
[2543] | 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)
|
---|
[1785] | 773 | {
|
---|
[2543] | 774 | (*it)->mCounter = 0;
|
---|
| 775 |
|
---|
[1785] | 776 | }
|
---|
| 777 |
|
---|
[2543] | 778 | ++ mFrame;
|
---|
| 779 |
|
---|
| 780 | //glCullFace(GL_FRONT);
|
---|
| 781 | glCullFace(GL_BACK);
|
---|
| 782 | glDisable(GL_CULL_FACE);
|
---|
| 783 |
|
---|
| 784 |
|
---|
[1785] | 785 | // query all 6 directions for a full point sample
|
---|
[2543] | 786 | for (int i = 0; i < 6; ++ i)
|
---|
[1785] | 787 | {
|
---|
[2543] | 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)
|
---|
[1785] | 804 | {
|
---|
[2543] | 805 | char filename[256];
|
---|
| 806 | sprintf(filename, "snap/frame-%04d-%d.png", mFrame, i);
|
---|
| 807 | // QImage im = toImage();
|
---|
| 808 | // im.save(filename, "PNG");
|
---|
[1785] | 809 | }
|
---|
[2543] | 810 |
|
---|
| 811 | // evaluate the sample
|
---|
| 812 | if (useOcclusionQueries)
|
---|
[1785] | 813 | {
|
---|
[2543] | 814 | EvalQueryWithOcclusionQueries();
|
---|
[1785] | 815 | }
|
---|
[2543] | 816 | else
|
---|
[1785] | 817 | {
|
---|
[2543] | 818 | EvalQueryWithItemBuffer();
|
---|
[1785] | 819 | }
|
---|
| 820 | }
|
---|
[2543] | 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)
|
---|
[1785] | 827 | {
|
---|
[2543] | 828 | Intersectable *obj = *it;
|
---|
| 829 | if (obj->mCounter >= threshold)
|
---|
[1785] | 830 | {
|
---|
[2543] | 831 | ++ sample.mVisibleObjects;
|
---|
| 832 | sample.mVisiblePixels += obj->mCounter;
|
---|
[1785] | 833 | }
|
---|
| 834 | }
|
---|
[2543] | 835 |
|
---|
| 836 | //cout << "RS=" << sample.mVisibleObjects << " ";
|
---|
[1585] | 837 | }
|
---|
[1785] | 838 |
|
---|
| 839 |
|
---|
| 840 | GlRendererBuffer::~GlRendererBuffer()
|
---|
| 841 | {
|
---|
[1990] | 842 | #if 0
|
---|
| 843 | #ifdef USE_CG
|
---|
| 844 | if (sCgFragmentProgram)
|
---|
[1785] | 845 | cgDestroyProgram(sCgFragmentProgram);
|
---|
| 846 | if (sCgContext)
|
---|
| 847 | cgDestroyContext(sCgContext);
|
---|
| 848 | #endif
|
---|
[1990] | 849 | #endif
|
---|
[2543] | 850 |
|
---|
[1785] | 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 | {
|
---|
[2543] | 861 | MakeCurrent();
|
---|
[1785] | 862 |
|
---|
[2543] | 863 | if (mPixelBuffer == NULL)
|
---|
| 864 | mPixelBuffer = new unsigned int[GetWidth()*GetHeight()];
|
---|
[1785] | 865 |
|
---|
[2543] | 866 | // using 90 degree projection to capture 360 view with 6 samples
|
---|
| 867 | SetupProjection(GetHeight(), GetHeight(), 90.0f);
|
---|
[1785] | 868 |
|
---|
[2543] | 869 | //samples.resize(numSamples);
|
---|
| 870 | halton.Reset();
|
---|
[1785] | 871 |
|
---|
[2543] | 872 | // the number of queries queried in batch mode
|
---|
| 873 | const int numQ = 500;
|
---|
[1785] | 874 |
|
---|
[2543] | 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();
|
---|
[1785] | 891 | }
|
---|
| 892 |
|
---|
| 893 |
|
---|
| 894 |
|
---|
| 895 |
|
---|
[2543] | 896 |
|
---|
[1785] | 897 | void
|
---|
[1931] | 898 | GlRenderer::ClearErrorBuffer()
|
---|
[1785] | 899 | {
|
---|
| 900 | for (int i=0; i < mPvsStatFrames; i++) {
|
---|
| 901 | mPvsErrorBuffer[i].mError = 1.0f;
|
---|
| 902 | }
|
---|
[2002] | 903 | mPvsStat.maxError = 0.0f;
|
---|
[1785] | 904 | }
|
---|
| 905 |
|
---|
| 906 |
|
---|
| 907 | void
|
---|
| 908 | GlRendererBuffer::EvalPvsStat()
|
---|
| 909 | {
|
---|
[2048] | 910 | MakeCurrent();
|
---|
[2049] | 911 |
|
---|
[2048] | 912 | GlRenderer::EvalPvsStat();
|
---|
[2049] | 913 |
|
---|
[2048] | 914 | DoneCurrent();
|
---|
[1785] | 915 | // mRenderingFinished.wakeAll();
|
---|
| 916 | }
|
---|
| 917 |
|
---|
| 918 |
|
---|
[2048] | 919 | void GlRendererBuffer::EvalPvsStat(const SimpleRayContainer &viewPoints)
|
---|
| 920 | {
|
---|
| 921 | MakeCurrent();
|
---|
| 922 |
|
---|
| 923 | GlRenderer::EvalPvsStat(viewPoints);
|
---|
| 924 |
|
---|
| 925 | DoneCurrent();
|
---|
| 926 | }
|
---|
| 927 |
|
---|
| 928 |
|
---|
[1785] | 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 |
|
---|
[1990] | 1080 | #if 0
|
---|
| 1081 | #ifdef USE_CG
|
---|
[1785] | 1082 | // bind pixel shader implementing the front depth buffer functionality
|
---|
| 1083 | cgGLBindProgram(sCgFragmentProgram);
|
---|
| 1084 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
| 1085 | #endif
|
---|
[1990] | 1086 | #endif
|
---|
[1785] | 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 |
|
---|
[1935] | 1140 | // 6. Use occlusion queries for all viewcell meshes associated with the beam ->
|
---|
[1785] | 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 |
|
---|
[1990] | 1212 | ////////
|
---|
[1785] | 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);
|
---|
[1935] | 1220 |
|
---|
[1990] | 1221 | #if 0
|
---|
| 1222 | #ifdef USE_CG
|
---|
[1785] | 1223 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
| 1224 | #endif
|
---|
[1990] | 1225 | #endif
|
---|
| 1226 |
|
---|
[1785] | 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 | {
|
---|
[1990] | 1270 | MakeCurrent();
|
---|
| 1271 | GlRenderer::InitGL();
|
---|
[1785] | 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 |
|
---|
[1990] | 1295 | #if 0
|
---|
| 1296 | #ifdef USE_CG
|
---|
| 1297 |
|
---|
[1785] | 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 |
|
---|
[2023] | 1346 | bool GlRendererBuffer::ValidViewPoint()
|
---|
[2048] | 1347 | {
|
---|
| 1348 | MakeCurrent();
|
---|
[1785] | 1349 |
|
---|
[2048] | 1350 | SetupProjection(GetWidth(), GetHeight());
|
---|
| 1351 |
|
---|
[2023] | 1352 | bool result = GlRenderer::ValidViewPoint();
|
---|
[2048] | 1353 |
|
---|
[2023] | 1354 | DoneCurrent();
|
---|
[2048] | 1355 |
|
---|
[2023] | 1356 | return result;
|
---|
| 1357 | }
|
---|
| 1358 |
|
---|
| 1359 |
|
---|
[1931] | 1360 | void
|
---|
| 1361 | GlRenderer::EvalPvsStat()
|
---|
| 1362 | {
|
---|
| 1363 | mPvsStat.Reset();
|
---|
| 1364 | halton.Reset();
|
---|
[1785] | 1365 |
|
---|
[1931] | 1366 | SetupProjection(GetWidth(), GetHeight());
|
---|
[1785] | 1367 |
|
---|
[1931] | 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();
|
---|
[1785] | 1375 |
|
---|
[2002] | 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 |
|
---|
[1931] | 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 |
|
---|
[2002] | 1399 | if (mPvsErrorBuffer[i].mError > 0.0f) {
|
---|
[1931] | 1400 | int pvsSize;
|
---|
[2002] | 1401 |
|
---|
[1931] | 1402 | float error = GetPixelError(pvsSize);
|
---|
| 1403 | mPvsErrorBuffer[i].mError = error;
|
---|
| 1404 | mPvsErrorBuffer[i].mPvsSize = pvsSize;
|
---|
[1785] | 1405 |
|
---|
[1931] | 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 | }
|
---|
[2002] | 1425 |
|
---|
[1931] | 1426 | glFinish();
|
---|
| 1427 | cout<<endl<<flush;
|
---|
[1785] | 1428 | }
|
---|
[1931] | 1429 |
|
---|
[2048] | 1430 |
|
---|
| 1431 | void GlRenderer::EvalPvsStat(const SimpleRayContainer &viewPoints)
|
---|
| 1432 | {
|
---|
[2050] | 1433 | mPvsStat.Reset();
|
---|
[2048] | 1434 |
|
---|
[2050] | 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)
|
---|
[2048] | 1443 | {
|
---|
[2050] | 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)
|
---|
[2048] | 1456 | {
|
---|
[2050] | 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 <<")";
|
---|
[2048] | 1466 | }
|
---|
[2050] | 1467 |
|
---|
| 1468 | err = mPvsErrorBuffer[i].mError;
|
---|
| 1469 |
|
---|
| 1470 | if (err >= 0.0f)
|
---|
[2048] | 1471 | {
|
---|
[2050] | 1472 | if (err > mPvsStat.maxError)
|
---|
| 1473 | mPvsStat.maxError = err;
|
---|
| 1474 |
|
---|
| 1475 | mPvsStat.sumError += err;
|
---|
[2048] | 1476 | mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
|
---|
[2050] | 1477 |
|
---|
[2048] | 1478 | if (err == 0.0f)
|
---|
[2050] | 1479 | ++ mPvsStat.errorFreeFrames;
|
---|
| 1480 |
|
---|
[2048] | 1481 | ++ mPvsStat.frames;
|
---|
| 1482 | }
|
---|
| 1483 | }
|
---|
[2050] | 1484 |
|
---|
| 1485 | glFinish();
|
---|
| 1486 | cout << endl << flush;
|
---|
[2048] | 1487 | }
|
---|
| 1488 |
|
---|
| 1489 |
|
---|
| 1490 |
|
---|
[2002] | 1491 | bool
|
---|
| 1492 | GlRenderer::ValidViewPoint()
|
---|
[1931] | 1493 | {
|
---|
[2543] | 1494 | //cout<<"VV4 ";
|
---|
[2048] | 1495 | if (!mDetectEmptyViewSpace)
|
---|
| 1496 | return true;
|
---|
| 1497 | //cout << "vp: " << mViewPoint << " dir: " << mViewDirection << endl;
|
---|
[1931] | 1498 |
|
---|
[2543] | 1499 | OcclusionQuery *query = mOcclusionQueries[0];
|
---|
[1931] | 1500 |
|
---|
[2543] | 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);
|
---|
[2002] | 1506 |
|
---|
[2543] | 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;
|
---|
[2002] | 1543 | }
|
---|
[1931] | 1544 |
|
---|
[2543] | 1545 |
|
---|
| 1546 | float GlRenderer::GetPixelError(int &pvsSize)
|
---|
[2002] | 1547 | {
|
---|
| 1548 | return -1.0f;
|
---|
[1931] | 1549 | }
|
---|
| 1550 |
|
---|
[2543] | 1551 |
|
---|
| 1552 | void GlRenderer::RenderViewPoint()
|
---|
[1983] | 1553 | {
|
---|
[2538] | 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;
|
---|
[1983] | 1565 | }
|
---|
[1931] | 1566 |
|
---|
| 1567 |
|
---|
[2538] | 1568 | void GlRenderer::EnableDrawArrays()
|
---|
| 1569 | {
|
---|
| 1570 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 1571 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 1572 | }
|
---|
[1931] | 1573 |
|
---|
| 1574 |
|
---|
[2538] | 1575 | void GlRenderer::DisableDrawArrays()
|
---|
| 1576 | {
|
---|
| 1577 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
| 1578 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
| 1579 | }
|
---|
[2006] | 1580 |
|
---|
[2538] | 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);
|
---|
[1931] | 1636 | }
|
---|
[2538] | 1637 |
|
---|
| 1638 | #else
|
---|
| 1639 |
|
---|
| 1640 | void GlRenderer::RenderKdLeaf(KdLeaf *leaf)
|
---|
| 1641 | {
|
---|
| 1642 | if (!leaf->mIndexBufferSize)
|
---|
| 1643 | return;
|
---|
| 1644 |
|
---|
[2539] | 1645 | size_t offset = mObjects.size() * 3;
|
---|
[2538] | 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 |
|
---|
[2539] | 1661 | size_t offset = mObjects.size() * 3;
|
---|
[2538] | 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 |
|
---|
[2539] | 1697 | cout << "\n******** created vertex buffer objects **********" << endl;
|
---|
[2538] | 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 | }
|
---|