[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"
|
---|
[2575] | 7 | #include "ViewCell.h"
|
---|
[589] | 8 | #include "Beam.h"
|
---|
[532] | 9 | #include "KdTree.h"
|
---|
[589] | 10 | #include "Environment.h"
|
---|
[1581] | 11 | #include "Triangle3.h"
|
---|
| 12 | #include "IntersectableWrapper.h"
|
---|
[1585] | 13 | #include "BvHierarchy.h"
|
---|
[1594] | 14 | #include "KdTree.h"
|
---|
[2587] | 15 | #include "SamplingStrategy.h"
|
---|
[2593] | 16 | #include "Preprocessor.h"
|
---|
[2600] | 17 | #include "SceneGraph.h"
|
---|
[513] | 18 |
|
---|
[2587] | 19 |
|
---|
[1990] | 20 | #ifdef USE_CG
|
---|
[1001] | 21 |
|
---|
[1990] | 22 | #include <Cg/cg.h>
|
---|
| 23 | #include <Cg/cgGL.h>
|
---|
[1001] | 24 |
|
---|
[1990] | 25 | #endif
|
---|
| 26 |
|
---|
[2613] | 27 | // if 1 = SAFE RENDERING OF PVS primitives without VBOs for Pvs error estimation
|
---|
[2620] | 28 | #define EVAL_ERROR 0
|
---|
[2613] | 29 |
|
---|
[863] | 30 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 31 |
|
---|
[516] | 32 |
|
---|
[1112] | 33 | static bool arbQuerySupport = false;
|
---|
| 34 | static bool nvQuerySupport = false;
|
---|
| 35 |
|
---|
[1925] | 36 | static GLuint frontDepthMap;
|
---|
| 37 | static GLuint backDepthMap;
|
---|
[1112] | 38 |
|
---|
[1785] | 39 | const int depthMapSize = 512;
|
---|
| 40 |
|
---|
[2538] | 41 |
|
---|
[1112] | 42 | static void InitExtensions()
|
---|
| 43 | {
|
---|
| 44 | GLenum err = glewInit();
|
---|
| 45 |
|
---|
| 46 | if (GLEW_OK != err)
|
---|
| 47 | {
|
---|
| 48 | // problem: glewInit failed, something is seriously wrong
|
---|
| 49 | cerr << "Error: " << glewGetErrorString(err) << endl;
|
---|
| 50 | exit(1);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | if (GLEW_ARB_occlusion_query)
|
---|
| 54 | arbQuerySupport = true;
|
---|
| 55 |
|
---|
| 56 | if (GLEW_NV_occlusion_query)
|
---|
| 57 | nvQuerySupport = true;
|
---|
| 58 |
|
---|
| 59 | if (!arbQuerySupport && !nvQuerySupport)
|
---|
| 60 | {
|
---|
| 61 | cout << "I require the GL_ARB_occlusion_query or the GL_NV_occlusion_query OpenGL extension to work.\n";
|
---|
| 62 | exit(1);
|
---|
| 63 | }
|
---|
[2538] | 64 |
|
---|
| 65 | if (!GLEW_ARB_vertex_buffer_object)
|
---|
| 66 | {
|
---|
| 67 | cout << "vbos not supported" << endl;
|
---|
| 68 | }
|
---|
| 69 | else
|
---|
| 70 | {
|
---|
| 71 | cout << "vbos supported" << endl;
|
---|
| 72 | }
|
---|
[1001] | 73 | }
|
---|
[589] | 74 |
|
---|
[811] | 75 |
|
---|
[589] | 76 | GlRenderer::GlRenderer(SceneGraph *sceneGraph,
|
---|
| 77 | ViewCellsManager *viewCellsManager,
|
---|
| 78 | KdTree *tree):
|
---|
[2543] | 79 | Renderer(sceneGraph, viewCellsManager),
|
---|
| 80 | mKdTree(tree),
|
---|
| 81 | mUseFalseColors(false),
|
---|
| 82 | mVboId(-1),
|
---|
| 83 | mData(NULL),
|
---|
| 84 | mIndices(NULL),
|
---|
[2560] | 85 | //mUseVbos(true),
|
---|
| 86 | mUseVbos(false),
|
---|
[2543] | 87 | mCurrentFrame(-1)
|
---|
[589] | 88 | {
|
---|
[2601] | 89 | mSceneGraph->CollectObjects(mObjects);
|
---|
[1112] | 90 |
|
---|
[2638] | 91 | #if 0
|
---|
[2543] | 92 | viewCellsManager->GetViewPoint(mViewPoint);
|
---|
| 93 | mViewDirection = Vector3(0,0,1);
|
---|
[2588] | 94 | #else
|
---|
[2638] | 95 |
|
---|
| 96 | // for sg snapshot
|
---|
| 97 | mViewPoint = Vector3(18.5133, 10.9818, -1032.24);
|
---|
| 98 | mViewDirection = Vector3(-0.5373, 0, 0.843391);
|
---|
| 99 |
|
---|
| 100 | // mViewPoint = Vector3(1213.85, 176.988, -437.364);
|
---|
| 101 | // mViewDirection = Vector3(0.433884, 0, -0.900969);
|
---|
[2619] | 102 |
|
---|
| 103 | //mViewPoint = Vector3(1099.9,183.0,-387);
|
---|
| 104 | //mViewDirection = Vector3(-0.6,0,-0.8);
|
---|
[2588] | 105 | #endif
|
---|
[2543] | 106 | mFrame = 0;
|
---|
| 107 | mWireFrame = false;
|
---|
| 108 | Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace",
|
---|
| 109 | mDetectEmptyViewSpace);
|
---|
[2613] | 110 |
|
---|
[2622] | 111 | mSnapErrorFrames = false;
|
---|
[2543] | 112 | mSnapPrefix = "snap/";
|
---|
| 113 | mUseForcedColors = false;
|
---|
| 114 | mRenderBoxes = false;
|
---|
| 115 | //mUseGlLists = true;
|
---|
| 116 | mUseGlLists = false;
|
---|
[878] | 117 |
|
---|
[2598] | 118 | if (mViewCellsManager->GetViewCellPointsList()->size())
|
---|
| 119 | mPvsStatFrames = (int)mViewCellsManager->GetViewCellPointsList()->size();
|
---|
[2543] | 120 | else
|
---|
[2611] | 121 | Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples",
|
---|
| 122 | mPvsStatFrames);
|
---|
| 123 |
|
---|
| 124 |
|
---|
[2543] | 125 | mPvsErrorBuffer.resize(mPvsStatFrames);
|
---|
| 126 | ClearErrorBuffer();
|
---|
[589] | 127 | }
|
---|
| 128 |
|
---|
[2538] | 129 |
|
---|
[589] | 130 | GlRenderer::~GlRenderer()
|
---|
| 131 | {
|
---|
[2543] | 132 | cerr<<"gl renderer destructor..\n";
|
---|
[589] | 133 |
|
---|
[2543] | 134 | CLEAR_CONTAINER(mOcclusionQueries);
|
---|
[2538] | 135 |
|
---|
[2543] | 136 | DeleteVbos();
|
---|
[2538] | 137 |
|
---|
[2543] | 138 | if (mData) delete [] mData;
|
---|
| 139 | if (mIndices) delete [] mIndices;
|
---|
| 140 |
|
---|
| 141 | glDeleteBuffersARB(1, &mVboId);
|
---|
| 142 | cerr<<"done."<<endl;
|
---|
[589] | 143 | }
|
---|
| 144 |
|
---|
[1145] | 145 |
|
---|
[2543] | 146 | void GlRenderer::RenderTriangle(TriangleIntersectable *object)
|
---|
[1581] | 147 | {
|
---|
[2609] | 148 | Triangle3 *t = &(object->GetItem());
|
---|
[2543] | 149 | glBegin(GL_TRIANGLES);
|
---|
[2575] | 150 | Vector3 normal = t->GetNormal();
|
---|
[2543] | 151 | glNormal3f(normal.x, normal.y, normal.z);
|
---|
[2575] | 152 | glVertex3f(t->mVertices[0].x, t->mVertices[0].y, t->mVertices[0].z);
|
---|
| 153 | glVertex3f(t->mVertices[1].x, t->mVertices[1].y, t->mVertices[1].z);
|
---|
| 154 | glVertex3f(t->mVertices[2].x, t->mVertices[2].y, t->mVertices[2].z);
|
---|
[2543] | 155 | glEnd();
|
---|
[1581] | 156 | }
|
---|
| 157 |
|
---|
[2543] | 158 |
|
---|
| 159 | void GlRenderer::RenderIntersectable(Intersectable *object)
|
---|
[589] | 160 | {
|
---|
[2619] | 161 | if (!object || (object->mRenderedFrame == mCurrentFrame))
|
---|
| 162 | return;
|
---|
[2050] | 163 |
|
---|
[2543] | 164 | object->mRenderedFrame = mCurrentFrame;
|
---|
[589] | 165 |
|
---|
[2543] | 166 | glPushAttrib(GL_CURRENT_BIT);
|
---|
| 167 |
|
---|
| 168 | if (mUseFalseColors)
|
---|
| 169 | SetupFalseColor(object->mId);
|
---|
| 170 |
|
---|
| 171 | switch (object->Type())
|
---|
| 172 | {
|
---|
| 173 | case Intersectable::MESH_INSTANCE:
|
---|
| 174 | RenderMeshInstance((MeshInstance *)object);
|
---|
| 175 | break;
|
---|
| 176 | case Intersectable::VIEW_CELL:
|
---|
| 177 | RenderViewCell(static_cast<ViewCell *>(object));
|
---|
| 178 | break;
|
---|
| 179 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
| 180 | RenderTransformedMeshInstance(static_cast<TransformedMeshInstance *>(object));
|
---|
| 181 | break;
|
---|
| 182 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
| 183 | RenderTriangle(static_cast<TriangleIntersectable *>(object));
|
---|
| 184 | break;
|
---|
| 185 | case Intersectable::BVH_INTERSECTABLE:
|
---|
| 186 | {
|
---|
| 187 | BvhNode *node = static_cast<BvhNode *>(object);
|
---|
| 188 |
|
---|
| 189 | if (mRenderBoxes)
|
---|
| 190 | RenderBox(node->GetBoundingBox());
|
---|
| 191 | else
|
---|
| 192 | RenderBvhNode(node);
|
---|
| 193 | break;
|
---|
| 194 | }
|
---|
| 195 | case Intersectable::KD_INTERSECTABLE:
|
---|
| 196 | {
|
---|
| 197 | KdNode *node = (static_cast<KdIntersectable *>(object))->GetItem();
|
---|
| 198 |
|
---|
| 199 | if (mRenderBoxes)
|
---|
| 200 | RenderBox(mKdTree->GetBox(node));
|
---|
| 201 | else
|
---|
| 202 | RenderKdNode(node);
|
---|
| 203 | break;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | default:
|
---|
| 207 | cerr<<"Rendering this object not yet implemented\n";
|
---|
| 208 | break;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | glPopAttrib();
|
---|
[589] | 212 | }
|
---|
| 213 |
|
---|
[2543] | 214 |
|
---|
[2593] | 215 | void GlRenderer::RenderRays(const VssRayContainer &rays, int colorType, int showDistribution, int maxAge)
|
---|
[1581] | 216 | {
|
---|
[2593] | 217 | float importance;
|
---|
[589] | 218 |
|
---|
[2543] | 219 | glBegin(GL_LINES);
|
---|
[2587] | 220 |
|
---|
[2593] | 221 | VssRayContainer::const_iterator it = rays.begin(), it_end = rays.end();
|
---|
| 222 |
|
---|
[2543] | 223 | for (; it != it_end; ++it)
|
---|
| 224 | {
|
---|
| 225 | VssRay *ray = *it;
|
---|
[2587] | 226 |
|
---|
[2591] | 227 | // only show distributions that were checked
|
---|
| 228 | if (((ray->mDistribution == SamplingStrategy::DIRECTION_BASED_DISTRIBUTION) && ((showDistribution & 1) == 0)) ||
|
---|
| 229 | ((ray->mDistribution == SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION) && ((showDistribution & 2) == 0)) ||
|
---|
| 230 | ((ray->mDistribution == SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION) && ((showDistribution & 4) == 0)) ||
|
---|
[2593] | 231 | ((ray->mDistribution == SamplingStrategy::MUTATION_BASED_DISTRIBUTION) && ((showDistribution & 8) == 0)) ||
|
---|
| 232 | ((mViewCellsManager->GetPreprocessor()->mPass - ray->mPass) >= maxAge))
|
---|
[2591] | 233 | {
|
---|
| 234 | continue;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[2587] | 237 | switch (colorType)
|
---|
| 238 | {
|
---|
| 239 | case 0:
|
---|
| 240 | glColor3f(1.0f, 0.0f, 0.0f);
|
---|
| 241 | break;
|
---|
| 242 |
|
---|
| 243 | case 1:
|
---|
| 244 | importance = 1.0f * ray->Length() / Magnitude(mViewCellsManager->GetViewSpaceBox().Diagonal());
|
---|
| 245 | glColor3f(importance, importance, importance);
|
---|
| 246 | break;
|
---|
| 247 |
|
---|
| 248 | case 2:
|
---|
| 249 | importance = log10(1e3 * ray->mPvsContribution) / 3.0f;
|
---|
| 250 | glColor3f(importance, importance, importance);
|
---|
| 251 | break;
|
---|
| 252 |
|
---|
| 253 | case 3:
|
---|
| 254 | {
|
---|
| 255 | // nested switches ok?
|
---|
| 256 | switch (ray->mDistribution)
|
---|
| 257 | {
|
---|
| 258 | case SamplingStrategy::SPATIAL_BOX_BASED_DISTRIBUTION:
|
---|
| 259 | glColor3f(1, 0, 0);
|
---|
| 260 | break;
|
---|
| 261 | case SamplingStrategy::MUTATION_BASED_DISTRIBUTION:
|
---|
| 262 | glColor3f(0, 1, 0);
|
---|
| 263 | break;
|
---|
| 264 | case SamplingStrategy::DIRECTION_BASED_DISTRIBUTION:
|
---|
| 265 | glColor3f(0, 1, 1);
|
---|
| 266 | break;
|
---|
| 267 | case SamplingStrategy::OBJECT_DIRECTION_BASED_DISTRIBUTION:
|
---|
| 268 | glColor3f(1, 1, 0);
|
---|
| 269 | break;
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[2543] | 274 | glVertex3fv(&ray->mOrigin.x);
|
---|
| 275 | glVertex3fv(&ray->mTermination.x);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | glEnd();
|
---|
[1581] | 279 | }
|
---|
| 280 |
|
---|
[1737] | 281 |
|
---|
[2543] | 282 | void GlRenderer::RenderViewCell(ViewCell *vc)
|
---|
[589] | 283 | {
|
---|
[2543] | 284 | if (vc->GetMesh())
|
---|
| 285 | {
|
---|
| 286 | if (!mUseFalseColors)
|
---|
| 287 | {
|
---|
| 288 | if (vc->GetValid())
|
---|
| 289 | glColor3f(0,1,0);
|
---|
| 290 | else
|
---|
| 291 | glColor3f(0,0,1);
|
---|
| 292 | }
|
---|
[589] | 293 |
|
---|
[2543] | 294 | RenderMesh(vc->GetMesh());
|
---|
| 295 | }
|
---|
| 296 | else
|
---|
| 297 | {
|
---|
| 298 | // render viewcells in the subtree
|
---|
| 299 | if (!vc->IsLeaf())
|
---|
| 300 | {
|
---|
| 301 | ViewCellInterior *vci = (ViewCellInterior *) vc;
|
---|
[599] | 302 |
|
---|
[2543] | 303 | ViewCellContainer::iterator it = vci->mChildren.begin();
|
---|
| 304 | for (; it != vci->mChildren.end(); ++it)
|
---|
| 305 | {
|
---|
| 306 | RenderViewCell(*it);
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | else
|
---|
| 310 | {
|
---|
| 311 | // cerr<<"Empty viewcell mesh\n";
|
---|
| 312 | }
|
---|
[599] | 313 | }
|
---|
[589] | 314 | }
|
---|
| 315 |
|
---|
[599] | 316 |
|
---|
[2530] | 317 | void GlRenderer::RenderMeshInstance(MeshInstance *mi)
|
---|
[589] | 318 | {
|
---|
[2530] | 319 | RenderMesh(mi->GetMesh());
|
---|
[589] | 320 | }
|
---|
| 321 |
|
---|
[1001] | 322 |
|
---|
[589] | 323 | void
|
---|
[1001] | 324 | GlRenderer::RenderTransformedMeshInstance(TransformedMeshInstance *mi)
|
---|
| 325 | {
|
---|
| 326 | // apply world transform before rendering
|
---|
| 327 | Matrix4x4 m;
|
---|
| 328 | mi->GetWorldTransform(m);
|
---|
| 329 |
|
---|
| 330 | glPushMatrix();
|
---|
| 331 | glMultMatrixf((float *)m.x);
|
---|
| 332 |
|
---|
| 333 | RenderMesh(mi->GetMesh());
|
---|
| 334 |
|
---|
| 335 | glPopMatrix();
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 |
|
---|
| 339 | void
|
---|
[1960] | 340 | GlRenderer::SetupFalseColor(const unsigned int id)
|
---|
[589] | 341 | {
|
---|
[1944] | 342 | // swap bits of the color
|
---|
| 343 | glColor3ub(id&255, (id>>8)&255, (id>>16)&255);
|
---|
[589] | 344 | }
|
---|
| 345 |
|
---|
| 346 |
|
---|
[1960] | 347 | unsigned int GlRenderer::GetId(const unsigned char r,
|
---|
| 348 | const unsigned char g,
|
---|
| 349 | const unsigned char b) const
|
---|
[589] | 350 | {
|
---|
| 351 | return r + (g << 8) + (b << 16);
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[1960] | 354 |
|
---|
[589] | 355 | void
|
---|
| 356 | GlRenderer::SetupMaterial(Material *m)
|
---|
| 357 | {
|
---|
| 358 | if (m)
|
---|
| 359 | glColor3fv(&(m->mDiffuseColor.r));
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[1960] | 362 |
|
---|
[2543] | 363 | void GlRenderer::RenderMesh(Mesh *mesh)
|
---|
[589] | 364 | {
|
---|
[2543] | 365 | int i = 0;
|
---|
[589] | 366 |
|
---|
[2543] | 367 | if (!mUseFalseColors && !mUseForcedColors)
|
---|
| 368 | SetupMaterial(mesh->mMaterial);
|
---|
[589] | 369 |
|
---|
[2562] | 370 | for (i = 0; i < mesh->mFaces.size(); i++)
|
---|
[2543] | 371 | {
|
---|
| 372 | if (mWireFrame)
|
---|
| 373 | glBegin(GL_LINE_LOOP);
|
---|
| 374 | else
|
---|
| 375 | glBegin(GL_POLYGON);
|
---|
| 376 |
|
---|
| 377 | Face *face = mesh->mFaces[i];
|
---|
[2614] | 378 | Vector3 normal = mesh->GetNormal(i);
|
---|
| 379 |
|
---|
| 380 | glNormal3f(normal.x, normal.y, normal.z);
|
---|
[2543] | 381 | for (int j = 0; j < face->mVertexIndices.size(); j++) {
|
---|
| 382 | glVertex3fv(&mesh->mVertices[face->mVertexIndices[j]].x);
|
---|
| 383 | }
|
---|
| 384 | glEnd();
|
---|
[589] | 385 | }
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[2572] | 388 | void GlRenderer::InitGL()
|
---|
[589] | 389 | {
|
---|
[2572] | 390 | mSphere = (GLUquadric *)gluNewQuadric();
|
---|
[589] | 391 |
|
---|
[2572] | 392 | glMatrixMode(GL_PROJECTION);
|
---|
| 393 | glLoadIdentity();
|
---|
[2562] | 394 |
|
---|
[2572] | 395 | glMatrixMode(GL_MODELVIEW);
|
---|
| 396 | glLoadIdentity();
|
---|
[746] | 397 |
|
---|
[2572] | 398 | glFrontFace(GL_CCW);
|
---|
| 399 | glCullFace(GL_BACK);
|
---|
[746] | 400 |
|
---|
[2572] | 401 | glShadeModel(GL_FLAT);
|
---|
| 402 | glDepthFunc(GL_LESS );
|
---|
| 403 | glEnable(GL_DEPTH_TEST);
|
---|
| 404 | glEnable(GL_CULL_FACE);
|
---|
[2538] | 405 |
|
---|
[2572] | 406 | InitExtensions();
|
---|
| 407 |
|
---|
| 408 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
---|
| 409 |
|
---|
| 410 | glEnable(GL_NORMALIZE);
|
---|
| 411 |
|
---|
[2604] | 412 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
---|
[2572] | 413 |
|
---|
| 414 | OcclusionQuery::GenQueries(mOcclusionQueries, 10);
|
---|
| 415 |
|
---|
[2601] | 416 | SceneGraphInterior *interior = mSceneGraph->GetRoot();
|
---|
| 417 |
|
---|
| 418 | SceneGraphNodeContainer::iterator ni = interior->mChildren.begin();
|
---|
| 419 |
|
---|
| 420 | for (; ni != interior->mChildren.end(); ni++)
|
---|
| 421 | {
|
---|
| 422 | CreateVertexArrays(static_cast<SceneGraphLeaf *>(*ni));
|
---|
| 423 | }
|
---|
[589] | 424 | }
|
---|
| 425 |
|
---|
[746] | 426 |
|
---|
[589] | 427 | void
|
---|
[811] | 428 | GlRenderer::SetupProjection(const int w, const int h, const float angle)
|
---|
[589] | 429 | {
|
---|
| 430 | glViewport(0, 0, w, h);
|
---|
| 431 | glMatrixMode(GL_PROJECTION);
|
---|
| 432 | glLoadIdentity();
|
---|
[811] | 433 | gluPerspective(angle, 1.0, 0.1, 2.0*Magnitude(mSceneGraph->GetBox().Diagonal()));
|
---|
[589] | 434 | glMatrixMode(GL_MODELVIEW);
|
---|
| 435 | }
|
---|
| 436 |
|
---|
[1581] | 437 |
|
---|
| 438 |
|
---|
[589] | 439 | void
|
---|
| 440 | GlRenderer::SetupCamera()
|
---|
| 441 | {
|
---|
[2544] | 442 | Vector3 target = mViewPoint + mViewDirection;
|
---|
| 443 |
|
---|
| 444 | Vector3 up(0,1,0);
|
---|
| 445 |
|
---|
[2582] | 446 | if (fabs(DotProd(mViewDirection, up)) > 0.99f)
|
---|
| 447 | up = Vector3(1, 0, 0);
|
---|
[2544] | 448 |
|
---|
| 449 | glLoadIdentity();
|
---|
| 450 | gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
|
---|
| 451 | target.x, target.y, target.z,
|
---|
| 452 | up.x, up.y, up.z);
|
---|
[589] | 453 | }
|
---|
| 454 |
|
---|
[2620] | 455 |
|
---|
| 456 |
|
---|
[1145] | 457 | void
|
---|
| 458 | GlRenderer::_RenderScene()
|
---|
| 459 | {
|
---|
[2619] | 460 |
|
---|
| 461 | ObjectContainer::const_iterator oi = mObjects.begin();
|
---|
| 462 | for (; oi != mObjects.end(); oi++)
|
---|
| 463 | RenderIntersectable(*oi);
|
---|
[1145] | 464 | }
|
---|
| 465 |
|
---|
[2538] | 466 | void GlRenderer::_RenderSceneTrianglesWithDrawArrays()
|
---|
[2002] | 467 | {
|
---|
[2538] | 468 | EnableDrawArrays();
|
---|
| 469 |
|
---|
| 470 | if (mUseVbos)
|
---|
| 471 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
[2002] | 472 |
|
---|
[2539] | 473 | int offset = (int)mObjects.size() * 3;
|
---|
[2538] | 474 | char *arrayPtr = mUseVbos ? NULL : (char *)mData;
|
---|
[2002] | 475 |
|
---|
[2538] | 476 | glVertexPointer(3, GL_FLOAT, 0, (char *)arrayPtr);
|
---|
| 477 | glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
|
---|
| 478 |
|
---|
[2539] | 479 | glDrawArrays(GL_TRIANGLES, 0, (int)mObjects.size() * 3);
|
---|
[2538] | 480 | //DisableDrawArrays();
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 |
|
---|
[2615] | 484 | void GlRenderer::_RenderDynamicObject(SceneGraphLeaf *leaf)
|
---|
| 485 | {
|
---|
| 486 | // apply world transform before rendering
|
---|
| 487 | Matrix4x4 m;
|
---|
| 488 | leaf->GetTransform(m);
|
---|
| 489 |
|
---|
| 490 | glPushMatrix();
|
---|
| 491 | glMultMatrixf((float *)m.x);
|
---|
| 492 |
|
---|
| 493 | glBegin(GL_TRIANGLES);
|
---|
| 494 |
|
---|
| 495 | ObjectContainer::const_iterator oi = leaf->mGeometry.begin();
|
---|
| 496 | for (; oi != leaf->mGeometry.end(); oi++)
|
---|
| 497 | {
|
---|
| 498 | TriangleIntersectable *object = (TriangleIntersectable *)*oi;
|
---|
| 499 | Triangle3 *t = &(object->GetItem());
|
---|
| 500 |
|
---|
| 501 | Vector3 normal = t->GetNormal();
|
---|
| 502 | glNormal3f(normal.x, normal.y, normal.z);
|
---|
| 503 |
|
---|
| 504 | glVertex3f(t->mVertices[0].x, t->mVertices[0].y, t->mVertices[0].z);
|
---|
| 505 | glVertex3f(t->mVertices[1].x, t->mVertices[1].y, t->mVertices[1].z);
|
---|
| 506 | glVertex3f(t->mVertices[2].x, t->mVertices[2].y, t->mVertices[2].z);
|
---|
| 507 |
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | glEnd();
|
---|
[2638] | 511 |
|
---|
| 512 | glPopMatrix();
|
---|
[2615] | 513 |
|
---|
[2638] | 514 | #if 1
|
---|
| 515 | // test the box of the object
|
---|
| 516 | AxisAlignedBox3 box = leaf->GetBox();
|
---|
| 517 | RenderBox(box);
|
---|
| 518 | #endif
|
---|
[2615] | 519 | }
|
---|
| 520 |
|
---|
| 521 |
|
---|
| 522 |
|
---|
[2538] | 523 | void GlRenderer::_RenderSceneTriangles()
|
---|
| 524 | {
|
---|
| 525 | glBegin(GL_TRIANGLES);
|
---|
| 526 |
|
---|
| 527 | ObjectContainer::const_iterator oi = mObjects.begin();
|
---|
| 528 | for (; oi != mObjects.end(); oi++) {
|
---|
| 529 |
|
---|
[2620] | 530 | if ((*oi)->Type() == Intersectable::TRIANGLE_INTERSECTABLE) {
|
---|
[2538] | 531 | TriangleIntersectable *object = (TriangleIntersectable *)*oi;
|
---|
[2575] | 532 | Triangle3 *t = &(object->GetItem());
|
---|
[2538] | 533 |
|
---|
[2575] | 534 | Vector3 normal = t->GetNormal();
|
---|
[2538] | 535 | glNormal3f(normal.x, normal.y, normal.z);
|
---|
[2575] | 536 | glVertex3f(t->mVertices[0].x, t->mVertices[0].y, t->mVertices[0].z);
|
---|
| 537 | glVertex3f(t->mVertices[1].x, t->mVertices[1].y, t->mVertices[1].z);
|
---|
| 538 | glVertex3f(t->mVertices[2].x, t->mVertices[2].y, t->mVertices[2].z);
|
---|
[2620] | 539 | }
|
---|
[2002] | 540 | }
|
---|
| 541 |
|
---|
[2538] | 542 | glEnd();
|
---|
[2002] | 543 | }
|
---|
| 544 |
|
---|
[2538] | 545 |
|
---|
[1145] | 546 | bool
|
---|
| 547 | GlRenderer::RenderScene()
|
---|
| 548 | {
|
---|
[2619] | 549 | mCurrentFrame++;
|
---|
| 550 |
|
---|
| 551 | Intersectable::NewMail();
|
---|
[2615] | 552 | #if DYNAMIC_OBJECTS_HACK
|
---|
[2609] | 553 | Preprocessor *p = mViewCellsManager->GetPreprocessor();
|
---|
| 554 | // handle dynamic objects
|
---|
| 555 | DynamicObjectsContainer::const_iterator dit, dit_end = p->mDynamicObjects.end();
|
---|
| 556 |
|
---|
| 557 | for (dit = p->mDynamicObjects.begin(); dit != dit_end; ++ dit)
|
---|
| 558 | {
|
---|
[2615] | 559 | #if USE_TRANSFORMED_MESH_INSTANCE_HACK
|
---|
| 560 | RenderIntersectable(*dit);
|
---|
| 561 | #else
|
---|
| 562 | _RenderDynamicObject(*dit);
|
---|
| 563 | #endif
|
---|
[2609] | 564 | }
|
---|
[2615] | 565 | #endif
|
---|
[2538] | 566 | #if 1
|
---|
[2600] | 567 | _RenderSceneTrianglesWithDrawArrays();
|
---|
| 568 |
|
---|
[2538] | 569 | #else
|
---|
[2600] | 570 | static int glList = -1;
|
---|
| 571 | if (mUseGlLists) {
|
---|
| 572 | if (glList == -1) {
|
---|
| 573 | glList = glGenLists(1);
|
---|
| 574 | glNewList(glList, GL_COMPILE);
|
---|
| 575 | _RenderSceneTriangles();
|
---|
| 576 | glEndList();
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | glCallList(glList);
|
---|
| 580 | } else
|
---|
| 581 | _RenderSceneTriangles();
|
---|
| 582 |
|
---|
[2538] | 583 | #endif
|
---|
[2600] | 584 | return true;
|
---|
[1145] | 585 | }
|
---|
| 586 |
|
---|
| 587 |
|
---|
[589] | 588 | void
|
---|
[2538] | 589 | GlRendererBuffer::EvalQueryWithItemBuffer()
|
---|
[997] | 590 | {
|
---|
| 591 | // read back the texture
|
---|
| 592 | glReadPixels(0, 0,
|
---|
| 593 | GetWidth(), GetHeight(),
|
---|
| 594 | GL_RGBA,
|
---|
| 595 | GL_UNSIGNED_BYTE,
|
---|
| 596 | mPixelBuffer);
|
---|
| 597 |
|
---|
| 598 |
|
---|
| 599 | unsigned int *p = mPixelBuffer;
|
---|
| 600 |
|
---|
| 601 | for (int y = 0; y < GetHeight(); y++)
|
---|
| 602 | {
|
---|
| 603 | for (int x = 0; x < GetWidth(); x++, p++)
|
---|
| 604 | {
|
---|
| 605 | unsigned int id = (*p) & 0xFFFFFF;
|
---|
| 606 |
|
---|
| 607 | if (id != 0xFFFFFF)
|
---|
[2053] | 608 | {
|
---|
[997] | 609 | ++ mObjects[id]->mCounter;
|
---|
[2053] | 610 | }
|
---|
[997] | 611 | }
|
---|
| 612 | }
|
---|
| 613 | }
|
---|
| 614 |
|
---|
[1145] | 615 |
|
---|
| 616 |
|
---|
| 617 | /****************************************************************/
|
---|
| 618 | /* GlRendererBuffer implementation */
|
---|
| 619 | /****************************************************************/
|
---|
| 620 |
|
---|
| 621 |
|
---|
| 622 |
|
---|
| 623 | GlRendererBuffer::GlRendererBuffer(SceneGraph *sceneGraph,
|
---|
| 624 | ViewCellsManager *viewcells,
|
---|
| 625 | KdTree *tree):
|
---|
| 626 | GlRenderer(sceneGraph, viewcells, tree)
|
---|
[2538] | 627 | {
|
---|
| 628 | mPixelBuffer = NULL;
|
---|
[1785] | 629 | // implement width and height in subclasses
|
---|
[1145] | 630 | }
|
---|
| 631 |
|
---|
| 632 |
|
---|
[997] | 633 | void
|
---|
| 634 | GlRendererBuffer::EvalQueryWithOcclusionQueries(
|
---|
| 635 | //RenderCostSample &sample
|
---|
| 636 | )
|
---|
| 637 | {
|
---|
| 638 | glDepthFunc(GL_LEQUAL);
|
---|
| 639 |
|
---|
| 640 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
| 641 | glDepthMask(GL_FALSE);
|
---|
[1000] | 642 |
|
---|
| 643 |
|
---|
| 644 | // simulate detectemptyviewspace using backface culling
|
---|
[1001] | 645 | if (mDetectEmptyViewSpace)
|
---|
[997] | 646 | {
|
---|
[1000] | 647 | glEnable(GL_CULL_FACE);
|
---|
[1001] | 648 | //cout << "culling" << endl;
|
---|
[1000] | 649 | }
|
---|
| 650 | else
|
---|
| 651 | {
|
---|
[1001] | 652 | //cout << "not culling" << endl;
|
---|
[1000] | 653 | glDisable(GL_CULL_FACE);
|
---|
[1001] | 654 | }
|
---|
[1000] | 655 |
|
---|
| 656 |
|
---|
[1001] | 657 | //const int numQ = 1;
|
---|
| 658 | const int numQ = (int)mOcclusionQueries.size();
|
---|
[1000] | 659 |
|
---|
[1001] | 660 | //glFinish();
|
---|
| 661 | #if 0
|
---|
[1000] | 662 | //-- now issue queries for all objects
|
---|
| 663 | for (int j = 0; j < (int)mObjects.size(); ++ j)
|
---|
[1001] | 664 | {
|
---|
| 665 | mOcclusionQueries[j]->BeginQuery();
|
---|
| 666 | RenderIntersectable(mObjects[j]);
|
---|
| 667 | mOcclusionQueries[j]->EndQuery();
|
---|
| 668 |
|
---|
| 669 | unsigned int pixelCount;
|
---|
| 670 |
|
---|
| 671 | pixelCount = mOcclusionQueries[j]->GetQueryResult();
|
---|
[2053] | 672 |
|
---|
[1001] | 673 | mObjects[j]->mCounter += pixelCount;
|
---|
| 674 | }
|
---|
| 675 | #else
|
---|
| 676 |
|
---|
| 677 | int q = 0;
|
---|
| 678 |
|
---|
| 679 | //-- now issue queries for all objects
|
---|
| 680 | for (int j = 0; j < (int)mObjects.size(); j += q)
|
---|
[1000] | 681 | {
|
---|
[1001] | 682 | for (q = 0; ((j + q) < (int)mObjects.size()) && (q < numQ); ++ q)
|
---|
[997] | 683 | {
|
---|
[1000] | 684 | //glFinish();
|
---|
[1001] | 685 | mOcclusionQueries[q]->BeginQuery();
|
---|
[1000] | 686 |
|
---|
[997] | 687 | RenderIntersectable(mObjects[j + q]);
|
---|
[1000] | 688 |
|
---|
[1001] | 689 | mOcclusionQueries[q]->EndQuery();
|
---|
[1000] | 690 | //glFinish();
|
---|
[997] | 691 | }
|
---|
[1001] | 692 | //cout << "q: " << q << endl;
|
---|
[997] | 693 | // collect results of the queries
|
---|
[1001] | 694 | for (int t = 0; t < q; ++ t)
|
---|
[997] | 695 | {
|
---|
| 696 | unsigned int pixelCount;
|
---|
[1001] | 697 |
|
---|
[997] | 698 | //-- reenable other state
|
---|
[1001] | 699 | #if 0
|
---|
| 700 | bool available;
|
---|
| 701 |
|
---|
[997] | 702 | do
|
---|
| 703 | {
|
---|
[1001] | 704 | available = mOcclusionQueries[t]->ResultAvailable();
|
---|
| 705 |
|
---|
| 706 | if (!available) cout << "W";
|
---|
[997] | 707 | }
|
---|
[1001] | 708 | while (!available);
|
---|
[997] | 709 | #endif
|
---|
| 710 |
|
---|
[1001] | 711 | pixelCount = mOcclusionQueries[t]->GetQueryResult();
|
---|
[997] | 712 |
|
---|
[1001] | 713 | //if (pixelCount > 0)
|
---|
| 714 | // cout <<"o="<<j+q<<" q="<<mOcclusionQueries[q]->GetQueryId()<<" pc="<<pixelCount<<" ";
|
---|
| 715 | mObjects[j + t]->mCounter += pixelCount;
|
---|
[2053] | 716 |
|
---|
[997] | 717 | }
|
---|
| 718 |
|
---|
[1001] | 719 | //j += q;
|
---|
[997] | 720 | }
|
---|
[1001] | 721 | #endif
|
---|
| 722 | //glFinish();
|
---|
[997] | 723 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
| 724 | glDepthMask(GL_TRUE);
|
---|
[1000] | 725 |
|
---|
| 726 | glEnable(GL_CULL_FACE);
|
---|
[997] | 727 | }
|
---|
| 728 |
|
---|
| 729 |
|
---|
| 730 | void
|
---|
[1931] | 731 | GlRenderer::RandomViewPoint()
|
---|
[1001] | 732 | {
|
---|
[589] | 733 | // do not use this function since it could return different viewpoints for
|
---|
| 734 | // different executions of the algorithm
|
---|
| 735 |
|
---|
[1112] | 736 | // mViewCellsManager->GetViewPoint(mViewPoint);
|
---|
[589] | 737 |
|
---|
| 738 | while (1) {
|
---|
| 739 | Vector3 pVector = Vector3(halton.GetNumber(1),
|
---|
| 740 | halton.GetNumber(2),
|
---|
| 741 | halton.GetNumber(3));
|
---|
| 742 |
|
---|
[1112] | 743 | mViewPoint = mViewCellsManager->GetViewSpaceBox().GetPoint(pVector);
|
---|
[589] | 744 | ViewCell *v = mViewCellsManager->GetViewCell(mViewPoint);
|
---|
| 745 | if (v && v->GetValid())
|
---|
| 746 | break;
|
---|
| 747 | // generate a new vector
|
---|
| 748 | halton.GenerateNext();
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | Vector3 dVector = Vector3(2*M_PI*halton.GetNumber(4),
|
---|
| 752 | M_PI*halton.GetNumber(5),
|
---|
| 753 | 0.0f);
|
---|
| 754 |
|
---|
| 755 | mViewDirection = Normalize(Vector3(sin(dVector.x),
|
---|
[2614] | 756 | // cos(dVector.y),
|
---|
[589] | 757 | 0.0f,
|
---|
| 758 | cos(dVector.x)));
|
---|
| 759 | halton.GenerateNext();
|
---|
| 760 | }
|
---|
| 761 |
|
---|
| 762 |
|
---|
[1585] | 763 | void
|
---|
| 764 | GlRenderer::RenderBox(const AxisAlignedBox3 &box)
|
---|
| 765 | {
|
---|
| 766 |
|
---|
| 767 | glBegin(GL_LINE_LOOP);
|
---|
| 768 | glVertex3d(box.Min().x, box.Max().y, box.Min().z );
|
---|
| 769 | glVertex3d(box.Max().x, box.Max().y, box.Min().z );
|
---|
| 770 | glVertex3d(box.Max().x, box.Min().y, box.Min().z );
|
---|
| 771 | glVertex3d(box.Min().x, box.Min().y, box.Min().z );
|
---|
| 772 | glEnd();
|
---|
| 773 |
|
---|
| 774 | glBegin(GL_LINE_LOOP);
|
---|
| 775 | glVertex3d(box.Min().x, box.Min().y, box.Max().z );
|
---|
| 776 | glVertex3d(box.Max().x, box.Min().y, box.Max().z );
|
---|
| 777 | glVertex3d(box.Max().x, box.Max().y, box.Max().z );
|
---|
| 778 | glVertex3d(box.Min().x, box.Max().y, box.Max().z );
|
---|
| 779 | glEnd();
|
---|
| 780 |
|
---|
| 781 | glBegin(GL_LINE_LOOP);
|
---|
| 782 | glVertex3d(box.Max().x, box.Min().y, box.Min().z );
|
---|
| 783 | glVertex3d(box.Max().x, box.Min().y, box.Max().z );
|
---|
| 784 | glVertex3d(box.Max().x, box.Max().y, box.Max().z );
|
---|
| 785 | glVertex3d(box.Max().x, box.Max().y, box.Min().z );
|
---|
| 786 | glEnd();
|
---|
| 787 |
|
---|
| 788 | glBegin(GL_LINE_LOOP);
|
---|
| 789 | glVertex3d(box.Min().x, box.Min().y, box.Min().z );
|
---|
| 790 | glVertex3d(box.Min().x, box.Min().y, box.Max().z );
|
---|
| 791 | glVertex3d(box.Min().x, box.Max().y, box.Max().z );
|
---|
| 792 | glVertex3d(box.Min().x, box.Max().y, box.Min().z );
|
---|
| 793 | glEnd();
|
---|
| 794 |
|
---|
| 795 | glBegin(GL_LINE_LOOP);
|
---|
| 796 | glVertex3d(box.Min().x, box.Min().y, box.Min().z );
|
---|
| 797 | glVertex3d(box.Max().x, box.Min().y, box.Min().z );
|
---|
| 798 | glVertex3d(box.Max().x, box.Min().y, box.Max().z );
|
---|
| 799 | glVertex3d(box.Min().x, box.Min().y, box.Max().z );
|
---|
| 800 | glEnd();
|
---|
| 801 |
|
---|
| 802 | glBegin(GL_LINE_LOOP);
|
---|
| 803 | glVertex3d(box.Min().x, box.Max().y, box.Min().z );
|
---|
| 804 | glVertex3d(box.Max().x, box.Max().y, box.Min().z );
|
---|
| 805 | glVertex3d(box.Max().x, box.Max().y, box.Max().z );
|
---|
| 806 | glVertex3d(box.Min().x, box.Max().y, box.Max().z );
|
---|
| 807 |
|
---|
| 808 | glEnd();
|
---|
| 809 |
|
---|
[811] | 810 | }
|
---|
[1585] | 811 |
|
---|
| 812 | void
|
---|
| 813 | GlRenderer::RenderBvhNode(BvhNode *node)
|
---|
| 814 | {
|
---|
| 815 | if (node->IsLeaf()) {
|
---|
| 816 | BvhLeaf *leaf = (BvhLeaf *) node;
|
---|
[1785] | 817 |
|
---|
| 818 | #if 0
|
---|
| 819 | if (leaf->mGlList == 0) {
|
---|
| 820 | leaf->mGlList = glGenLists(1);
|
---|
| 821 | if (leaf->mGlList != 0)
|
---|
| 822 | glNewList(leaf->mGlList, GL_COMPILE);
|
---|
| 823 |
|
---|
| 824 | for (int i=0; i < leaf->mObjects.size(); i++)
|
---|
| 825 | RenderIntersectable(leaf->mObjects[i]);
|
---|
| 826 |
|
---|
| 827 | if (leaf->mGlList != 0)
|
---|
| 828 | glEndList();
|
---|
| 829 | }
|
---|
| 830 |
|
---|
| 831 | if (leaf->mGlList != 0)
|
---|
| 832 | glCallList(leaf->mGlList);
|
---|
| 833 | #else
|
---|
[1983] | 834 | for (int i=0; i < leaf->mObjects.size(); i++)
|
---|
| 835 | RenderIntersectable(leaf->mObjects[i]);
|
---|
[1785] | 836 | #endif
|
---|
[1585] | 837 | } else {
|
---|
| 838 | BvhInterior *in = (BvhInterior *)node;
|
---|
| 839 | RenderBvhNode(in->GetBack());
|
---|
| 840 | RenderBvhNode(in->GetFront());
|
---|
| 841 | }
|
---|
[1594] | 842 | }
|
---|
| 843 |
|
---|
| 844 | void
|
---|
| 845 | GlRenderer::RenderKdNode(KdNode *node)
|
---|
| 846 | {
|
---|
[2620] | 847 | if (node->IsLeaf())
|
---|
[2538] | 848 | {
|
---|
[2613] | 849 | #if !EVAL_ERROR
|
---|
[2538] | 850 | RenderKdLeaf(static_cast<KdLeaf *>(node));
|
---|
| 851 | #else
|
---|
[2619] | 852 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
[2538] | 853 | for (int i=0; i < leaf->mObjects.size(); i++)
|
---|
[2619] | 854 | {
|
---|
[2538] | 855 | RenderIntersectable(leaf->mObjects[i]);
|
---|
[2620] | 856 |
|
---|
[2619] | 857 | }
|
---|
[2538] | 858 | #endif
|
---|
| 859 | }
|
---|
| 860 | else
|
---|
| 861 | {
|
---|
[2619] | 862 | KdInterior *inter = static_cast<KdInterior *>(node);
|
---|
| 863 | RenderKdNode(inter->mBack);
|
---|
| 864 | RenderKdNode(inter->mFront);
|
---|
[1594] | 865 | }
|
---|
[1585] | 866 | }
|
---|
| 867 |
|
---|
[1785] | 868 |
|
---|
| 869 |
|
---|
| 870 |
|
---|
| 871 | void
|
---|
| 872 | GlRendererBuffer::EvalRenderCostSample(RenderCostSample &sample,
|
---|
| 873 | const bool useOcclusionQueries,
|
---|
| 874 | const int threshold
|
---|
| 875 | )
|
---|
| 876 | {
|
---|
[2543] | 877 | // choose a random view point
|
---|
| 878 | mViewCellsManager->GetViewPoint(mViewPoint);
|
---|
| 879 | sample.mPosition = mViewPoint;
|
---|
| 880 | //cout << "viewpoint: " << mViewPoint << endl;
|
---|
| 881 |
|
---|
| 882 | // take a render cost sample by rendering a cube
|
---|
| 883 | Vector3 directions[6];
|
---|
| 884 |
|
---|
| 885 | directions[0] = Vector3(1,0,0);
|
---|
| 886 | directions[1] = Vector3(0,1,0);
|
---|
| 887 | directions[2] = Vector3(0,0,1);
|
---|
| 888 | directions[3] = Vector3(-1,0,0);
|
---|
| 889 | directions[4] = Vector3(0,-1,0);
|
---|
| 890 | directions[5] = Vector3(0,0,-1);
|
---|
| 891 |
|
---|
| 892 | sample.mVisibleObjects = 0;
|
---|
| 893 |
|
---|
| 894 | // reset object counters
|
---|
| 895 | ObjectContainer::const_iterator it, it_end = mObjects.end();
|
---|
| 896 |
|
---|
| 897 | for (it = mObjects.begin(); it != it_end; ++ it)
|
---|
[1785] | 898 | {
|
---|
[2543] | 899 | (*it)->mCounter = 0;
|
---|
| 900 |
|
---|
[1785] | 901 | }
|
---|
| 902 |
|
---|
[2543] | 903 | ++ mFrame;
|
---|
| 904 |
|
---|
| 905 | //glCullFace(GL_FRONT);
|
---|
| 906 | glCullFace(GL_BACK);
|
---|
| 907 | glDisable(GL_CULL_FACE);
|
---|
| 908 |
|
---|
| 909 |
|
---|
[1785] | 910 | // query all 6 directions for a full point sample
|
---|
[2543] | 911 | for (int i = 0; i < 6; ++ i)
|
---|
[1785] | 912 | {
|
---|
[2543] | 913 | mViewDirection = directions[i];
|
---|
| 914 | SetupCamera();
|
---|
| 915 |
|
---|
| 916 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
| 917 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 918 | //glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE);
|
---|
| 919 | glDepthFunc(GL_LESS);
|
---|
| 920 |
|
---|
| 921 | mUseFalseColors = true;
|
---|
| 922 |
|
---|
| 923 | // the actual scene rendering fills the depth (for occlusion queries)
|
---|
| 924 | // and the frame buffer (for item buffer)
|
---|
| 925 | RenderScene();
|
---|
| 926 |
|
---|
| 927 |
|
---|
| 928 | if (0)
|
---|
[1785] | 929 | {
|
---|
[2543] | 930 | char filename[256];
|
---|
| 931 | sprintf(filename, "snap/frame-%04d-%d.png", mFrame, i);
|
---|
| 932 | // QImage im = toImage();
|
---|
| 933 | // im.save(filename, "PNG");
|
---|
[1785] | 934 | }
|
---|
[2543] | 935 |
|
---|
| 936 | // evaluate the sample
|
---|
| 937 | if (useOcclusionQueries)
|
---|
[1785] | 938 | {
|
---|
[2543] | 939 | EvalQueryWithOcclusionQueries();
|
---|
[1785] | 940 | }
|
---|
[2543] | 941 | else
|
---|
[1785] | 942 | {
|
---|
[2543] | 943 | EvalQueryWithItemBuffer();
|
---|
[1785] | 944 | }
|
---|
| 945 | }
|
---|
[2543] | 946 |
|
---|
| 947 | // now evaluate the statistics over that sample
|
---|
| 948 | // currently only the number of visible objects is taken into account
|
---|
| 949 | sample.Reset();
|
---|
| 950 |
|
---|
| 951 | for (it = mObjects.begin(); it != it_end; ++ it)
|
---|
[1785] | 952 | {
|
---|
[2543] | 953 | Intersectable *obj = *it;
|
---|
| 954 | if (obj->mCounter >= threshold)
|
---|
[1785] | 955 | {
|
---|
[2543] | 956 | ++ sample.mVisibleObjects;
|
---|
| 957 | sample.mVisiblePixels += obj->mCounter;
|
---|
[1785] | 958 | }
|
---|
| 959 | }
|
---|
[2543] | 960 |
|
---|
| 961 | //cout << "RS=" << sample.mVisibleObjects << " ";
|
---|
[1585] | 962 | }
|
---|
[1785] | 963 |
|
---|
| 964 |
|
---|
| 965 | GlRendererBuffer::~GlRendererBuffer()
|
---|
| 966 | {
|
---|
[1990] | 967 | #if 0
|
---|
| 968 | #ifdef USE_CG
|
---|
| 969 | if (sCgFragmentProgram)
|
---|
[1785] | 970 | cgDestroyProgram(sCgFragmentProgram);
|
---|
| 971 | if (sCgContext)
|
---|
| 972 | cgDestroyContext(sCgContext);
|
---|
| 973 | #endif
|
---|
[1990] | 974 | #endif
|
---|
[2543] | 975 |
|
---|
[1785] | 976 | }
|
---|
| 977 |
|
---|
| 978 |
|
---|
| 979 | void
|
---|
| 980 | GlRendererBuffer::SampleRenderCost(const int numSamples,
|
---|
| 981 | vector<RenderCostSample> &samples,
|
---|
| 982 | const bool useOcclusionQueries,
|
---|
| 983 | const int threshold
|
---|
| 984 | )
|
---|
| 985 | {
|
---|
[2543] | 986 | MakeCurrent();
|
---|
[1785] | 987 |
|
---|
[2543] | 988 | if (mPixelBuffer == NULL)
|
---|
| 989 | mPixelBuffer = new unsigned int[GetWidth()*GetHeight()];
|
---|
[1785] | 990 |
|
---|
[2543] | 991 | // using 90 degree projection to capture 360 view with 6 samples
|
---|
| 992 | SetupProjection(GetHeight(), GetHeight(), 90.0f);
|
---|
[1785] | 993 |
|
---|
[2543] | 994 | //samples.resize(numSamples);
|
---|
| 995 | halton.Reset();
|
---|
[1785] | 996 |
|
---|
[2543] | 997 | // the number of queries queried in batch mode
|
---|
| 998 | const int numQ = 500;
|
---|
[1785] | 999 |
|
---|
[2543] | 1000 | //const int numQ = (int)mObjects.size();
|
---|
| 1001 | if (useOcclusionQueries)
|
---|
| 1002 | {
|
---|
| 1003 | cout << "\ngenerating " << numQ << " queries ... ";
|
---|
| 1004 | OcclusionQuery::GenQueries(mOcclusionQueries, numQ);
|
---|
| 1005 | cout << "finished" << endl;
|
---|
| 1006 | }
|
---|
| 1007 |
|
---|
| 1008 | // sampling queries
|
---|
| 1009 | for (int i = 0; i < numSamples; ++ i)
|
---|
| 1010 | {
|
---|
| 1011 | cout << ".";
|
---|
| 1012 | EvalRenderCostSample(samples[i], useOcclusionQueries, threshold);
|
---|
| 1013 | }
|
---|
| 1014 |
|
---|
| 1015 | DoneCurrent();
|
---|
[1785] | 1016 | }
|
---|
| 1017 |
|
---|
| 1018 |
|
---|
| 1019 |
|
---|
| 1020 |
|
---|
[2543] | 1021 |
|
---|
[1785] | 1022 | void
|
---|
[1931] | 1023 | GlRenderer::ClearErrorBuffer()
|
---|
[1785] | 1024 | {
|
---|
| 1025 | for (int i=0; i < mPvsStatFrames; i++) {
|
---|
| 1026 | mPvsErrorBuffer[i].mError = 1.0f;
|
---|
| 1027 | }
|
---|
[2002] | 1028 | mPvsStat.maxError = 0.0f;
|
---|
[1785] | 1029 | }
|
---|
| 1030 |
|
---|
| 1031 |
|
---|
| 1032 | void
|
---|
| 1033 | GlRendererBuffer::EvalPvsStat()
|
---|
| 1034 | {
|
---|
[2048] | 1035 | MakeCurrent();
|
---|
[2049] | 1036 |
|
---|
[2048] | 1037 | GlRenderer::EvalPvsStat();
|
---|
[2049] | 1038 |
|
---|
[2048] | 1039 | DoneCurrent();
|
---|
[1785] | 1040 | // mRenderingFinished.wakeAll();
|
---|
| 1041 | }
|
---|
| 1042 |
|
---|
| 1043 |
|
---|
[2048] | 1044 | void GlRendererBuffer::EvalPvsStat(const SimpleRayContainer &viewPoints)
|
---|
| 1045 | {
|
---|
| 1046 | MakeCurrent();
|
---|
| 1047 |
|
---|
| 1048 | GlRenderer::EvalPvsStat(viewPoints);
|
---|
| 1049 |
|
---|
| 1050 | DoneCurrent();
|
---|
| 1051 | }
|
---|
| 1052 |
|
---|
| 1053 |
|
---|
[1785] | 1054 | void GlRendererBuffer::SampleBeamContributions(Intersectable *sourceObject,
|
---|
| 1055 | Beam &beam,
|
---|
| 1056 | const int desiredSamples,
|
---|
| 1057 | BeamSampleStatistics &stat)
|
---|
| 1058 | {
|
---|
| 1059 | // TODO: should be moved out of here (not to be done every time)
|
---|
| 1060 | // only back faces are interesting for the depth pass
|
---|
| 1061 | glShadeModel(GL_FLAT);
|
---|
| 1062 | glDisable(GL_LIGHTING);
|
---|
| 1063 |
|
---|
| 1064 | // needed to kill the fragments for the front buffer
|
---|
| 1065 | glEnable(GL_ALPHA_TEST);
|
---|
| 1066 | glAlphaFunc(GL_GREATER, 0);
|
---|
| 1067 |
|
---|
| 1068 | // assumes that the beam is constructed and contains kd-tree nodes
|
---|
| 1069 | // and viewcells which it intersects
|
---|
| 1070 |
|
---|
| 1071 |
|
---|
| 1072 | // Get the number of viewpoints to be sampled
|
---|
| 1073 | // Now it is a sqrt but in general a wiser decision could be made.
|
---|
| 1074 | // The less viewpoints the better for rendering performance, since less passes
|
---|
| 1075 | // over the beam is needed.
|
---|
| 1076 | // The viewpoints could actually be generated outside of the bounding box which
|
---|
| 1077 | // would distribute the 'efective viewpoints' of the object surface and thus
|
---|
| 1078 | // with a few viewpoints better sample the viewpoint space....
|
---|
| 1079 |
|
---|
| 1080 | //TODO: comment in
|
---|
| 1081 | //int viewPointSamples = sqrt((float)desiredSamples);
|
---|
| 1082 | int viewPointSamples = max(desiredSamples / (GetWidth() * GetHeight()), 1);
|
---|
| 1083 |
|
---|
| 1084 | // the number of direction samples per pass is given by the number of viewpoints
|
---|
| 1085 | int directionalSamples = desiredSamples / viewPointSamples;
|
---|
| 1086 |
|
---|
| 1087 | Debug << "directional samples: " << directionalSamples << endl;
|
---|
| 1088 | for (int i = 0; i < viewPointSamples; ++ i)
|
---|
| 1089 | {
|
---|
| 1090 | Vector3 viewPoint = beam.mBox.GetRandomPoint();
|
---|
| 1091 |
|
---|
| 1092 | // perhaps the viewpoint should be shifted back a little bit so that it always lies
|
---|
| 1093 | // inside the source object
|
---|
| 1094 | // 'ideally' the viewpoints would be distributed on the soureObject surface, but this
|
---|
| 1095 | // would require more complicated sampling (perhaps hierarchical rejection sampling of
|
---|
| 1096 | // the object surface is an option here - only the mesh faces which are inside the box
|
---|
| 1097 | // are considered as candidates)
|
---|
| 1098 |
|
---|
| 1099 | SampleViewpointContributions(sourceObject,
|
---|
| 1100 | viewPoint,
|
---|
| 1101 | beam,
|
---|
| 1102 | directionalSamples,
|
---|
| 1103 | stat);
|
---|
| 1104 | }
|
---|
| 1105 |
|
---|
| 1106 |
|
---|
| 1107 | // note:
|
---|
| 1108 | // this routine would be called only if the number of desired samples is sufficiently
|
---|
| 1109 | // large - for other rss tree cells the cpu based sampling is perhaps more efficient
|
---|
| 1110 | // distributing the work between cpu and gpu would also allow us to place more sophisticated
|
---|
| 1111 | // sample distributions (silhouette ones) using the cpu and the jittered once on the GPU
|
---|
| 1112 | // in order that thios scheme is working well the gpu render buffer should run in a separate
|
---|
| 1113 | // thread than the cpu sampler, which would not be such a big problem....
|
---|
| 1114 |
|
---|
| 1115 | // disable alpha test again
|
---|
| 1116 | glDisable(GL_ALPHA_TEST);
|
---|
| 1117 | }
|
---|
| 1118 |
|
---|
| 1119 |
|
---|
| 1120 |
|
---|
| 1121 | void GlRendererBuffer::SampleViewpointContributions(Intersectable *sourceObject,
|
---|
| 1122 | const Vector3 viewPoint,
|
---|
| 1123 | Beam &beam,
|
---|
| 1124 | const int samples,
|
---|
| 1125 | BeamSampleStatistics &stat)
|
---|
| 1126 | {
|
---|
| 1127 | // 1. setup the view port to match the desired samples
|
---|
| 1128 | glViewport(0, 0, samples, samples);
|
---|
| 1129 |
|
---|
| 1130 | // 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox
|
---|
| 1131 | SetupProjectionForViewPoint(viewPoint, beam, sourceObject);
|
---|
| 1132 |
|
---|
| 1133 |
|
---|
| 1134 | // 3. reset z-buffer to 0 and render the source object for the beam
|
---|
| 1135 | // with glCullFace(Enabled) and glFrontFace(GL_CW)
|
---|
| 1136 | // save result to the front depth map
|
---|
| 1137 | // the front depth map holds ray origins
|
---|
| 1138 |
|
---|
| 1139 |
|
---|
| 1140 | // front depth buffer must be initialised to 0
|
---|
| 1141 | float clearDepth;
|
---|
| 1142 |
|
---|
| 1143 | glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clearDepth);
|
---|
| 1144 | glClearDepth(0.0f);
|
---|
| 1145 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
---|
| 1146 |
|
---|
| 1147 |
|
---|
| 1148 | // glFrontFace(GL_CCW);
|
---|
| 1149 | glEnable(GL_CULL_FACE);
|
---|
| 1150 | glCullFace(GL_FRONT);
|
---|
| 1151 | glColorMask(0, 0, 0, 0);
|
---|
| 1152 |
|
---|
| 1153 |
|
---|
| 1154 | // stencil is increased where the source object is located
|
---|
| 1155 | glEnable(GL_STENCIL_TEST);
|
---|
| 1156 | glStencilFunc(GL_ALWAYS, 0x1, 0x1);
|
---|
| 1157 | glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
|
---|
| 1158 |
|
---|
| 1159 |
|
---|
| 1160 | #if 0
|
---|
| 1161 | static int glSourceObjList = -1;
|
---|
| 1162 | if (glSourceObjList != -1)
|
---|
| 1163 | {
|
---|
| 1164 | glSourceObjList = glGenLists(1);
|
---|
| 1165 | glNewList(glSourceObjList, GL_COMPILE);
|
---|
| 1166 |
|
---|
| 1167 | RenderIntersectable(sourceObject);
|
---|
| 1168 |
|
---|
| 1169 | glEndList();
|
---|
| 1170 | }
|
---|
| 1171 | glCallList(glSourceObjList);
|
---|
| 1172 |
|
---|
| 1173 | #else
|
---|
| 1174 | RenderIntersectable(sourceObject);
|
---|
| 1175 |
|
---|
| 1176 | #endif
|
---|
| 1177 |
|
---|
| 1178 | // copy contents of the front depth buffer into depth texture
|
---|
| 1179 | glBindTexture(GL_TEXTURE_2D, frontDepthMap);
|
---|
| 1180 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize);
|
---|
| 1181 |
|
---|
| 1182 | // reset clear function
|
---|
| 1183 | glClearDepth(clearDepth);
|
---|
| 1184 |
|
---|
| 1185 |
|
---|
| 1186 | // 4. set up the termination depth buffer (= standard depth buffer)
|
---|
| 1187 | // only rays which have non-zero entry in the origin buffer are valid since
|
---|
| 1188 | // they realy start on the object surface (this is tagged by setting a
|
---|
| 1189 | // stencil buffer bit at step 3).
|
---|
| 1190 |
|
---|
| 1191 | glStencilFunc(GL_EQUAL, 0x1, 0x1);
|
---|
| 1192 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
|
---|
| 1193 |
|
---|
| 1194 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1195 | glDepthMask(1);
|
---|
| 1196 |
|
---|
| 1197 | glEnable(GL_DEPTH_TEST);
|
---|
| 1198 |
|
---|
| 1199 | glEnable(GL_CULL_FACE);
|
---|
| 1200 | glCullFace(GL_BACK);
|
---|
| 1201 |
|
---|
| 1202 | // setup front depth buffer
|
---|
| 1203 | glEnable(GL_TEXTURE_2D);
|
---|
| 1204 |
|
---|
[1990] | 1205 | #if 0
|
---|
| 1206 | #ifdef USE_CG
|
---|
[1785] | 1207 | // bind pixel shader implementing the front depth buffer functionality
|
---|
| 1208 | cgGLBindProgram(sCgFragmentProgram);
|
---|
| 1209 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
| 1210 | #endif
|
---|
[1990] | 1211 | #endif
|
---|
[1785] | 1212 | // 5. render all objects inside the beam
|
---|
| 1213 | // we can use id based false color to read them back for gaining the pvs
|
---|
| 1214 |
|
---|
| 1215 | glColorMask(1, 1, 1, 1);
|
---|
| 1216 |
|
---|
| 1217 |
|
---|
| 1218 | // if objects not stored in beam => extract objects
|
---|
| 1219 | if (beam.mFlags & !Beam::STORE_OBJECTS)
|
---|
| 1220 | {
|
---|
| 1221 | vector<KdNode *>::const_iterator it, it_end = beam.mKdNodes.end();
|
---|
| 1222 |
|
---|
| 1223 | Intersectable::NewMail();
|
---|
| 1224 | for (it = beam.mKdNodes.begin(); it != it_end; ++ it)
|
---|
| 1225 | {
|
---|
| 1226 | mKdTree->CollectObjects(*it, beam.mObjects);
|
---|
| 1227 | }
|
---|
| 1228 | }
|
---|
| 1229 |
|
---|
| 1230 |
|
---|
| 1231 | // (objects can be compiled to a gl list now so that subsequent rendering for
|
---|
| 1232 | // this beam is fast - the same hold for step 3)
|
---|
| 1233 | // Afterwards we have two depth buffers defining the ray origin and termination
|
---|
| 1234 |
|
---|
| 1235 |
|
---|
| 1236 | #if 0
|
---|
| 1237 | static int glObjList = -1;
|
---|
| 1238 | if (glObjList != -1)
|
---|
| 1239 | {
|
---|
| 1240 | glObjList = glGenLists(1);
|
---|
| 1241 | glNewList(glObjList, GL_COMPILE);
|
---|
| 1242 |
|
---|
| 1243 | ObjectContainer::const_iterator it, it_end = beam.mObjects.end();
|
---|
| 1244 | for (it = beam.mObjects.begin(); it != it_end; ++ it)
|
---|
| 1245 | {
|
---|
| 1246 | // render all objects except the source object
|
---|
| 1247 | if (*it != sourceObject)
|
---|
| 1248 | RenderIntersectable(*it);
|
---|
| 1249 | }
|
---|
| 1250 |
|
---|
| 1251 | glEndList();
|
---|
| 1252 | }
|
---|
| 1253 |
|
---|
| 1254 | glCallList(glObjList);
|
---|
| 1255 | #else
|
---|
| 1256 | ObjectContainer::const_iterator it, it_end = beam.mObjects.end();
|
---|
| 1257 | for (it = beam.mObjects.begin(); it != it_end; ++ it)
|
---|
| 1258 | {
|
---|
| 1259 | // render all objects except the source object
|
---|
| 1260 | if (*it != sourceObject)
|
---|
| 1261 | RenderIntersectable(*it);
|
---|
| 1262 | }
|
---|
| 1263 | #endif
|
---|
| 1264 |
|
---|
[1935] | 1265 | // 6. Use occlusion queries for all viewcell meshes associated with the beam ->
|
---|
[1785] | 1266 | // a fragment passes if the corresponding stencil fragment is set and its depth is
|
---|
| 1267 | // between origin and termination buffer
|
---|
| 1268 |
|
---|
| 1269 | // create new queries if necessary
|
---|
| 1270 | OcclusionQuery::GenQueries(mOcclusionQueries, (int)beam.mViewCells.size());
|
---|
| 1271 |
|
---|
| 1272 | // check whether any backfacing polygon would pass the depth test?
|
---|
| 1273 | // matt: should check both back /front facing because of dual depth buffer
|
---|
| 1274 | // and danger of cutting the near plane with front facing polys.
|
---|
| 1275 |
|
---|
| 1276 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
| 1277 | glDepthMask(GL_FALSE);
|
---|
| 1278 | glDisable(GL_CULL_FACE);
|
---|
| 1279 |
|
---|
| 1280 |
|
---|
| 1281 | ViewCellContainer::const_iterator vit, vit_end = beam.mViewCells.end();
|
---|
| 1282 |
|
---|
| 1283 | int queryIdx = 0;
|
---|
| 1284 |
|
---|
| 1285 | for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit)
|
---|
| 1286 | {
|
---|
| 1287 | mOcclusionQueries[queryIdx ++]->BeginQuery();
|
---|
| 1288 | RenderIntersectable(*vit);
|
---|
| 1289 | mOcclusionQueries[queryIdx]->EndQuery();
|
---|
| 1290 |
|
---|
| 1291 | ++ queryIdx;
|
---|
| 1292 | }
|
---|
| 1293 |
|
---|
| 1294 | // at this point, if possible, go and do some other computation
|
---|
| 1295 |
|
---|
| 1296 | // 7. The number of visible pixels is the number of sample rays which see the source
|
---|
| 1297 | // object from the corresponding viewcell -> remember these values for later update
|
---|
| 1298 | // of the viewcell pvs - or update immediately?
|
---|
| 1299 |
|
---|
| 1300 | queryIdx = 0;
|
---|
| 1301 |
|
---|
| 1302 | for (vit = beam.mViewCells.begin(); vit != vit_end; ++ vit)
|
---|
| 1303 | {
|
---|
| 1304 | // fetch queries
|
---|
| 1305 | unsigned int pixelCount = mOcclusionQueries[queryIdx ++]->GetQueryResult();
|
---|
| 1306 |
|
---|
| 1307 | if (pixelCount)
|
---|
| 1308 | Debug << "view cell " << (*vit)->GetId() << " visible pixels: " << pixelCount << endl;
|
---|
| 1309 | }
|
---|
| 1310 |
|
---|
| 1311 |
|
---|
| 1312 | // 8. Copmpute rendering statistics
|
---|
| 1313 | // In general it is not neccessary to remember to extract all the rays cast. I hope it
|
---|
| 1314 | // would be sufficient to gain only the intergral statistics about the new contributions
|
---|
| 1315 | // and so the rss tree would actually store no new rays (only the initial ones)
|
---|
| 1316 | // the subdivision of the tree would only be driven by the statistics (the glrender could
|
---|
| 1317 | // evaluate the contribution entropy for example)
|
---|
| 1318 | // However might be an option to extract/store only those the rays which made a contribution
|
---|
| 1319 | // (new viewcell has been discovered) or relative contribution greater than a threshold ...
|
---|
| 1320 |
|
---|
| 1321 | ObjectContainer pvsObj;
|
---|
| 1322 | stat.pvsSize = ComputePvs(beam.mObjects, pvsObj);
|
---|
| 1323 |
|
---|
| 1324 | // to gain ray source and termination
|
---|
| 1325 | // copy contents of ray termination buffer into depth texture
|
---|
| 1326 | // and compare with ray source buffer
|
---|
| 1327 | #if 0
|
---|
| 1328 | VssRayContainer rays;
|
---|
| 1329 |
|
---|
| 1330 | glBindTexture(GL_TEXTURE_2D, backDepthMap);
|
---|
| 1331 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, depthMapSize, depthMapSize);
|
---|
| 1332 |
|
---|
| 1333 | ComputeRays(Intersectable *sourceObj, rays);
|
---|
| 1334 |
|
---|
| 1335 | #endif
|
---|
| 1336 |
|
---|
[1990] | 1337 | ////////
|
---|
[1785] | 1338 | //-- cleanup
|
---|
| 1339 |
|
---|
| 1340 | // reset gl state
|
---|
| 1341 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
| 1342 | glDepthMask(GL_TRUE);
|
---|
| 1343 | glEnable(GL_CULL_FACE);
|
---|
| 1344 | glDisable(GL_STENCIL_TEST);
|
---|
[1935] | 1345 |
|
---|
[1990] | 1346 | #if 0
|
---|
| 1347 | #ifdef USE_CG
|
---|
[1785] | 1348 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
| 1349 | #endif
|
---|
[1990] | 1350 | #endif
|
---|
| 1351 |
|
---|
[1785] | 1352 | glDisable(GL_TEXTURE_2D);
|
---|
| 1353 |
|
---|
| 1354 | // remove objects from beam
|
---|
| 1355 | if (beam.mFlags & !Beam::STORE_OBJECTS)
|
---|
| 1356 | beam.mObjects.clear();
|
---|
| 1357 | }
|
---|
| 1358 |
|
---|
| 1359 |
|
---|
| 1360 | void GlRendererBuffer::SetupProjectionForViewPoint(const Vector3 &viewPoint,
|
---|
| 1361 | const Beam &beam,
|
---|
| 1362 | Intersectable *sourceObject)
|
---|
| 1363 | {
|
---|
| 1364 | float left, right, bottom, top, znear, zfar;
|
---|
| 1365 |
|
---|
| 1366 | beam.ComputePerspectiveFrustum(left, right, bottom, top, znear, zfar,
|
---|
| 1367 | mSceneGraph->GetBox());
|
---|
| 1368 |
|
---|
| 1369 | //Debug << left << " " << right << " " << bottom << " " << top << " " << znear << " " << zfar << endl;
|
---|
| 1370 | glMatrixMode(GL_PROJECTION);
|
---|
| 1371 | glLoadIdentity();
|
---|
| 1372 | glFrustum(left, right, bottom, top, znear, zfar);
|
---|
| 1373 | //glFrustum(-1, 1, -1, 1, 1, 20000);
|
---|
| 1374 |
|
---|
| 1375 | const Vector3 center = viewPoint + beam.GetMainDirection() * (zfar - znear) * 0.3f;
|
---|
| 1376 | const Vector3 up =
|
---|
| 1377 | Normalize(CrossProd(beam.mPlanes[0].mNormal, beam.mPlanes[4].mNormal));
|
---|
| 1378 |
|
---|
| 1379 | #ifdef GTP_DEBUG
|
---|
| 1380 | Debug << "view point: " << viewPoint << endl;
|
---|
| 1381 | Debug << "eye: " << center << endl;
|
---|
| 1382 | Debug << "up: " << up << endl;
|
---|
| 1383 | #endif
|
---|
| 1384 |
|
---|
| 1385 | glMatrixMode(GL_MODELVIEW);
|
---|
| 1386 | glLoadIdentity();
|
---|
| 1387 | gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
|
---|
| 1388 | center.x, center.y, center.z,
|
---|
| 1389 | up.x, up.y, up.z);
|
---|
| 1390 | }
|
---|
| 1391 |
|
---|
| 1392 |
|
---|
| 1393 | void GlRendererBuffer::InitGL()
|
---|
| 1394 | {
|
---|
[1990] | 1395 | MakeCurrent();
|
---|
| 1396 | GlRenderer::InitGL();
|
---|
[1785] | 1397 |
|
---|
| 1398 | // initialise dual depth buffer textures
|
---|
| 1399 | glGenTextures(1, &frontDepthMap);
|
---|
| 1400 | glBindTexture(GL_TEXTURE_2D, frontDepthMap);
|
---|
[2552] | 1401 |
|
---|
[1785] | 1402 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,
|
---|
[2552] | 1403 | depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
|
---|
[1785] | 1404 |
|
---|
| 1405 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 1406 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 1407 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
---|
| 1408 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
---|
| 1409 |
|
---|
| 1410 | glGenTextures(1, &backDepthMap);
|
---|
| 1411 | glBindTexture(GL_TEXTURE_2D, backDepthMap);
|
---|
[2552] | 1412 |
|
---|
[1785] | 1413 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, depthMapSize,
|
---|
[2612] | 1414 | depthMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
|
---|
[1785] | 1415 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
| 1416 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
| 1417 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
---|
| 1418 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
---|
| 1419 |
|
---|
[1990] | 1420 | #if 0
|
---|
| 1421 | #ifdef USE_CG
|
---|
| 1422 |
|
---|
[1785] | 1423 | // cg initialization
|
---|
| 1424 | cgSetErrorCallback(handleCgError);
|
---|
| 1425 | sCgContext = cgCreateContext();
|
---|
[2552] | 1426 |
|
---|
[1785] | 1427 | if (cgGLIsProfileSupported(CG_PROFILE_ARBFP1))
|
---|
| 1428 | sCgFragmentProfile = CG_PROFILE_ARBFP1;
|
---|
| 1429 | else
|
---|
| 1430 | {
|
---|
[2552] | 1431 | // try FP30
|
---|
| 1432 | if (cgGLIsProfileSupported(CG_PROFILE_FP30))
|
---|
| 1433 | sCgFragmentProfile = CG_PROFILE_FP30;
|
---|
| 1434 | else
|
---|
| 1435 | {
|
---|
| 1436 | Debug << "Neither arbfp1 or fp30 fragment profiles supported on this system" << endl;
|
---|
| 1437 | exit(1);
|
---|
| 1438 | }
|
---|
| 1439 | }
|
---|
[1785] | 1440 |
|
---|
[2552] | 1441 | sCgFragmentProgram = cgCreateProgramFromFile(sCgContext,
|
---|
| 1442 | CG_SOURCE, "../src/dual_depth.cg",
|
---|
| 1443 | sCgFragmentProfile,
|
---|
| 1444 | NULL,
|
---|
| 1445 | NULL);
|
---|
[1785] | 1446 |
|
---|
[2552] | 1447 | if (!cgIsProgramCompiled(sCgFragmentProgram))
|
---|
| 1448 | cgCompileProgram(sCgFragmentProgram);
|
---|
[1785] | 1449 |
|
---|
[2552] | 1450 | cgGLLoadProgram(sCgFragmentProgram);
|
---|
| 1451 | cgGLBindProgram(sCgFragmentProgram);
|
---|
[1785] | 1452 |
|
---|
[2552] | 1453 | Debug << "---- PROGRAM BEGIN ----\n" <<
|
---|
| 1454 | cgGetProgramString(sCgFragmentProgram, CG_COMPILED_PROGRAM) << "---- PROGRAM END ----\n";
|
---|
[1785] | 1455 |
|
---|
| 1456 | #endif
|
---|
| 1457 | #endif
|
---|
[2552] | 1458 | DoneCurrent();
|
---|
[1785] | 1459 | }
|
---|
| 1460 |
|
---|
| 1461 |
|
---|
| 1462 | void GlRendererBuffer::ComputeRays(Intersectable *sourceObj, VssRayContainer &rays)
|
---|
| 1463 | {
|
---|
| 1464 | for (int i = 0; i < depthMapSize * depthMapSize; ++ i)
|
---|
| 1465 | {
|
---|
| 1466 | //todo glGetTexImage()
|
---|
| 1467 | }
|
---|
| 1468 | }
|
---|
| 1469 |
|
---|
[2023] | 1470 | bool GlRendererBuffer::ValidViewPoint()
|
---|
[2048] | 1471 | {
|
---|
| 1472 | MakeCurrent();
|
---|
[1785] | 1473 |
|
---|
[2048] | 1474 | SetupProjection(GetWidth(), GetHeight());
|
---|
| 1475 |
|
---|
[2023] | 1476 | bool result = GlRenderer::ValidViewPoint();
|
---|
[2612] | 1477 |
|
---|
[2023] | 1478 | DoneCurrent();
|
---|
[2048] | 1479 |
|
---|
[2023] | 1480 | return result;
|
---|
| 1481 | }
|
---|
| 1482 |
|
---|
| 1483 |
|
---|
[1931] | 1484 | void
|
---|
| 1485 | GlRenderer::EvalPvsStat()
|
---|
| 1486 | {
|
---|
| 1487 | mPvsStat.Reset();
|
---|
| 1488 | halton.Reset();
|
---|
[1785] | 1489 |
|
---|
[1931] | 1490 | SetupProjection(GetWidth(), GetHeight());
|
---|
[1785] | 1491 |
|
---|
[2611] | 1492 | cout<<"Random Pvs STATS, mPvsStatFrames="<<mPvsStatFrames<<endl;
|
---|
[1931] | 1493 |
|
---|
| 1494 | for (int i=0; i < mPvsStatFrames; i++) {
|
---|
| 1495 | float err;
|
---|
| 1496 | // set frame id for saving the error buffer
|
---|
| 1497 | mFrame = i;
|
---|
[2619] | 1498 |
|
---|
[2611] | 1499 | // cerr<<"RV"<<endl;
|
---|
[1931] | 1500 | RandomViewPoint();
|
---|
[2611] | 1501 | // cerr<<"RV2"<<endl;
|
---|
[1785] | 1502 |
|
---|
[2002] | 1503 | if (mPvsErrorBuffer[i].mError == 1.0f) {
|
---|
| 1504 | // check if the view point is valid
|
---|
[2616] | 1505 | if (!ValidViewPoint()) {
|
---|
[2002] | 1506 | mPvsErrorBuffer[i].mError = -1.0f;
|
---|
[2616] | 1507 | }
|
---|
| 1508 |
|
---|
[2002] | 1509 | // manualy corrected view point
|
---|
| 1510 | if (mFrame == 5105)
|
---|
| 1511 | mPvsErrorBuffer[i].mError = -1.0f;
|
---|
| 1512 | }
|
---|
| 1513 |
|
---|
| 1514 |
|
---|
[1931] | 1515 | // atlanta problematic frames: 325 525 691 1543
|
---|
| 1516 | #if 0
|
---|
| 1517 | if (mFrame != 325 &&
|
---|
| 1518 | mFrame != 525 &&
|
---|
| 1519 | mFrame != 691 &&
|
---|
| 1520 | mFrame != 1543)
|
---|
| 1521 | mPvsErrorBuffer[i] = -1;
|
---|
| 1522 | else {
|
---|
| 1523 | Debug<<"frame ="<<mFrame<<" vp="<<mViewPoint<<" vd="<<mViewDirection<<endl;
|
---|
| 1524 | }
|
---|
| 1525 | #endif
|
---|
| 1526 |
|
---|
[2616] | 1527 |
|
---|
[2002] | 1528 | if (mPvsErrorBuffer[i].mError > 0.0f) {
|
---|
[1931] | 1529 | int pvsSize;
|
---|
[2611] | 1530 |
|
---|
[1931] | 1531 | float error = GetPixelError(pvsSize);
|
---|
[2611] | 1532 |
|
---|
[1931] | 1533 | mPvsErrorBuffer[i].mError = error;
|
---|
| 1534 | mPvsErrorBuffer[i].mPvsSize = pvsSize;
|
---|
[1785] | 1535 |
|
---|
[1931] | 1536 | // emit UpdatePvsErrorItem(i,
|
---|
| 1537 | // mPvsErrorBuffer[i]);
|
---|
[2619] | 1538 | cout<<"("<<i<<" ["<<mViewPoint<<"]["<<mViewDirection<<"] "<<mPvsErrorBuffer[i].mError<<")";
|
---|
[1931] | 1539 | // swapBuffers();
|
---|
| 1540 | }
|
---|
| 1541 |
|
---|
| 1542 | err = mPvsErrorBuffer[i].mError;
|
---|
| 1543 |
|
---|
| 1544 | if (err >= 0.0f) {
|
---|
| 1545 | if (err > mPvsStat.maxError)
|
---|
| 1546 | mPvsStat.maxError = err;
|
---|
| 1547 | mPvsStat.sumError += err;
|
---|
| 1548 | mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
|
---|
| 1549 |
|
---|
| 1550 | if (err == 0.0f)
|
---|
| 1551 | mPvsStat.errorFreeFrames++;
|
---|
| 1552 | mPvsStat.frames++;
|
---|
| 1553 | }
|
---|
| 1554 | }
|
---|
[2002] | 1555 |
|
---|
[1931] | 1556 | glFinish();
|
---|
| 1557 | cout<<endl<<flush;
|
---|
[1785] | 1558 | }
|
---|
[1931] | 1559 |
|
---|
[2048] | 1560 |
|
---|
| 1561 | void GlRenderer::EvalPvsStat(const SimpleRayContainer &viewPoints)
|
---|
| 1562 | {
|
---|
[2050] | 1563 | mPvsStat.Reset();
|
---|
[2048] | 1564 |
|
---|
[2050] | 1565 | SetupProjection(GetWidth(), GetHeight());
|
---|
| 1566 |
|
---|
| 1567 | cout << "mPvsStatFrames=" << mPvsStatFrames << endl;
|
---|
| 1568 |
|
---|
| 1569 | SimpleRayContainer::const_iterator sit, sit_end = viewPoints.end();
|
---|
| 1570 | int i = 0;
|
---|
| 1571 |
|
---|
| 1572 | for (sit = viewPoints.begin(); sit != sit_end; ++ sit, ++ i)
|
---|
[2048] | 1573 | {
|
---|
[2050] | 1574 |
|
---|
| 1575 | //cout << "view point: " << (*sit) << endl;;
|
---|
| 1576 | SimpleRay sray = *sit;
|
---|
| 1577 |
|
---|
| 1578 | float err;
|
---|
| 1579 |
|
---|
| 1580 | // set frame id for saving the error buffer
|
---|
| 1581 | mFrame = i;
|
---|
| 1582 | mViewPoint = sray.mOrigin;
|
---|
| 1583 | mViewDirection = sray.mDirection;
|
---|
| 1584 |
|
---|
| 1585 | if (mPvsErrorBuffer[i].mError > 0.0f)
|
---|
[2571] | 1586 | {
|
---|
[2050] | 1587 | int pvsSize;
|
---|
[2571] | 1588 |
|
---|
[2050] | 1589 | // compute the pixel error
|
---|
| 1590 | float error = GetPixelError(pvsSize);
|
---|
[2620] | 1591 |
|
---|
[2050] | 1592 | mPvsErrorBuffer[i].mError = error;
|
---|
| 1593 | mPvsErrorBuffer[i].mPvsSize = pvsSize;
|
---|
[2571] | 1594 |
|
---|
[2050] | 1595 | cout << "(" << i << "," << mPvsErrorBuffer[i].mError <<")";
|
---|
[2571] | 1596 | }
|
---|
| 1597 |
|
---|
[2050] | 1598 | err = mPvsErrorBuffer[i].mError;
|
---|
[2571] | 1599 |
|
---|
[2050] | 1600 | if (err >= 0.0f)
|
---|
[2571] | 1601 | {
|
---|
[2050] | 1602 | if (err > mPvsStat.maxError)
|
---|
[2571] | 1603 | mPvsStat.maxError = err;
|
---|
| 1604 |
|
---|
[2050] | 1605 | mPvsStat.sumError += err;
|
---|
[2571] | 1606 | mPvsStat.sumPvsSize += mPvsErrorBuffer[i].mPvsSize;
|
---|
| 1607 |
|
---|
| 1608 | if (err == 0.0f)
|
---|
[2050] | 1609 | ++ mPvsStat.errorFreeFrames;
|
---|
[2571] | 1610 |
|
---|
| 1611 | ++ mPvsStat.frames;
|
---|
| 1612 | }
|
---|
[2048] | 1613 | }
|
---|
[2050] | 1614 |
|
---|
| 1615 | glFinish();
|
---|
| 1616 | cout << endl << flush;
|
---|
[2048] | 1617 | }
|
---|
| 1618 |
|
---|
| 1619 |
|
---|
| 1620 |
|
---|
[2002] | 1621 | bool
|
---|
| 1622 | GlRenderer::ValidViewPoint()
|
---|
[1931] | 1623 | {
|
---|
[2543] | 1624 | //cout<<"VV4 ";
|
---|
[2048] | 1625 | if (!mDetectEmptyViewSpace)
|
---|
| 1626 | return true;
|
---|
| 1627 | //cout << "vp: " << mViewPoint << " dir: " << mViewDirection << endl;
|
---|
[1931] | 1628 |
|
---|
[2543] | 1629 | OcclusionQuery *query = mOcclusionQueries[0];
|
---|
[1931] | 1630 |
|
---|
[2543] | 1631 | // now check whether any backfacing polygon would pass the depth test
|
---|
| 1632 | SetupCamera();
|
---|
| 1633 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1634 | glEnable( GL_CULL_FACE );
|
---|
| 1635 | glCullFace(GL_BACK);
|
---|
[2002] | 1636 |
|
---|
[2543] | 1637 | //cout<<"VV1 ";
|
---|
| 1638 | RenderScene();
|
---|
| 1639 |
|
---|
| 1640 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
| 1641 | glDepthMask(GL_FALSE);
|
---|
| 1642 | glDisable( GL_CULL_FACE );
|
---|
| 1643 |
|
---|
| 1644 | query->BeginQuery();
|
---|
| 1645 |
|
---|
| 1646 | // cout<<"VV2 ";
|
---|
| 1647 | RenderScene();
|
---|
| 1648 | // cout<<"VV3 ";
|
---|
| 1649 |
|
---|
| 1650 | query->EndQuery();
|
---|
| 1651 |
|
---|
| 1652 | // at this point, if possible, go and do some other computation
|
---|
| 1653 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
| 1654 | glDepthMask(GL_TRUE);
|
---|
| 1655 | glEnable( GL_CULL_FACE );
|
---|
| 1656 |
|
---|
[2612] | 1657 | // int wait = 0;
|
---|
| 1658 | // while (!query->ResultAvailable()) {
|
---|
| 1659 | // wait++;
|
---|
| 1660 | // }
|
---|
[2543] | 1661 |
|
---|
| 1662 | // reenable other state
|
---|
| 1663 | unsigned int pixelCount = query->GetQueryResult();
|
---|
| 1664 | // cout<<"VV4 ";
|
---|
| 1665 |
|
---|
| 1666 |
|
---|
| 1667 | if (pixelCount > 0)
|
---|
| 1668 | {
|
---|
| 1669 | return false; // backfacing polygon found -> not a valid viewspace sample
|
---|
| 1670 | }
|
---|
| 1671 | return true;
|
---|
[2002] | 1672 | }
|
---|
[1931] | 1673 |
|
---|
[2543] | 1674 |
|
---|
| 1675 | float GlRenderer::GetPixelError(int &pvsSize)
|
---|
[2002] | 1676 | {
|
---|
| 1677 | return -1.0f;
|
---|
[1931] | 1678 | }
|
---|
| 1679 |
|
---|
[2543] | 1680 |
|
---|
| 1681 | void GlRenderer::RenderViewPoint()
|
---|
[1983] | 1682 | {
|
---|
[2538] | 1683 | mWireFrame = true;
|
---|
| 1684 | glPushMatrix();
|
---|
| 1685 | glTranslatef(mViewPoint.x, mViewPoint.y, mViewPoint.z);
|
---|
[2564] | 1686 | glScalef(5.0f, 5.0f, 5.0f);
|
---|
[2538] | 1687 | glPushAttrib(GL_CURRENT_BIT);
|
---|
| 1688 | glColor3f(1.0f, 0.0f, 0.0f);
|
---|
| 1689 | gluSphere((::GLUquadric *)mSphere,
|
---|
| 1690 | 1e-3*Magnitude(mViewCellsManager->GetViewSpaceBox().Size()), 6, 6);
|
---|
| 1691 | glPopAttrib();
|
---|
| 1692 | glPopMatrix();
|
---|
| 1693 | mWireFrame = false;
|
---|
[1983] | 1694 | }
|
---|
[1931] | 1695 |
|
---|
| 1696 |
|
---|
[2538] | 1697 | void GlRenderer::EnableDrawArrays()
|
---|
| 1698 | {
|
---|
[2620] | 1699 | glEnableClientState(GL_VERTEX_ARRAY);
|
---|
| 1700 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
[2538] | 1701 | }
|
---|
[1931] | 1702 |
|
---|
| 1703 |
|
---|
[2538] | 1704 | void GlRenderer::DisableDrawArrays()
|
---|
| 1705 | {
|
---|
| 1706 | glDisableClientState(GL_VERTEX_ARRAY);
|
---|
| 1707 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
| 1708 | }
|
---|
[2006] | 1709 |
|
---|
[2538] | 1710 |
|
---|
| 1711 | #if 0
|
---|
| 1712 |
|
---|
| 1713 | void GlRenderer::RenderKdLeaf(KdLeaf *leaf)
|
---|
| 1714 | {
|
---|
| 1715 | int bufferSize = 0;
|
---|
| 1716 |
|
---|
| 1717 | // count #new triangles
|
---|
| 1718 | for (size_t i = 0; i < leaf->mObjects.size(); ++ i)
|
---|
| 1719 | {
|
---|
| 1720 | TriangleIntersectable *obj = static_cast<TriangleIntersectable *>(leaf->mObjects[i]);
|
---|
| 1721 |
|
---|
| 1722 | // check if already rendered
|
---|
| 1723 | if (!obj->Mailed())
|
---|
| 1724 | bufferSize += 3;
|
---|
| 1725 | //else cout << obj->mMailbox << " " << obj->sMailId << " ";
|
---|
| 1726 | }
|
---|
| 1727 |
|
---|
| 1728 | Vector3 *vertices = new Vector3[bufferSize];
|
---|
| 1729 | Vector3 *normals = new Vector3[bufferSize];
|
---|
| 1730 |
|
---|
| 1731 | int j = 0;
|
---|
| 1732 |
|
---|
| 1733 | for (size_t i = 0; i < leaf->mObjects.size(); ++ i)
|
---|
| 1734 | {
|
---|
| 1735 | TriangleIntersectable *obj = static_cast<TriangleIntersectable *>(leaf->mObjects[i]);
|
---|
| 1736 |
|
---|
| 1737 | // check if already rendered
|
---|
| 1738 | if (obj->Mailed())
|
---|
| 1739 | continue;
|
---|
| 1740 |
|
---|
| 1741 | obj->Mail();
|
---|
| 1742 |
|
---|
| 1743 | Triangle3 tri = obj->GetItem();
|
---|
| 1744 |
|
---|
| 1745 | vertices[j * 3 + 0] = tri.mVertices[0];
|
---|
| 1746 | vertices[j * 3 + 1] = tri.mVertices[1];
|
---|
| 1747 | vertices[j * 3 + 2] = tri.mVertices[2];
|
---|
| 1748 |
|
---|
| 1749 | Vector3 n = tri.GetNormal();
|
---|
| 1750 |
|
---|
| 1751 | normals[j * 3 + 0] = n;
|
---|
| 1752 | normals[j * 3 + 1] = n;
|
---|
| 1753 | normals[j * 3 + 2] = n;
|
---|
| 1754 |
|
---|
| 1755 | ++ j;
|
---|
| 1756 | }
|
---|
| 1757 |
|
---|
| 1758 | glVertexPointer(3, GL_FLOAT, 0, vertices);
|
---|
| 1759 | glNormalPointer(GL_FLOAT, 0, normals);
|
---|
| 1760 |
|
---|
| 1761 | glDrawArrays(GL_TRIANGLES, 0, bufferSize);
|
---|
| 1762 |
|
---|
| 1763 | DEL_PTR(vertices);
|
---|
| 1764 | DEL_PTR(normals);
|
---|
[1931] | 1765 | }
|
---|
[2538] | 1766 |
|
---|
| 1767 | #else
|
---|
| 1768 |
|
---|
| 1769 | void GlRenderer::RenderKdLeaf(KdLeaf *leaf)
|
---|
| 1770 | {
|
---|
| 1771 | if (!leaf->mIndexBufferSize)
|
---|
| 1772 | return;
|
---|
| 1773 |
|
---|
[2539] | 1774 | size_t offset = mObjects.size() * 3;
|
---|
[2538] | 1775 | char *arrayPtr = mUseVbos ? NULL : (char *)mData;
|
---|
| 1776 |
|
---|
| 1777 | glVertexPointer(3, GL_FLOAT, 0, (char *)arrayPtr);
|
---|
| 1778 | glNormalPointer(GL_FLOAT, 0, (char *)arrayPtr + offset * sizeof(Vector3));
|
---|
| 1779 |
|
---|
| 1780 | glDrawElements(GL_TRIANGLES, leaf->mIndexBufferSize, GL_UNSIGNED_INT, mIndices + leaf->mIndexBufferStart);
|
---|
| 1781 | }
|
---|
| 1782 |
|
---|
| 1783 | #endif
|
---|
| 1784 |
|
---|
[2600] | 1785 |
|
---|
[2620] | 1786 |
|
---|
| 1787 | void GlRenderer::PreparePvs(const ObjectPvs &pvs)
|
---|
| 1788 | {
|
---|
| 1789 | int indexBufferSize = 0;
|
---|
| 1790 |
|
---|
| 1791 | KdNode::NewMail();
|
---|
[2633] | 1792 |
|
---|
[2620] | 1793 | ObjectPvsIterator it = pvs.GetIterator();
|
---|
| 1794 |
|
---|
| 1795 | while (it.HasMoreEntries())
|
---|
| 1796 | {
|
---|
| 1797 | Intersectable *obj = it.Next();
|
---|
| 1798 | switch (obj->Type())
|
---|
| 1799 | {
|
---|
| 1800 | case Intersectable::KD_INTERSECTABLE:
|
---|
| 1801 | {
|
---|
| 1802 | KdNode *node = static_cast<KdIntersectable *>(obj)->GetItem();
|
---|
| 1803 | _UpdatePvsIndices(node, indexBufferSize);
|
---|
| 1804 | }
|
---|
| 1805 | break;
|
---|
| 1806 |
|
---|
| 1807 | }
|
---|
| 1808 | }
|
---|
| 1809 |
|
---|
| 1810 | mIndexBufferSize = indexBufferSize;
|
---|
| 1811 | }
|
---|
| 1812 |
|
---|
| 1813 | void GlRenderer::_UpdatePvsIndices(KdNode *node, int &indexBufferSize)
|
---|
| 1814 | {
|
---|
| 1815 | if (node->Mailed())
|
---|
| 1816 | return;
|
---|
| 1817 |
|
---|
| 1818 | node->Mail();
|
---|
| 1819 |
|
---|
| 1820 | // if (mObjects.size() * 3 < indexBufferSize) cerr << "problem: " << mObjects.size() * 3 << " < " << indexBufferSize << endl;
|
---|
| 1821 | if (!node->IsLeaf())
|
---|
| 1822 | {
|
---|
| 1823 | KdInterior *kdInterior = static_cast<KdInterior *>(node);
|
---|
| 1824 |
|
---|
| 1825 | _UpdatePvsIndices(kdInterior->mBack, indexBufferSize);
|
---|
| 1826 | _UpdatePvsIndices(kdInterior->mFront, indexBufferSize);
|
---|
| 1827 | }
|
---|
| 1828 | else
|
---|
| 1829 | {
|
---|
| 1830 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
| 1831 |
|
---|
| 1832 | leaf->mIndexBufferStart = indexBufferSize;
|
---|
| 1833 |
|
---|
| 1834 | for (size_t i = 0; i < leaf->mObjects.size(); ++ i)
|
---|
| 1835 | {
|
---|
| 1836 | TriangleIntersectable *obj =
|
---|
| 1837 | static_cast<TriangleIntersectable *>(leaf->mObjects[i]);
|
---|
| 1838 |
|
---|
| 1839 | if (obj->mRenderedFrame != mCurrentFrame)
|
---|
| 1840 | {
|
---|
| 1841 | obj->mRenderedFrame = mCurrentFrame;
|
---|
| 1842 |
|
---|
| 1843 | mIndices[indexBufferSize + 0] = (obj->GetId() - 1) * 3 + 0;
|
---|
| 1844 | mIndices[indexBufferSize + 1] = (obj->GetId() - 1) * 3 + 1;
|
---|
| 1845 | mIndices[indexBufferSize + 2] = (obj->GetId() - 1) * 3 + 2;
|
---|
| 1846 |
|
---|
| 1847 | indexBufferSize += 3;
|
---|
| 1848 | }
|
---|
| 1849 | }
|
---|
| 1850 |
|
---|
| 1851 | leaf->mIndexBufferSize = indexBufferSize - leaf->mIndexBufferStart;
|
---|
| 1852 | }
|
---|
| 1853 |
|
---|
| 1854 | //cout << "id: " << indexBufferSize << endl;
|
---|
| 1855 | }
|
---|
| 1856 |
|
---|
[2600] | 1857 | void GlRenderer::CreateVertexArrays(SceneGraphLeaf *leaf)
|
---|
[2538] | 1858 | {
|
---|
[2600] | 1859 | mData = new Vector3[leaf->mGeometry.size() * 6];
|
---|
| 1860 | mIndices = new unsigned int[leaf->mGeometry.size() * 3];
|
---|
[2538] | 1861 |
|
---|
[2600] | 1862 | size_t offset = leaf->mGeometry.size() * 3;
|
---|
[2538] | 1863 |
|
---|
[2600] | 1864 | for (size_t i = 0; i < leaf->mGeometry.size(); ++ i)
|
---|
[2538] | 1865 | {
|
---|
[2600] | 1866 | TriangleIntersectable *obj = static_cast<TriangleIntersectable *>(leaf->mGeometry[i]);
|
---|
[2538] | 1867 |
|
---|
| 1868 | Triangle3 tri = obj->GetItem();
|
---|
| 1869 | const Vector3 n = tri.GetNormal();
|
---|
| 1870 |
|
---|
| 1871 | mData[i * 3 + 0] = tri.mVertices[0];
|
---|
| 1872 | mData[i * 3 + 1] = tri.mVertices[1];
|
---|
| 1873 | mData[i * 3 + 2] = tri.mVertices[2];
|
---|
| 1874 |
|
---|
| 1875 | mData[offset + i * 3 + 0] = n;
|
---|
| 1876 | mData[offset + i * 3 + 1] = n;
|
---|
| 1877 | mData[offset + i * 3 + 2] = n;
|
---|
| 1878 | }
|
---|
| 1879 |
|
---|
| 1880 | if (mUseVbos)
|
---|
| 1881 | {
|
---|
| 1882 | EnableDrawArrays();
|
---|
| 1883 |
|
---|
| 1884 | glGenBuffersARB(1, &mVboId);
|
---|
| 1885 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
| 1886 |
|
---|
| 1887 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
| 1888 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + offset * sizeof(Vector3));
|
---|
| 1889 |
|
---|
[2601] | 1890 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, leaf->mGeometry.size() * 6 * sizeof(Vector3), mData, GL_STATIC_DRAW_ARB);
|
---|
[2538] | 1891 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
|
---|
| 1892 |
|
---|
| 1893 | DisableDrawArrays();
|
---|
| 1894 |
|
---|
| 1895 | delete [] mData;
|
---|
| 1896 | }
|
---|
| 1897 |
|
---|
[2539] | 1898 | cout << "\n******** created vertex buffer objects **********" << endl;
|
---|
[2538] | 1899 | }
|
---|
| 1900 |
|
---|
| 1901 |
|
---|
| 1902 | void GlRenderer::DeleteVbos()
|
---|
| 1903 | {
|
---|
| 1904 | glDeleteBuffersARB(1, &mVboId);
|
---|
| 1905 |
|
---|
| 1906 | /*
|
---|
| 1907 | KdLeafContainer leaves;
|
---|
| 1908 | mKdTree->CollectLeaves(leaves);
|
---|
| 1909 |
|
---|
| 1910 | KdLeafContainer::const_iterator kit, kit_end = leaves.end();
|
---|
| 1911 |
|
---|
| 1912 | for (kit = leaves.begin(); kit != kit_end; ++ kit)
|
---|
| 1913 | {
|
---|
| 1914 | KdLeaf *leaf = *kit;
|
---|
| 1915 |
|
---|
| 1916 | if (leaf->mVboId == -1)
|
---|
| 1917 | {
|
---|
| 1918 | // delete old vbo
|
---|
| 1919 | glDeleteBuffersARB(1, &leaf->mVboId);
|
---|
| 1920 | leaf->mVboId = -1;
|
---|
| 1921 | }
|
---|
| 1922 | }
|
---|
| 1923 | */
|
---|
| 1924 | }
|
---|
| 1925 |
|
---|
| 1926 |
|
---|
| 1927 |
|
---|
| 1928 | }
|
---|