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

Revision 2603, 9.7 KB checked in by bittner, 16 years ago (diff)

sse disabled for hr

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