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