[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"
|
---|
[1953] | 7 | // the devil library
|
---|
| 8 | #include <IL/il.h>
|
---|
| 9 | #include <IL/ilu.h>
|
---|
| 10 | #include <IL/ilut.h>
|
---|
[1944] | 11 |
|
---|
| 12 | #include <Cg/cg.h>
|
---|
| 13 | #include <Cg/cgGL.h>
|
---|
| 14 |
|
---|
| 15 | //#include <QtOpenGL>
|
---|
| 16 |
|
---|
| 17 | namespace GtpVisibilityPreprocessor {
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | static CGcontext sCgContext = NULL;
|
---|
| 21 | static CGprogram sCgDepthPeelingProgram = NULL;
|
---|
| 22 | static CGprogram sCgPassThroughProgram = NULL;
|
---|
| 23 |
|
---|
| 24 | static CGprofile sCgFragmentProfile;
|
---|
| 25 | static CGparameter sTextureParam;
|
---|
| 26 |
|
---|
| 27 | GlobalLinesRenderer *globalLinesRenderer = NULL;
|
---|
| 28 |
|
---|
[1950] | 29 | static int texWidth = 256;
|
---|
| 30 | static int texHeight = 256;
|
---|
[1944] | 31 |
|
---|
[1953] | 32 |
|
---|
| 33 | static void InitDevIl()
|
---|
| 34 | {
|
---|
| 35 | ilInit();
|
---|
| 36 | ILuint ImageName;
|
---|
| 37 | ilGenImages(1, &ImageName);
|
---|
| 38 | ilBindImage(ImageName);
|
---|
| 39 | ilEnable(IL_FILE_OVERWRITE);
|
---|
| 40 |
|
---|
| 41 | // ilRegisterFormat(IL_RGBA);
|
---|
| 42 | // ilRegisterType(IL_FLOAT);
|
---|
| 43 |
|
---|
| 44 | // ilEnable(IL_ORIGIN_SET);
|
---|
| 45 | // ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 |
|
---|
[1944] | 49 | static void cgErrorCallback()
|
---|
| 50 | {
|
---|
| 51 | CGerror lastError = cgGetError();
|
---|
| 52 |
|
---|
| 53 | if(lastError)
|
---|
| 54 | {
|
---|
| 55 | printf("%s\n\n", cgGetErrorString(lastError));
|
---|
| 56 | printf("%s\n", cgGetLastListing(sCgContext));
|
---|
| 57 | printf("Cg error, exiting...\n");
|
---|
| 58 |
|
---|
| 59 | exit(0);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | static void PrintGLerror(char *msg)
|
---|
| 64 | {
|
---|
| 65 | GLenum errCode;
|
---|
| 66 | const GLubyte *errStr;
|
---|
| 67 |
|
---|
| 68 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
| 69 | {
|
---|
| 70 | errStr = gluErrorString(errCode);
|
---|
| 71 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | void Reshape(int w, int h)
|
---|
| 77 | {
|
---|
| 78 | if (h == 0) h = 1;
|
---|
| 79 |
|
---|
| 80 | glViewport(0, 0, w, h);
|
---|
| 81 |
|
---|
| 82 | glMatrixMode(GL_PROJECTION);
|
---|
| 83 | glLoadIdentity();
|
---|
| 84 |
|
---|
[1949] | 85 | //gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 3, 5000.0);
|
---|
[1944] | 86 | //gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.5, 10.0);
|
---|
[1951] | 87 | glOrtho(-1, 1, -1, 1, 0.5, 15);
|
---|
[1944] | 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
[1953] | 91 | void SetFrustum(int sizeX, int sizeY, float nearPlane, float farPlane)
|
---|
| 92 | {
|
---|
| 93 | glMatrixMode(GL_PROJECTION);
|
---|
| 94 | glLoadIdentity();
|
---|
| 95 |
|
---|
| 96 | glOrtho(-sizeX / 2, sizeX / 2,
|
---|
| 97 | -sizeY / 2, sizeY / 2,
|
---|
| 98 | nearPlane, farPlane);
|
---|
| 99 |
|
---|
| 100 | /*glOrtho(0, sizeX,
|
---|
| 101 | 0, sizeY ,
|
---|
| 102 | nearPlane, farPlane);*/
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[1944] | 105 | void Display()
|
---|
| 106 | {
|
---|
| 107 | //globalLinesRenderer->DrawGeometry();
|
---|
| 108 | globalLinesRenderer->CastGlobalLines(Beam(), 0);
|
---|
| 109 | PrintGLerror("display");
|
---|
| 110 |
|
---|
| 111 | glutSwapBuffers();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | void Idle()
|
---|
| 116 | {
|
---|
| 117 | glutPostRedisplay();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | void Keyboard(unsigned char key, int x, int y)
|
---|
| 122 | {
|
---|
| 123 | switch(key)
|
---|
| 124 | {
|
---|
[1949] | 125 | case '2':
|
---|
| 126 | ++ globalLinesRenderer->mMaxDepth;
|
---|
| 127 | cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
|
---|
[1944] | 128 | return;
|
---|
[1949] | 129 | case '1':
|
---|
| 130 | -- globalLinesRenderer->mMaxDepth;
|
---|
| 131 | cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
|
---|
| 132 | return;
|
---|
[1953] | 133 | case '3':
|
---|
| 134 | //globalLinesRenderer->ApplyDepthPeeling(Beam(), 0);
|
---|
| 135 | globalLinesRenderer->GrabDepthBuffer(globalLinesRenderer->mNewDepthBuffer, globalLinesRenderer->mNewTexture);
|
---|
[1944] | 136 | default:
|
---|
| 137 | return;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[1953] | 141 |
|
---|
[1944] | 142 | /*GlobalLinesRenderer::GlobalLinesRenderer(RenderTexture *buffer1,
|
---|
| 143 | RenderTexture *buffer2,
|
---|
| 144 | Preprocessor *preprocessor,
|
---|
| 145 | GlRenderer *renderer)
|
---|
[1953] | 146 | : mNewTexture(buffer1), mOldTexture(buffer2), mPreprocessor(preprocessor), mMaxDepth(100),
|
---|
[1944] | 147 | mRenderer(renderer)
|
---|
| 148 | {
|
---|
| 149 | }
|
---|
| 150 | */
|
---|
[1953] | 151 |
|
---|
| 152 |
|
---|
[1949] | 153 | GlobalLinesRenderer::GlobalLinesRenderer(Preprocessor *preprocessor,
|
---|
| 154 | GlRenderer *renderer):
|
---|
[1953] | 155 | mNewTexture(NULL),
|
---|
| 156 | mOldTexture(NULL),
|
---|
[1949] | 157 | mMaxDepth(0),
|
---|
[1944] | 158 | mRenderer(renderer),
|
---|
| 159 | mPreprocessor(preprocessor)
|
---|
| 160 | {
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | GlobalLinesRenderer::~GlobalLinesRenderer()
|
---|
| 165 | {
|
---|
| 166 | if (sCgDepthPeelingProgram)
|
---|
| 167 | cgDestroyProgram(sCgDepthPeelingProgram);
|
---|
| 168 | if (sCgContext)
|
---|
| 169 | cgDestroyContext(sCgContext);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | void GlobalLinesRenderer::CastGlobalLines(Beam &beam, const int samples)
|
---|
| 174 | {
|
---|
| 175 | // bind pixel shader implementing the front depth buffer functionality
|
---|
| 176 | ApplyDepthPeeling(beam, samples);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | void GlobalLinesRenderer::RenderObject(Intersectable *obj)
|
---|
| 181 | {
|
---|
| 182 | mRenderer->RenderIntersectable(obj);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 | void GlobalLinesRenderer::DrawGeometry()
|
---|
[1945] | 187 | {
|
---|
[1951] | 188 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 189 | glMatrixMode(GL_MODELVIEW);
|
---|
[1944] | 190 |
|
---|
[1951] | 191 | glPushMatrix();
|
---|
| 192 | {
|
---|
[1953] | 193 | //glLoadIdentity();
|
---|
| 194 |
|
---|
[1951] | 195 | //mRenderer->mUseFalseColors = true;
|
---|
| 196 | ObjectContainer::const_iterator oit, oit_end = mPreprocessor->mObjects.end();
|
---|
| 197 |
|
---|
| 198 | Intersectable::NewMail();
|
---|
| 199 |
|
---|
| 200 | for (oit = mPreprocessor->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
| 201 | {
|
---|
| 202 | RenderObject(*oit);
|
---|
| 203 | }
|
---|
[1944] | 204 | }
|
---|
[1951] | 205 | glPopMatrix();
|
---|
[1944] | 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
| 209 | void GlobalLinesRenderer::SwitchRenderTextures()
|
---|
| 210 | {
|
---|
[1953] | 211 | RenderTexture *buffer = mOldTexture;
|
---|
| 212 | mOldTexture = mNewTexture;
|
---|
| 213 | mNewTexture = buffer;
|
---|
[1944] | 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | void GlobalLinesRenderer::InitGl()
|
---|
| 218 | {
|
---|
[1953] | 219 | InitDevIl();
|
---|
| 220 |
|
---|
[1944] | 221 | glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
|
---|
| 222 | glutInitWindowPosition(50, 50);
|
---|
| 223 | glutInitWindowSize(512, 512);
|
---|
| 224 | glutCreateWindow("TestRenderDepthTexture");
|
---|
| 225 |
|
---|
| 226 | int err = glewInit();
|
---|
| 227 | if (GLEW_OK != err)
|
---|
| 228 | {
|
---|
| 229 | // problem: glewInit failed, something is seriously wrong
|
---|
| 230 | fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
|
---|
| 231 | exit(-1);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | glutKeyboardFunc(Keyboard);
|
---|
| 235 | glutDisplayFunc(Display);
|
---|
| 236 | glutIdleFunc(Idle);
|
---|
| 237 | glutReshapeFunc(Reshape);
|
---|
| 238 |
|
---|
| 239 | Reshape(512, 512);
|
---|
| 240 | glMatrixMode(GL_MODELVIEW);
|
---|
| 241 | glLoadIdentity();
|
---|
| 242 |
|
---|
| 243 | AxisAlignedBox3 bbox = globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
|
---|
[1951] | 244 |
|
---|
[1953] | 245 | Vector3 midPoint = bbox.Center();
|
---|
| 246 |
|
---|
| 247 | const float sceneSize = Magnitude(bbox.Diagonal());
|
---|
| 248 | Vector3 viewPoint = midPoint + Vector3(0.5 * sceneSize, 0, 0);
|
---|
| 249 |
|
---|
[1951] | 250 | cout << "mid point: " << midPoint << endl;
|
---|
| 251 | cout << "view point: " << viewPoint << endl;
|
---|
[1944] | 252 | cout << "scene: " << bbox << endl;
|
---|
| 253 |
|
---|
| 254 | /*gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
|
---|
| 255 | midPoint.x, midPoint.y, midPoint.z,
|
---|
| 256 | 0, 1, 0);
|
---|
| 257 | */
|
---|
| 258 | gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
|
---|
| 259 |
|
---|
[1949] | 260 | //glDisable(GL_CULL_FACE);
|
---|
| 261 | glEnable(GL_CULL_FACE);
|
---|
[1944] | 262 | glDisable(GL_LIGHTING);
|
---|
| 263 | glDisable(GL_COLOR_MATERIAL);
|
---|
| 264 | glEnable(GL_DEPTH_TEST);
|
---|
| 265 | glClearColor(0.1, 0.2, 0.3, 1);
|
---|
| 266 |
|
---|
| 267 | // A square, mipmapped, anisotropically filtered 8-bit RGBA texture with
|
---|
| 268 | // depth and stencil.
|
---|
| 269 | // Note that RT_COPY_TO_TEXTURE is required for depth textures on ATI hardware
|
---|
| 270 |
|
---|
[1953] | 271 | mNewTexture = new RenderTexture(texWidth, texHeight, true, true);
|
---|
| 272 | //mNewTexture->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
| 273 | mNewTexture->Initialize(true, true, false, true, true, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
[1949] | 274 |
|
---|
[1953] | 275 | mOldTexture = new RenderTexture(texWidth, texHeight, true, true);
|
---|
| 276 | //mOldTexture ->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
| 277 | mOldTexture ->Initialize(true, true, false, true, true, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
|
---|
[1944] | 278 |
|
---|
| 279 | // Setup Cg
|
---|
| 280 | cgSetErrorCallback(cgErrorCallback);
|
---|
| 281 |
|
---|
| 282 | // Create cgContext.
|
---|
| 283 | sCgContext = cgCreateContext();
|
---|
| 284 |
|
---|
| 285 | // get the best profile for this hardware
|
---|
| 286 | sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
|
---|
| 287 |
|
---|
| 288 | //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);
|
---|
| 289 | cgGLSetOptimalOptions(sCgFragmentProfile);
|
---|
| 290 |
|
---|
| 291 | sCgDepthPeelingProgram =
|
---|
| 292 | cgCreateProgramFromFile(sCgContext,
|
---|
| 293 | CG_SOURCE,
|
---|
[1953] | 294 | mNewTexture->IsRectangleTexture() ?
|
---|
[1951] | 295 | "../src/depth_peelingRect.cg" : "../src/depth_peeling2d.cg",
|
---|
[1944] | 296 | GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
|
---|
| 297 | NULL,
|
---|
| 298 | NULL);
|
---|
| 299 |
|
---|
| 300 | if(sCgDepthPeelingProgram != NULL)
|
---|
| 301 | {
|
---|
| 302 | cgGLLoadProgram(sCgDepthPeelingProgram);
|
---|
| 303 | sTextureParam = cgGetNamedParameter(sCgDepthPeelingProgram, "depthTex");
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | sCgPassThroughProgram =
|
---|
| 307 | cgCreateProgramFromFile(sCgContext,
|
---|
| 308 | CG_SOURCE,
|
---|
| 309 | "../src/passthrough.cg",
|
---|
| 310 | GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
|
---|
| 311 | NULL,
|
---|
| 312 | NULL);
|
---|
| 313 |
|
---|
| 314 | if(sCgPassThroughProgram != NULL)
|
---|
| 315 | {
|
---|
| 316 | cgGLLoadProgram(sCgPassThroughProgram);
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | // setup the rendering context for the RenderTexture
|
---|
[1953] | 320 | mNewTexture->BeginCapture();
|
---|
[1944] | 321 | {
|
---|
[1953] | 322 | //Reshape(texWidth, texHeight);
|
---|
| 323 | glViewport(0, 0, texWidth, texHeight);
|
---|
| 324 | SetFrustum(sceneSize, sceneSize, 1, sceneSize);
|
---|
| 325 |
|
---|
[1944] | 326 | glClearColor(0.1, 0.7, 0.2, 1);
|
---|
| 327 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
[1949] | 328 |
|
---|
| 329 | glFrontFace(GL_CCW);
|
---|
| 330 | glCullFace(GL_BACK);
|
---|
| 331 | glEnable(GL_CULL_FACE);
|
---|
| 332 | glShadeModel(GL_FLAT);
|
---|
| 333 | glEnable(GL_DEPTH_TEST);
|
---|
| 334 |
|
---|
[1944] | 335 | glMatrixMode(GL_MODELVIEW);
|
---|
| 336 | glLoadIdentity();
|
---|
| 337 | gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
|
---|
| 338 | midPoint.x, midPoint.y, midPoint.z,
|
---|
| 339 | 0, 1, 0);
|
---|
[1953] | 340 |
|
---|
| 341 | //SetFrustum(sceneSize, sceneSize, 1, sceneSize);
|
---|
[1944] | 342 | }
|
---|
[1953] | 343 | mNewTexture->EndCapture();
|
---|
[1944] | 344 |
|
---|
| 345 | // setup the rendering context for the RenderTexture
|
---|
[1953] | 346 | mOldTexture->BeginCapture();
|
---|
[1944] | 347 | {
|
---|
[1953] | 348 | glClearColor(0.1, 0.7, 0.2, 1);
|
---|
| 349 |
|
---|
| 350 | //Reshape(texWidth, texHeight);
|
---|
| 351 | glViewport(0, 0, texWidth, texHeight);
|
---|
| 352 | SetFrustum(sceneSize, sceneSize, 1, sceneSize);
|
---|
| 353 |
|
---|
[1944] | 354 | glMatrixMode(GL_MODELVIEW);
|
---|
| 355 | glLoadIdentity();
|
---|
[1949] | 356 |
|
---|
| 357 | glFrontFace(GL_CCW);
|
---|
| 358 | glCullFace(GL_BACK);
|
---|
| 359 | glEnable(GL_CULL_FACE);
|
---|
| 360 | glShadeModel(GL_FLAT);
|
---|
| 361 | glEnable(GL_DEPTH_TEST);
|
---|
| 362 |
|
---|
[1944] | 363 | gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
|
---|
| 364 | midPoint.x, midPoint.y, midPoint.z,
|
---|
| 365 | 0, 1, 0);
|
---|
| 366 |
|
---|
[1953] | 367 | //SetFrustum(sceneSize, sceneSize, 1, sceneSize);
|
---|
[1944] | 368 | }
|
---|
[1953] | 369 | mOldTexture->EndCapture();
|
---|
[1944] | 370 |
|
---|
| 371 | PrintGLerror("init");
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 |
|
---|
[1953] | 375 | Intersectable *GetObject(const int index)
|
---|
[1944] | 376 | {
|
---|
[1953] | 377 | return NULL;
|
---|
[1944] | 378 | }
|
---|
| 379 |
|
---|
| 380 |
|
---|
[1953] | 381 | void GlobalLinesRenderer::ProcessDepthBuffer(VssRayContainer &vssRays)
|
---|
| 382 | {
|
---|
| 383 | GrabDepthBuffer(mNewDepthBuffer, mNewTexture);
|
---|
| 384 | GrabDepthBuffer(mOldDepthBuffer, mOldTexture);
|
---|
| 385 |
|
---|
| 386 | GrabItemBuffer(mNewItemBuffer, mNewTexture);
|
---|
| 387 | GrabItemBuffer(mOldItemBuffer, mOldTexture);
|
---|
| 388 |
|
---|
| 389 | for (int y = 0; y < texHeight; ++ y)
|
---|
| 390 | {
|
---|
| 391 | for (int x = 0; x < texWidth; ++ x)
|
---|
| 392 | {
|
---|
| 393 | const int index = x + texWidth * y;
|
---|
| 394 | const float newDepth = mNewDepthBuffer[index];
|
---|
| 395 | const float oldDepth = mOldDepthBuffer[index];
|
---|
| 396 |
|
---|
| 397 | float newDummy1 = mNear + (mFar - mNear) * newDepth;
|
---|
| 398 | float oldDummy1 = mNear + (mFar - mNear) * oldDepth;
|
---|
| 399 |
|
---|
| 400 | float newDummy2 = x - texWidth / 2;
|
---|
| 401 | float newDummy3 = y - texHeight / 2;
|
---|
| 402 |
|
---|
| 403 | float oldDummy2 = newDummy2;
|
---|
| 404 | float oldDummy3 = newDummy3;
|
---|
| 405 |
|
---|
| 406 | const Vector3 newPt = newDepth * mEyeVec + newDummy1 * mLeftVec + newDummy2 * mUpVec;
|
---|
| 407 | const Vector3 oldPt = oldDepth * mEyeVec + oldDummy1 * mLeftVec + oldDummy2 * mUpVec;
|
---|
| 408 |
|
---|
| 409 | Intersectable *termObj1 = GetObject(mNewItemBuffer[index]);
|
---|
| 410 | Intersectable *termObj2 = GetObject(mOldItemBuffer[index]);
|
---|
| 411 |
|
---|
| 412 | // create rays in both directions
|
---|
| 413 | vssRays.push_back(new VssRay(oldPt, newPt, NULL, termObj1));
|
---|
| 414 | vssRays.push_back(new VssRay(newPt, oldPt, NULL, termObj2));
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 |
|
---|
[1944] | 420 | void GlobalLinesRenderer::Run()
|
---|
| 421 | {
|
---|
| 422 | glutMainLoop();
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 |
|
---|
[1953] | 426 | void GlobalLinesRenderer::GrabDepthBuffer(float *data, RenderTexture *rt)
|
---|
| 427 | {
|
---|
| 428 | rt->BindDepth();
|
---|
| 429 | rt->EnableTextureTarget();
|
---|
| 430 | cout << "depth: " << mNewTexture->GetDepthBits() << endl;
|
---|
| 431 |
|
---|
| 432 | const int texFormat = GL_DEPTH_COMPONENT;
|
---|
| 433 | glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
|
---|
| 434 |
|
---|
| 435 | mNewTexture->DisableTextureTarget();
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 |
|
---|
| 439 | void GlobalLinesRenderer::GrabItemBuffer(float *data, RenderTexture *rt)
|
---|
| 440 | {
|
---|
| 441 | rt->BindDepth();
|
---|
| 442 | rt->EnableTextureTarget();
|
---|
| 443 | cout << "depth: " << mNewTexture->GetDepthBits() << endl;
|
---|
| 444 |
|
---|
| 445 | const int texFormat = GL_DEPTH_COMPONENT;
|
---|
| 446 | glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
|
---|
| 447 |
|
---|
| 448 | mNewTexture->DisableTextureTarget();
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | void GlobalLinesRenderer::ExportDepthBuffer()
|
---|
| 452 | {
|
---|
| 453 | mNewTexture->BindDepth();
|
---|
| 454 | mNewTexture->EnableTextureTarget();
|
---|
| 455 | cout << "depth: " << mNewTexture->GetDepthBits() << endl;
|
---|
| 456 |
|
---|
| 457 | const bool components = 1;//mNewTexture->GetDepthBits() / 8;
|
---|
| 458 |
|
---|
| 459 | float *data = new float[texWidth * texHeight * components];
|
---|
| 460 | //const int texFormat = WGL_TEXTURE_DEPTH_COMPONENT_NV;
|
---|
| 461 | const int texFormat = GL_DEPTH_COMPONENT;
|
---|
| 462 | //const int texFormat = GL_RGBA;
|
---|
| 463 | glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
|
---|
| 464 |
|
---|
| 465 | /*for (int i = texWidth * 123; i < texWidth * 124; ++ i)
|
---|
| 466 | {
|
---|
| 467 | cout << data[i] << " ";
|
---|
| 468 | }
|
---|
| 469 | cout << "Saving image " << texWidth << " " << texHeight << endl;
|
---|
| 470 | */
|
---|
| 471 | string filename("depth.tga");
|
---|
| 472 | ilRegisterType(IL_FLOAT);
|
---|
| 473 |
|
---|
| 474 | const int depth = 1;
|
---|
| 475 | const int bpp = components;
|
---|
| 476 |
|
---|
| 477 | ilTexImage(texWidth, texHeight, depth, bpp, IL_LUMINANCE, IL_FLOAT, data);
|
---|
| 478 | ilSaveImage((char *const)filename.c_str());
|
---|
| 479 |
|
---|
| 480 | cout << "finished" << endl;
|
---|
| 481 | delete data;
|
---|
| 482 | cout << "data deleted" << endl;
|
---|
| 483 | mNewTexture->DisableTextureTarget();
|
---|
| 484 | PrintGLerror("grab texture");
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 |
|
---|
[1944] | 488 | void GlobalLinesRenderer::ApplyDepthPeeling(Beam &beam, const int samples)
|
---|
| 489 | {
|
---|
[1953] | 490 | mNewTexture->BeginCapture();
|
---|
[1944] | 491 | {
|
---|
| 492 | //cgGLBindProgram(sCgPassThroughProgram);
|
---|
| 493 | //cgGLEnableProfile(sCgFragmentProfile);
|
---|
[1951] | 494 | DrawGeometry();
|
---|
[1944] | 495 | }
|
---|
[1953] | 496 | mNewTexture->EndCapture();
|
---|
[1944] | 497 |
|
---|
[1951] | 498 | PrintGLerror("firstpass");
|
---|
[1950] | 499 |
|
---|
[1953] | 500 | if (mNewTexture->IsRectangleTexture()) cout << "rect" << endl;
|
---|
[1944] | 501 |
|
---|
[1949] | 502 | for(int i = 0; i < mMaxDepth; ++ i)
|
---|
[1944] | 503 | {
|
---|
| 504 | // Peel another layer
|
---|
| 505 | SwitchRenderTextures(); // switch pointer between rendertextures
|
---|
| 506 |
|
---|
[1953] | 507 | mNewTexture->BeginCapture();
|
---|
[1944] | 508 | {
|
---|
[1953] | 509 | //if (mNewTexture->IsDoubleBuffered())
|
---|
[1944] | 510 | // glDrawBuffer(GL_BACK);
|
---|
| 511 |
|
---|
| 512 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 513 |
|
---|
| 514 | cgGLBindProgram(sCgDepthPeelingProgram);
|
---|
[1949] | 515 | // cgGLBindProgram(sCgPassThroughProgram);
|
---|
[1944] | 516 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
[1953] | 517 | cgGLSetTextureParameter(sTextureParam, mOldTexture->GetDepthTextureID());
|
---|
[1944] | 518 | cgGLEnableTextureParameter(sTextureParam);
|
---|
| 519 |
|
---|
[1951] | 520 | //glutSolidTorus(0.25, 1, 32, 64);
|
---|
| 521 | DrawGeometry();
|
---|
| 522 |
|
---|
[1949] | 523 | glPopMatrix();
|
---|
| 524 |
|
---|
[1944] | 525 | cgGLDisableTextureParameter(sTextureParam);
|
---|
[1949] | 526 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
[1944] | 527 | }
|
---|
[1953] | 528 | mNewTexture->EndCapture();
|
---|
[1944] | 529 | }
|
---|
| 530 |
|
---|
[1951] | 531 | PrintGLerror("endpeeling");
|
---|
[1949] | 532 |
|
---|
[1953] | 533 | //mNewTexture->Bind();
|
---|
| 534 | mNewTexture->BindDepth();
|
---|
| 535 | mNewTexture->EnableTextureTarget();
|
---|
[1951] | 536 |
|
---|
| 537 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 538 |
|
---|
[1953] | 539 | if (mNewTexture->IsRectangleTexture())
|
---|
[1944] | 540 | {
|
---|
[1951] | 541 | glBegin(GL_QUADS);
|
---|
| 542 | glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
|
---|
[1953] | 543 | glTexCoord2f(mNewTexture->GetWidth(), 0); glVertex3f( 1, -1, -0.5f);
|
---|
| 544 | glTexCoord2f(mNewTexture->GetWidth(), mNewTexture->GetHeight()); glVertex3f( 1, 1, -0.5f);
|
---|
| 545 | glTexCoord2f(0, mNewTexture->GetHeight()); glVertex3f(-1, 1, -0.5f);
|
---|
[1951] | 546 | glEnd();
|
---|
[1944] | 547 | }
|
---|
[1951] | 548 | else
|
---|
[1944] | 549 | {
|
---|
[1951] | 550 | glBegin(GL_QUADS);
|
---|
| 551 | glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
|
---|
| 552 | glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f);
|
---|
| 553 | glTexCoord2f(1, 1); glVertex3f( 1, 1, -0.5f);
|
---|
| 554 | glTexCoord2f(0, 1); glVertex3f(-1, 1, -0.5f);
|
---|
| 555 | glEnd();
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[1953] | 558 | mNewTexture->DisableTextureTarget();
|
---|
[1951] | 559 | PrintGLerror("displaytexture");
|
---|
[1944] | 560 | }
|
---|
| 561 |
|
---|
| 562 |
|
---|
| 563 | }
|
---|