source: GTP/trunk/Lib/Vis/Preprocessing/src/GlobalLinesRenderer.cpp @ 1961

Revision 1961, 21.0 KB checked in by mattausch, 18 years ago (diff)
RevLine 
[1944]1#include "glInterface.h"
2#include "GlobalLinesRenderer.h"
3#include "common.h"
4#include "RenderTexture.h"
5#include "Preprocessor.h"
6#include "GlRenderer.h"
[1960]7#include "Exporter.h"
8
[1953]9// the devil library
10#include <IL/il.h>
11#include <IL/ilu.h>
12#include <IL/ilut.h>
[1944]13
14#include <Cg/cg.h>
15#include <Cg/cgGL.h>
16
17//#include <QtOpenGL>
18
19namespace GtpVisibilityPreprocessor {
20
21
22static CGcontext sCgContext = NULL;
23static CGprogram sCgDepthPeelingProgram = NULL;
24static CGprogram sCgPassThroughProgram = NULL;
25
26static CGprofile sCgFragmentProfile;
27static CGparameter sTextureParam;
28
29GlobalLinesRenderer *globalLinesRenderer = NULL;
30
[1961]31static int texWidth = 256;
32static int texHeight = 256;
[1944]33
[1960]34static bool isDepth = true;
[1953]35
36static 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
[1944]52static 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
66static 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
79void 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
[1949]88        //gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 3, 5000.0);
[1944]89        //gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.5, 10.0);
[1951]90        glOrtho(-1, 1, -1, 1, 0.5, 15);
[1944]91}
92
93
[1960]94void SetFrustum(const int sizeX, const int sizeY,
95                                const float nearPlane, const float farPlane)
[1953]96{
97        glMatrixMode(GL_PROJECTION);
98        glLoadIdentity();
99
[1960]100        glOrtho(-sizeX * 0.5, sizeX * 0.5,
101                        -sizeY * 0.5, sizeY * 0.5,
[1953]102                        nearPlane, farPlane);
103
104        /*glOrtho(0, sizeX,
105                    0, sizeY ,
106                        nearPlane, farPlane);*/
107}
108
[1944]109void Display()
110{
111        //globalLinesRenderer->DrawGeometry();
[1958]112        //globalLinesRenderer->CastGlobalLines(Beam(), 0);
[1960]113        globalLinesRenderer->DisplayBuffer(isDepth);
[1944]114        PrintGLerror("display");
115
116        glutSwapBuffers();
117}
118
119
120void Idle()
121{
122        glutPostRedisplay();
123}
124
125
126void Keyboard(unsigned char key, int x, int y)
127{
128        switch(key)
129        {
[1949]130        case '2':
[1958]131                {
132                        VssRayContainer rays;
133
134                        ++ globalLinesRenderer->mMaxDepth;
135                        globalLinesRenderer->ApplyDepthPeeling(rays, Beam(), 0);
136
137                        cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
[1960]138                        CLEAR_CONTAINER(rays);
[1958]139                        return;
140                }
[1949]141        case '1':
[1958]142                {
143                        VssRayContainer rays;
144
145                        -- globalLinesRenderer->mMaxDepth;
146                        cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
147                       
148                        globalLinesRenderer->ApplyDepthPeeling(rays, Beam(), 0);
[1960]149                        CLEAR_CONTAINER(rays);
[1958]150                        return;
151                }
[1953]152        case '3':
[1958]153                {
154                        //globalLinesRenderer->ApplyDepthPeeling(Beam(), 0);
155                        globalLinesRenderer->ExportDepthBuffer();
[1960]156                       
[1958]157                        return;
158                }
159        case '4':
160                {
[1960]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                {
[1958]173                        VssRayContainer rays;
[1960]174                        globalLinesRenderer->ApplyDepthPeeling(rays, Beam(), 0);
175                        VssRayContainer outRays;
176                        VssRayContainer::const_iterator vit, vit_end = rays.end();
[1958]177
[1960]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;
[1958]195                        globalLinesRenderer->ApplyDepthPeeling(rays, Beam(), 0);
[1960]196                        CLEAR_CONTAINER(rays);
[1958]197                        return;
198                }
[1944]199        default:
200                return;
201        }
202}
203
[1953]204
[1944]205/*GlobalLinesRenderer::GlobalLinesRenderer(RenderTexture *buffer1,
206                                                                                 RenderTexture *buffer2,
207                                                                                 Preprocessor *preprocessor,
208                                                                                 GlRenderer *renderer)
[1953]209: mNewTexture(buffer1), mOldTexture(buffer2), mPreprocessor(preprocessor), mMaxDepth(100),
[1944]210mRenderer(renderer)
211{
212}
213*/
[1953]214
215
[1949]216GlobalLinesRenderer::GlobalLinesRenderer(Preprocessor *preprocessor,
217                                                                                 GlRenderer *renderer):
[1953]218mNewTexture(NULL),
219mOldTexture(NULL),
[1949]220mMaxDepth(0),
[1944]221mRenderer(renderer),
222mPreprocessor(preprocessor)
223{
224}
225
226
[1960]227void GlobalLinesRenderer::DisplayBuffer(const bool isDepth)
[1958]228{
[1960]229        if (!isDepth)
230                mNewTexture->Bind();
231        else
232                mNewTexture->BindDepth();
[1958]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
[1944]261GlobalLinesRenderer::~GlobalLinesRenderer()
262{
263        if (sCgDepthPeelingProgram)
264                cgDestroyProgram(sCgDepthPeelingProgram);
265        if (sCgContext)
266                cgDestroyContext(sCgContext);
[1958]267
268        // init the receiving buffers
269        delete mNewDepthBuffer;
270        delete mOldDepthBuffer;
271       
272        delete mNewItemBuffer;
273        delete mOldItemBuffer;
[1944]274}
275
276 
277void GlobalLinesRenderer::CastGlobalLines(Beam &beam, const int samples)
278{
[1958]279        VssRayContainer rays;
280
[1944]281        // bind pixel shader implementing the front depth buffer functionality
[1958]282        ApplyDepthPeeling(rays, beam, samples);
[1944]283}
284
285
286void GlobalLinesRenderer::RenderObject(Intersectable *obj)
287{
288        mRenderer->RenderIntersectable(obj);
289}
290
291
292void GlobalLinesRenderer::DrawGeometry()
[1945]293{
[1951]294        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
295        glMatrixMode(GL_MODELVIEW);
[1944]296
[1951]297        glPushMatrix();
298        {
[1953]299                //glLoadIdentity();
[1951]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                {
[1960]306                        cout << (*oit)->GetId() << " ";
[1951]307                        RenderObject(*oit);
308                }
[1944]309        }
[1951]310        glPopMatrix();
[1944]311}
312
313
314void GlobalLinesRenderer::SwitchRenderTextures()
315{
[1953]316        RenderTexture *buffer = mOldTexture;
317        mOldTexture = mNewTexture;
318        mNewTexture = buffer;
[1944]319}
320
321
[1958]322void 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
[1944]338void GlobalLinesRenderer::InitGl()
339{
[1953]340        InitDevIl();
341
[1960]342    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
[1944]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
[1958]364        // initialise the receiving buffers
[1954]365        mNewDepthBuffer = new float[texWidth * texHeight];
[1960]366        mNewItemBuffer = new unsigned char[texWidth * texHeight * 4];
367
[1954]368        mOldDepthBuffer = new float[texWidth * texHeight];
[1960]369    mOldItemBuffer = new unsigned char[texWidth * texHeight * 4];
370
[1958]371        for (int i = 0; i < texWidth * texHeight; ++ i)
372        {
373                mNewDepthBuffer[i] = 1;
374                mOldDepthBuffer[i] = 1;
375
[1960]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;
[1958]385        }
386
[1944]387        AxisAlignedBox3 bbox = globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
[1951]388       
[1960]389        Vector3 midPoint = bbox.Center();
[1953]390       
391        const float sceneSize = Magnitude(bbox.Diagonal());
[1960]392        mViewPoint = midPoint + Vector3(0.5 * sceneSize, 0, 0);
[1953]393       
[1960]394        cout << "mid point: " << midPoint << endl;
395        cout << "view point: " << mViewPoint << endl;
[1944]396        cout << "scene: " << bbox << endl;
397
[1960]398        mNear = 1;
399        mFar = sceneSize;
400        mWidth = sceneSize;
401
402        mEyeVec = Normalize(midPoint - mViewPoint);
[1958]403        mEyeVec.RightHandedBase(mUpVec, mLeftVec);
404
[1960]405        /*gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
[1944]406                          midPoint.x, midPoint.y, midPoint.z,
407                          0, 1, 0);
408*/
409        gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
410
[1949]411        //glDisable(GL_CULL_FACE);
412        glEnable(GL_CULL_FACE);
[1944]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       
[1953]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);
[1949]425       
[1953]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);
[1944]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,
[1953]445                                                                mNewTexture->IsRectangleTexture() ?
[1951]446                                                                "../src/depth_peelingRect.cg" : "../src/depth_peeling2d.cg",
[1944]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
[1953]471        mNewTexture->BeginCapture();
[1944]472        {
[1953]473                //Reshape(texWidth, texHeight);
474                glViewport(0, 0, texWidth, texHeight);
[1960]475                SetFrustum(mWidth, mWidth, mNear, mFar);
[1953]476
[1958]477                // for item buffer: white means no color
478                glClearColor(1, 1, 1, 1);
[1944]479                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
[1949]480
481                glFrontFace(GL_CCW);
482                glCullFace(GL_BACK);
[1960]483
484                glDisable(GL_CULL_FACE);
485                //glEnable(GL_CULL_FACE);
486
[1949]487                glShadeModel(GL_FLAT);
488                glEnable(GL_DEPTH_TEST);
489               
[1944]490                glMatrixMode(GL_MODELVIEW);
491                glLoadIdentity();
[1960]492                gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
493                                  midPoint.x, midPoint.y, midPoint.z,
[1944]494                                  0, 1, 0);
[1953]495
[1944]496        }
[1953]497        mNewTexture->EndCapture();
[1944]498
[1958]499        // setup the rendering context for the other depth buffer
[1953]500        mOldTexture->BeginCapture();
[1944]501        {
[1958]502                // for item buffer: white means no color
503                glClearColor(1, 1, 1, 1);
[1953]504       
505                //Reshape(texWidth, texHeight);
506                glViewport(0, 0, texWidth, texHeight);
[1960]507                SetFrustum(mWidth, mWidth, mNear, mFar);
[1953]508
[1944]509                glMatrixMode(GL_MODELVIEW);
510                glLoadIdentity();
[1949]511               
512                glFrontFace(GL_CCW);
513                glCullFace(GL_BACK);
[1960]514               
515                glDisable(GL_CULL_FACE);
516                //glEnable(GL_CULL_FACE);
517               
[1949]518                glShadeModel(GL_FLAT);
519                glEnable(GL_DEPTH_TEST);
520               
[1960]521                gluLookAt(mViewPoint.x, mViewPoint.y, mViewPoint.z,
522                                  midPoint.x, midPoint.y, midPoint.z,
[1944]523                                  0, 1, 0);
524
[1953]525                //SetFrustum(sceneSize, sceneSize, 1, sceneSize);
[1944]526        }
[1953]527        mOldTexture->EndCapture();
[1944]528
529        PrintGLerror("init");
530}
531
532
[1960]533Intersectable *GlobalLinesRenderer::ExtractSamplePoint(float *depthBuffer,
534                                                                                                           unsigned char *itemBuffer,
535                                                                                                           const int x,
536                                                                                                           const int y,
537                                                                                                           Vector3 &hitPoint,
538                                                                                                           const bool isFrontBuffer) const
[1944]539{
[1960]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
[1961]571        // test for invalid view space
572        if (DotProd(dir, norm) >= -Limits::Small)
573                return NULL;
574
575        return intersect;
[1944]576}
577
578
[1960]579void GlobalLinesRenderer::ProcessDepthBuffer(VssRayContainer &vssRays,
580                                                                                         const bool oldBufferInitialised)
[1953]581{
582        GrabDepthBuffer(mNewDepthBuffer, mNewTexture);
583        GrabItemBuffer(mNewItemBuffer, mNewTexture);
584
[1960]585        if (oldBufferInitialised)
[1953]586        {
[1960]587                GrabDepthBuffer(mOldDepthBuffer, mOldTexture);
588                GrabItemBuffer(mOldItemBuffer, mOldTexture);
589        }
590        else
591        {
592                for (int i = 0; i < texWidth * texHeight; ++ i)
[1953]593                {
[1960]594                        mOldDepthBuffer[i] = 0;
[1953]595
[1960]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        }
[1953]602
[1960]603        cout << "vp: " << "eye: " << mEyeVec << " left: " << mLeftVec << " up: " << mUpVec << endl;
[1953]604
[1960]605        for (int y = 0; y < texHeight; ++ y)
606        {
607                for (int x = 0; x < texWidth; ++ x)
608                {
609                        Vector3 newPt, oldPt;
[1953]610
[1960]611                        Intersectable *termObj1 = ExtractSamplePoint(mNewDepthBuffer,
612                                                                                                                 mNewItemBuffer,
613                                                                                                                 x,
614                                                                                                                 y,
615                                                                                                                 newPt,
616                                                                                                                 true);
[1953]617
[1960]618                        Intersectable *termObj2 = ExtractSamplePoint(mOldDepthBuffer,
619                                                                                                                 mOldItemBuffer,
620                                                                                                                 x,
621                                                                                                                 y,
622                                                                                                                 oldPt,
623                                                                                                                 false);
[1953]624
[1960]625                        // create rays in both directions
626                        if (termObj1)
[1958]627                        {
[1960]628                                vssRays.push_back(new VssRay(oldPt, newPt, NULL, termObj1));
629                                //cout << "new pt: " << newPt << endl;
[1958]630                        }
631
[1961]632                        if (termObj2)
633                        {
634                                vssRays.push_back(new VssRay(newPt, oldPt, NULL, termObj2));
635                                //cout << "old pt: " << oldPt << endl;
636                        }
[1953]637                }
638        }
639}
640
641
[1944]642void GlobalLinesRenderer::Run()
643{
644        glutMainLoop();
645}
646
647
[1960]648void 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
661void 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
[1961]681//      exporter->ExportRays(vssRays);
[1960]682
683        delete exporter;
684}
685
686
[1953]687void 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
[1960]700void GlobalLinesRenderer::GrabItemBuffer(unsigned char *data, RenderTexture *rt)
[1953]701{
[1958]702        rt->Bind();
[1953]703        rt->EnableTextureTarget();
[1958]704        //cout << "depth: " << mNewTexture->GetDepthBits() << endl;
[1953]705
[1958]706        const int texFormat = GL_RGBA;
[1960]707        glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_UNSIGNED_BYTE, data);
[1953]708
709        mNewTexture->DisableTextureTarget();
710}
711
[1958]712
[1953]713void GlobalLinesRenderer::ExportDepthBuffer()
714{
715        mNewTexture->BindDepth();
716        mNewTexture->EnableTextureTarget();
717        cout << "depth: " << mNewTexture->GetDepthBits() << endl;
718
[1960]719        const int components = 1;//mNewTexture->GetDepthBits() / 8;
[1953]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
[1958]749/*
750void GlobalLinesRenderer::ExportItemBuffer()
[1944]751{
[1960]752        mNewTexture->Bind();
[1958]753        mNewTexture->EnableTextureTarget();
754        cout << "depth: " << mNewTexture->GetDepthBits() << endl;
755
[1960]756        const int components = 4;//mNewTexture->GetDepthBits() / 8;
[1958]757
[1960]758        float *data = new float[texWidth * texHeight * components*8];
759        const int texFormat = GL_RGBA;
[1958]760        glGetTexImage(mNewTexture->GetTextureTarget(), 0, texFormat, GL_FLOAT, data);
761
[1960]762        string filename("items.tga");
[1958]763        ilRegisterType(IL_FLOAT);
764
765        const int depth = 1;
766        const int bpp = components;
767
[1960]768        cout << "saving items to file " << endl;
769
770        ilTexImage(16, 16, depth, bpp, IL_RGBA, IL_FLOAT, data);
[1958]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*/
[1960]780void GlobalLinesRenderer::ExportItemBuffer()
781{
782        mNewTexture->Bind();
783        mNewTexture->EnableTextureTarget();
784        cout << "depth: " << mNewTexture->GetDepthBits() << endl;
[1958]785
[1960]786        const int components = 4;//mNewTexture->GetDepthBits() / 8;
[1958]787
[1960]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
[1958]810void GlobalLinesRenderer::ApplyDepthPeeling(VssRayContainer &rays,
811                                                                                        Beam &beam,
812                                                                                        const int samples)
813{
[1953]814        mNewTexture->BeginCapture();
[1944]815        {
816                //cgGLBindProgram(sCgPassThroughProgram);
817                //cgGLEnableProfile(sCgFragmentProfile);
[1951]818                DrawGeometry();
[1944]819        }
[1953]820        mNewTexture->EndCapture();
[1944]821       
[1951]822        PrintGLerror("firstpass");
[1953]823        if (mNewTexture->IsRectangleTexture()) cout << "rect" << endl;
[1944]824
[1958]825        // process the buffers for the first layer
[1960]826        ProcessDepthBuffer(rays, false);
[1958]827
[1949]828        for(int i = 0; i < mMaxDepth; ++ i)
[1944]829        {
830                // Peel another layer
831                SwitchRenderTextures(); // switch pointer between rendertextures
832
[1953]833                mNewTexture->BeginCapture();
[1944]834                {
[1953]835                        //if (mNewTexture->IsDoubleBuffered())
[1944]836                        //      glDrawBuffer(GL_BACK);
837
838                        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         
839
840                        cgGLBindProgram(sCgDepthPeelingProgram);
841                        cgGLEnableProfile(sCgFragmentProfile);
[1953]842                        cgGLSetTextureParameter(sTextureParam, mOldTexture->GetDepthTextureID());
[1944]843                        cgGLEnableTextureParameter(sTextureParam);
844
[1951]845                        //glutSolidTorus(0.25, 1, 32, 64);
846                        DrawGeometry();
847                       
[1949]848                        glPopMatrix();
849
[1944]850                        cgGLDisableTextureParameter(sTextureParam);
[1949]851                        cgGLDisableProfile(sCgFragmentProfile);
[1944]852                }
[1953]853                mNewTexture->EndCapture();
[1960]854
855                // process the buffers for following layer
856                ProcessDepthBuffer(rays, true);
[1944]857        }
858
[1951]859        PrintGLerror("endpeeling");
[1944]860}
861
862
863}
Note: See TracBrowser for help on using the repository browser.