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

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