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