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