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