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

Revision 2659, 10.2 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
143  int returnCode = 0;
144
145  //Now just call this function at the start of your program and if you're
146  //compiling in debug mode (F5), any leaks will be displayed in the Output
147  //window when the program shuts down. If you're not in debug mode this will
148  //be ignored. Use it as you will!
149  //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
150
151//#define _CRT_SET
152
153#ifdef _CRT_SET
154  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
155  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
156  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
157#endif
158
159        InitTiming();
160        Debug.open("debug.log");
161
162        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
163        MeshKdTree::ParseEnvironment();
164
165        char buff[128];
166        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
167        string preprocessorType(buff);
168
169        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
170        {
171                Environment::DelSingleton();
172                cerr << "Unknown preprocessor type" << endl;
173                exit(1);
174        }
175
176       
177        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
178        string filename(buff);
179       
180        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
181        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
182                                                                                                  ".kdm" : ".kdt");
183
184        if (preprocessor->InitRayCast(externKdTree, internKdTree))
185        {
186                cout << "ray casting initialized!" << endl;
187        }
188        else
189        {
190                cout << "ray casting initialization failed!" << endl;
191                Cleanup();
192                exit(1);
193        }
194
195       
196        //Debug << "using pvs type " << PVS_TYPE << endl;
197
198        /////////////
199        //-- load scene
200
201        if (!preprocessor->LoadScene(filename))
202        {
203                cout << "loading file " << filename << " failed" << endl;
204                Cleanup();
205                exit(1);
206        }
207       
208
209        ////////////
210        //-- initialize external ray caster
211
212        if (preprocessor->LoadInternKdTree(internKdTree))
213        {
214                cout << "intern kd tree loaded!" << endl;
215        }
216        else
217        {
218                cout << "loading intern kd tree failed!" << endl;
219                Cleanup();
220                exit(1);
221        }
222
223        // export objects as obj
224        if (preprocessor->mExportObj)
225        {
226                if (strstr(filename.c_str(), ".obj"))
227                {
228                        cerr << "already in obj format" << endl;
229                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
230                }
231                else
232                {
233                 
234                        const string objname = GetInternFilename(filename, ".obj");
235
236                        cout << "exporting scene to " << objname << endl;
237                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
238
239                       
240                        if (success)
241                                cout << "finished exporting obj" << endl;
242                        else
243                                cerr << "error exporting " << objname << endl;
244                }
245        }
246       
247        // parse view cells related options
248        if (!preprocessor->PrepareViewCells())
249        {
250                cerr << "error: view cells could not be loaded" << endl;
251
252                Cleanup();
253                exit(1);
254        }
255        else
256                cout << "view cells successfully loaded" << endl;
257
258        string viewCellPointsFile;
259
260        if (strstr(filename.c_str(), ".obj"))
261                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
262        else if (strstr(filename.c_str(), ".dat"))
263                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
264        else if (strstr(filename.c_str(), ".x3d"))
265                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
266
267        bool importRandomViewCells;
268        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
269                                                                                           importRandomViewCells);
270
271        if (importRandomViewCells)
272        {
273                cout << "importing random view cells" << endl;
274                preprocessor->mViewCellsManager->ImportViewCellsList(viewCellPointsFile);
275                cout << "finished" << endl;
276        }
277
278        bool useHwGlobalLines;
279        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
280                                                                                           useHwGlobalLines);
281
282        if (useHwGlobalLines)
283                preprocessor->PrepareHwGlobalLines();
284
285        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
286        PreprocessorThread *pt = NULL;
287       
288        //preprocessor->PrepareHwGlobalLines();
289        //globalLinesRenderer->Run();
290
291
292        bool guiSupported = false;
293        const bool useRendererBuffer = true;
294
295        int frames;
296        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
297
298        bool evalPixelError;
299        Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", evalPixelError);
300return 0;
301#ifdef USE_QT
302
303        // create a qt application first (must be created before any opengl widget ...)
304        QApplication *app = new QApplication(argc, NULL);
305        pt = new QtPreprocessorThread(preprocessor);   
306       
307        //      if (preprocessor->mUseGlRenderer && (importRandomViewCells || frames))
308
309        if (evalPixelError && (importRandomViewCells || frames))
310        {
311                QGLFormat f;
312                f.setStencil(true);
313                QGLFormat::setDefaultFormat(f);
314
315                // NOTE: render texture should be power of 2 and square
316                // renderer must be initialised
317                // $$matt
318                preprocessor->renderer =
319                  new QtGlRendererBuffer(1024, 1024,
320                                                                 preprocessor->mSceneGraph,
321                                                                 preprocessor->mViewCellsManager,
322                                                                 preprocessor->mKdTree);
323               
324        }
325
326        bool exportRandomViewCells;
327        Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
328                                                                                          exportRandomViewCells);
329       
330        if (exportRandomViewCells)
331          {
332                cout << "exporting random view cells" << endl;
333                preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
334                cout << "finished" << endl;
335          }
336
337        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
338        {
339                ////////
340                //-- create and run the preprocessor application in a parallel thread
341
342                cout << "using gl widget" << endl;
343
344                pt->InitThread();
345                pt->RunThread();
346
347                // display the render widget
348                if (!rendererWidget)
349                {
350                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
351                                cout << "damn" << endl;
352                                QMessageBox::information(0, "OpenGL pbuffers",
353                                        "This system does not support OpenGL/pbuffers.",
354                                        QMessageBox::Ok);
355                                return NULL;
356                        }
357
358                        rendererWidget =
359                                new QtGlRendererWidget(preprocessor->mSceneGraph,
360                                                                           preprocessor->mViewCellsManager,
361                                                                           preprocessor->mKdTree);
362
363                        rendererWidget->Show();
364
365                        // hack matt
366                        preprocessor->mRendererWidget = rendererWidget;
367                        //rendererWidget->SetWindowTitle("Global Visualization");
368
369                        if (1) // not working with vbo
370                        {
371                                cout << "starting the qt viewer" << endl;
372                                QtGlViewer *viewer =
373                                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
374                                viewer->show();
375                        }
376
377                        guiSupported = true;
378                }
379
380
381                /*bool evaluatePixelError;
382                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
383
384                if (evaluatePixelError)
385                {
386                cout << "evaluating pixel error" << endl;
387                preprocessor->ComputeRenderError();
388                }*/
389
390                qApp->exec();
391        }
392#else // USE_QUT
393
394        //#if USE_THREADS
395        //      pt = new BoostPreprocessorThread(preprocessor);
396        //#else
397                // use a dummy thread
398                pt = new DummyPreprocessorThread(preprocessor);
399        //#endif
400#endif
401
402        preprocessor->SetThread(pt);
403
404        // no gui available
405        if (!guiSupported)
406        {
407                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
408                        cout << "warning: gui not supported!" << endl;
409
410                preprocessor->mUseGlRenderer = false;
411                preprocessor->mUseGlDebugger = false;
412        }
413
414        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
415        {
416                cout << "executing main thread" << endl;
417                // just call the mail method -> will be executed in the main thread
418                pt->Main();
419        }       
420
421        // release memory
422        Cleanup();
423        delete pt;
424
425        return returnCode;
426}
427
Note: See TracBrowser for help on using the repository browser.