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

Revision 2647, 10.1 KB checked in by bittner, 16 years ago (diff)
RevLine 
[1926]1#define USE_THREADS 0
[1457]2
[1199]3#ifdef UNICODE
4#undef UNICODE
5#endif
6
[2176]7#define NOMINMAX
[2634]8#ifdef _WIN32
[1151]9#include <windows.h>
[2575]10#ifdef _CRT_SET
[1151]11#include <crtdbg.h>
[2575]12#endif // _CRT_SET
13#endif
14#include <cstdio>
[1151]15
[2575]16#include "Camera.h"
[1486]17#include "PreprocessorFactory.h"
[162]18#include "Parser.h"
19#include "Environment.h"
[170]20#include "MeshKdTree.h"
[1486]21#include "Preprocessor.h"
[1634]22#include "common.h"
[1457]23#include "PreprocessorThread.h"
[1694]24#include "ObjExporter.h"
25#include "SceneGraph.h"
[1949]26#include "GlobalLinesRenderer.h"
[2575]27#include "RayCaster.h"
[1926]28#include "ViewCellsManager.h"
[1694]29
[2643]30//#include "testrt.h"
[2629]31
32
[2568]33#ifdef USE_QT 
[1926]34        #include "QtPreprocessorThread.h"
35        #include "QtGlViewer.h"
36        #include "QtGlRenderer.h"
[2560]37        #include <QGLWidget>
38
[2568]39#else
[1926]40        #if USE_THREADS
41                #include "BoostPreprocessorThread.h"
42        #endif
[2568]43#endif
[1272]44
[1001]45#include "ResourceManager.h"
[492]46#include "GlRenderer.h"
47
[1272]48
[162]49#define USE_EXE_PATH false
50
[372]51
[863]52using namespace GtpVisibilityPreprocessor;
[492]53
[1888]54//Preprocessor *preprocessor = NULL;
[1151]55GlRendererWidget *rendererWidget = NULL;
[1940]56//GlobalLinesRenderer *globalLinesRenderer = NULL;
[1145]57
[1940]58
[1151]59// DLL function signature
[1153]60typedef GlRendererWidget *(*importFunction)(Preprocessor *);
[1145]61
[1241]62
[1199]63
[1145]64void Cleanup()
65{
[1151]66        DEL_PTR(rendererWidget);
[1145]67        DEL_PTR(preprocessor);
68
69        Environment::DelSingleton();
70        MeshManager::DelSingleton();
71        MaterialManager::DelSingleton();
72}
73
74
75
[2452]76
[1695]77static string GetInternFilename(const string &filename, const string newSuffix)
[1634]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)
[1633]87        {
[1634]88                string currentFile = *sit;
89                string strippedFilename;
[1633]90
[1634]91                if (i == 0)
92                {       
93                        strippedFilename = currentFile;
94                }
95                else
96                {
[1640]97                        char *str = StripPath(currentFile.c_str());
98                        strippedFilename = string(str);
99
[1695]100                        delete [] str;
[1634]101                }
102               
103                string suffix("_");
104
105                if (i == (int)filenames.size() - 1)
106                {
[1695]107                        suffix = newSuffix;
[1634]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                {
[1658]120                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
[1634]121                }
122                else
123                {
124                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
125                }
[1626]126        }
[1272]127
[1634]128        //cout << "kdfilename: " << kdFilename << endl;
129        return kdFilename;
[1272]130}
131
[162]132int
[492]133main(int argc, char **argv)
[997]134{
[2575]135#if 0
136  // Test code by VH
[2629]137  TestRTcamera(argc, argv);
[2575]138  //TestRTfromFile(argc, argv);
[2629]139  //TestRTfromFilePackets(argc, argv);
140  //TestRT_4_fromFile(argc, argv);
[2575]141  return 0;
142#endif
[2560]143
[2575]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() {
[2582]151
152//#define _CRT_SET
153
[2575]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
[1145]160        InitTiming();
[2020]161        Debug.open("debug.log");
[997]162
[1145]163        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
164        MeshKdTree::ParseEnvironment();
[1002]165
[1145]166        char buff[128];
167        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
168        string preprocessorType(buff);
169
[1486]170        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]171        {
[1486]172                Environment::DelSingleton();
173                cerr << "Unknown preprocessor type" << endl;
174                exit(1);
[1328]175        }
[997]176
[2608]177       
[1145]178        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
179        string filename(buff);
[1695]180       
[2003]181        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
[2049]182        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
183                                                                                                  ".kdm" : ".kdt");
[1328]184
[2003]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
[2582]196       
[2176]197        //Debug << "using pvs type " << PVS_TYPE << endl;
[2003]198
199        /////////////
200        //-- load scene
201
[1328]202        if (!preprocessor->LoadScene(filename))
203        {
204                cout << "loading file " << filename << " failed" << endl;
205                Cleanup();
206                exit(1);
207        }
[1926]208       
[2560]209
[1697]210        ////////////
211        //-- initialize external ray caster
[1999]212
[2003]213        if (preprocessor->LoadInternKdTree(internKdTree))
[1328]214        {
[2003]215                cout << "intern kd tree loaded!" << endl;
[1328]216        }
[1221]217        else
218        {
[2003]219                cout << "loading intern kd tree failed!" << endl;
[1328]220                Cleanup();
[1221]221                exit(1);
222        }
[1344]223
[1695]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;
[1701]230                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]231                }
232                else
233                {
[2049]234                 
[1695]235                        const string objname = GetInternFilename(filename, ".obj");
236
[1701]237                        cout << "exporting scene to " << objname << endl;
[1695]238                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
239
[2049]240                       
[1695]241                        if (success)
242                                cout << "finished exporting obj" << endl;
243                        else
244                                cerr << "error exporting " << objname << endl;
245                }
246        }
[1713]247       
[1145]248        // parse view cells related options
[1522]249        if (!preprocessor->PrepareViewCells())
250        {
[1926]251                cerr << "error: view cells could not be loaded" << endl;
252
[1522]253                Cleanup();
254                exit(1);
255        }
[1999]256
[2048]257        string viewCellPointsFile;
[2017]258
[2048]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");
[2023]265
[2048]266        bool importRandomViewCells;
267        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
268                                                                                           importRandomViewCells);
269
270        if (importRandomViewCells)
271        {
272                cout << "importing random view cells" << endl;
[2598]273                preprocessor->mViewCellsManager->ImportViewCellsList(viewCellPointsFile);
[2048]274                cout << "finished" << endl;
275        }
276
[1968]277        bool useHwGlobalLines;
278        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
279                                                                                           useHwGlobalLines);
280
281        if (useHwGlobalLines)
282                preprocessor->PrepareHwGlobalLines();
283
[1239]284        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1926]285        PreprocessorThread *pt = NULL;
[1968]286       
[2560]287        //preprocessor->PrepareHwGlobalLines();
288        //globalLinesRenderer->Run();
[1940]289
290
[1248]291        bool guiSupported = false;
[2560]292        const bool useRendererBuffer = true;
[2049]293
294        int frames;
295        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[2560]296
[2611]297        bool evalPixelError;
298        Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", evalPixelError);
299
[2049]300#ifdef USE_QT
301
[2560]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       
[2611]306        //      if (preprocessor->mUseGlRenderer && (importRandomViewCells || frames))
[2610]307
[2611]308        if (evalPixelError && (importRandomViewCells || frames))
[2625]309        {
[2049]310                QGLFormat f;
311                f.setStencil(true);
312                QGLFormat::setDefaultFormat(f);
[2625]313
[2049]314                // NOTE: render texture should be power of 2 and square
315                // renderer must be initialised
316                // $$matt
317                preprocessor->renderer =
[2634]318                  new QtGlRendererBuffer(1024, 1024,
319                                                                 preprocessor->mSceneGraph,
320                                                                 preprocessor->mViewCellsManager,
321                                                                 preprocessor->mKdTree);
322               
[2647]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;
[2634]334          }
[2647]335
[2560]336        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
337        {
338                ////////
339                //-- create and run the preprocessor application in a parallel thread
[1926]340
[2560]341                cout << "using gl widget" << endl;
[2049]342
[2560]343                pt->InitThread();
344                pt->RunThread();
[1613]345
[2560]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                        }
[2017]356
[2560]357                        rendererWidget =
358                                new QtGlRendererWidget(preprocessor->mSceneGraph,
[2625]359                                                                           preprocessor->mViewCellsManager,
360                                                                           preprocessor->mKdTree);
[2017]361
[2560]362                        rendererWidget->Show();
[2023]363
[2625]364                        // hack matt
365                        preprocessor->mRendererWidget = rendererWidget;
[2615]366                        //rendererWidget->SetWindowTitle("Global Visualization");
367
[2560]368                        if (1) // not working with vbo
369                        {
[2569]370                                cout << "starting the qt viewer" << endl;
[2560]371                                QtGlViewer *viewer =
372                                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
373                                viewer->show();
374                        }
[2538]375
[2560]376                        guiSupported = true;
377                }
[2538]378
379
[2560]380                /*bool evaluatePixelError;
381                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
[2538]382
[2560]383                if (evaluatePixelError)
384                {
385                cout << "evaluating pixel error" << endl;
386                preprocessor->ComputeRenderError();
387                }*/
[2538]388
[2560]389                qApp->exec();
390        }
391#else // USE_QUT
[2538]392
[2560]393        //#if USE_THREADS
394        //      pt = new BoostPreprocessorThread(preprocessor);
395        //#else
396                // use a dummy thread
397                pt = new DummyPreprocessorThread(preprocessor);
398        //#endif
[1387]399#endif
[1785]400
[2560]401        preprocessor->SetThread(pt);
[1696]402
[2560]403        // no gui available
404        if (!guiSupported)
405        {
406                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
407                        cout << "warning: gui not supported!" << endl;
[2538]408
[2560]409                preprocessor->mUseGlRenderer = false;
410                preprocessor->mUseGlDebugger = false;
411        }
[1999]412
[2560]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        }       
[2538]419
[2560]420        // release memory
421        Cleanup();
422        delete pt;
423
424        return returnCode;
[162]425}
426
Note: See TracBrowser for help on using the repository browser.