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