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

Revision 1785, 7.9 KB checked in by bittner, 18 years ago (diff)

merge, filter update, renderebuffer made functional

Line 
1#ifdef GTP_INTERNAL
2#ifndef NO_QT
3#define USE_QT 1
4#else
5#define USE_QT 0
6#endif
7#endif
8
9#define USE_THREADS 1
10
11#ifdef UNICODE
12#undef UNICODE
13#endif
14
15#include <windows.h>
16#include <stdio.h>
17#include <crtdbg.h>
18
19#include "PreprocessorFactory.h"
20#include "Parser.h"
21#include "Environment.h"
22#include "MeshKdTree.h"
23#include "Preprocessor.h"
24#include "common.h"
25
26#include "PreprocessorThread.h"
27#include "ObjExporter.h"
28#include "SceneGraph.h"
29
30
31
32#if !USE_QT && USE_THREADS
33#include "BoostPreprocessorThread.h"
34#endif
35
36#include "ResourceManager.h"
37#include "GlRenderer.h"
38
39#if USE_QT
40#include "QtPreprocessorThread.h"
41#include "QtGlRenderer.h"
42#endif
43
44#define USE_EXE_PATH false
45
46
47using namespace GtpVisibilityPreprocessor;
48
49Preprocessor *preprocessor = NULL;
50GlRendererWidget *rendererWidget = NULL;
51
52// DLL function signature
53typedef GlRendererWidget *(*importFunction)(Preprocessor *);
54
55
56
57void Cleanup()
58{
59        DEL_PTR(rendererWidget);
60        DEL_PTR(preprocessor);
61
62        Environment::DelSingleton();
63        MeshManager::DelSingleton();
64        MaterialManager::DelSingleton();
65}
66
67
68static string ReplaceSuffix(string filename, string a, string b)
69{
70        string result = filename;
71
72        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
73        if (pos == filename.size() - a.size()) {
74                result.replace(pos, a.size(), b);
75        }
76        return result;
77}
78
79
80static int SplitFilenames(const string str, vector<string> &filenames)
81{
82        int pos = 0;
83
84        while(1) {
85                int npos = (int)str.find(';', pos);
86               
87                if (npos < 0 || npos - pos < 1)
88                        break;
89                filenames.push_back(string(str, pos, npos - pos));
90                pos = npos + 1;
91        }
92       
93        filenames.push_back(string(str, pos, str.size() - pos));
94        return (int)filenames.size();
95}
96
97
98static string GetInternFilename(const string &filename, const string newSuffix)
99{
100        vector<string> filenames;
101        const int files = SplitFilenames(filename, filenames);
102
103        vector<string>::const_iterator sit, sit_end = filenames.end();
104        string kdFilename;
105
106        int i = 0;
107        for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
108        {
109                string currentFile = *sit;
110                string strippedFilename;
111
112                if (i == 0)
113                {       
114                        strippedFilename = currentFile;
115                }
116                else
117                {
118                        char *str = StripPath(currentFile.c_str());
119                        strippedFilename = string(str);
120
121                        delete [] str;
122                }
123               
124                string suffix("_");
125
126                if (i == (int)filenames.size() - 1)
127                {
128                        suffix = newSuffix;
129                }
130
131                if (strstr(strippedFilename.c_str(), ".x3d"))
132                {
133                        kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
134                }
135        else if (strstr(strippedFilename.c_str(), ".dat"))
136                {
137                        kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
138                }
139                else if (strstr(strippedFilename.c_str(), ".obj"))
140                {
141                        kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
142                }
143                else
144                {
145                        cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
146                }
147        }
148
149        //cout << "kdfilename: " << kdFilename << endl;
150        return kdFilename;
151}
152
153#include "ViewCellsManager.h"
154int
155main(int argc, char **argv)
156{
157
158        //Now just call this function at the start of your program and if you're
159        //compiling in debug mode (F5), any leaks will be displayed in the Output
160        //window when the program shuts down. If you're not in debug mode this will
161        //be ignored. Use it as you will!
162        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
163#if 1
164  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
165  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
166  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
167#endif
168
169        int returnCode = 0;
170       
171        InitTiming();
172
173        Debug.open("debug.log");
174 
175        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
176        MeshKdTree::ParseEnvironment();
177
178       
179        char buff[128];
180        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
181        string preprocessorType(buff);
182
183        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
184        {
185                Environment::DelSingleton();
186                cerr << "Unknown preprocessor type" << endl;
187                exit(1);
188        }
189
190
191        /////////////
192        //-- load scene
193
194        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
195        string filename(buff);
196       
197
198        if (!preprocessor->LoadScene(filename))
199        {
200                cout << "loading file " << filename << " failed" << endl;
201                Cleanup();
202                exit(1);
203        }
204
205        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
206        const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
207
208        ////////////
209        //-- initialize external ray caster
210       
211        if (preprocessor->InitRayCast(externKdTree, internKdTree))
212        {
213                cout << "ray casting initialized!" << endl;
214        }
215        else
216        {
217                cout << "ray casting initialization failed" << endl;
218                Cleanup();
219                exit(1);
220        }
221
222       
223        // export objects as obj
224        if (preprocessor->mExportObj)
225        {
226                if (strstr(filename.c_str(), ".obj"))
227                {
228                        cerr << "already in obj format" << endl;
229                        if (0)  preprocessor->ExportObj("test.obj", preprocessor->mObjects);
230                }
231                else
232                {
233                        const string objname = GetInternFilename(filename, ".obj");
234
235                        cout << "exporting scene to " << objname << endl;
236                        bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
237
238                        if (success)
239                        {
240                                cout << "finished exporting obj" << endl;
241                        }
242                        else
243                        {
244                                cerr << "error exporting " << objname << endl;
245                        }
246                }
247        }
248       
249       
250        // parse view cells related options
251        if (!preprocessor->PrepareViewCells())
252        {
253                Cleanup();
254                exit(1);
255        }
256       
257        if (0)
258        {
259                preprocessor->Export(filename + "-out.x3d", true, false);
260                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
261        }
262
263        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
264        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
265        PreprocessorThread *pt;
266
267
268#if USE_QT
269        // create a qt application first (must be created before any opengl widget ...)
270        QApplication *app = new QApplication(argc, NULL);
271       
272        pt = new QtPreprocessorThread(preprocessor);
273#else
274#if USE_THREADS
275        pt = new BoostPreprocessorThread(preprocessor);
276#else
277        pt = new PreprocessorThread(preprocessor);
278#endif
279#endif
280
281        bool guiSupported = false;
282
283        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
284          {
285                cout << "using gl widget" << endl;
286                // create and run the preprocessor application in a parallel thread
287#if USE_QT             
288#if USE_THREADS
289                pt->InitThread();
290                if (!preprocessor->mDelayVisibilityComputation)
291                  pt->RunThread();
292#endif
293               
294                // display the render widget
295                if (!rendererWidget)
296                {
297                 
298                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
299                        QMessageBox::information(0, "OpenGL pbuffers",
300                                                                         "This system does not support OpenGL/pbuffers.",
301                                                                         QMessageBox::Ok);
302                        return NULL;
303                  }
304                 
305                 
306                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
307                                                                                                  preprocessor->mViewCellsManager,
308                                                                                                  preprocessor->mKdTree);
309
310                  ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
311                  rendererWidget->Show();
312                  guiSupported = true;
313                }
314
315                int frames;
316                Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
317                if (frames) {
318                  // NOTE: render texture should be power of 2 and square
319                  // renderer must be initialised
320                  // $$matt
321                  preprocessor->renderer =
322                        new QtGlRendererBuffer(512, 512,
323                                                                   preprocessor->mSceneGraph,
324                                                                   preprocessor->mViewCellsManager,
325                                                                   preprocessor->mKdTree);
326                  //            preprocessor->Getrenderer->makeCurrent();
327                }
328                qApp->exec();
329#endif
330          }
331
332        if (!guiSupported) {
333          preprocessor->mUseGlRenderer = false;
334          preprocessor->mUseGlDebugger = false;
335        }
336
337        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
338                // just call the mail method -> will be executed in the main thread
339                pt->Main();
340        }       
341
342        // release memory
343        Cleanup();
344       
345        return returnCode;
346}
347
Note: See TracBrowser for help on using the repository browser.