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

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

pixel error computation revival

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
[2608]175       
[1145]176        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
177        string filename(buff);
[1695]178       
[2003]179        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
[2049]180        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
181                                                                                                  ".kdm" : ".kdt");
[1328]182
[2003]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
[2582]194       
[2176]195        //Debug << "using pvs type " << PVS_TYPE << endl;
[2003]196
197        /////////////
198        //-- load scene
199
[1328]200        if (!preprocessor->LoadScene(filename))
201        {
202                cout << "loading file " << filename << " failed" << endl;
203                Cleanup();
204                exit(1);
205        }
[1926]206       
[2560]207
[1697]208        ////////////
209        //-- initialize external ray caster
[1999]210
[2003]211        if (preprocessor->LoadInternKdTree(internKdTree))
[1328]212        {
[2003]213                cout << "intern kd tree loaded!" << endl;
[1328]214        }
[1221]215        else
216        {
[2003]217                cout << "loading intern kd tree failed!" << endl;
[1328]218                Cleanup();
[1221]219                exit(1);
220        }
[1344]221
[1695]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;
[1701]228                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]229                }
230                else
231                {
[2049]232                 
[1695]233                        const string objname = GetInternFilename(filename, ".obj");
234
[1701]235                        cout << "exporting scene to " << objname << endl;
[1695]236                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
237
[2049]238                       
[1695]239                        if (success)
240                                cout << "finished exporting obj" << endl;
241                        else
242                                cerr << "error exporting " << objname << endl;
243                }
244        }
[1713]245       
[1145]246        // parse view cells related options
[1522]247        if (!preprocessor->PrepareViewCells())
248        {
[1926]249                cerr << "error: view cells could not be loaded" << endl;
250
[1522]251                Cleanup();
252                exit(1);
253        }
[1999]254
[2048]255        string viewCellPointsFile;
[2017]256
[2048]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");
[2023]263
[2048]264        bool importRandomViewCells;
265        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
266                                                                                           importRandomViewCells);
267
268        if (importRandomViewCells)
269        {
270                cout << "importing random view cells" << endl;
[2598]271                preprocessor->mViewCellsManager->ImportViewCellsList(viewCellPointsFile);
[2048]272                cout << "finished" << endl;
273        }
274
[1968]275        bool useHwGlobalLines;
276        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
277                                                                                           useHwGlobalLines);
278
279        if (useHwGlobalLines)
280                preprocessor->PrepareHwGlobalLines();
281
[1239]282        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1926]283        PreprocessorThread *pt = NULL;
[1968]284       
[2560]285        //preprocessor->PrepareHwGlobalLines();
286        //globalLinesRenderer->Run();
[1940]287
288
[1248]289        bool guiSupported = false;
[2560]290        const bool useRendererBuffer = true;
[2049]291
292        int frames;
293        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[2560]294
[2049]295#ifdef USE_QT
296
[2560]297        // create a qt application first (must be created before any opengl widget ...)
298        QApplication *app = new QApplication(argc, NULL);
299        pt = new QtPreprocessorThread(preprocessor);   
300       
[2610]301        if (preprocessor->mUseGlRenderer && (importRandomViewCells || frames))
302
303          {
[2049]304                QGLFormat f;
305                f.setStencil(true);
306                QGLFormat::setDefaultFormat(f);
307                // NOTE: render texture should be power of 2 and square
308                // renderer must be initialised
309                // $$matt
310                preprocessor->renderer =
[2610]311                  new QtGlRendererBuffer(512, 512,
312                                                                 preprocessor->mSceneGraph,
313                                                                 preprocessor->mViewCellsManager,
314                                                                 preprocessor->mKdTree);
315               
316          }
317       
[2560]318        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
319        {
320                ////////
321                //-- create and run the preprocessor application in a parallel thread
[1926]322
[2560]323                cout << "using gl widget" << endl;
[2049]324
[2560]325                pt->InitThread();
326                pt->RunThread();
[1613]327
[2560]328                // display the render widget
329                if (!rendererWidget)
330                {
331                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
332                                cout << "damn" << endl;
333                                QMessageBox::information(0, "OpenGL pbuffers",
334                                        "This system does not support OpenGL/pbuffers.",
335                                        QMessageBox::Ok);
336                                return NULL;
337                        }
[2017]338
[2560]339                        rendererWidget =
340                                new QtGlRendererWidget(preprocessor->mSceneGraph,
341                                preprocessor->mViewCellsManager,
342                                preprocessor->mKdTree);
[2017]343
[2560]344                        rendererWidget->Show();
[2023]345
[2560]346                        if (1) // not working with vbo
347                        {
[2569]348                                cout << "starting the qt viewer" << endl;
[2560]349                                QtGlViewer *viewer =
350                                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
351                                viewer->show();
352                        }
[2538]353
[2560]354                        guiSupported = true;
355                }
[2538]356
[2560]357                bool exportRandomViewCells;
358                Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
359                        exportRandomViewCells);
[2538]360
[2560]361                if (exportRandomViewCells)
362                {
363                        cout << "exporting random view cells" << endl;
364                        preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
365                        cout << "finished" << endl;
366                }
[2538]367
[2560]368                /*bool evaluatePixelError;
369                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
[2538]370
[2560]371                if (evaluatePixelError)
372                {
373                cout << "evaluating pixel error" << endl;
374                preprocessor->ComputeRenderError();
375                }*/
[2538]376
[2560]377                qApp->exec();
378        }
379#else // USE_QUT
[2538]380
[2560]381        //#if USE_THREADS
382        //      pt = new BoostPreprocessorThread(preprocessor);
383        //#else
384                // use a dummy thread
385                pt = new DummyPreprocessorThread(preprocessor);
386        //#endif
[1387]387#endif
[1785]388
[2560]389        preprocessor->SetThread(pt);
[1696]390
[2560]391        // no gui available
392        if (!guiSupported)
393        {
394                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
395                        cout << "warning: gui not supported!" << endl;
[2538]396
[2560]397                preprocessor->mUseGlRenderer = false;
398                preprocessor->mUseGlDebugger = false;
399        }
[1999]400
[2560]401        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
402        {
403                cout << "executing main thread" << endl;
404                // just call the mail method -> will be executed in the main thread
405                pt->Main();
406        }       
[2538]407
[2560]408        // release memory
409        Cleanup();
410        delete pt;
411
412        return returnCode;
[162]413}
414
Note: See TracBrowser for help on using the repository browser.