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

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