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