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

Revision 1945, 8.8 KB checked in by mattausch, 17 years ago (diff)
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}
65
66
67void Display()
68{
69        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
70        glMatrixMode(GL_MODELVIEW);
71        glPushMatrix();
72       
73        //globalLinesRenderer->DrawGeometry();
74       
75        globalLinesRenderer->CastGlobalLines(Beam(), 0);
76
77        PrintGLerror("display");
78
79        glPopMatrix();
80        glutSwapBuffers();
81}
82
83
84void Idle()
85{
86        glutPostRedisplay();
87}
88
89
90void Keyboard(unsigned char key, int x, int y)
91{
92        switch(key)
93        {
94        case ' ':
95                return;
96        default:
97                return;
98        }
99}
100
101/*GlobalLinesRenderer::GlobalLinesRenderer(RenderTexture *buffer1,
102                                                                                 RenderTexture *buffer2,
103                                                                                 Preprocessor *preprocessor,
104                                                                                 GlRenderer *renderer)
105: mNewBuffer(buffer1), mOldBuffer(buffer2), mPreprocessor(preprocessor), mMaxDepth(100),
106mRenderer(renderer)
107{
108}
109*/
110GlobalLinesRenderer::GlobalLinesRenderer(Preprocessor *preprocessor, GlRenderer *renderer):
111mNewBuffer(NULL),
112mOldBuffer(NULL),
113mMaxDepth(100),
114mRenderer(renderer),
115mPreprocessor(preprocessor)
116{
117}
118
119
120GlobalLinesRenderer::~GlobalLinesRenderer()
121{
122        if (sCgDepthPeelingProgram)
123                cgDestroyProgram(sCgDepthPeelingProgram);
124        if (sCgContext)
125                cgDestroyContext(sCgContext);
126}
127
128 
129void GlobalLinesRenderer::CastGlobalLines(Beam &beam, const int samples)
130{
131        // bind pixel shader implementing the front depth buffer functionality
132        ApplyDepthPeeling(beam, samples);
133}
134
135
136void GlobalLinesRenderer::RenderObject(Intersectable *obj)
137{
138        mRenderer->RenderIntersectable(obj);
139}
140
141
142void GlobalLinesRenderer::DrawGeometry()
143{
144        //mRenderer->mUseFalseColors = true;
145        ObjectContainer::const_iterator oit, oit_end = mPreprocessor->mObjects.end();
146
147        Intersectable::NewMail();
148
149        for (oit = mPreprocessor->mObjects.begin(); oit != oit_end; ++ oit)
150        {
151                RenderObject(*oit);
152        }
153}
154
155
156void GlobalLinesRenderer::SwitchRenderTextures()
157{
158        RenderTexture *buffer = mOldBuffer;
159        mOldBuffer = mNewBuffer;
160        mNewBuffer = buffer;
161}
162
163
164void GlobalLinesRenderer::InitGl()
165{
166        glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
167        glutInitWindowPosition(50, 50);
168        glutInitWindowSize(512, 512);
169        glutCreateWindow("TestRenderDepthTexture"); 
170
171        int err = glewInit();
172        if (GLEW_OK != err)
173        {
174                // problem: glewInit failed, something is seriously wrong
175                fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
176                exit(-1);
177        } 
178       
179        glutKeyboardFunc(Keyboard);
180        glutDisplayFunc(Display);
181        glutIdleFunc(Idle);
182        glutReshapeFunc(Reshape);
183
184        Reshape(512, 512);
185        glMatrixMode(GL_MODELVIEW);
186        glLoadIdentity();
187
188        AxisAlignedBox3 bbox = globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
189        const Vector3 midPoint = bbox.Center();
190        const Vector3 viewPoint = midPoint + Vector3(-800, 0, 0);
191
192        cout << "midPoint: " << midPoint << endl;
193        cout << "viewPoint: " << viewPoint << endl;
194        cout << "scene: " << bbox << endl;
195
196        /*gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
197                          midPoint.x, midPoint.y, midPoint.z,
198                          0, 1, 0);
199*/
200        gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
201
202        glDisable(GL_CULL_FACE);
203        glDisable(GL_LIGHTING);
204        //glEnable(GL_COLOR_MATERIAL);
205        glDisable(GL_COLOR_MATERIAL);
206        glEnable(GL_DEPTH_TEST);
207        glClearColor(0.1, 0.2, 0.3, 1);
208
209    int texWidth = 256, texHeight = 256;
210       
211        // A square, mipmapped, anisotropically filtered 8-bit RGBA texture with
212        // depth and stencil.
213        // Note that RT_COPY_TO_TEXTURE is required for depth textures on ATI hardware
214       
215        mNewBuffer = new RenderTexture(texWidth, texHeight, true, true);
216        mNewBuffer->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
217
218        mOldBuffer = new RenderTexture(texWidth, texHeight, true, true);
219        mOldBuffer ->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
220
221        // Setup Cg
222        cgSetErrorCallback(cgErrorCallback);
223
224        // Create cgContext.
225        sCgContext = cgCreateContext();
226
227        // get the best profile for this hardware
228        sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
229       
230        //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);
231        cgGLSetOptimalOptions(sCgFragmentProfile);
232
233        sCgDepthPeelingProgram =
234                cgCreateProgramFromFile(sCgContext,
235                                                                CG_SOURCE,
236                                                                "../src/depth_peeling.cg",
237                                                                GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
238                                                                NULL,
239                                                                NULL);
240
241        if(sCgDepthPeelingProgram != NULL)
242        {
243                cgGLLoadProgram(sCgDepthPeelingProgram);
244                sTextureParam = cgGetNamedParameter(sCgDepthPeelingProgram, "depthTex"); 
245        }
246
247        sCgPassThroughProgram =
248                cgCreateProgramFromFile(sCgContext,
249                                                                CG_SOURCE,
250                                                                "../src/passthrough.cg",
251                                                                GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
252                                                                NULL,
253                                                                NULL);
254
255        if(sCgPassThroughProgram != NULL)
256        {
257                cgGLLoadProgram(sCgPassThroughProgram);
258        }
259
260
261    // setup the rendering context for the RenderTexture
262        mNewBuffer->BeginCapture();
263        {
264                Reshape(texWidth, texHeight);
265                glClearColor(0.1, 0.7, 0.2, 1);
266                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
267       
268                glMatrixMode(GL_MODELVIEW);
269                glLoadIdentity();
270                gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
271                                  midPoint.x, midPoint.y, midPoint.z,
272                                  0, 1, 0);
273
274                glEnable(GL_DEPTH_TEST);
275        }
276        mNewBuffer->EndCapture();
277
278        // setup the rendering context for the RenderTexture
279        mOldBuffer->BeginCapture();
280        {
281                Reshape(texWidth, texHeight);
282                glMatrixMode(GL_MODELVIEW);
283                glLoadIdentity();
284                gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
285                                  midPoint.x, midPoint.y, midPoint.z,
286                                  0, 1, 0);
287
288                glEnable(GL_DEPTH_TEST);
289                glClearColor(0.6, 0.2, 0.2, 1);
290        }
291        mOldBuffer->EndCapture();
292
293        PrintGLerror("init");
294}
295
296
297void GlobalLinesRenderer::ProcessDepthBuffer()
298{
299}
300
301
302void GlobalLinesRenderer::Run()
303{
304        glutMainLoop();
305}
306
307
308void GlobalLinesRenderer::ApplyDepthPeeling(Beam &beam, const int samples)
309{
310        //if (mNewBuffer->IsDoubleBuffered())
311        //      glDrawBuffer(GL_BACK);
312
313        mNewBuffer->BeginCapture();
314        {
315                //cgGLBindProgram(sCgPassThroughProgram);
316                //cgGLEnableProfile(sCgFragmentProfile);
317                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
318                glMatrixMode(GL_MODELVIEW);
319                glPushMatrix();
320                {
321                        //glutSolidTorus(0.25, 1, 32, 64);
322                        DrawGeometry();
323                }
324                glPopMatrix();
325        }
326        mNewBuffer->EndCapture();
327       
328        //mNewBuffer->Bind();
329        mNewBuffer->BindDepth();
330        mNewBuffer->EnableTextureTarget();
331
332
333        //glBindTexture(texture_target,texID);
334        //const int texID = mNewBuffer->GetTextureTarget();
335        //glGetTexImage(texture_target,0,texture_format,GL_FLOAT,data);
336/*
337        for(int l = 0; l < mMaxDepth; ++ l)
338        {
339                // Peel another layer
340                SwitchRenderTextures(); // switch pointer between rendertextures
341
342                mNewBuffer->BeginCapture();
343                {
344                        //if (mNewBuffer->IsDoubleBuffered())
345                        //      glDrawBuffer(GL_BACK);
346
347                        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         
348
349                        cgGLBindProgram(sCgDepthPeelingProgram);
350                        cgGLEnableProfile(sCgFragmentProfile);
351                        cgGLSetTextureParameter(sTextureParam, mOldBuffer->GetDepthTextureID());
352                        cgGLEnableTextureParameter(sTextureParam);
353
354                        //glColor3f(0,0.0,0.0);
355                        DrawGeometry();
356                        cgGLDisableTextureParameter(sTextureParam);
357                }
358                mNewBuffer->EndCapture();
359        }
360*/
361
362        PrintGLerror("display 2");
363        glColor3f(1,1,1);
364        glBegin(GL_QUADS);
365    glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
366    glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f);
367    glTexCoord2f(1, 1); glVertex3f( 1,  1, -0.5f);
368    glTexCoord2f(0, 1); glVertex3f(-1,  1, -0.5f);
369    glEnd();
370        PrintGLerror("display 1");
371
372    if (mNewBuffer->IsFloatTexture())
373        {
374                // cgGLDisableTextureParameter(textureParam);
375                // cgGLDisableProfile(theProfile);
376        }
377        else
378        {
379                mNewBuffer->DisableTextureTarget();
380        }
381}
382
383
384}
Note: See TracBrowser for help on using the repository browser.