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

Revision 1944, 9.2 KB checked in by mattausch, 17 years ago (diff)

worked on global lines

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        //cgGLBindProgram(sCgDepthPeelingProgram);
133        //cgGLEnableProfile(sCgFragmentProfile);
134
135        ApplyDepthPeeling(beam, samples);
136}
137
138
139void GlobalLinesRenderer::RenderObject(Intersectable *obj)
140{
141        mRenderer->RenderIntersectable(obj);
142}
143
144
145void GlobalLinesRenderer::DrawGeometry()
146{ //glutWireTorus(0.25, 1, 32, 64);
147        mRenderer->mUseFalseColors = true;
148        ObjectContainer::const_iterator oit, oit_end = mPreprocessor->mObjects.end();
149
150        float r = Random(1.0f);
151        float g = Random(1.0f);
152        float b = Random(1.0f);
153
154        Intersectable::NewMail();
155
156        //cout << r << " " << g << " " << b << " ";
157        for (oit = mPreprocessor->mObjects.begin(); oit != oit_end; ++ oit)
158        {
159                glColor3f(r, g, b);
160                RenderObject(*oit);
161        }
162}
163
164
165void GlobalLinesRenderer::SwitchRenderTextures()
166{
167        RenderTexture *buffer = mOldBuffer;
168        mOldBuffer = mNewBuffer;
169        mNewBuffer = buffer;
170}
171
172
173void GlobalLinesRenderer::InitGl()
174{
175        glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
176        glutInitWindowPosition(50, 50);
177        glutInitWindowSize(512, 512);
178        glutCreateWindow("TestRenderDepthTexture"); 
179
180        int err = glewInit();
181        if (GLEW_OK != err)
182        {
183                // problem: glewInit failed, something is seriously wrong
184                fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
185                exit(-1);
186        } 
187       
188        glutKeyboardFunc(Keyboard);
189        glutDisplayFunc(Display);
190        glutIdleFunc(Idle);
191        glutReshapeFunc(Reshape);
192
193        Reshape(512, 512);
194        glMatrixMode(GL_MODELVIEW);
195        glLoadIdentity();
196
197        AxisAlignedBox3 bbox = globalLinesRenderer->mPreprocessor->mKdTree->GetBox();
198        const Vector3 midPoint = bbox.Center();
199        const Vector3 viewPoint = midPoint + Vector3(-800, 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        glDisable(GL_LIGHTING);
213        //glEnable(GL_COLOR_MATERIAL);
214        glDisable(GL_COLOR_MATERIAL);
215        glEnable(GL_DEPTH_TEST);
216        glClearColor(0.1, 0.2, 0.3, 1);
217
218    int texWidth = 256, texHeight = 256;
219       
220        // A square, mipmapped, anisotropically filtered 8-bit RGBA texture with
221        // depth and stencil.
222        // Note that RT_COPY_TO_TEXTURE is required for depth textures on ATI hardware
223       
224        mNewBuffer = new RenderTexture(texWidth, texHeight, true, true);
225        mNewBuffer->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
226
227        mOldBuffer = new RenderTexture(texWidth, texHeight, true, true);
228        mOldBuffer ->Initialize(true, true, false, true, true, 8, 8, 8, 8);//, RenderTexture::RT_COPY_TO_TEXTURE);
229
230        // Setup Cg
231        cgSetErrorCallback(cgErrorCallback);
232
233        // Create cgContext.
234        sCgContext = cgCreateContext();
235
236        // get the best profile for this hardware
237        sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
238       
239        //assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);
240        cgGLSetOptimalOptions(sCgFragmentProfile);
241
242        sCgDepthPeelingProgram =
243                cgCreateProgramFromFile(sCgContext,
244                                                                CG_SOURCE,
245                                                                "../src/depth_peeling.cg",
246                                                                GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
247                                                                NULL,
248                                                                NULL);
249
250        if(sCgDepthPeelingProgram != NULL)
251        {
252                cgGLLoadProgram(sCgDepthPeelingProgram);
253                sTextureParam = cgGetNamedParameter(sCgDepthPeelingProgram, "depthTex"); 
254        }
255
256        sCgPassThroughProgram =
257                cgCreateProgramFromFile(sCgContext,
258                                                                CG_SOURCE,
259                                                                "../src/passthrough.cg",
260                                                                GLEW_ARB_fragment_program ? CG_PROFILE_ARBFP1 : CG_PROFILE_FP30,
261                                                                NULL,
262                                                                NULL);
263
264        if(sCgPassThroughProgram != NULL)
265        {
266                cgGLLoadProgram(sCgPassThroughProgram);
267        }
268
269
270    // setup the rendering context for the RenderTexture
271        mNewBuffer->BeginCapture();
272        {
273                Reshape(texWidth, texHeight);
274                glClearColor(0.1, 0.7, 0.2, 1);
275                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
276       
277                glMatrixMode(GL_MODELVIEW);
278                glLoadIdentity();
279                gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
280                                  midPoint.x, midPoint.y, midPoint.z,
281                                  0, 1, 0);
282//gluLookAt(0, 0, 3, 0, 0, 0, 0, 1, 0);
283                //glDisable(GL_DEPTH_TEST);
284                glEnable(GL_DEPTH_TEST);
285        }
286        mNewBuffer->EndCapture();
287
288        // setup the rendering context for the RenderTexture
289        mOldBuffer->BeginCapture();
290        {
291                Reshape(texWidth, texHeight);
292                glMatrixMode(GL_MODELVIEW);
293                glLoadIdentity();
294                gluLookAt(viewPoint.x, viewPoint.y, viewPoint.z,
295                                  midPoint.x, midPoint.y, midPoint.z,
296                                  0, 1, 0);
297
298                //glDisable(GL_DEPTH_TEST);
299                glEnable(GL_DEPTH_TEST);
300                glClearColor(0.6, 0.2, 0.2, 1);
301        }
302        mOldBuffer->EndCapture();
303
304        PrintGLerror("init");
305}
306
307
308void GlobalLinesRenderer::ProcessDepthBuffer()
309{
310}
311
312
313void GlobalLinesRenderer::Run()
314{
315        glutMainLoop();
316}
317
318
319void GlobalLinesRenderer::ApplyDepthPeeling(Beam &beam, const int samples)
320{
321        //if (mNewBuffer->IsDoubleBuffered())
322        //      glDrawBuffer(GL_BACK);
323
324        mNewBuffer->BeginCapture();
325        {
326                //cgGLBindProgram(sCgPassThroughProgram);
327                //cgGLEnableProfile(sCgFragmentProfile);
328                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
329                glMatrixMode(GL_MODELVIEW);
330                glPushMatrix();
331                { //glutSolidTorus(0.25, 1, 32, 64);
332                        DrawGeometry();
333                }
334                glPopMatrix();
335        }
336        mNewBuffer->EndCapture();
337       
338        //mNewBuffer->Bind();
339        mNewBuffer->BindDepth();
340        mNewBuffer->EnableTextureTarget();
341
342
343        //glBindTexture(texture_target,texID);
344        //const int texID = mNewBuffer->GetTextureTarget();
345        //glGetTexImage(texture_target,0,texture_format,GL_FLOAT,data);
346/*
347        for(int l = 0; l < mMaxDepth; ++ l)
348        {
349                // Peel another layer
350                SwitchRenderTextures(); // switch pointer between rendertextures
351
352                mNewBuffer->BeginCapture();
353                {
354                        //if (mNewBuffer->IsDoubleBuffered())
355                        //      glDrawBuffer(GL_BACK);
356
357                        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         
358
359                        cgGLBindProgram(sCgDepthPeelingProgram);
360                        cgGLEnableProfile(sCgFragmentProfile);
361                        cgGLSetTextureParameter(sTextureParam, mOldBuffer->GetDepthTextureID());
362                        cgGLEnableTextureParameter(sTextureParam);
363
364                        //glColor3f(0,0.0,0.0);
365                        DrawGeometry();
366                        cgGLDisableTextureParameter(sTextureParam);
367                }
368                mNewBuffer->EndCapture();
369        }
370*/
371
372        PrintGLerror("display 2");
373        glColor3f(1,1,1);
374        glBegin(GL_QUADS);
375    glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
376    glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f);
377    glTexCoord2f(1, 1); glVertex3f( 1,  1, -0.5f);
378    glTexCoord2f(0, 1); glVertex3f(-1,  1, -0.5f);
379    glEnd();
380        PrintGLerror("display 1");
381
382    if (mNewBuffer->IsFloatTexture())
383        {
384                // cgGLDisableTextureParameter(textureParam);
385                // cgGLDisableProfile(theProfile);
386        }
387        else
388        {
389                mNewBuffer->DisableTextureTarget();
390        }
391}
392
393
394}
Note: See TracBrowser for help on using the repository browser.