source: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp @ 2647

Revision 2647, 10.1 KB checked in by bittner, 16 years ago (diff)
Line 
1#define USE_THREADS 0
2
3#ifdef UNICODE
4#undef UNICODE
5#endif
6
7#define NOMINMAX
8#ifdef _WIN32
9#include <windows.h>
10#ifdef _CRT_SET
11#include <crtdbg.h>
12#endif // _CRT_SET
13#endif
14#include <cstdio>
15
16#include "Camera.h"
17#include "PreprocessorFactory.h"
18#include "Parser.h"
19#include "Environment.h"
20#include "MeshKdTree.h"
21#include "Preprocessor.h"
22#include "common.h"
23#include "PreprocessorThread.h"
24#include "ObjExporter.h"
25#include "SceneGraph.h"
26#include "GlobalLinesRenderer.h"
27#include "RayCaster.h"
28#include "ViewCellsManager.h"
29
30//#include "testrt.h"
31
32
33#ifdef USE_QT 
34        #include "QtPreprocessorThread.h"
35        #include "QtGlViewer.h"
36        #include "QtGlRenderer.h"
37        #include <QGLWidget>
38
39#else
40        #if USE_THREADS
41                #include "BoostPreprocessorThread.h"
42        #endif
43#endif
44
45#include "ResourceManager.h"
46#include "GlRenderer.h"
47
48
49#define USE_EXE_PATH false
50
51
52using namespace GtpVisibilityPreprocessor;
53
54//Preprocessor *preprocessor = NULL;
55GlRendererWidget *rendererWidget = NULL;
56//GlobalLinesRenderer *globalLinesRenderer = NULL;
57
58
59// DLL function signature
60typedef GlRendererWidget *(*importFunction)(Preprocessor *);
61
62
63
64void Cleanup()
65{
66        DEL_PTR(rendererWidget);
67        DEL_PTR(preprocessor);
68
69        Environment::DelSingleton();
70        MeshManager::DelSingleton();
71        MaterialManager::DelSingleton();
72}
73
74
75
76
77static string GetInternFilename(const string &filename, const string newSuffix)
78{
79        vector<string> filenames;
80        const int files = SplitFilenames(filename, filenames);
81
82        vector<string>::const_iterator sit, sit_end = filenames.end();
83        string kdFilename;
84
85        int i = 0;
86        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
87        {
88                string currentFile = *sit;
89                string strippedFilename;
90
91                if (i == 0)
92                {       
93                        strippedFilename = currentFile;
94                }
95                else
96                {
97                        char *str = StripPath(currentFile.c_str());
98                        strippedFilename = string(str);
99
100                        delete [] str;
101                }
102               
103                string suffix("_");
104
105                if (i == (int)filenames.size() - 1)
106                {
107                        suffix = newSuffix;
108                }
109
110                if (strstr(strippedFilename.c_str(), ".x3d"))
111                {
112                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
113                }
114        else if (strstr(strippedFilename.c_str(), ".dat"))
115                {
116                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
117                }
118                else if (strstr(strippedFilename.c_str(), ".obj"))
119                {
120                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
121                }
122                else
123                {
124                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
125                }
126        }
127
128        //cout << "kdfilename: " << kdFilename << endl;
129        return kdFilename;
130}
131
132int
133main(int argc, char **argv)
134{
135#if 0
136  // Test code by VH
137  TestRTcamera(argc, argv);
138  //TestRTfromFile(argc, argv);
139  //TestRTfromFilePackets(argc, argv);
140  //TestRT_4_fromFile(argc, argv);
141  return 0;
142#endif
143
144  int returnCode = 0;
145
146  //Now just call this function at the start of your program and if you're
147  //compiling in debug mode (F5), any leaks will be displayed in the Output
148  //window when the program shuts down. If you're not in debug mode this will
149  //be ignored. Use it as you will!
150  //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
151
152//#define _CRT_SET
153
154#ifdef _CRT_SET
155  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
156  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
157  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
158#endif
159
160        InitTiming();
161        Debug.open("debug.log");
162
163        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
164        MeshKdTree::ParseEnvironment();
165
166        char buff[128];
167        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
168        string preprocessorType(buff);
169
170        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
171        {
172                Environment::DelSingleton();
173                cerr << "Unknown preprocessor type" << endl;
174                exit(1);
175        }
176
177       
178        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
179        string filename(buff);
180       
181        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
182        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
183                                                                                                  ".kdm" : ".kdt");
184
185        if (preprocessor->InitRayCast(externKdTree, internKdTree))
186        {
187                cout << "ray casting initialized!" << endl;
188        }
189        else
190        {
191                cout << "ray casting initialization failed!" << endl;
192                Cleanup();
193                exit(1);
194        }
195
196       
197        //Debug << "using pvs type " << PVS_TYPE << endl;
198
199        /////////////
200        //-- load scene
201
202        if (!preprocessor->LoadScene(filename))
203        {
204                cout << "loading file " << filename << " failed" << endl;
205                Cleanup();
206                exit(1);
207        }
208       
209
210        ////////////
211        //-- initialize external ray caster
212
213        if (preprocessor->LoadInternKdTree(internKdTree))
214        {
215                cout << "intern kd tree loaded!" << endl;
216        }
217        else
218        {
219                cout << "loading intern kd tree failed!" << endl;
220                Cleanup();
221                exit(1);
222        }
223
224        // export objects as obj
225        if (preprocessor->mExportObj)
226        {
227                if (strstr(filename.c_str(), ".obj"))
228                {
229                        cerr << "already in obj format" << endl;
230                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
231                }
232                else
233                {
234                 
235                        const string objname = GetInternFilename(filename, ".obj");
236
237                        cout << "exporting scene to " << objname << endl;
238                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
239
240                       
241                        if (success)
242                                cout << "finished exporting obj" << endl;
243                        else
244                                cerr << "error exporting " << objname << endl;
245                }
246        }
247       
248        // parse view cells related options
249        if (!preprocessor->PrepareViewCells())
250        {
251                cerr << "error: view cells could not be loaded" << endl;
252
253                Cleanup();
254                exit(1);
255        }
256
257        string viewCellPointsFile;
258
259        if (strstr(filename.c_str(), ".obj"))
260                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
261        else if (strstr(filename.c_str(), ".dat"))
262                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
263        else if (strstr(filename.c_str(), ".x3d"))
264                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
265
266        bool importRandomViewCells;
267        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
268                                                                                           importRandomViewCells);
269
270        if (importRandomViewCells)
271        {
272                cout << "importing random view cells" << endl;
273                preprocessor->mViewCellsManager->ImportViewCellsList(viewCellPointsFile);
274                cout << "finished" << endl;
275        }
276
277        bool useHwGlobalLines;
278        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
279                                                                                           useHwGlobalLines);
280
281        if (useHwGlobalLines)
282                preprocessor->PrepareHwGlobalLines();
283
284        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
285        PreprocessorThread *pt = NULL;
286       
287        //preprocessor->PrepareHwGlobalLines();
288        //globalLinesRenderer->Run();
289
290
291        bool guiSupported = false;
292        const bool useRendererBuffer = true;
293
294        int frames;
295        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
296
297        bool evalPixelError;
298        Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", evalPixelError);
299
300#ifdef USE_QT
301
302        // create a qt application first (must be created before any opengl widget ...)
303        QApplication *app = new QApplication(argc, NULL);
304        pt = new QtPreprocessorThread(preprocessor);   
305       
306        //      if (preprocessor->mUseGlRenderer && (importRandomViewCells || frames))
307
308        if (evalPixelError && (importRandomViewCells || frames))
309        {
310                QGLFormat f;
311                f.setStencil(true);
312                QGLFormat::setDefaultFormat(f);
313
314                // NOTE: render texture should be power of 2 and square
315                // renderer must be initialised
316                // $$matt
317                preprocessor->renderer =
318                  new QtGlRendererBuffer(1024, 1024,
319                                                                 preprocessor->mSceneGraph,
320                                                                 preprocessor->mViewCellsManager,
321                                                                 preprocessor->mKdTree);
322               
323        }
324
325        bool exportRandomViewCells;
326        Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
327                                                                                          exportRandomViewCells);
328       
329        if (exportRandomViewCells)
330          {
331                cout << "exporting random view cells" << endl;
332                preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
333                cout << "finished" << endl;
334          }
335
336        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
337        {
338                ////////
339                //-- create and run the preprocessor application in a parallel thread
340
341                cout << "using gl widget" << endl;
342
343                pt->InitThread();
344                pt->RunThread();
345
346                // display the render widget
347                if (!rendererWidget)
348                {
349                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
350                                cout << "damn" << endl;
351                                QMessageBox::information(0, "OpenGL pbuffers",
352                                        "This system does not support OpenGL/pbuffers.",
353                                        QMessageBox::Ok);
354                                return NULL;
355                        }
356
357                        rendererWidget =
358                                new QtGlRendererWidget(preprocessor->mSceneGraph,
359                                                                           preprocessor->mViewCellsManager,
360                                                                           preprocessor->mKdTree);
361
362                        rendererWidget->Show();
363
364                        // hack matt
365                        preprocessor->mRendererWidget = rendererWidget;
366                        //rendererWidget->SetWindowTitle("Global Visualization");
367
368                        if (1) // not working with vbo
369                        {
370                                cout << "starting the qt viewer" << endl;
371                                QtGlViewer *viewer =
372                                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
373                                viewer->show();
374                        }
375
376                        guiSupported = true;
377                }
378
379
380                /*bool evaluatePixelError;
381                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
382
383                if (evaluatePixelError)
384                {
385                cout << "evaluating pixel error" << endl;
386                preprocessor->ComputeRenderError();
387                }*/
388
389                qApp->exec();
390        }
391#else // USE_QUT
392
393        //#if USE_THREADS
394        //      pt = new BoostPreprocessorThread(preprocessor);
395        //#else
396                // use a dummy thread
397                pt = new DummyPreprocessorThread(preprocessor);
398        //#endif
399#endif
400
401        preprocessor->SetThread(pt);
402
403        // no gui available
404        if (!guiSupported)
405        {
406                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
407                        cout << "warning: gui not supported!" << endl;
408
409                preprocessor->mUseGlRenderer = false;
410                preprocessor->mUseGlDebugger = false;
411        }
412
413        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
414        {
415                cout << "executing main thread" << endl;
416                // just call the mail method -> will be executed in the main thread
417                pt->Main();
418        }       
419
420        // release memory
421        Cleanup();
422        delete pt;
423
424        return returnCode;
425}
426
Note: See TracBrowser for help on using the repository browser.