[1944] | 1 | #include "glInterface.h"
|
---|
| 2 | #include "GlobalLinesRenderer.h"
|
---|
| 3 | #include "common.h"
|
---|
| 4 | #include "RenderTexture.h"
|
---|
| 5 | #include "Preprocessor.h"
|
---|
| 6 | #include "GlRenderer.h"
|
---|
[1960] | 7 | #include "Exporter.h"
|
---|
[1963] | 8 | #include "ViewCellsManager.h"
|
---|
[1969] | 9 | #include "SamplingStrategy.h"
|
---|
[1960] | 10 |
|
---|
[1963] | 11 |
|
---|
[1953] | 12 | // the devil library
|
---|
| 13 | #include <IL/il.h>
|
---|
| 14 | #include <IL/ilu.h>
|
---|
| 15 | #include <IL/ilut.h>
|
---|
[1944] | 16 |
|
---|
| 17 | #include <Cg/cg.h>
|
---|
| 18 | #include <Cg/cgGL.h>
|
---|
| 19 |
|
---|
| 20 | //#include <QtOpenGL>
|
---|
| 21 |
|
---|
| 22 | namespace GtpVisibilityPreprocessor {
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | static CGcontext sCgContext = NULL;
|
---|
| 26 | static CGprogram sCgDepthPeelingProgram = NULL;
|
---|
| 27 | static CGprogram sCgPassThroughProgram = NULL;
|
---|
| 28 |
|
---|
| 29 | static CGprofile sCgFragmentProfile;
|
---|
| 30 | static CGparameter sTextureParam;
|
---|
[1964] | 31 | static CGparameter sTexWidthParam;
|
---|
| 32 | static CGparameter sStepSizeParam;
|
---|
[1944] | 33 |
|
---|
| 34 | GlobalLinesRenderer *globalLinesRenderer = NULL;
|
---|
| 35 |
|
---|
[1960] | 36 | static bool isDepth = true;
|
---|
[1953] | 37 |
|
---|
| 38 | static void InitDevIl()
|
---|
| 39 | {
|
---|
| 40 | ilInit();
|
---|
| 41 | ILuint ImageName;
|
---|
| 42 | ilGenImages(1, &ImageName);
|
---|
| 43 | ilBindImage(ImageName);
|
---|
| 44 | ilEnable(IL_FILE_OVERWRITE);
|
---|
| 45 |
|
---|
| 46 | // ilRegisterFormat(IL_RGBA);
|
---|
| 47 | // ilRegisterType(IL_FLOAT);
|
---|
| 48 |
|
---|
| 49 | // ilEnable(IL_ORIGIN_SET);
|
---|
| 50 | // ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 |
|
---|
[1944] | 54 | static void cgErrorCallback()
|
---|
| 55 | {
|
---|
| 56 | CGerror lastError = cgGetError();
|
---|
| 57 |
|
---|
| 58 | if(lastError)
|
---|
| 59 | {
|
---|
| 60 | printf("%s\n\n", cgGetErrorString(lastError));
|
---|
| 61 | printf("%s\n", cgGetLastListing(sCgContext));
|
---|
| 62 | printf("Cg error, exiting...\n");
|
---|
| 63 |
|
---|
| 64 | exit(0);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | static void PrintGLerror(char *msg)
|
---|
| 69 | {
|
---|
| 70 | GLenum errCode;
|
---|
| 71 | const GLubyte *errStr;
|
---|
| 72 |
|
---|
| 73 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
| 74 | {
|
---|
| 75 | errStr = gluErrorString(errCode);
|
---|
| 76 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | void Reshape(int w, int h)
|
---|
| 82 | {
|
---|
| 83 | if (h == 0) h = 1;
|
---|
| 84 |
|
---|
| 85 | glViewport(0, 0, w, h);
|
---|
| 86 |
|
---|
| 87 | glMatrixMode(GL_PROJECTION);
|
---|
| 88 | glLoadIdentity();
|
---|
| 89 |
|
---|
[1949] | 90 | //gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 3, 5000.0);
|
---|
[1944] | 91 | //gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.5, 10.0);
|
---|
[1951] | 92 | glOrtho(-1, 1, -1, 1, 0.5, 15);
|
---|
[1944] | 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
[1960] | 96 | void SetFrustum(const int sizeX, const int sizeY,
|
---|
| 97 | const float nearPlane, const float farPlane)
|
---|
[1953] | 98 | {
|
---|
| 99 | glMatrixMode(GL_PROJECTION);
|
---|
| 100 | glLoadIdentity();
|
---|
| 101 |
|
---|
[1960] | 102 | glOrtho(-sizeX * 0.5, sizeX * 0.5,
|
---|
| 103 | -sizeY * 0.5, sizeY * 0.5,
|
---|
[1953] | 104 | nearPlane, farPlane);
|
---|
| 105 |
|
---|
| 106 | /*glOrtho(0, sizeX,
|
---|
| 107 | 0, sizeY ,
|
---|
| 108 | nearPlane, farPlane);*/
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[1944] | 111 | void Display()
|
---|
| 112 | {
|
---|
| 113 | //globalLinesRenderer->DrawGeometry();
|
---|
[1958] | 114 | //globalLinesRenderer->CastGlobalLines(Beam(), 0);
|
---|
[1960] | 115 | globalLinesRenderer->DisplayBuffer(isDepth);
|
---|
[1944] | 116 | PrintGLerror("display");
|
---|
| 117 |
|
---|
| 118 | glutSwapBuffers();
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 | void Idle()
|
---|
| 123 | {
|
---|
| 124 | glutPostRedisplay();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | void Keyboard(unsigned char key, int x, int y)
|
---|
| 129 | {
|
---|
| 130 | switch(key)
|
---|
| 131 | {
|
---|
[1949] | 132 | case '2':
|
---|
[1958] | 133 | {
|
---|
| 134 | VssRayContainer rays;
|
---|
| 135 |
|
---|
| 136 | ++ globalLinesRenderer->mMaxDepth;
|
---|
[1964] | 137 | globalLinesRenderer->ApplyDepthPeeling(rays);
|
---|
[1958] | 138 |
|
---|
| 139 | cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
|
---|
[1960] | 140 | CLEAR_CONTAINER(rays);
|
---|
[1958] | 141 | return;
|
---|
| 142 | }
|
---|
[1949] | 143 | case '1':
|
---|
[1958] | 144 | {
|
---|
| 145 | VssRayContainer rays;
|
---|
| 146 |
|
---|
| 147 | -- globalLinesRenderer->mMaxDepth;
|
---|
| 148 | cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
|
---|
| 149 |
|
---|
[1964] | 150 | globalLinesRenderer->ApplyDepthPeeling(rays);
|
---|
[1960] | 151 | CLEAR_CONTAINER(rays);
|
---|
[1958] | 152 | return;
|
---|
| 153 | }
|
---|
[1953] | 154 | case '3':
|
---|
[1958] | 155 | {
|
---|
| 156 | globalLinesRenderer->ExportDepthBuffer();
|
---|
| 157 | return;
|
---|
| 158 | }
|
---|
| 159 | case '4':
|
---|
| 160 | {
|
---|
[1960] | 161 | globalLinesRenderer->ExportItemBuffer();
|
---|
| 162 | return;
|
---|
| 163 | }
|
---|
[1969] | 164 | case '5':
|
---|
| 165 | {
|
---|
| 166 | VssRayContainer rays;
|
---|
| 167 |
|
---|
| 168 | HwGlobalLinesDistribution glStrategy(*globalLinesRenderer->mPreprocessor);
|
---|
| 169 |
|
---|
| 170 | SimpleRayContainer simpleRays;
|
---|
| 171 | glStrategy.GenerateSamples(5, simpleRays);
|
---|
| 172 |
|
---|
| 173 | //cout << "simple ray: " << simpleRays[4] << endl;
|
---|
| 174 | globalLinesRenderer->CastGlobalLines(simpleRays[1], rays);
|
---|
| 175 |
|
---|
| 176 | // visualize
|
---|
| 177 | VssRayContainer outRays;
|
---|
| 178 | VssRayContainer::const_iterator vit, vit_end = rays.end();
|
---|
| 179 |
|
---|
| 180 | const float p = 8.0f / (float)rays.size();
|
---|
| 181 |
|
---|
| 182 | for (vit = rays.begin(); vit != vit_end; ++ vit)
|
---|
| 183 | {
|
---|
| 184 | if (Random(1.0f) < p)
|
---|
| 185 | {
|
---|
| 186 | outRays.push_back(*vit);
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | globalLinesRenderer->Visualize(rays);
|
---|
| 191 |
|
---|
| 192 | CLEAR_CONTAINER(rays);
|
---|
| 193 | return;
|
---|
| 194 | }
|
---|
[1960] | 195 | case '8':
|
---|
| 196 | {
|
---|
| 197 | isDepth = !isDepth;
|
---|
| 198 | return;
|
---|
| 199 | }
|
---|
| 200 | case '9':
|
---|
| 201 | {
|
---|
[1958] | 202 | VssRayContainer rays;
|
---|
[1964] | 203 | globalLinesRenderer->ApplyDepthPeeling(rays);
|
---|
[1969] | 204 |
|
---|
| 205 | // visualize
|
---|
[1960] | 206 | VssRayContainer outRays;
|
---|
| 207 | VssRayContainer::const_iterator vit, vit_end = rays.end();
|
---|
[1958] | 208 |
|
---|
[1960] | 209 | const float p = 8.0f / (float)rays.size();
|
---|
| 210 |
|
---|
| 211 | for (vit = rays.begin(); vit != vit_end; ++ vit)
|
---|
| 212 | {
|
---|
| 213 | if (Random(1.0f) < p)
|
---|
| 214 | {
|
---|
| 215 | outRays.push_back(*vit);
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | globalLinesRenderer->Visualize(rays);
|
---|
| 220 | CLEAR_CONTAINER(rays);
|
---|
| 221 | return;
|
---|
| 222 | }
|
---|
| 223 | case '0':
|
---|
| 224 | {
|
---|
| 225 | VssRayContainer rays;
|
---|
[1964] | 226 | globalLinesRenderer->ApplyDepthPeeling(rays);
|
---|
[1960] | 227 | CLEAR_CONTAINER(rays);
|
---|
[1958] | 228 | return;
|
---|
| 229 | }
|
---|
[1944] | 230 | default:
|
---|
| 231 | return;
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[1953] | 235 |
|
---|
[1949] | 236 | GlobalLinesRenderer::GlobalLinesRenderer(Preprocessor *preprocessor,
|
---|
[1968] | 237 | const int texHeight,
|
---|
| 238 | const int texWidth,
|
---|
| 239 | const float eps,
|
---|
| 240 | const int maxDepth,
|
---|
| 241 | const bool sampleReverse):
|
---|
[1953] | 242 | mNewTexture(NULL),
|
---|
| 243 | mOldTexture(NULL),
|
---|
[1964] | 244 | mPreprocessor(preprocessor),
|
---|
[1969] | 245 | mTexHeight(texHeight),
|
---|
| 246 | mTexWidth(texWidth),
|
---|
[1968] | 247 | mEpsilon(eps),
|
---|
| 248 | mMaxDepth(maxDepth),
|
---|
| 249 | mSampleReverse(sampleReverse)
|
---|
[1944] | 250 | {
|
---|
[1968] | 251 | mRenderer = new GlRenderer(mPreprocessor->mSceneGraph,
|
---|
| 252 | mPreprocessor->mViewCellsManager,
|
---|
| 253 | mPreprocessor->mKdTree);
|
---|
| 254 |
|
---|
[1944] | 255 | }
|
---|
| 256 |
|
---|
| 257 |
|
---|
[1968] | 258 | GlobalLinesRenderer::GlobalLinesRenderer(Preprocessor *preprocessor):
|
---|
[1964] | 259 | mNewTexture(NULL),
|
---|
| 260 | mOldTexture(NULL),
|
---|
[1968] | 261 | mMaxDepth(40),
|
---|
[1964] | 262 | mPreprocessor(preprocessor),
|
---|
| 263 | mTexHeight(128),
|
---|
| 264 | mTexWidth(128),
|
---|
[1968] | 265 | mEpsilon(0.0001),
|
---|
| 266 | mSampleReverse(true)
|
---|
| 267 | {
|
---|
| 268 | mRenderer = new GlRenderer(mPreprocessor->mSceneGraph,
|
---|
| 269 | mPreprocessor->mViewCellsManager,
|
---|
| 270 | mPreprocessor->mKdTree);
|
---|
| 271 | }
|
---|
[1964] | 272 |
|
---|
| 273 |
|
---|
[1960] | 274 | void GlobalLinesRenderer::DisplayBuffer(const bool isDepth)
|
---|
[1958] | 275 | {
|
---|
[1960] | 276 | if (!isDepth)
|
---|
| 277 | mNewTexture->Bind();
|
---|
| 278 | else
|
---|
| 279 | mNewTexture->BindDepth();
|
---|
[1970] | 280 |
|
---|
[1958] | 281 | mNewTexture->EnableTextureTarget();
|
---|
| 282 |
|
---|
| 283 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 284 |
|
---|
| 285 | if (mNewTexture->IsRectangleTexture())
|
---|
| 286 | {
|
---|
| 287 | glBegin(GL_QUADS);
|
---|
| 288 | glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
|
---|
| 289 | glTexCoord2f(mNewTexture->GetWidth(), 0); glVertex3f( 1, -1, -0.5f);
|
---|
| 290 | glTexCoord2f(mNewTexture->GetWidth(), mNewTexture->GetHeight()); glVertex3f( 1, 1, -0.5f);
|
---|
| 291 | glTexCoord2f(0, mNewTexture->GetHeight()); glVertex3f(-1, 1, -0.5f);
|
---|
| 292 | glEnd();
|
---|
| 293 | }
|
---|
| 294 | else
|
---|
| 295 | {
|
---|
| 296 | glBegin(GL_QUADS);
|
---|
| 297 | glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
|
---|
| 298 | glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f);
|
---|
| 299 | glTexCoord2f(1, 1); glVertex3f( 1, 1, -0.5f);
|
---|
| 300 | glTexCoord2f(0, 1); glVertex3f(-1, 1, -0.5f);
|
---|
| 301 | glEnd();
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | mNewTexture->DisableTextureTarget();
|
---|
| 305 | PrintGLerror("displaytexture");
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 |
|
---|
[1944] | 309 | GlobalLinesRenderer::~GlobalLinesRenderer()
|
---|
| 310 | {
|
---|
| 311 | if (sCgDepthPeelingProgram)
|
---|
| 312 | cgDestroyProgram(sCgDepthPeelingProgram);
|
---|
| 313 | if (sCgContext)
|
---|
| 314 | cgDestroyContext(sCgContext);
|
---|
[1958] | 315 |
|
---|
| 316 | // init the receiving buffers
|
---|
| 317 | delete mNewDepthBuffer;
|
---|
| 318 | delete mOldDepthBuffer;
|
---|
| 319 |
|
---|
| 320 | delete mNewItemBuffer;
|
---|
| 321 | delete mOldItemBuffer;
|
---|
[1968] | 322 |
|
---|
| 323 | DEL_PTR(mRenderer);
|
---|
[1944] | 324 | }
|
---|
| 325 |
|
---|
| 326 |
|
---|
[1968] | 327 | void GlobalLinesRenderer::InitRenderTexture(RenderTexture *rt)
|
---|
| 328 | {
|
---|
| 329 | // setup the rendering context for the RenderTexture
|
---|
| 330 | rt->BeginCapture();
|
---|
| 331 | {
|
---|
| 332 | //Reshape(mTexWidth, mTexHeight);
|
---|
| 333 | glViewport(0, 0, mTexWidth, mTexHeight);
|
---|
| 334 | SetFrustum(mWidth, mWidth, mNear, mFar);
|
---|
| 335 |
|
---|
| 336 | // for item buffer: white means no color
|
---|
| 337 | glClearColor(1, 1, 1, 1);
|
---|
| 338 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 339 |
|
---|
| 340 | glFrontFace(GL_CCW);
|
---|
| 341 | glCullFace(GL_BACK);
|
---|
| 342 |
|
---|
| 343 | glDisable(GL_CULL_FACE);
|
---|
| 344 | //glEnable(GL_CULL_FACE);
|
---|
| 345 |
|
---|
| 346 | glShadeModel(GL_FLAT);
|
---|
| 347 | glEnable(GL_DEPTH_TEST);
|
---|
| 348 |
|
---|
| 349 | glMatrixMode(GL_MODELVIEW);
|
---|
| 350 | glLoadIdentity();
|
---|
| 351 | gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
|
---|
| 352 | mTermination.x, mTermination.y, mTermination.z,
|
---|
| 353 | mUpVec.x, mUpVec.y, mUpVec.z);
|
---|
| 354 |
|
---|
| 355 | }
|
---|
| 356 | rt->EndCapture();
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 |
|
---|
[1964] | 360 | void GlobalLinesRenderer::InitScene(const float alpha, const float beta)
|
---|
[1944] | 361 | {
|
---|
[1968] | 362 | AxisAlignedBox3 bbox =
|
---|
| 363 | globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
|
---|
[1970] | 364 |
|
---|
[1964] | 365 | const float sceneSize = Magnitude(bbox.Diagonal());
|
---|
[1958] | 366 |
|
---|
[1964] | 367 | // compute the center of the scene
|
---|
[1968] | 368 | mTermination = bbox.Center();
|
---|
[1964] | 369 |
|
---|
| 370 | // add a small offset to provide some randomness in the sampling
|
---|
[1968] | 371 | if (0)
|
---|
| 372 | {
|
---|
| 373 | Vector3 offset(
|
---|
| 374 | Random(sceneSize * 1e-3f),
|
---|
| 375 | Random(sceneSize * 1e-3f),
|
---|
| 376 | Random(sceneSize * 1e-3f));
|
---|
[1964] | 377 |
|
---|
[1968] | 378 | mTermination += offset;
|
---|
| 379 | }
|
---|
[1964] | 380 |
|
---|
| 381 | mNear = 1;
|
---|
| 382 | mFar = sceneSize * 2;
|
---|
| 383 | mWidth = sceneSize;
|
---|
| 384 |
|
---|
| 385 | ComputeLookAt(alpha,
|
---|
| 386 | beta,
|
---|
| 387 | mEyeVec,
|
---|
| 388 | mUpVec,
|
---|
| 389 | mLeftVec);
|
---|
| 390 |
|
---|
[1968] | 391 | mViewPoint = mTermination - 0.5f * sceneSize * mEyeVec;
|
---|
[1964] | 392 |
|
---|
[1969] | 393 | //cout << "termination point: " << mTermination << endl;
|
---|
| 394 | //cout << "view point: " << mViewPoint << endl;
|
---|
| 395 | //cout << "scene: " << bbox << endl;
|
---|
[1964] | 396 |
|
---|
[1968] | 397 | InitRenderTexture(mNewTexture);
|
---|
| 398 | InitRenderTexture(mOldTexture);
|
---|
| 399 |
|
---|
[1969] | 400 | //cout << "eye: " << mEyeVec << " left: " << mLeftVec << " up: " << mUpVec << endl;
|
---|
[1968] | 401 | }
|
---|
[1964] | 402 |
|
---|
| 403 |
|
---|
[1968] | 404 | void GlobalLinesRenderer::InitScene(const SimpleRay &ray)
|
---|
| 405 | {
|
---|
| 406 | AxisAlignedBox3 bbox =
|
---|
| 407 | globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
|
---|
| 408 |
|
---|
| 409 | const float sceneSize = Magnitude(bbox.Diagonal());
|
---|
[1964] | 410 |
|
---|
[1968] | 411 | // compute the center of the scene
|
---|
| 412 | mViewPoint = ray.mOrigin;
|
---|
| 413 | mTermination = ray.mOrigin + ray.mDirection;
|
---|
[1964] | 414 |
|
---|
[1968] | 415 | mEyeVec = Normalize(ray.mDirection);
|
---|
[1964] | 416 |
|
---|
[1968] | 417 | mNear = 1;
|
---|
| 418 | mFar = sceneSize * 2;
|
---|
| 419 | mWidth = sceneSize;
|
---|
[1964] | 420 |
|
---|
[1968] | 421 | mEyeVec.RightHandedBase(mUpVec, mLeftVec);
|
---|
[1964] | 422 |
|
---|
[1969] | 423 | //cout << "termination point: " << mTermination << endl;
|
---|
| 424 | //cout << "view point: " << mViewPoint << endl;
|
---|
| 425 | //cout << "scene: " << bbox << endl;
|
---|
[1964] | 426 |
|
---|
[1968] | 427 | InitRenderTexture(mNewTexture);
|
---|
| 428 | InitRenderTexture(mOldTexture);
|
---|
| 429 |
|
---|
[1969] | 430 | //cout << "eye: " << mEyeVec << " left: " << mLeftVec << " up: " << mUpVec << endl;
|
---|
[1964] | 431 | }
|
---|
| 432 |
|
---|
| 433 |
|
---|
[1969] | 434 | int GlobalLinesRenderer::CastGlobalLines(const float alpha,
|
---|
[1964] | 435 | const float beta,
|
---|
| 436 | VssRayContainer &rays)
|
---|
| 437 | {
|
---|
| 438 | InitScene(alpha, beta);
|
---|
| 439 |
|
---|
[1944] | 440 | // bind pixel shader implementing the front depth buffer functionality
|
---|
[1969] | 441 | const int layers = ApplyDepthPeeling(rays);
|
---|
| 442 |
|
---|
| 443 | return layers;
|
---|
[1944] | 444 | }
|
---|
| 445 |
|
---|
| 446 |
|
---|
[1969] | 447 | int GlobalLinesRenderer::CastGlobalLines(const SimpleRay &ray,
|
---|
[1968] | 448 | VssRayContainer &rays)
|
---|
| 449 | {
|
---|
[1969] | 450 | const long startTime = GetTime();
|
---|
| 451 | cout << "casting global lines ... " << endl;
|
---|
| 452 |
|
---|
[1968] | 453 | InitScene(ray);
|
---|
| 454 |
|
---|
| 455 | // bind pixel shader implementing the front depth buffer functionality
|
---|
[1969] | 456 | const int layers = ApplyDepthPeeling(rays);
|
---|
[1968] | 457 |
|
---|
[1972] | 458 | const float rays_per_sec = (float)rays.size() / (TimeDiff(startTime, GetTime()) * 1e-3f);
|
---|
| 459 | cout << "cast " << rays.size() << " samples in " << layers << " layers in "
|
---|
| 460 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs (" << rays_per_sec << " rays/sec)" << endl;
|
---|
| 461 |
|
---|
[1969] | 462 | return layers;
|
---|
[1944] | 463 | }
|
---|
| 464 |
|
---|
| 465 |
|
---|
| 466 | void GlobalLinesRenderer::DrawGeometry()
|
---|
[1945] | 467 | {
|
---|
[1951] | 468 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 469 | glMatrixMode(GL_MODELVIEW);
|
---|
[1944] | 470 |
|
---|
[1951] | 471 | glPushMatrix();
|
---|
| 472 | {
|
---|
[1953] | 473 | //glLoadIdentity();
|
---|
[1951] | 474 | ObjectContainer::const_iterator oit, oit_end = mPreprocessor->mObjects.end();
|
---|
| 475 |
|
---|
| 476 | Intersectable::NewMail();
|
---|
[1969] | 477 |
|
---|
| 478 | mRenderer->RenderScene();
|
---|
[1944] | 479 | }
|
---|
[1951] | 480 | glPopMatrix();
|
---|
[1944] | 481 | }
|
---|
| 482 |
|
---|
| 483 |
|
---|
| 484 | void GlobalLinesRenderer::SwitchRenderTextures()
|
---|
| 485 | {
|
---|
[1953] | 486 | RenderTexture *buffer = mOldTexture;
|
---|
| 487 | mOldTexture = mNewTexture;
|
---|
| 488 | mNewTexture = buffer;
|
---|
[1944] | 489 | }
|
---|
| 490 |
|
---|
| 491 |
|
---|
[1958] | 492 | void GlobalLinesRenderer::ComputeLookAt(const float alpha,
|
---|
| 493 | const float beta,
|
---|
| 494 | Vector3 &eye,
|
---|
| 495 | Vector3 &up,
|
---|
[1964] | 496 | Vector3 &left)
|
---|
[1958] | 497 | {
|
---|
| 498 | eye.x = sin(alpha) * cos(beta);
|
---|
| 499 | eye.y = sin(alpha) * sin(beta);
|
---|
[1964] | 500 | eye.z = cos(alpha);
|
---|
[1958] | 501 |
|
---|
| 502 | eye.RightHandedBase(up, left);
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 |
|
---|
[1944] | 506 | void GlobalLinesRenderer::InitGl()
|
---|
| 507 | {
|
---|
[1953] | 508 | InitDevIl();
|
---|
[1970] | 509 | cout << "texwidth: " << mTexWidth << " texheight: " << mTexHeight << endl;
|
---|
[1960] | 510 | glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
|
---|
[1944] | 511 | glutInitWindowPosition(50, 50);
|
---|
| 512 | glutInitWindowSize(512, 512);
|
---|
| 513 | glutCreateWindow("TestRenderDepthTexture");
|
---|
| 514 |
|
---|
| 515 | int err = glewInit();
|
---|
| 516 | if (GLEW_OK != err)
|
---|
| 517 | {
|
---|
| 518 | // problem: glewInit failed, something is seriously wrong
|
---|
| 519 | fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
|
---|
| 520 | exit(-1);
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | glutKeyboardFunc(Keyboard);
|
---|
| 524 | glutDisplayFunc(Display);
|
---|
| 525 | glutIdleFunc(Idle);
|
---|
| 526 | glutReshapeFunc(Reshape);
|
---|
| 527 |
|
---|
| 528 | Reshape(512, 512);
|
---|
| 529 | glMatrixMode(GL_MODELVIEW);
|
---|
| 530 | glLoadIdentity();
|
---|
| 531 |
|
---|
[1958] | 532 | // initialise the receiving buffers
|
---|
[1964] | 533 | mNewDepthBuffer = new float[mTexWidth * mTexHeight];
|
---|
| 534 | mNewItemBuffer = new unsigned char[mTexWidth * mTexHeight * 4];
|
---|
[1960] | 535 |
|
---|
[1964] | 536 | mOldDepthBuffer = new float[mTexWidth * mTexHeight];
|
---|
| 537 | mOldItemBuffer = new unsigned char[mTexWidth * mTexHeight * 4];
|
---|
[1960] | 538 |
|
---|
[1964] | 539 | for (int i = 0; i < mTexWidth * mTexHeight; ++ i)
|
---|
[1958] | 540 | {
|
---|
| 541 | mNewDepthBuffer[i] = 1;
|
---|
| 542 | mOldDepthBuffer[i] = 1;
|
---|
| 543 |
|
---|
[1960] | 544 | mNewItemBuffer[i * 4] = 255;
|
---|
| 545 | mNewItemBuffer[i * 4 + 1] = 255;
|
---|
| 546 | mNewItemBuffer[i * 4 + 2] = 255;
|
---|
| 547 | mNewItemBuffer[i * 4 + 3] = 255;
|
---|
| 548 |
|
---|
| 549 | mOldItemBuffer[i * 4] = 255;
|
---|
| 550 | mOldItemBuffer[i * 4 + 1] = 255;
|
---|
| 551 | mOldItemBuffer[i * 4 + 2] = 255;
|
---|
| 552 | mOldItemBuffer[i * 4 + 3] = 255;
|
---|
[1958] | 553 | }
|
---|
| 554 |
|
---|
[1960] | 555 | /*gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
|
---|
[1944] | 556 | midPoint.x, midPoint.y, midPoint.z,
|
---|
| 557 | 0, 1, 0);
|
---|
| 558 | */
|
---|
| 559 | gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
|
---|
| 560 |
|
---|
[1949] | 561 | //glDisable(GL_CULL_FACE);
|
---|
| 562 | glEnable(GL_CULL_FACE);
|
---|
[1944] | 563 | glDisable(GL_LIGHTING);
|
---|
| 564 | glDisable(GL_COLOR_MATERIAL);
|
---|
| 565 | glEnable(GL_DEPTH_TEST);
|
---|
| 566 | glClearColor(0.1, 0.2, 0.3, 1);
|
---|
| 567 |
|
---|
| 568 | // A square, mipmapped, anisotropically filtered 8-bit RGBA texture with
|
---|
| 569 | // depth and stencil.
|
---|
| 570 | // Note that RT_COPY_TO_TEXTURE is required for depth textures on ATI hardware
|
---|
[1970] | 571 |
|
---|
[1964] | 572 | mNewTexture = new RenderTexture(mTexWidth, mTexHeight, true, true);
|
---|
| 573 | #ifdef ATI
|
---|
[1969] | 574 | mNewTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
[1964] | 575 | #else
|
---|
[1969] | 576 | mNewTexture->Initialize(true, true, false, false, false, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
[1964] | 577 | #endif
|
---|
| 578 |
|
---|
| 579 | mOldTexture = new RenderTexture(mTexWidth, mTexHeight, true, true);
|
---|
| 580 |
|
---|
| 581 | #ifdef ATI
|
---|
[1969] | 582 | mOldTexture ->Initialize(true, true, false, false, false, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
[1964] | 583 | #else
|
---|
[1969] | 584 | mOldTexture ->Initialize(true, true, false, false, false, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
[1964] | 585 | #endif
|
---|
[1944] | 586 |
|
---|
| 587 | // Setup Cg
|
---|
| 588 | cgSetErrorCallback(cgErrorCallback);
|
---|
| 589 |
|
---|
| 590 | // Create cgContext.
|
---|
| 591 | sCgContext = cgCreateContext();
|
---|
| 592 |
|
---|
| 593 | // get the best profile for this hardware
|
---|
| 594 | sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
|
---|
| 595 |
|
---|
| 596 | //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);
|
---|
| 597 | cgGLSetOptimalOptions(sCgFragmentProfile);
|
---|
| 598 |
|
---|
| 599 | sCgDepthPeelingProgram =
|
---|
| 600 | cgCreateProgramFromFile(sCgContext,
|
---|
| 601 | CG_SOURCE,
|
---|
[1953] | 602 | mNewTexture->IsRectangleTexture() ?
|
---|
[1951] | 603 | "../src/depth_peelingRect.cg" : "../src/depth_peeling2d.cg",
|
---|
[1944] | 604 | GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
|
---|
| 605 | NULL,
|
---|
| 606 | NULL);
|
---|
| 607 |
|
---|
| 608 | if(sCgDepthPeelingProgram != NULL)
|
---|
| 609 | {
|
---|
| 610 | cgGLLoadProgram(sCgDepthPeelingProgram);
|
---|
| 611 | sTextureParam = cgGetNamedParameter(sCgDepthPeelingProgram, "depthTex");
|
---|
[1964] | 612 |
|
---|
| 613 | // we need size of texture for scaling
|
---|
| 614 | if (!mNewTexture->IsRectangleTexture())
|
---|
| 615 | {
|
---|
[1969] | 616 | sTexWidthParam = cgGetNamedParameter(sCgDepthPeelingProgram, "invTexWidth");
|
---|
[1964] | 617 | }
|
---|
| 618 |
|
---|
| 619 | sStepSizeParam = cgGetNamedParameter(sCgDepthPeelingProgram, "stepSize");
|
---|
| 620 |
|
---|
[1969] | 621 | cgGLSetParameter1f(sTexWidthParam, 1.0f / (float)mTexWidth);
|
---|
[1964] | 622 | cgGLSetParameter1f(sStepSizeParam, mEpsilon);
|
---|
[1944] | 623 | }
|
---|
| 624 |
|
---|
| 625 | sCgPassThroughProgram =
|
---|
| 626 | cgCreateProgramFromFile(sCgContext,
|
---|
| 627 | CG_SOURCE,
|
---|
| 628 | "../src/passthrough.cg",
|
---|
| 629 | GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
|
---|
| 630 | NULL,
|
---|
| 631 | NULL);
|
---|
| 632 |
|
---|
| 633 | if(sCgPassThroughProgram != NULL)
|
---|
| 634 | {
|
---|
| 635 | cgGLLoadProgram(sCgPassThroughProgram);
|
---|
| 636 | }
|
---|
[1970] | 637 |
|
---|
[1964] | 638 | const float alpha = 1.1;
|
---|
| 639 | const float beta = 0.9;
|
---|
[1970] | 640 |
|
---|
[1964] | 641 | InitScene(alpha, beta);
|
---|
[1970] | 642 |
|
---|
[1944] | 643 | PrintGLerror("init");
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 |
|
---|
[1960] | 647 | Intersectable *GlobalLinesRenderer::ExtractSamplePoint(float *depthBuffer,
|
---|
| 648 | unsigned char *itemBuffer,
|
---|
| 649 | const int x,
|
---|
| 650 | const int y,
|
---|
| 651 | Vector3 &hitPoint,
|
---|
| 652 | const bool isFrontBuffer) const
|
---|
[1944] | 653 | {
|
---|
[1964] | 654 | const int depthIndex = x + mTexWidth * y;
|
---|
[1960] | 655 | const int itemIndex = 4 * depthIndex;
|
---|
| 656 |
|
---|
| 657 | const float depth = depthBuffer[depthIndex];
|
---|
| 658 | const float eyeDist = mNear + (mFar - mNear) * depth;
|
---|
| 659 |
|
---|
[1964] | 660 | const float leftDist = -0.5f * mWidth + mWidth * ((float)x + 0.5f) / mTexWidth;
|
---|
| 661 | //const float leftDist = 0.5f * mWidth - mWidth * ((float)x + 0.5f) / mTexWidth;
|
---|
[1960] | 662 |
|
---|
[1964] | 663 | const float upDist = -0.5f * mWidth + mWidth * ((float)y + 0.5f) / mTexHeight;
|
---|
| 664 | //const float upDist = 0.5f * mWidth - mWidth * ((float)y + 0.5f) / mTexHeight;
|
---|
| 665 |
|
---|
[1960] | 666 | hitPoint = mViewPoint +
|
---|
| 667 | eyeDist * mEyeVec +
|
---|
[1964] | 668 | upDist * mUpVec +
|
---|
| 669 | leftDist * mLeftVec;
|
---|
[1960] | 670 |
|
---|
| 671 | unsigned char r = itemBuffer[itemIndex];
|
---|
| 672 | unsigned char g = itemBuffer[itemIndex + 1];
|
---|
| 673 | unsigned char b = itemBuffer[itemIndex + 2];
|
---|
| 674 |
|
---|
| 675 | // 3 times 255 means no valid object
|
---|
| 676 | if ((r == 255) && (g == 255) && (b == 255))
|
---|
| 677 | return NULL;
|
---|
| 678 |
|
---|
| 679 | const int id = mRenderer->GetId(r, g, b);
|
---|
| 680 | //cout << "r: " << (int)r << "g: " << (int)g << " b: " << (int)b << " id: " << id << "|";
|
---|
| 681 | Intersectable *intersect = mPreprocessor->GetObjectById(id);
|
---|
| 682 |
|
---|
| 683 | const Vector3 dir = isFrontBuffer ? mEyeVec : -mEyeVec;
|
---|
| 684 | // HACK: assume triangle intersectable
|
---|
| 685 | const Vector3 norm = intersect->GetNormal(0);
|
---|
| 686 |
|
---|
[1961] | 687 | // test for invalid view space
|
---|
| 688 | if (DotProd(dir, norm) >= -Limits::Small)
|
---|
| 689 | return NULL;
|
---|
| 690 |
|
---|
| 691 | return intersect;
|
---|
[1944] | 692 | }
|
---|
| 693 |
|
---|
| 694 |
|
---|
[1980] | 695 | void GlobalLinesRenderer::ComputeBoundingQuad(float &xMin,
|
---|
| 696 | float &yMin,
|
---|
| 697 | float &xMax,
|
---|
| 698 | float &yMax)
|
---|
| 699 | {
|
---|
| 700 | const AxisAlignedBox3 bbox = mPreprocessor->mKdTree->GetBox();
|
---|
| 701 |
|
---|
| 702 | Matrix4x4 m(mEyeVec, mUpVec, mLeftVec);
|
---|
| 703 |
|
---|
| 704 | m.Invert();
|
---|
| 705 |
|
---|
| 706 | Vector3 eye(m.x[0][0], m.x[0][1], m.x[0][2]);
|
---|
| 707 | Vector3 up(m.x[1][0], m.x[1][1], m.x[1][2]);
|
---|
| 708 | Vector3 left(m.x[2][0], m.x[2][1], m.x[2][2]);
|
---|
| 709 |
|
---|
| 710 | Vector3 boxMin(1e20f);
|
---|
| 711 | Vector3 boxMax(-1e20f);
|
---|
| 712 |
|
---|
| 713 | /* for (int i = 0; i < 6; ++ i)
|
---|
| 714 | {
|
---|
| 715 | Vector3 pt;
|
---|
| 716 | // TODO
|
---|
| 717 | if (boxMin.x < pt[i].x)
|
---|
| 718 | boxMin.x = pt[i].x;
|
---|
| 719 |
|
---|
| 720 | if (boxMin.y < pt[i].y)
|
---|
| 721 | boxMin.y = pt[i].y;
|
---|
| 722 |
|
---|
| 723 | if (boxMin.z < pt[i].z)
|
---|
| 724 | boxMin.z = pt[i].z;
|
---|
| 725 | }
|
---|
| 726 | */
|
---|
| 727 | xMin = boxMin.x;
|
---|
| 728 | yMin = boxMin.y;
|
---|
| 729 |
|
---|
| 730 | xMax = boxMax.x;
|
---|
| 731 | yMax = boxMax.y;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 |
|
---|
[1968] | 735 | bool GlobalLinesRenderer::ProcessDepthBuffer(VssRayContainer &vssRays,
|
---|
[1963] | 736 | const bool oldBufferInitialised,
|
---|
| 737 | const int pass)
|
---|
[1953] | 738 | {
|
---|
| 739 | GrabDepthBuffer(mNewDepthBuffer, mNewTexture);
|
---|
| 740 | GrabItemBuffer(mNewItemBuffer, mNewTexture);
|
---|
| 741 |
|
---|
[1960] | 742 | if (oldBufferInitialised)
|
---|
[1953] | 743 | {
|
---|
[1960] | 744 | GrabDepthBuffer(mOldDepthBuffer, mOldTexture);
|
---|
| 745 | GrabItemBuffer(mOldItemBuffer, mOldTexture);
|
---|
| 746 | }
|
---|
| 747 | else
|
---|
| 748 | {
|
---|
[1964] | 749 | for (int i = 0; i < mTexWidth * mTexHeight; ++ i)
|
---|
[1953] | 750 | {
|
---|
[1960] | 751 | mOldDepthBuffer[i] = 0;
|
---|
[1953] | 752 |
|
---|
[1960] | 753 | mOldItemBuffer[i * 4] = 255;
|
---|
| 754 | mOldItemBuffer[i * 4 + 1] = 255;
|
---|
| 755 | mOldItemBuffer[i * 4 + 2] = 255;
|
---|
| 756 | mOldItemBuffer[i * 4 + 3] = 255;
|
---|
| 757 | }
|
---|
| 758 | }
|
---|
[1953] | 759 |
|
---|
[1968] | 760 | /////////////////
|
---|
| 761 | // test for validity
|
---|
| 762 |
|
---|
| 763 | bool buffersEqual = true;
|
---|
| 764 | bool bufferEmpty = true;
|
---|
| 765 |
|
---|
[1964] | 766 | for (int y = 0; y < mTexHeight; ++ y)
|
---|
[1960] | 767 | {
|
---|
[1964] | 768 | for (int x = 0; x < mTexWidth; ++ x)
|
---|
[1960] | 769 | {
|
---|
[1968] | 770 | const int depthIndex = x + mTexWidth * y;
|
---|
| 771 | const int itemIndex = 4 * depthIndex;
|
---|
| 772 |
|
---|
| 773 | if (mOldItemBuffer[itemIndex] != mNewItemBuffer[itemIndex])
|
---|
| 774 | {
|
---|
| 775 | buffersEqual = false;
|
---|
| 776 | }
|
---|
| 777 |
|
---|
| 778 | unsigned char r = mNewItemBuffer[itemIndex];
|
---|
| 779 | unsigned char g = mNewItemBuffer[itemIndex + 1];
|
---|
| 780 | unsigned char b = mNewItemBuffer[itemIndex + 2];
|
---|
| 781 |
|
---|
| 782 | // 3 times 255 means no valid object
|
---|
| 783 | if (!((r == 255) && (g == 255) && (b == 255)))
|
---|
| 784 | {
|
---|
| 785 | bufferEmpty = false;
|
---|
| 786 | }
|
---|
| 787 | }
|
---|
| 788 |
|
---|
| 789 | // early exit
|
---|
| 790 | if (!buffersEqual && !bufferEmpty)
|
---|
| 791 | break;
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | // depth buffer not valid
|
---|
| 795 | if (buffersEqual || bufferEmpty)
|
---|
| 796 | {
|
---|
| 797 | cout << "stopped at layer " << pass << endl;
|
---|
| 798 | return false;
|
---|
| 799 | }
|
---|
| 800 |
|
---|
| 801 | for (int y = 0; y < mTexHeight; ++ y)
|
---|
| 802 | {
|
---|
| 803 | for (int x = 0; x < mTexWidth; ++ x)
|
---|
| 804 | {
|
---|
[1960] | 805 | Vector3 newPt, oldPt;
|
---|
[1953] | 806 |
|
---|
[1960] | 807 | Intersectable *termObj1 = ExtractSamplePoint(mNewDepthBuffer,
|
---|
| 808 | mNewItemBuffer,
|
---|
| 809 | x,
|
---|
| 810 | y,
|
---|
| 811 | newPt,
|
---|
| 812 | true);
|
---|
[1953] | 813 |
|
---|
[1960] | 814 | Intersectable *termObj2 = ExtractSamplePoint(mOldDepthBuffer,
|
---|
| 815 | mOldItemBuffer,
|
---|
| 816 | x,
|
---|
| 817 | y,
|
---|
| 818 | oldPt,
|
---|
| 819 | false);
|
---|
[1953] | 820 |
|
---|
[1963] | 821 | if (!termObj1 && !termObj2) // we do not create a ray
|
---|
| 822 | continue;
|
---|
| 823 |
|
---|
| 824 | Vector3 clippedOldPt, clippedNewPt;
|
---|
| 825 |
|
---|
[1970] | 826 | if (ClipToViewSpaceBox(oldPt, newPt, clippedOldPt, clippedNewPt))//;if(1)
|
---|
[1958] | 827 | {
|
---|
[1969] | 828 | //clippedOldPt = oldPt;
|
---|
| 829 | //clippedNewPt = newPt;
|
---|
[1958] | 830 |
|
---|
[1969] | 831 | // create rays in both directions
|
---|
| 832 | if (termObj1)
|
---|
| 833 | {
|
---|
| 834 | vssRays.push_back(new VssRay(clippedOldPt, clippedNewPt, NULL, termObj1, pass));
|
---|
| 835 | //cout << "new pt: " << newPt << endl;
|
---|
| 836 | }
|
---|
| 837 |
|
---|
| 838 | if (mSampleReverse && termObj2)
|
---|
| 839 | {
|
---|
| 840 | vssRays.push_back(new VssRay(clippedNewPt, clippedOldPt, NULL, termObj2, pass));
|
---|
| 841 | //cout << "old pt: " << oldPt << endl;
|
---|
| 842 | }
|
---|
[1961] | 843 | }
|
---|
[1953] | 844 | }
|
---|
| 845 | }
|
---|
[1968] | 846 |
|
---|
| 847 | return true;
|
---|
[1953] | 848 | }
|
---|
| 849 |
|
---|
| 850 |
|
---|
[1963] | 851 | bool GlobalLinesRenderer::ClipToViewSpaceBox(const Vector3 &origin,
|
---|
| 852 | const Vector3 &termination,
|
---|
| 853 | Vector3 &clippedOrigin,
|
---|
| 854 | Vector3 &clippedTermination)
|
---|
[1944] | 855 | {
|
---|
[1963] | 856 | Ray ray(origin, termination - origin, Ray::LINE_SEGMENT);
|
---|
| 857 | ray.Precompute();
|
---|
| 858 |
|
---|
| 859 | float tmin, tmax;
|
---|
| 860 |
|
---|
| 861 | //const AxisAlignedBox3 bbox = mPreprocessor->mViewCellsManager->GetViewSpaceBox();
|
---|
| 862 | // hack
|
---|
[1970] | 863 | AxisAlignedBox3 bbox = mPreprocessor->mKdTree->GetBox();
|
---|
[1963] | 864 |
|
---|
[1970] | 865 | //bbox.Enlarge(1);
|
---|
| 866 | if (!bbox.ComputeMinMaxT(ray, &tmin, &tmax) || (tmin >= tmax))
|
---|
[1963] | 867 | {
|
---|
| 868 | return false;
|
---|
| 869 | }
|
---|
| 870 |
|
---|
[1970] | 871 | if (tmin >= 1.0f || tmax <= 0.0f)
|
---|
[1963] | 872 | return false;
|
---|
| 873 |
|
---|
| 874 | if (tmin > 0.0f)
|
---|
| 875 | clippedOrigin = ray.Extrap(tmin);
|
---|
| 876 | else
|
---|
| 877 | clippedOrigin = origin;
|
---|
| 878 |
|
---|
| 879 | if (tmax < 1.0f)
|
---|
| 880 | clippedTermination = ray.Extrap(tmax);
|
---|
| 881 | else
|
---|
| 882 | clippedTermination = termination;
|
---|
| 883 |
|
---|
| 884 | return true;
|
---|
[1944] | 885 | }
|
---|
| 886 |
|
---|
| 887 |
|
---|
[1963] | 888 | void GlobalLinesRenderer::Run()
|
---|
[1960] | 889 | {
|
---|
[1963] | 890 | glutMainLoop();
|
---|
[1960] | 891 | }
|
---|
| 892 |
|
---|
| 893 |
|
---|
| 894 | void GlobalLinesRenderer::Visualize(const VssRayContainer &vssRays)
|
---|
| 895 | {
|
---|
[1972] | 896 | cout << "exporting visualization ... ";
|
---|
[1960] | 897 | Exporter *exporter = Exporter::GetExporter("globalLines.wrl");
|
---|
| 898 |
|
---|
| 899 | if (!exporter)
|
---|
| 900 | return;
|
---|
| 901 |
|
---|
| 902 | exporter->SetWireframe();
|
---|
| 903 | //exporter->ExportGeometry(preprocessor->mObjects);
|
---|
| 904 | exporter->SetFilled();
|
---|
| 905 |
|
---|
| 906 | VssRayContainer::const_iterator vit, vit_end = vssRays.end();
|
---|
[1963] | 907 | VssRayContainer outRays;
|
---|
[1960] | 908 |
|
---|
[1963] | 909 | Intersectable::NewMail();
|
---|
| 910 |
|
---|
[1960] | 911 | for (vit = vssRays.begin(); vit != vit_end; ++ vit)
|
---|
| 912 | {
|
---|
| 913 | VssRay *ray = *vit;
|
---|
[1963] | 914 | Intersectable *obj = (*vit)->mTerminationObject;
|
---|
| 915 |
|
---|
| 916 | if (!obj->Mailed())
|
---|
| 917 | {
|
---|
| 918 | obj->Mail();
|
---|
| 919 | exporter->ExportIntersectable(obj);
|
---|
| 920 | }
|
---|
| 921 |
|
---|
[1972] | 922 | //if (ray->mPass == 4)
|
---|
[1963] | 923 | outRays.push_back(ray);
|
---|
[1960] | 924 | }
|
---|
| 925 |
|
---|
[1972] | 926 | //exporter->ExportRays(outRays);
|
---|
| 927 | cout << "finished" << endl;
|
---|
[1960] | 928 | delete exporter;
|
---|
| 929 | }
|
---|
| 930 |
|
---|
| 931 |
|
---|
[1953] | 932 | void GlobalLinesRenderer::GrabDepthBuffer(float *data, RenderTexture *rt)
|
---|
| 933 | {
|
---|
| 934 | rt->BindDepth();
|
---|
| 935 | rt->EnableTextureTarget();
|
---|
| 936 |
|
---|
| 937 | const int texFormat = GL_DEPTH_COMPONENT;
|
---|
| 938 | glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
|
---|
| 939 |
|
---|
| 940 | mNewTexture->DisableTextureTarget();
|
---|
| 941 | }
|
---|
| 942 |
|
---|
| 943 |
|
---|
[1960] | 944 | void GlobalLinesRenderer::GrabItemBuffer(unsigned char *data, RenderTexture *rt)
|
---|
[1953] | 945 | {
|
---|
[1958] | 946 | rt->Bind();
|
---|
[1953] | 947 | rt->EnableTextureTarget();
|
---|
| 948 |
|
---|
[1958] | 949 | const int texFormat = GL_RGBA;
|
---|
[1960] | 950 | glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_UNSIGNED_BYTE, data);
|
---|
[1953] | 951 |
|
---|
| 952 | mNewTexture->DisableTextureTarget();
|
---|
| 953 | }
|
---|
| 954 |
|
---|
[1958] | 955 |
|
---|
[1953] | 956 | void GlobalLinesRenderer::ExportDepthBuffer()
|
---|
| 957 | {
|
---|
| 958 | mNewTexture->BindDepth();
|
---|
| 959 | mNewTexture->EnableTextureTarget();
|
---|
| 960 | cout << "depth: " << mNewTexture->GetDepthBits() << endl;
|
---|
| 961 |
|
---|
[1960] | 962 | const int components = 1;//mNewTexture->GetDepthBits() / 8;
|
---|
[1953] | 963 |
|
---|
[1964] | 964 | float *data = new float[mTexWidth * mTexHeight * components];
|
---|
[1953] | 965 | //const int texFormat = WGL_TEXTURE_DEPTH_COMPONENT_NV;
|
---|
| 966 | const int texFormat = GL_DEPTH_COMPONENT;
|
---|
| 967 | //const int texFormat = GL_RGBA;
|
---|
| 968 | glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
|
---|
| 969 |
|
---|
| 970 | string filename("depth.tga");
|
---|
| 971 | ilRegisterType(IL_FLOAT);
|
---|
| 972 |
|
---|
| 973 | const int depth = 1;
|
---|
| 974 | const int bpp = components;
|
---|
| 975 |
|
---|
[1964] | 976 | ilTexImage(mTexWidth, mTexHeight, depth, bpp, IL_LUMINANCE, IL_FLOAT, data);
|
---|
[1953] | 977 | ilSaveImage((char *const)filename.c_str());
|
---|
| 978 |
|
---|
| 979 | cout << "finished" << endl;
|
---|
| 980 | delete data;
|
---|
| 981 | cout << "data deleted" << endl;
|
---|
| 982 | mNewTexture->DisableTextureTarget();
|
---|
| 983 | PrintGLerror("grab texture");
|
---|
| 984 | }
|
---|
| 985 |
|
---|
[1958] | 986 |
|
---|
[1960] | 987 | void GlobalLinesRenderer::ExportItemBuffer()
|
---|
| 988 | {
|
---|
| 989 | mNewTexture->Bind();
|
---|
| 990 | mNewTexture->EnableTextureTarget();
|
---|
| 991 | cout << "depth: " << mNewTexture->GetDepthBits() << endl;
|
---|
[1958] | 992 |
|
---|
[1960] | 993 | const int components = 4;//mNewTexture->GetDepthBits() / 8;
|
---|
[1958] | 994 |
|
---|
[1964] | 995 | unsigned char *data = new unsigned char [mTexWidth * mTexHeight * components];
|
---|
[1960] | 996 | //const int texFormat = WGL_TEXTURE_DEPTH_COMPONENT_NV;
|
---|
| 997 | const int texFormat = GL_RGBA;
|
---|
| 998 | glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_UNSIGNED_BYTE, data);
|
---|
| 999 |
|
---|
| 1000 | string filename("items.jpg");
|
---|
| 1001 | ilRegisterType(IL_UNSIGNED_BYTE);
|
---|
| 1002 |
|
---|
| 1003 | const int depth = 1;
|
---|
| 1004 | const int bpp = components;
|
---|
| 1005 |
|
---|
[1964] | 1006 | ilTexImage(mTexWidth, mTexHeight, depth, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data);
|
---|
[1960] | 1007 | ilSaveImage((char *const)filename.c_str());
|
---|
| 1008 |
|
---|
| 1009 | cout << "finished" << endl;
|
---|
| 1010 | delete data;
|
---|
| 1011 | cout << "data deleted" << endl;
|
---|
| 1012 | mNewTexture->DisableTextureTarget();
|
---|
| 1013 | PrintGLerror("grab texture");
|
---|
| 1014 | }
|
---|
| 1015 |
|
---|
| 1016 |
|
---|
[1969] | 1017 | int GlobalLinesRenderer::ApplyDepthPeeling(VssRayContainer &rays)
|
---|
[1958] | 1018 | {
|
---|
[1969] | 1019 | int layers = 1;
|
---|
| 1020 |
|
---|
[1953] | 1021 | mNewTexture->BeginCapture();
|
---|
[1944] | 1022 | {
|
---|
| 1023 | //cgGLBindProgram(sCgPassThroughProgram);
|
---|
| 1024 | //cgGLEnableProfile(sCgFragmentProfile);
|
---|
[1951] | 1025 | DrawGeometry();
|
---|
[1944] | 1026 | }
|
---|
[1953] | 1027 | mNewTexture->EndCapture();
|
---|
[1944] | 1028 |
|
---|
[1951] | 1029 | PrintGLerror("firstpass");
|
---|
[1953] | 1030 | if (mNewTexture->IsRectangleTexture()) cout << "rect" << endl;
|
---|
[1944] | 1031 |
|
---|
[1958] | 1032 | // process the buffers for the first layer
|
---|
[1963] | 1033 | ProcessDepthBuffer(rays, false, 0);
|
---|
[1958] | 1034 |
|
---|
[1971] | 1035 | long renderTime = 0;
|
---|
| 1036 | long bufferTime = 0;
|
---|
| 1037 |
|
---|
[1969] | 1038 | for(; layers < mMaxDepth; ++ layers)
|
---|
[1944] | 1039 | {
|
---|
[1971] | 1040 | cout << ".";
|
---|
| 1041 | const long startRenderTime = GetTime();
|
---|
| 1042 |
|
---|
[1944] | 1043 | // Peel another layer
|
---|
[1962] | 1044 | // switch pointer between rendertextures
|
---|
| 1045 | SwitchRenderTextures();
|
---|
[1944] | 1046 |
|
---|
[1953] | 1047 | mNewTexture->BeginCapture();
|
---|
[1944] | 1048 | {
|
---|
| 1049 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 1050 |
|
---|
| 1051 | cgGLBindProgram(sCgDepthPeelingProgram);
|
---|
| 1052 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
[1953] | 1053 | cgGLSetTextureParameter(sTextureParam, mOldTexture->GetDepthTextureID());
|
---|
[1944] | 1054 | cgGLEnableTextureParameter(sTextureParam);
|
---|
| 1055 |
|
---|
[1951] | 1056 | DrawGeometry();
|
---|
| 1057 |
|
---|
[1944] | 1058 | cgGLDisableTextureParameter(sTextureParam);
|
---|
[1949] | 1059 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
[1944] | 1060 | }
|
---|
[1953] | 1061 | mNewTexture->EndCapture();
|
---|
[1960] | 1062 |
|
---|
[1971] | 1063 | renderTime += TimeDiff(startRenderTime, GetTime());
|
---|
| 1064 |
|
---|
| 1065 | const long startBufferTime = GetTime();
|
---|
| 1066 |
|
---|
[1960] | 1067 | // process the buffers for following layer
|
---|
[1968] | 1068 | // jump out of loop for the first invalid buffer
|
---|
[1969] | 1069 | if (!ProcessDepthBuffer(rays, true, layers))
|
---|
[1968] | 1070 | break;
|
---|
[1971] | 1071 |
|
---|
| 1072 | bufferTime += TimeDiff(startBufferTime, GetTime());
|
---|
[1944] | 1073 | }
|
---|
| 1074 |
|
---|
[1972] | 1075 | Debug << "time spent in rendering: " << renderTime * 1e-3f << endl;
|
---|
| 1076 | Debug << "time spent in buffer: " << bufferTime * 1e-3f << endl;
|
---|
[1971] | 1077 |
|
---|
[1951] | 1078 | PrintGLerror("endpeeling");
|
---|
[1969] | 1079 |
|
---|
| 1080 | return layers;
|
---|
[1944] | 1081 | }
|
---|
| 1082 |
|
---|
| 1083 |
|
---|
| 1084 | }
|
---|