[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"
|
---|
[513] | 15 |
|
---|
[1146] | 16 | //#include <Cg/cg.h>
|
---|
| 17 | //#include <Cg/cgGL.h>
|
---|
[1001] | 18 |
|
---|
| 19 |
|
---|
[863] | 20 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 21 |
|
---|
[516] | 22 |
|
---|
[1112] | 23 | static bool arbQuerySupport = false;
|
---|
| 24 | static bool nvQuerySupport = false;
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | static void InitExtensions()
|
---|
| 28 | {
|
---|
| 29 | GLenum err = glewInit();
|
---|
| 30 |
|
---|
| 31 | if (GLEW_OK != err)
|
---|
| 32 | {
|
---|
| 33 | // problem: glewInit failed, something is seriously wrong
|
---|
| 34 | cerr << "Error: " << glewGetErrorString(err) << endl;
|
---|
| 35 | exit(1);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | if (GLEW_ARB_occlusion_query)
|
---|
| 39 | arbQuerySupport = true;
|
---|
| 40 |
|
---|
| 41 | if (GLEW_NV_occlusion_query)
|
---|
| 42 | nvQuerySupport = true;
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | if (!arbQuerySupport && !nvQuerySupport)
|
---|
| 46 | {
|
---|
| 47 | cout << "I require the GL_ARB_occlusion_query or the GL_NV_occlusion_query OpenGL extension to work.\n";
|
---|
| 48 | exit(1);
|
---|
| 49 | }
|
---|
[1001] | 50 | }
|
---|
[589] | 51 |
|
---|
[811] | 52 |
|
---|
[589] | 53 | GlRenderer::GlRenderer(SceneGraph *sceneGraph,
|
---|
| 54 | ViewCellsManager *viewCellsManager,
|
---|
| 55 | KdTree *tree):
|
---|
| 56 | Renderer(sceneGraph, viewCellsManager),
|
---|
| 57 | mKdTree(tree)
|
---|
| 58 | {
|
---|
| 59 | mSceneGraph->CollectObjects(&mObjects);
|
---|
[1112] | 60 |
|
---|
| 61 | // mViewCellsManager->GetViewPoint(mViewPoint);
|
---|
| 62 |
|
---|
[589] | 63 | mViewPoint = mSceneGraph->GetBox().Center();
|
---|
| 64 | mViewDirection = Vector3(0,0,1);
|
---|
[608] | 65 |
|
---|
[1112] | 66 | // mViewPoint = Vector3(991.7, 187.8, -271);
|
---|
| 67 | // mViewDirection = Vector3(0.9, 0, -0.4);
|
---|
[878] | 68 |
|
---|
[589] | 69 | // timerId = startTimer(10);
|
---|
| 70 | // debug coords for atlanta
|
---|
[608] | 71 | // mViewPoint = Vector3(3473, 6.778, -1699);
|
---|
| 72 | // mViewDirection = Vector3(-0.2432, 0, 0.97);
|
---|
[589] | 73 |
|
---|
| 74 | mFrame = 0;
|
---|
| 75 | mWireFrame = false;
|
---|
[1004] | 76 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace);
|
---|
[746] | 77 | mSnapErrorFrames = true;
|
---|
| 78 | mSnapPrefix = "snap/";
|
---|
| 79 | mUseForcedColors = false;
|
---|
[811] | 80 |
|
---|
| 81 | mUseGlLists = true;
|
---|
[1001] | 82 | //mUseGlLists = false;
|
---|
[589] | 83 | }
|
---|
| 84 |
|
---|
| 85 | GlRenderer::~GlRenderer()
|
---|
| 86 | {
|
---|
| 87 | cerr<<"gl renderer destructor..\n";
|
---|
[1145] | 88 |
|
---|
| 89 | //CLEAR_CONTAINER(sQueries);
|
---|
[1001] | 90 | CLEAR_CONTAINER(mOcclusionQueries);
|
---|
[589] | 91 |
|
---|
[1145] | 92 | cerr<<"done."<<endl;
|
---|
[589] | 93 | }
|
---|
| 94 |
|
---|
[1145] | 95 |
|
---|
[589] | 96 | void
|
---|
[1581] | 97 | GlRenderer::RenderTriangle(TriangleIntersectable *object)
|
---|
| 98 | {
|
---|
| 99 | Triangle3 &t = object->GetItem();
|
---|
| 100 | glBegin(GL_TRIANGLES);
|
---|
| 101 | glVertex3f(t.mVertices[0].x, t.mVertices[0].y, t.mVertices[0].z);
|
---|
| 102 | glVertex3f(t.mVertices[1].x, t.mVertices[1].y, t.mVertices[1].z);
|
---|
| 103 | glVertex3f(t.mVertices[2].x, t.mVertices[2].y, t.mVertices[2].z);
|
---|
| 104 | glEnd();
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | void
|
---|
[589] | 108 | GlRenderer::RenderIntersectable(Intersectable *object)
|
---|
| 109 | {
|
---|
| 110 |
|
---|
| 111 | glPushAttrib(GL_CURRENT_BIT);
|
---|
| 112 | if (mUseFalseColors)
|
---|
| 113 | SetupFalseColor(object->mId);
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | switch (object->Type()) {
|
---|
| 117 | case Intersectable::MESH_INSTANCE:
|
---|
| 118 | RenderMeshInstance((MeshInstance *)object);
|
---|
| 119 | break;
|
---|
| 120 | case Intersectable::VIEW_CELL:
|
---|
[1581] | 121 | RenderViewCell(dynamic_cast<ViewCell *>(object));
|
---|
| 122 | break;
|
---|
[1001] | 123 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
[1581] | 124 | RenderTransformedMeshInstance(dynamic_cast<TransformedMeshInstance *>(object));
|
---|
| 125 | break;
|
---|
| 126 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
| 127 | RenderTriangle(dynamic_cast<TriangleIntersectable *>(object));
|
---|
| 128 | break;
|
---|
[1585] | 129 | case Intersectable::BVH_INTERSECTABLE: {
|
---|
| 130 |
|
---|
| 131 | BvhNode *node = (dynamic_cast<BvhIntersectable *>(object))->GetItem();
|
---|
| 132 | // RenderBvhNode(node);
|
---|
| 133 | RenderBox(node->GetBoundingBox());
|
---|
| 134 | break;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[589] | 137 | default:
|
---|
| 138 | cerr<<"Rendering this object not yet implemented\n";
|
---|
| 139 | break;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | glPopAttrib();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[1581] | 145 | void
|
---|
| 146 | GlRenderer::RenderRays(const VssRayContainer &rays)
|
---|
| 147 | {
|
---|
| 148 | VssRayContainer::const_iterator it = rays.begin(), it_end = rays.end();
|
---|
[589] | 149 |
|
---|
[1581] | 150 | glBegin(GL_LINES);
|
---|
| 151 | for (; it != it_end; ++it) {
|
---|
| 152 | VssRay *ray = *it;
|
---|
| 153 | float importance = log10(1e3*ray->mWeightedPvsContribution)/3.0f;
|
---|
| 154 | // cout<<"w="<<ray->mWeightedPvsContribution<<" r="<<ray->mWeightedPvsContribution;
|
---|
| 155 | glColor3f(importance, importance, importance);
|
---|
| 156 | glVertex3fv(&ray->mOrigin.x);
|
---|
| 157 | glVertex3fv(&ray->mTermination.x);
|
---|
| 158 | }
|
---|
| 159 | glEnd();
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[589] | 162 | void
|
---|
| 163 | GlRenderer::RenderViewCell(ViewCell *vc)
|
---|
| 164 | {
|
---|
| 165 | if (vc->GetMesh()) {
|
---|
| 166 |
|
---|
[599] | 167 | if (!mUseFalseColors) {
|
---|
| 168 | if (vc->GetValid())
|
---|
| 169 | glColor3f(0,1,0);
|
---|
| 170 | else
|
---|
| 171 | glColor3f(0,0,1);
|
---|
| 172 | }
|
---|
[589] | 173 |
|
---|
| 174 | RenderMesh(vc->GetMesh());
|
---|
[599] | 175 | } else {
|
---|
| 176 | // render viewcells in the subtree
|
---|
| 177 | if (!vc->IsLeaf()) {
|
---|
| 178 | ViewCellInterior *vci = (ViewCellInterior *) vc;
|
---|
| 179 |
|
---|
| 180 | ViewCellContainer::iterator it = vci->mChildren.begin();
|
---|
| 181 | for (; it != vci->mChildren.end(); ++it) {
|
---|
| 182 | RenderViewCell(*it);
|
---|
| 183 | }
|
---|
[685] | 184 | } else {
|
---|
| 185 | // cerr<<"Empty viewcell mesh\n";
|
---|
[599] | 186 | }
|
---|
[589] | 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[599] | 190 |
|
---|
[589] | 191 | void
|
---|
| 192 | GlRenderer::RenderMeshInstance(MeshInstance *mi)
|
---|
| 193 | {
|
---|
| 194 | RenderMesh(mi->GetMesh());
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[1001] | 197 |
|
---|
[589] | 198 | void
|
---|
[1001] | 199 | GlRenderer::RenderTransformedMeshInstance(TransformedMeshInstance *mi)
|
---|
| 200 | {
|
---|
| 201 | // apply world transform before rendering
|
---|
| 202 | Matrix4x4 m;
|
---|
| 203 | mi->GetWorldTransform(m);
|
---|
| 204 |
|
---|
| 205 | glPushMatrix();
|
---|
[1002] | 206 | /* cout << "\n";
|
---|
[1001] | 207 | for (int i = 0; i < 4; ++ i)
|
---|
| 208 | for (int j = 0; j < 4; ++ j)
|
---|
[1002] | 209 | cout << m.x[i][j] << " "; cout << "\n"*/
|
---|
[1001] | 210 |
|
---|
| 211 | glMultMatrixf((float *)m.x);
|
---|
| 212 |
|
---|
[1002] | 213 | /*GLfloat dummy[16];
|
---|
| 214 | glGetFloatv(GL_MODELVIEW_MATRIX, dummy);
|
---|
[1001] | 215 | for (int i = 0; i < 16; ++ i)
|
---|
| 216 | cout << dummy[i] << " ";
|
---|
| 217 | cout << endl;*/
|
---|
| 218 | RenderMesh(mi->GetMesh());
|
---|
| 219 |
|
---|
| 220 | glPopMatrix();
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 |
|
---|
| 224 | void
|
---|
[589] | 225 | GlRenderer::SetupFalseColor(const int id)
|
---|
| 226 | {
|
---|
| 227 | // swap bits of the color
|
---|
| 228 | glColor3ub(id&255, (id>>8)&255, (id>>16)&255);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 |
|
---|
| 232 | int GlRenderer::GetId(int r, int g, int b) const
|
---|
| 233 | {
|
---|
| 234 | return r + (g << 8) + (b << 16);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | void
|
---|
| 238 | GlRenderer::SetupMaterial(Material *m)
|
---|
| 239 | {
|
---|
| 240 | if (m)
|
---|
| 241 | glColor3fv(&(m->mDiffuseColor.r));
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | void
|
---|
| 245 | GlRenderer::RenderMesh(Mesh *mesh)
|
---|
| 246 | {
|
---|
| 247 | int i = 0;
|
---|
| 248 |
|
---|
[746] | 249 | if (!mUseFalseColors && !mUseForcedColors)
|
---|
[589] | 250 | SetupMaterial(mesh->mMaterial);
|
---|
| 251 |
|
---|
| 252 | for (i=0; i < mesh->mFaces.size(); i++) {
|
---|
| 253 | if (mWireFrame)
|
---|
| 254 | glBegin(GL_LINE_LOOP);
|
---|
| 255 | else
|
---|
| 256 | glBegin(GL_POLYGON);
|
---|
| 257 |
|
---|
| 258 | Face *face = mesh->mFaces[i];
|
---|
| 259 | for (int j = 0; j < face->mVertexIndices.size(); j++) {
|
---|
| 260 | glVertex3fv(&mesh->mVertices[face->mVertexIndices[j]].x);
|
---|
| 261 | }
|
---|
| 262 | glEnd();
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | void
|
---|
| 267 | GlRenderer::InitGL()
|
---|
| 268 | {
|
---|
| 269 | glMatrixMode(GL_PROJECTION);
|
---|
| 270 | glLoadIdentity();
|
---|
| 271 |
|
---|
| 272 | glMatrixMode(GL_MODELVIEW);
|
---|
| 273 | glLoadIdentity();
|
---|
| 274 |
|
---|
| 275 | glEnable(GL_CULL_FACE);
|
---|
| 276 | glShadeModel(GL_FLAT);
|
---|
| 277 | glEnable(GL_DEPTH_TEST);
|
---|
| 278 | glEnable(GL_CULL_FACE);
|
---|
| 279 |
|
---|
[1001] | 280 | InitExtensions();
|
---|
[811] | 281 |
|
---|
[746] | 282 | #if 0
|
---|
| 283 | GLfloat mat_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
|
---|
| 284 | /* mat_specular and mat_shininess are NOT default values */
|
---|
| 285 | GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
|
---|
| 286 | GLfloat mat_specular[] = { 0.3, 0.3, 0.3, 1.0 };
|
---|
| 287 | GLfloat mat_shininess[] = { 1.0 };
|
---|
| 288 |
|
---|
| 289 | GLfloat light_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
|
---|
| 290 | GLfloat light_diffuse[] = { 0.4, 0.4, 0.4, 1.0 };
|
---|
| 291 | GLfloat light_specular[] = { 0.3, 0.3, 0.3, 1.0 };
|
---|
| 292 |
|
---|
| 293 | GLfloat lmodel_ambient[] = { 0.3, 0.3, 0.3, 1.0 };
|
---|
| 294 |
|
---|
| 295 |
|
---|
| 296 | // default Material
|
---|
| 297 | glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
|
---|
| 298 | glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
|
---|
| 299 | glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
|
---|
| 300 | glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
|
---|
| 301 |
|
---|
| 302 | // a light
|
---|
| 303 | glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
|
---|
| 304 | glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
|
---|
| 305 | glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
|
---|
| 306 |
|
---|
| 307 | glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
|
---|
| 308 | glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
|
---|
| 309 | glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
|
---|
| 310 |
|
---|
| 311 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
|
---|
| 312 |
|
---|
| 313 | glEnable(GL_LIGHTING);
|
---|
| 314 | glEnable(GL_LIGHT0);
|
---|
| 315 | glEnable(GL_LIGHT1);
|
---|
| 316 |
|
---|
| 317 |
|
---|
| 318 | // set position of the light
|
---|
| 319 | GLfloat infinite_light[] = { 1.0, 0.8, 1.0, 0.0 };
|
---|
| 320 | glLightfv (GL_LIGHT0, GL_POSITION, infinite_light);
|
---|
| 321 |
|
---|
| 322 | // set position of the light2
|
---|
| 323 | GLfloat infinite_light2[] = { -0.3, 1.5, 1.0, 0.0 };
|
---|
| 324 | glLightfv (GL_LIGHT1, GL_POSITION, infinite_light2);
|
---|
| 325 |
|
---|
| 326 | glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
|
---|
| 327 | // glColorMaterial( GL_FRONT_AND_BACK, GL_SPECULAR);
|
---|
| 328 | glEnable(GL_COLOR_MATERIAL);
|
---|
| 329 |
|
---|
| 330 | glShadeModel( GL_FLAT );
|
---|
| 331 |
|
---|
| 332 | glDepthFunc( GL_LESS );
|
---|
| 333 | glEnable( GL_DEPTH_TEST );
|
---|
| 334 | #endif
|
---|
| 335 |
|
---|
| 336 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
|
---|
| 337 |
|
---|
| 338 | glEnable( GL_NORMALIZE );
|
---|
| 339 |
|
---|
| 340 | glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
|
---|
[589] | 341 | }
|
---|
| 342 |
|
---|
[746] | 343 |
|
---|
[589] | 344 | void
|
---|
[811] | 345 | GlRenderer::SetupProjection(const int w, const int h, const float angle)
|
---|
[589] | 346 | {
|
---|
| 347 | glViewport(0, 0, w, h);
|
---|
| 348 | glMatrixMode(GL_PROJECTION);
|
---|
| 349 | glLoadIdentity();
|
---|
[811] | 350 | gluPerspective(angle, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
|
---|
[589] | 351 | glMatrixMode(GL_MODELVIEW);
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[1581] | 354 |
|
---|
| 355 |
|
---|
[589] | 356 | void
|
---|
| 357 | GlRenderer::SetupCamera()
|
---|
| 358 | {
|
---|
| 359 | Vector3 target = mViewPoint + mViewDirection;
|
---|
[811] | 360 |
|
---|
[589] | 361 | Vector3 up(0,1,0);
|
---|
| 362 |
|
---|
[811] | 363 | if (abs(DotProd(mViewDirection, up)) > 0.99f)
|
---|
| 364 | up = Vector3(1, 0, 0);
|
---|
| 365 |
|
---|
[589] | 366 | glLoadIdentity();
|
---|
| 367 | gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
|
---|
| 368 | target.x, target.y, target.z,
|
---|
| 369 | up.x, up.y, up.z);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[1145] | 372 | void
|
---|
| 373 | GlRenderer::_RenderScene()
|
---|
| 374 | {
|
---|
| 375 | ObjectContainer::const_iterator oi = mObjects.begin();
|
---|
[811] | 376 |
|
---|
[1145] | 377 | for (; oi != mObjects.end(); oi++)
|
---|
| 378 | RenderIntersectable(*oi);
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | bool
|
---|
| 382 | GlRenderer::RenderScene()
|
---|
| 383 | {
|
---|
| 384 | static int glList = -1;
|
---|
| 385 | if (mUseGlLists) {
|
---|
| 386 | if (glList == -1) {
|
---|
| 387 | glList = glGenLists(1);
|
---|
| 388 | glNewList(glList, GL_COMPILE);
|
---|
| 389 | _RenderScene();
|
---|
| 390 | glEndList();
|
---|
| 391 | }
|
---|
| 392 | glCallList(glList);
|
---|
| 393 | } else
|
---|
| 394 | _RenderScene();
|
---|
| 395 |
|
---|
| 396 |
|
---|
| 397 | return true;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 |
|
---|
[589] | 401 | void
|
---|
[997] | 402 | GlRendererBuffer::EvalQueryWithItemBuffer(
|
---|
[1001] | 403 | //RenderCostSample &sample
|
---|
[997] | 404 | )
|
---|
| 405 | {
|
---|
| 406 | // read back the texture
|
---|
| 407 | glReadPixels(0, 0,
|
---|
| 408 | GetWidth(), GetHeight(),
|
---|
| 409 | GL_RGBA,
|
---|
| 410 | GL_UNSIGNED_BYTE,
|
---|
| 411 | mPixelBuffer);
|
---|
| 412 |
|
---|
| 413 |
|
---|
| 414 | unsigned int *p = mPixelBuffer;
|
---|
| 415 |
|
---|
| 416 | for (int y = 0; y < GetHeight(); y++)
|
---|
| 417 | {
|
---|
| 418 | for (int x = 0; x < GetWidth(); x++, p++)
|
---|
| 419 | {
|
---|
| 420 | unsigned int id = (*p) & 0xFFFFFF;
|
---|
| 421 |
|
---|
| 422 | if (id != 0xFFFFFF)
|
---|
| 423 | ++ mObjects[id]->mCounter;
|
---|
| 424 | }
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[1145] | 428 |
|
---|
| 429 |
|
---|
| 430 | /****************************************************************/
|
---|
| 431 | /* GlRendererBuffer implementation */
|
---|
| 432 | /****************************************************************/
|
---|
| 433 |
|
---|
| 434 |
|
---|
| 435 |
|
---|
| 436 | GlRendererBuffer::GlRendererBuffer(SceneGraph *sceneGraph,
|
---|
| 437 | ViewCellsManager *viewcells,
|
---|
| 438 | KdTree *tree):
|
---|
| 439 | GlRenderer(sceneGraph, viewcells, tree)
|
---|
| 440 | {
|
---|
| 441 | mPixelBuffer = NULL;
|
---|
| 442 |
|
---|
| 443 | // implement width and height in subclasses
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 |
|
---|
[997] | 447 | void
|
---|
| 448 | GlRendererBuffer::EvalQueryWithOcclusionQueries(
|
---|
| 449 | //RenderCostSample &sample
|
---|
| 450 | )
|
---|
| 451 | {
|
---|
| 452 | glDepthFunc(GL_LEQUAL);
|
---|
| 453 |
|
---|
| 454 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
| 455 | glDepthMask(GL_FALSE);
|
---|
[1000] | 456 |
|
---|
| 457 |
|
---|
| 458 | // simulate detectemptyviewspace using backface culling
|
---|
[1001] | 459 | if (mDetectEmptyViewSpace)
|
---|
[997] | 460 | {
|
---|
[1000] | 461 | glEnable(GL_CULL_FACE);
|
---|
[1001] | 462 | //cout << "culling" << endl;
|
---|
[1000] | 463 | }
|
---|
| 464 | else
|
---|
| 465 | {
|
---|
[1001] | 466 | //cout << "not culling" << endl;
|
---|
[1000] | 467 | glDisable(GL_CULL_FACE);
|
---|
[1001] | 468 | }
|
---|
[1000] | 469 |
|
---|
| 470 |
|
---|
[1001] | 471 | //const int numQ = 1;
|
---|
| 472 | const int numQ = (int)mOcclusionQueries.size();
|
---|
[1000] | 473 |
|
---|
[1001] | 474 | //glFinish();
|
---|
| 475 | #if 0
|
---|
[1000] | 476 | //-- now issue queries for all objects
|
---|
| 477 | for (int j = 0; j < (int)mObjects.size(); ++ j)
|
---|
[1001] | 478 | {
|
---|
| 479 | mOcclusionQueries[j]->BeginQuery();
|
---|
| 480 | RenderIntersectable(mObjects[j]);
|
---|
| 481 | mOcclusionQueries[j]->EndQuery();
|
---|
| 482 |
|
---|
| 483 | unsigned int pixelCount;
|
---|
| 484 |
|
---|
| 485 | pixelCount = mOcclusionQueries[j]->GetQueryResult();
|
---|
| 486 | mObjects[j]->mCounter += pixelCount;
|
---|
| 487 | }
|
---|
| 488 | #else
|
---|
| 489 |
|
---|
| 490 | int q = 0;
|
---|
| 491 |
|
---|
| 492 | //-- now issue queries for all objects
|
---|
| 493 | for (int j = 0; j < (int)mObjects.size(); j += q)
|
---|
[1000] | 494 | {
|
---|
[1001] | 495 | for (q = 0; ((j + q) < (int)mObjects.size()) && (q < numQ); ++ q)
|
---|
[997] | 496 | {
|
---|
[1000] | 497 | //glFinish();
|
---|
[1001] | 498 | mOcclusionQueries[q]->BeginQuery();
|
---|
[1000] | 499 |
|
---|
[997] | 500 | RenderIntersectable(mObjects[j + q]);
|
---|
[1000] | 501 |
|
---|
[1001] | 502 | mOcclusionQueries[q]->EndQuery();
|
---|
[1000] | 503 | //glFinish();
|
---|
[997] | 504 | }
|
---|
[1001] | 505 | //cout << "q: " << q << endl;
|
---|
[997] | 506 | // collect results of the queries
|
---|
[1001] | 507 | for (int t = 0; t < q; ++ t)
|
---|
[997] | 508 | {
|
---|
| 509 | unsigned int pixelCount;
|
---|
[1001] | 510 |
|
---|
[997] | 511 | //-- reenable other state
|
---|
[1001] | 512 | #if 0
|
---|
| 513 | bool available;
|
---|
| 514 |
|
---|
[997] | 515 | do
|
---|
| 516 | {
|
---|
[1001] | 517 | available = mOcclusionQueries[t]->ResultAvailable();
|
---|
| 518 |
|
---|
| 519 | if (!available) cout << "W";
|
---|
[997] | 520 | }
|
---|
[1001] | 521 | while (!available);
|
---|
[997] | 522 | #endif
|
---|
| 523 |
|
---|
[1001] | 524 | pixelCount = mOcclusionQueries[t]->GetQueryResult();
|
---|
[997] | 525 |
|
---|
[1001] | 526 | //if (pixelCount > 0)
|
---|
| 527 | // cout <<"o="<<j+q<<" q="<<mOcclusionQueries[q]->GetQueryId()<<" pc="<<pixelCount<<" ";
|
---|
| 528 | mObjects[j + t]->mCounter += pixelCount;
|
---|
[997] | 529 | }
|
---|
| 530 |
|
---|
[1001] | 531 | //j += q;
|
---|
[997] | 532 | }
|
---|
[1001] | 533 | #endif
|
---|
| 534 | //glFinish();
|
---|
[997] | 535 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
| 536 | glDepthMask(GL_TRUE);
|
---|
[1000] | 537 |
|
---|
| 538 | glEnable(GL_CULL_FACE);
|
---|
[997] | 539 | }
|
---|
| 540 |
|
---|
| 541 |
|
---|
| 542 | void
|
---|
[589] | 543 | GlRendererBuffer::RandomViewPoint()
|
---|
[1001] | 544 | {
|
---|
[589] | 545 | // do not use this function since it could return different viewpoints for
|
---|
| 546 | // different executions of the algorithm
|
---|
| 547 |
|
---|
[1112] | 548 | // mViewCellsManager->GetViewPoint(mViewPoint);
|
---|
[589] | 549 |
|
---|
| 550 | while (1) {
|
---|
| 551 | Vector3 pVector = Vector3(halton.GetNumber(1),
|
---|
| 552 | halton.GetNumber(2),
|
---|
| 553 | halton.GetNumber(3));
|
---|
| 554 |
|
---|
[1112] | 555 | mViewPoint = mViewCellsManager->GetViewSpaceBox().GetPoint(pVector);
|
---|
[589] | 556 | ViewCell *v = mViewCellsManager->GetViewCell(mViewPoint);
|
---|
| 557 | if (v && v->GetValid())
|
---|
| 558 | break;
|
---|
| 559 | // generate a new vector
|
---|
| 560 | halton.GenerateNext();
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | Vector3 dVector = Vector3(2*M_PI*halton.GetNumber(4),
|
---|
| 564 | M_PI*halton.GetNumber(5),
|
---|
| 565 | 0.0f);
|
---|
| 566 |
|
---|
| 567 | mViewDirection = Normalize(Vector3(sin(dVector.x),
|
---|
| 568 | // cos(dVector.y),
|
---|
| 569 | 0.0f,
|
---|
| 570 | cos(dVector.x)));
|
---|
| 571 | halton.GenerateNext();
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 |
|
---|
[1585] | 575 | void
|
---|
| 576 | GlRenderer::RenderBox(const AxisAlignedBox3 &box)
|
---|
| 577 | {
|
---|
| 578 |
|
---|
| 579 | glBegin(GL_LINE_LOOP);
|
---|
| 580 | glVertex3d(box.Min().x, box.Max().y, box.Min().z );
|
---|
| 581 | glVertex3d(box.Max().x, box.Max().y, box.Min().z );
|
---|
| 582 | glVertex3d(box.Max().x, box.Min().y, box.Min().z );
|
---|
| 583 | glVertex3d(box.Min().x, box.Min().y, box.Min().z );
|
---|
| 584 | glEnd();
|
---|
| 585 |
|
---|
| 586 | glBegin(GL_LINE_LOOP);
|
---|
| 587 | glVertex3d(box.Min().x, box.Min().y, box.Max().z );
|
---|
| 588 | glVertex3d(box.Max().x, box.Min().y, box.Max().z );
|
---|
| 589 | glVertex3d(box.Max().x, box.Max().y, box.Max().z );
|
---|
| 590 | glVertex3d(box.Min().x, box.Max().y, box.Max().z );
|
---|
| 591 | glEnd();
|
---|
| 592 |
|
---|
| 593 | glBegin(GL_LINE_LOOP);
|
---|
| 594 | glVertex3d(box.Max().x, box.Min().y, box.Min().z );
|
---|
| 595 | glVertex3d(box.Max().x, box.Min().y, box.Max().z );
|
---|
| 596 | glVertex3d(box.Max().x, box.Max().y, box.Max().z );
|
---|
| 597 | glVertex3d(box.Max().x, box.Max().y, box.Min().z );
|
---|
| 598 | glEnd();
|
---|
| 599 |
|
---|
| 600 | glBegin(GL_LINE_LOOP);
|
---|
| 601 | glVertex3d(box.Min().x, box.Min().y, box.Min().z );
|
---|
| 602 | glVertex3d(box.Min().x, box.Min().y, box.Max().z );
|
---|
| 603 | glVertex3d(box.Min().x, box.Max().y, box.Max().z );
|
---|
| 604 | glVertex3d(box.Min().x, box.Max().y, box.Min().z );
|
---|
| 605 | glEnd();
|
---|
| 606 |
|
---|
| 607 | glBegin(GL_LINE_LOOP);
|
---|
| 608 | glVertex3d(box.Min().x, box.Min().y, box.Min().z );
|
---|
| 609 | glVertex3d(box.Max().x, box.Min().y, box.Min().z );
|
---|
| 610 | glVertex3d(box.Max().x, box.Min().y, box.Max().z );
|
---|
| 611 | glVertex3d(box.Min().x, box.Min().y, box.Max().z );
|
---|
| 612 | glEnd();
|
---|
| 613 |
|
---|
| 614 | glBegin(GL_LINE_LOOP);
|
---|
| 615 | glVertex3d(box.Min().x, box.Max().y, box.Min().z );
|
---|
| 616 | glVertex3d(box.Max().x, box.Max().y, box.Min().z );
|
---|
| 617 | glVertex3d(box.Max().x, box.Max().y, box.Max().z );
|
---|
| 618 | glVertex3d(box.Min().x, box.Max().y, box.Max().z );
|
---|
| 619 |
|
---|
| 620 | glEnd();
|
---|
| 621 |
|
---|
[811] | 622 | }
|
---|
[1585] | 623 |
|
---|
| 624 | void
|
---|
| 625 | GlRenderer::RenderBvhNode(BvhNode *node)
|
---|
| 626 | {
|
---|
| 627 | if (node->IsLeaf()) {
|
---|
| 628 | BvhLeaf *leaf = (BvhLeaf *) node;
|
---|
[1589] | 629 | for (int i=0; i < leaf->mObjects.size(); i++)
|
---|
| 630 | cout << "leaf obj " << i << endl;
|
---|
| 631 | //RenderIntersectable(mObjects[i]);
|
---|
[1585] | 632 | } else {
|
---|
| 633 | BvhInterior *in = (BvhInterior *)node;
|
---|
| 634 | RenderBvhNode(in->GetBack());
|
---|
| 635 | RenderBvhNode(in->GetFront());
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 | }
|
---|