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