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

Revision 2582, 10.4 KB checked in by bittner, 17 years ago (diff)

Havran Ray Caster compiles and links, but still does not work

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
[2452]74static string ReplaceSuffix(const string &filename, const string &a, const string &b)
[1626]75{
76        string result = filename;
[1145]77
[1626]78        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
[2452]79        if (pos == filename.size() - a.size())
80        {
[1626]81                result.replace(pos, a.size(), b);
82        }
[2452]83
[1626]84        return result;
85}
[1145]86
[1626]87
[2452]88static int SplitFilenames(const string &str, vector<string> &filenames)
[1272]89{
[1634]90        int pos = 0;
[1633]91
[1634]92        while(1) {
93                int npos = (int)str.find(';', pos);
94               
95                if (npos < 0 || npos - pos < 1)
96                        break;
97                filenames.push_back(string(str, pos, npos - pos));
98                pos = npos + 1;
[1633]99        }
[1634]100       
101        filenames.push_back(string(str, pos, str.size() - pos));
102        return (int)filenames.size();
103}
104
105
[1695]106static string GetInternFilename(const string &filename, const string newSuffix)
[1634]107{
108        vector<string> filenames;
109        const int files = SplitFilenames(filename, filenames);
110
111        vector<string>::const_iterator sit, sit_end = filenames.end();
112        string kdFilename;
113
114        int i = 0;
115        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
[1633]116        {
[1634]117                string currentFile = *sit;
118                string strippedFilename;
[1633]119
[1634]120                if (i == 0)
121                {       
122                        strippedFilename = currentFile;
123                }
124                else
125                {
[1640]126                        char *str = StripPath(currentFile.c_str());
127                        strippedFilename = string(str);
128
[1695]129                        delete [] str;
[1634]130                }
131               
132                string suffix("_");
133
134                if (i == (int)filenames.size() - 1)
135                {
[1695]136                        suffix = newSuffix;
[1634]137                }
138
139                if (strstr(strippedFilename.c_str(), ".x3d"))
140                {
141                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
142                }
143        else if (strstr(strippedFilename.c_str(), ".dat"))
144                {
145                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
146                }
147                else if (strstr(strippedFilename.c_str(), ".obj"))
148                {
[1658]149                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
[1634]150                }
151                else
152                {
153                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
154                }
[1626]155        }
[1272]156
[1634]157        //cout << "kdfilename: " << kdFilename << endl;
158        return kdFilename;
[1272]159}
160
[162]161int
[492]162main(int argc, char **argv)
[997]163{
[2575]164#if 0
165  // Test code by VH
166  //TestRTcamera(argc, argv);
167  //TestRTfromFile(argc, argv);
168  TestRTfromFilePackets(argc, argv);
169  return 0;
170#endif
[2560]171
[2575]172  int returnCode = 0;
173
174  //Now just call this function at the start of your program and if you're
175  //compiling in debug mode (F5), any leaks will be displayed in the Output
176  //window when the program shuts down. If you're not in debug mode this will
177  //be ignored. Use it as you will!
178  //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
[2582]179
180//#define _CRT_SET
181
[2575]182#ifdef _CRT_SET
183  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
184  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
185  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
186#endif
187
[1145]188        InitTiming();
[2020]189        Debug.open("debug.log");
[997]190
[1145]191        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
192        MeshKdTree::ParseEnvironment();
[1002]193
[1145]194        char buff[128];
195        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
196        string preprocessorType(buff);
197
[1486]198        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
[1328]199        {
[1486]200                Environment::DelSingleton();
201                cerr << "Unknown preprocessor type" << endl;
202                exit(1);
[1328]203        }
[997]204
[1145]205        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
206        string filename(buff);
[1695]207       
[2003]208        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
[2049]209        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
210                                                                                                  ".kdm" : ".kdt");
[1328]211
[2003]212        if (preprocessor->InitRayCast(externKdTree, internKdTree))
213        {
214                cout << "ray casting initialized!" << endl;
215        }
216        else
217        {
218                cout << "ray casting initialization failed!" << endl;
219                Cleanup();
220                exit(1);
221        }
222
[2582]223       
[2176]224        //Debug << "using pvs type " << PVS_TYPE << endl;
[2003]225
226        /////////////
227        //-- load scene
228
[1328]229        if (!preprocessor->LoadScene(filename))
230        {
231                cout << "loading file " << filename << " failed" << endl;
232                Cleanup();
233                exit(1);
234        }
[1926]235       
[2560]236
[1697]237        ////////////
238        //-- initialize external ray caster
[1999]239
[2003]240        if (preprocessor->LoadInternKdTree(internKdTree))
[1328]241        {
[2003]242                cout << "intern kd tree loaded!" << endl;
[1328]243        }
[1221]244        else
245        {
[2003]246                cout << "loading intern kd tree failed!" << endl;
[1328]247                Cleanup();
[1221]248                exit(1);
249        }
[1344]250
[1695]251        // export objects as obj
252        if (preprocessor->mExportObj)
253        {
254                if (strstr(filename.c_str(), ".obj"))
255                {
256                        cerr << "already in obj format" << endl;
[1701]257                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
[1695]258                }
259                else
260                {
[2049]261                 
[1695]262                        const string objname = GetInternFilename(filename, ".obj");
263
[1701]264                        cout << "exporting scene to " << objname << endl;
[1695]265                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
266
[2049]267                       
[1695]268                        if (success)
269                                cout << "finished exporting obj" << endl;
270                        else
271                                cerr << "error exporting " << objname << endl;
272                }
273        }
[1713]274       
[1145]275        // parse view cells related options
[1522]276        if (!preprocessor->PrepareViewCells())
277        {
[1926]278                cerr << "error: view cells could not be loaded" << endl;
279
[1522]280                Cleanup();
281                exit(1);
282        }
[1999]283
[2048]284        string viewCellPointsFile;
[2017]285
[2048]286        if (strstr(filename.c_str(), ".obj"))
287                viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
288        else if (strstr(filename.c_str(), ".dat"))
289                viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
290        else if (strstr(filename.c_str(), ".x3d"))
291                viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
[2023]292
[2048]293        bool importRandomViewCells;
294        Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
295                                                                                           importRandomViewCells);
296
297        if (importRandomViewCells)
298        {
299                cout << "importing random view cells" << endl;
300                preprocessor->mViewCellsManager->ImportRandomViewCells(viewCellPointsFile);
301                cout << "finished" << endl;
302        }
303
[1968]304        bool useHwGlobalLines;
305        Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
306                                                                                           useHwGlobalLines);
307
308        if (useHwGlobalLines)
309                preprocessor->PrepareHwGlobalLines();
310
[1239]311        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
[1926]312        PreprocessorThread *pt = NULL;
[1968]313       
[2560]314        //preprocessor->PrepareHwGlobalLines();
315        //globalLinesRenderer->Run();
[1940]316
317
[1248]318        bool guiSupported = false;
[2560]319        const bool useRendererBuffer = true;
[2049]320
321        int frames;
322        Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
[2560]323
[2049]324#ifdef USE_QT
325
[2560]326        // create a qt application first (must be created before any opengl widget ...)
327        QApplication *app = new QApplication(argc, NULL);
328        pt = new QtPreprocessorThread(preprocessor);   
329       
[2543]330        if (0 && preprocessor->mUseGlRenderer && (importRandomViewCells || frames))
331        {
[2049]332                QGLFormat f;
333                f.setStencil(true);
334                QGLFormat::setDefaultFormat(f);
335
336                // NOTE: render texture should be power of 2 and square
337                // renderer must be initialised
338                // $$matt
339                preprocessor->renderer =
[2543]340                        new QtGlRendererBuffer(1024, 1024,
[2560]341                        preprocessor->mSceneGraph,
342                        preprocessor->mViewCellsManager,
343                        preprocessor->mKdTree);
[2543]344        }
[2049]345
[2560]346        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
347        {
348                ////////
349                //-- create and run the preprocessor application in a parallel thread
[1926]350
[2560]351                cout << "using gl widget" << endl;
[2049]352
[2560]353                pt->InitThread();
354                pt->RunThread();
[1613]355
[2560]356                // display the render widget
357                if (!rendererWidget)
358                {
359                        if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
360                                cout << "damn" << endl;
361                                QMessageBox::information(0, "OpenGL pbuffers",
362                                        "This system does not support OpenGL/pbuffers.",
363                                        QMessageBox::Ok);
364                                return NULL;
365                        }
[2017]366
[2560]367                        rendererWidget =
368                                new QtGlRendererWidget(preprocessor->mSceneGraph,
369                                preprocessor->mViewCellsManager,
370                                preprocessor->mKdTree);
[2017]371
[2560]372                        rendererWidget->Show();
[2023]373
[2560]374                        if (1) // not working with vbo
375                        {
[2569]376                                cout << "starting the qt viewer" << endl;
[2560]377                                QtGlViewer *viewer =
378                                        new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
379                                viewer->show();
380                        }
[2538]381
[2560]382                        guiSupported = true;
383                }
[2538]384
[2560]385                bool exportRandomViewCells;
386                Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
387                        exportRandomViewCells);
[2538]388
[2560]389                if (exportRandomViewCells)
390                {
391                        cout << "exporting random view cells" << endl;
392                        preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
393                        cout << "finished" << endl;
394                }
[2538]395
[2560]396                /*bool evaluatePixelError;
397                Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError",  evaluatePixelError);
[2538]398
[2560]399                if (evaluatePixelError)
400                {
401                cout << "evaluating pixel error" << endl;
402                preprocessor->ComputeRenderError();
403                }*/
[2538]404
[2560]405                qApp->exec();
406        }
407#else // USE_QUT
[2538]408
[2560]409        //#if USE_THREADS
410        //      pt = new BoostPreprocessorThread(preprocessor);
411        //#else
412                // use a dummy thread
413                pt = new DummyPreprocessorThread(preprocessor);
414        //#endif
[1387]415#endif
[1785]416
[2560]417        preprocessor->SetThread(pt);
[1696]418
[2560]419        // no gui available
420        if (!guiSupported)
421        {
422                if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
423                        cout << "warning: gui not supported!" << endl;
[2538]424
[2560]425                preprocessor->mUseGlRenderer = false;
426                preprocessor->mUseGlDebugger = false;
427        }
[1999]428
[2560]429        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
430        {
431                cout << "executing main thread" << endl;
432                // just call the mail method -> will be executed in the main thread
433                pt->Main();
434        }       
[2538]435
[2560]436        // release memory
437        Cleanup();
438        delete pt;
439
440        return returnCode;
[162]441}
442
Note: See TracBrowser for help on using the repository browser.