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

Revision 1949, 9.9 KB checked in by mattausch, 18 years ago (diff)

worked on depth peeling

Line 
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
8#include <Cg/cg.h>
9#include <Cg/cgGL.h>
10
11//#include <QtOpenGL>
12
13namespace GtpVisibilityPreprocessor {
14
15
16static CGcontext sCgContext = NULL;
17static CGprogram sCgDepthPeelingProgram = NULL;
18static CGprogram sCgPassThroughProgram = NULL;
19
20static CGprofile sCgFragmentProfile;
21static CGparameter sTextureParam;
22
23GlobalLinesRenderer *globalLinesRenderer = NULL;
24
25
26static void cgErrorCallback()
27{
28        CGerror lastError = cgGetError();
29
30        if(lastError)
31        {
32                printf("%s\n\n", cgGetErrorString(lastError));
33                printf("%s\n", cgGetLastListing(sCgContext));
34                printf("Cg error, exiting...\n");
35
36                exit(0);
37        }
38}
39
40static void PrintGLerror(char *msg)
41{
42        GLenum errCode;
43        const GLubyte *errStr;
44       
45        if ((errCode = glGetError()) != GL_NO_ERROR)
46        {
47                errStr = gluErrorString(errCode);
48                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
49        }
50}
51
52
53void Reshape(int w, int h)
54{
55        if (h == 0) h = 1;
56
57        glViewport(0, 0, w, h);
58
59        glMatrixMode(GL_PROJECTION);
60        glLoadIdentity();
61
62        //gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 3, 5000.0);
63        //gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 0.5, 10.0);
64        glOrtho(-1000, 1000, -1000, 1000, 1, 1000);
65}
66
67
68void Display()
69{
70        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
71        glMatrixMode(GL_MODELVIEW);
72        glPushMatrix();
73       
74        //globalLinesRenderer->DrawGeometry();
75       
76        globalLinesRenderer->CastGlobalLines(Beam(), 0);
77
78        PrintGLerror("display");
79
80        glPopMatrix();
81        glutSwapBuffers();
82}
83
84
85void Idle()
86{
87        glutPostRedisplay();
88}
89
90
91void Keyboard(unsigned char key, int x, int y)
92{
93        switch(key)
94        {
95        case '2':
96                ++ globalLinesRenderer->mMaxDepth;
97                cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
98                return;
99        case '1':
100                -- globalLinesRenderer->mMaxDepth;
101                cout << "max depth: " << globalLinesRenderer->mMaxDepth << endl;
102                return;
103        default:
104                return;
105        }
106}
107
108/*GlobalLinesRenderer::GlobalLinesRenderer(RenderTexture *buffer1,
109                                                                                 RenderTexture *buffer2,
110                                                                                 Preprocessor *preprocessor,
111                                                                                 GlRenderer *renderer)
112: mNewBuffer(buffer1), mOldBuffer(buffer2), mPreprocessor(preprocessor), mMaxDepth(100),
113mRenderer(renderer)
114{
115}
116*/
117GlobalLinesRenderer::GlobalLinesRenderer(Preprocessor *preprocessor,
118                                                                                 GlRenderer *renderer):
119mNewBuffer(NULL),
120mOldBuffer(NULL),
121mMaxDepth(0),
122mRenderer(renderer),
123mPreprocessor(preprocessor)
124{
125}
126
127
128GlobalLinesRenderer::~GlobalLinesRenderer()
129{
130        if (sCgDepthPeelingProgram)
131                cgDestroyProgram(sCgDepthPeelingProgram);
132        if (sCgContext)
133                cgDestroyContext(sCgContext);
134}
135
136 
137void GlobalLinesRenderer::CastGlobalLines(Beam &beam, const int samples)
138{
139        // bind pixel shader implementing the front depth buffer functionality
140        ApplyDepthPeeling(beam, samples);
141}
142
143
144void GlobalLinesRenderer::RenderObject(Intersectable *obj)
145{
146        mRenderer->RenderIntersectable(obj);
147}
148
149
150void GlobalLinesRenderer::DrawGeometry()
151{
152        //mRenderer->mUseFalseColors = true;
153        ObjectContainer::const_iterator oit, oit_end = mPreprocessor->mObjects.end();
154
155        Intersectable::NewMail();
156
157        for (oit = mPreprocessor->mObjects.begin(); oit != oit_end; ++ oit)
158        {
159                RenderObject(*oit);
160        }
161}
162
163
164void GlobalLinesRenderer::SwitchRenderTextures()
165{
166        RenderTexture *buffer = mOldBuffer;
167        mOldBuffer = mNewBuffer;
168        mNewBuffer = buffer;
169}
170
171
172void GlobalLinesRenderer::InitGl()
173{
174        glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
175        glutInitWindowPosition(50, 50);
176        glutInitWindowSize(512, 512);
177        glutCreateWindow("TestRenderDepthTexture"); 
178
179        int err = glewInit();
180        if (GLEW_OK != err)
181        {
182                // problem: glewInit failed, something is seriously wrong
183                fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
184                exit(-1);
185        } 
186       
187        glutKeyboardFunc(Keyboard);
188        glutDisplayFunc(Display);
189        glutIdleFunc(Idle);
190        glutReshapeFunc(Reshape);
191
192        Reshape(512, 512);
193        glMatrixMode(GL_MODELVIEW);
194        glLoadIdentity();
195
196        AxisAlignedBox3 bbox = globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
197        const Vector3 midPoint = bbox.Center();
198        //const Vector3 viewPoint = midPoint + Vector3(-800, 0, 0);
199        const Vector3 viewPoint = midPoint + Vector3(2, 0, 0);
200
201        cout << "midPoint: " << midPoint << endl;
202        cout << "viewPoint: " << viewPoint << endl;
203        cout << "scene: " << bbox << endl;
204
205        /*gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
206                          midPoint.x, midPoint.y, midPoint.z,
207                          0, 1, 0);
208*/
209        gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
210
211        //glDisable(GL_CULL_FACE);
212        glEnable(GL_CULL_FACE);
213        glDisable(GL_LIGHTING);
214        //glEnable(GL_COLOR_MATERIAL);
215        glDisable(GL_COLOR_MATERIAL);
216        glEnable(GL_DEPTH_TEST);
217        glClearColor(0.1, 0.2, 0.3, 1);
218
219    int texWidth = 256, texHeight = 256;
220       
221        // A square, mipmapped, anisotropically filtered 8-bit RGBA texture with
222        // depth and stencil.
223        // Note that RT_COPY_TO_TEXTURE is required for depth textures on ATI hardware
224       
225        mNewBuffer = new RenderTexture(texWidth, texHeight, true, true);
226        //mNewBuffer->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
227        mNewBuffer->Initialize(true, true, false, true, true, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
228       
229        mOldBuffer = new RenderTexture(texWidth, texHeight, true, true);
230        //mOldBuffer ->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
231        mOldBuffer ->Initialize(true, true, false, true, true, 8, 8, 8, 8, RenderTexture::RT_COPY_TO_TEXTURE);
232
233        // Setup Cg
234        cgSetErrorCallback(cgErrorCallback);
235
236        // Create cgContext.
237        sCgContext = cgCreateContext();
238
239        // get the best profile for this hardware
240        sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
241       
242        //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);
243        cgGLSetOptimalOptions(sCgFragmentProfile);
244
245        sCgDepthPeelingProgram =
246                cgCreateProgramFromFile(sCgContext,
247                                                                CG_SOURCE,
248                                                                "../src/depth_peeling.cg",
249                                                                GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
250                                                                NULL,
251                                                                NULL);
252
253        if(sCgDepthPeelingProgram != NULL)
254        {
255                cgGLLoadProgram(sCgDepthPeelingProgram);
256                sTextureParam = cgGetNamedParameter(sCgDepthPeelingProgram, "depthTex"); 
257        }
258
259        sCgPassThroughProgram =
260                cgCreateProgramFromFile(sCgContext,
261                                                                CG_SOURCE,
262                                                                "../src/passthrough.cg",
263                                                                GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
264                                                                NULL,
265                                                                NULL);
266
267        if(sCgPassThroughProgram != NULL)
268        {
269                cgGLLoadProgram(sCgPassThroughProgram);
270        }
271
272
273    // setup the rendering context for the RenderTexture
274        mNewBuffer->BeginCapture();
275        {
276                Reshape(texWidth, texHeight);
277                glClearColor(0.1, 0.7, 0.2, 1);
278                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
279
280                glFrontFace(GL_CCW);
281                glCullFace(GL_BACK);
282                glEnable(GL_CULL_FACE);
283                glShadeModel(GL_FLAT);
284                glEnable(GL_DEPTH_TEST);
285               
286                glMatrixMode(GL_MODELVIEW);
287                glLoadIdentity();
288                gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
289                                  midPoint.x, midPoint.y, midPoint.z,
290                                  0, 1, 0);
291
292        }
293        mNewBuffer->EndCapture();
294
295        // setup the rendering context for the RenderTexture
296        mOldBuffer->BeginCapture();
297        {
298                Reshape(texWidth, texHeight);
299                glMatrixMode(GL_MODELVIEW);
300                glLoadIdentity();
301               
302                glFrontFace(GL_CCW);
303                glCullFace(GL_BACK);
304                glEnable(GL_CULL_FACE);
305                glShadeModel(GL_FLAT);
306                glEnable(GL_DEPTH_TEST);
307               
308                gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
309                                  midPoint.x, midPoint.y, midPoint.z,
310                                  0, 1, 0);
311
312                glClearColor(0.6, 0.2, 0.2, 1);
313        }
314        mOldBuffer->EndCapture();
315
316        PrintGLerror("init");
317}
318
319
320void GlobalLinesRenderer::ProcessDepthBuffer()
321{
322}
323
324
325void GlobalLinesRenderer::Run()
326{
327        glutMainLoop();
328}
329
330
331void GlobalLinesRenderer::ApplyDepthPeeling(Beam &beam, const int samples)
332{
333        //if (mNewBuffer->IsDoubleBuffered())
334        //      glDrawBuffer(GL_BACK);
335
336        mNewBuffer->BeginCapture();
337        {
338                //cgGLBindProgram(sCgPassThroughProgram);
339                //cgGLEnableProfile(sCgFragmentProfile);
340                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
341                glMatrixMode(GL_MODELVIEW);
342                glPushMatrix();
343                {
344                        //glutSolidTorus(0.25, 1, 32, 64);
345                        DrawGeometry();
346                }
347                glPopMatrix();
348        }
349        mNewBuffer->EndCapture();
350       
351        //mNewBuffer->BindDepth();
352        //float data[262144];
353        //glGetTexImage(mNewBuffer->GetTextureTarget(), 0, mNewBuffer->texture_format, GL_FLOAT, data);
354        if (mNewBuffer->IsRectangleTexture()) cout << "rect" << endl;
355//else cout << "haga"<<endl;
356
357        for(int i = 0; i < mMaxDepth; ++ i)
358        {
359                // Peel another layer
360                SwitchRenderTextures(); // switch pointer between rendertextures
361
362                mNewBuffer->BeginCapture();
363                {
364                        //if (mNewBuffer->IsDoubleBuffered())
365                        //      glDrawBuffer(GL_BACK);
366
367                        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         
368
369                        cgGLBindProgram(sCgDepthPeelingProgram);
370                //      cgGLBindProgram(sCgPassThroughProgram);
371                        cgGLEnableProfile(sCgFragmentProfile);
372                        cgGLSetTextureParameter(sTextureParam, mOldBuffer->GetDepthTextureID());
373                        cgGLEnableTextureParameter(sTextureParam);
374
375                        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
376                        glMatrixMode(GL_MODELVIEW);
377                        glPushMatrix();
378                        {
379                                //glutSolidTorus(0.25, 1, 32, 64);
380                                DrawGeometry();
381                        }
382                        glPopMatrix();
383
384                        cgGLDisableTextureParameter(sTextureParam);
385                        cgGLDisableProfile(sCgFragmentProfile);
386                }
387                mNewBuffer->EndCapture();
388        }
389
390        PrintGLerror("display 1");
391
392        //mNewBuffer->Bind();
393        mNewBuffer->BindDepth();
394        mNewBuffer->EnableTextureTarget();
395
396
397        PrintGLerror("display 2");
398        glColor3f(1,1,1);
399        glBegin(GL_QUADS);
400    glTexCoord2f(0, 0); glVertex3f(-1000, -1000, -0.5f);
401    glTexCoord2f(1, 0); glVertex3f( 1000, -1000, -0.5f);
402    glTexCoord2f(1, 1); glVertex3f( 1000,  1000, -0.5f);
403    glTexCoord2f(0, 1); glVertex3f(-1000,  1000, -0.5f);
404    glEnd();
405
406    if (mNewBuffer->IsFloatTexture())
407        {
408                // cgGLDisableTextureParameter(textureParam);
409                // cgGLDisableProfile(theProfile);
410        }
411        else
412        {
413                mNewBuffer->DisableTextureTarget();
414        }
415}
416
417
418}
Note: See TracBrowser for help on using the repository browser.