1 | #define USE_THREADS 0
|
---|
2 |
|
---|
3 | #ifdef UNICODE
|
---|
4 | #undef UNICODE
|
---|
5 | #endif
|
---|
6 |
|
---|
7 | #define NOMINMAX
|
---|
8 | #ifdef _WIN32
|
---|
9 | #include <windows.h>
|
---|
10 | #ifdef _CRT_SET
|
---|
11 | #include <crtdbg.h>
|
---|
12 | #endif // _CRT_SET
|
---|
13 | #endif
|
---|
14 | #include <cstdio>
|
---|
15 |
|
---|
16 | #include "Camera.h"
|
---|
17 | #include "PreprocessorFactory.h"
|
---|
18 | #include "Parser.h"
|
---|
19 | #include "Environment.h"
|
---|
20 | #include "MeshKdTree.h"
|
---|
21 | #include "Preprocessor.h"
|
---|
22 | #include "common.h"
|
---|
23 | #include "PreprocessorThread.h"
|
---|
24 | #include "ObjExporter.h"
|
---|
25 | #include "SceneGraph.h"
|
---|
26 | #include "GlobalLinesRenderer.h"
|
---|
27 | #include "RayCaster.h"
|
---|
28 | #include "ViewCellsManager.h"
|
---|
29 | #include "SepPlanesBox3.h"
|
---|
30 |
|
---|
31 |
|
---|
32 | #ifdef USE_QT
|
---|
33 | #include "QtPreprocessorThread.h"
|
---|
34 | #include "QtGlViewer.h"
|
---|
35 | #include "QtGlRenderer.h"
|
---|
36 | #include <QGLWidget>
|
---|
37 |
|
---|
38 | #else
|
---|
39 | #if USE_THREADS
|
---|
40 | #include "BoostPreprocessorThread.h"
|
---|
41 | #endif
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #include "ResourceManager.h"
|
---|
45 | #include "GlRenderer.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | #define USE_EXE_PATH false
|
---|
49 |
|
---|
50 |
|
---|
51 | using namespace GtpVisibilityPreprocessor;
|
---|
52 |
|
---|
53 | //Preprocessor *preprocessor = NULL;
|
---|
54 | GlRendererWidget *rendererWidget = NULL;
|
---|
55 | //GlobalLinesRenderer *globalLinesRenderer = NULL;
|
---|
56 |
|
---|
57 |
|
---|
58 | // DLL function signature
|
---|
59 | typedef GlRendererWidget *(*importFunction)(Preprocessor *);
|
---|
60 |
|
---|
61 |
|
---|
62 | static string GetViewPointsListName(const string &filename)
|
---|
63 | {
|
---|
64 | char buff[200];
|
---|
65 | Environment::GetSingleton()->GetStringValue("ViewCells.randomViewPointsList", buff);
|
---|
66 | string viewCellPointsFile = buff;
|
---|
67 |
|
---|
68 | if (viewCellPointsFile == "")
|
---|
69 | {
|
---|
70 | // if not specified, take file that has same prefix as scene file
|
---|
71 | if (strstr(filename.c_str(), ".obj"))
|
---|
72 | viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
|
---|
73 | else if (strstr(filename.c_str(), ".dat"))
|
---|
74 | viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
|
---|
75 | else if (strstr(filename.c_str(), ".x3d"))
|
---|
76 | viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
|
---|
77 | }
|
---|
78 |
|
---|
79 | return viewCellPointsFile;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | void Cleanup()
|
---|
84 | {
|
---|
85 | DEL_PTR(rendererWidget);
|
---|
86 | DEL_PTR(preprocessor);
|
---|
87 |
|
---|
88 | Environment::DelSingleton();
|
---|
89 | MeshManager::DelSingleton();
|
---|
90 | MaterialManager::DelSingleton();
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 |
|
---|
95 |
|
---|
96 | static string GetInternFilename(const string &filename, const string newSuffix)
|
---|
97 | {
|
---|
98 | vector<string> filenames;
|
---|
99 | const int files = SplitFilenames(filename, filenames);
|
---|
100 |
|
---|
101 | vector<string>::const_iterator sit, sit_end = filenames.end();
|
---|
102 | string kdFilename;
|
---|
103 |
|
---|
104 | int i = 0;
|
---|
105 | for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
|
---|
106 | {
|
---|
107 | string currentFile = *sit;
|
---|
108 | string strippedFilename;
|
---|
109 |
|
---|
110 | if (i == 0)
|
---|
111 | {
|
---|
112 | strippedFilename = currentFile;
|
---|
113 | }
|
---|
114 | else
|
---|
115 | {
|
---|
116 | char *str = StripPath(currentFile.c_str());
|
---|
117 | strippedFilename = string(str);
|
---|
118 |
|
---|
119 | delete [] str;
|
---|
120 | }
|
---|
121 |
|
---|
122 | string suffix("_");
|
---|
123 |
|
---|
124 | if (i == (int)filenames.size() - 1)
|
---|
125 | {
|
---|
126 | suffix = newSuffix;
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (strstr(strippedFilename.c_str(), ".x3d"))
|
---|
130 | {
|
---|
131 | kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
|
---|
132 | }
|
---|
133 | else if (strstr(strippedFilename.c_str(), ".dat"))
|
---|
134 | {
|
---|
135 | kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
|
---|
136 | }
|
---|
137 | else if (strstr(strippedFilename.c_str(), ".obj"))
|
---|
138 | {
|
---|
139 | kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
|
---|
140 | }
|
---|
141 | else
|
---|
142 | {
|
---|
143 | cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | //cout << "kdfilename: " << kdFilename << endl;
|
---|
148 | return kdFilename;
|
---|
149 | }
|
---|
150 |
|
---|
151 | int
|
---|
152 | main(int argc, char **argv)
|
---|
153 | {
|
---|
154 | int returnCode = 0;
|
---|
155 |
|
---|
156 | #ifdef _CRT_SET
|
---|
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 |
|
---|
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 | InitTiming();
|
---|
170 | Debug.open("debug.log");
|
---|
171 |
|
---|
172 |
|
---|
173 | //TestSepAxis();
|
---|
174 |
|
---|
175 | Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
|
---|
176 | MeshKdTree::ParseEnvironment();
|
---|
177 |
|
---|
178 | char buff[128];
|
---|
179 | Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
|
---|
180 | string preprocessorType(buff);
|
---|
181 |
|
---|
182 | if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
|
---|
183 | {
|
---|
184 | Environment::DelSingleton();
|
---|
185 | cerr << "Unknown preprocessor type" << endl;
|
---|
186 | exit(1);
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
|
---|
191 | string filename(buff);
|
---|
192 |
|
---|
193 | const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
|
---|
194 | const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
|
---|
195 | ".kdm" : ".kdt");
|
---|
196 |
|
---|
197 | if (preprocessor->InitRayCast(externKdTree, internKdTree))
|
---|
198 | {
|
---|
199 | cout << "ray casting initialized!" << endl;
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | cout << "ray casting initialization failed!" << endl;
|
---|
204 | Cleanup();
|
---|
205 | exit(1);
|
---|
206 | }
|
---|
207 |
|
---|
208 | /////////////
|
---|
209 | //-- load scene
|
---|
210 |
|
---|
211 | if (!preprocessor->LoadScene(filename))
|
---|
212 | {
|
---|
213 | cout << "loading file " << filename << " failed" << endl;
|
---|
214 | Cleanup();
|
---|
215 | exit(1);
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | ////////////
|
---|
220 | //-- initialize external ray caster
|
---|
221 | if (preprocessor->LoadInternKdTree(internKdTree))
|
---|
222 | {
|
---|
223 | cout << "intern kd tree loaded!" << endl;
|
---|
224 | }
|
---|
225 | else
|
---|
226 | {
|
---|
227 | cout << "loading intern kd tree failed!" << endl;
|
---|
228 | Cleanup();
|
---|
229 | exit(1);
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | // export objects as obj
|
---|
234 | if (preprocessor->mExportObj)
|
---|
235 | {
|
---|
236 | if (strstr(filename.c_str(), ".obj"))
|
---|
237 | {
|
---|
238 | cerr << "already in obj format" << endl;
|
---|
239 | if (0) preprocessor->ExportObj("test.obj", preprocessor->mObjects);
|
---|
240 | }
|
---|
241 | else
|
---|
242 | {
|
---|
243 | const string objname = GetInternFilename(filename, ".obj");
|
---|
244 |
|
---|
245 | cout << "exporting scene to " << objname << endl;
|
---|
246 | bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
|
---|
247 |
|
---|
248 | if (success)
|
---|
249 | cout << "finished exporting obj" << endl;
|
---|
250 | else
|
---|
251 | cerr << "error exporting " << objname << endl;
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | // parse view cells related options
|
---|
256 | if (!preprocessor->PrepareViewCells())
|
---|
257 | {
|
---|
258 | cerr << "error: view cells could not be loaded" << endl;
|
---|
259 | Cleanup();
|
---|
260 | exit(1);
|
---|
261 | }
|
---|
262 | else
|
---|
263 | cout << "view cells successfully loaded" << endl;
|
---|
264 |
|
---|
265 |
|
---|
266 | const string viewCellPointsFile = GetViewPointsListName(filename);
|
---|
267 |
|
---|
268 | bool importRandomViewCells;
|
---|
269 | Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells", importRandomViewCells);
|
---|
270 |
|
---|
271 | if (importRandomViewCells)
|
---|
272 | {
|
---|
273 | cout << "importing random view points" << endl;
|
---|
274 |
|
---|
275 | if (preprocessor->mViewCellsManager->ImportViewCellsList(viewCellPointsFile))
|
---|
276 | cout << "successfully loaded " << viewCellPointsFile << endl;
|
---|
277 | else
|
---|
278 | cerr << "error: file " << viewCellPointsFile << " not found" << endl;
|
---|
279 | }
|
---|
280 |
|
---|
281 | // create meshes of view cells
|
---|
282 | preprocessor->mViewCellsManager->FinalizeViewCells(true);
|
---|
283 |
|
---|
284 | cout << "view space box: " << preprocessor->mViewCellsManager->GetViewSpaceBox() << endl;
|
---|
285 | bool useHwGlobalLines;
|
---|
286 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
|
---|
287 | useHwGlobalLines);
|
---|
288 |
|
---|
289 | if (useHwGlobalLines)
|
---|
290 | preprocessor->PrepareHwGlobalLines();
|
---|
291 |
|
---|
292 | // create a preprocessor thread (note: capsulates calls to boost fuctions!)
|
---|
293 | PreprocessorThread *pt = NULL;
|
---|
294 |
|
---|
295 | //preprocessor->PrepareHwGlobalLines();
|
---|
296 | //globalLinesRenderer->Run();
|
---|
297 |
|
---|
298 |
|
---|
299 | bool guiSupported = false;
|
---|
300 | const bool useRendererBuffer = true;
|
---|
301 |
|
---|
302 | int frames;
|
---|
303 | Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
|
---|
304 |
|
---|
305 | bool evalPixelError;
|
---|
306 | Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", evalPixelError);
|
---|
307 |
|
---|
308 |
|
---|
309 | #ifdef USE_QT
|
---|
310 |
|
---|
311 | // create a qt application first (must be created before any opengl widget ...)
|
---|
312 | QApplication *app = new QApplication(argc, NULL);
|
---|
313 |
|
---|
314 | pt = new QtPreprocessorThread(preprocessor);
|
---|
315 |
|
---|
316 | if (evalPixelError && (importRandomViewCells || frames))
|
---|
317 | {
|
---|
318 | if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers())
|
---|
319 | {
|
---|
320 | cerr << "this system does not support OpenGL/pbuffers" << endl;
|
---|
321 |
|
---|
322 | QMessageBox::information(0, "OpenGL pbuffers",
|
---|
323 | "This system does not support OpenGL/pbuffers.",
|
---|
324 | QMessageBox::Ok);
|
---|
325 |
|
---|
326 | return NULL;
|
---|
327 | }
|
---|
328 |
|
---|
329 | QGLFormat f;
|
---|
330 | f.setStencil(true);
|
---|
331 | QGLFormat::setDefaultFormat(f);
|
---|
332 |
|
---|
333 | // NOTE: render texture should be power of 2 and square
|
---|
334 | // renderer must be initialised
|
---|
335 | const float w = 1024;
|
---|
336 | const float h = 1024;
|
---|
337 |
|
---|
338 | QtGlRendererBuffer *glbuf =
|
---|
339 | new QtGlRendererBuffer(w, h,
|
---|
340 | preprocessor->mSceneGraph,
|
---|
341 | preprocessor->mViewCellsManager,
|
---|
342 | preprocessor->mKdTree);
|
---|
343 |
|
---|
344 | preprocessor->renderer = glbuf;
|
---|
345 | }
|
---|
346 |
|
---|
347 | bool exportRandomViewCells;
|
---|
348 | Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
|
---|
349 | exportRandomViewCells);
|
---|
350 |
|
---|
351 | if (exportRandomViewCells)
|
---|
352 | {
|
---|
353 | cout << "exporting random view cells" << endl;
|
---|
354 | preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
|
---|
355 | cout << "finished" << endl;
|
---|
356 | }
|
---|
357 |
|
---|
358 | if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
|
---|
359 | {
|
---|
360 | ////////
|
---|
361 | //-- create and run the preprocessor application in a parallel thread
|
---|
362 |
|
---|
363 | cout << "using gl widget" << endl;
|
---|
364 |
|
---|
365 | pt->InitThread();
|
---|
366 | pt->RunThread();
|
---|
367 |
|
---|
368 | // display the render widget
|
---|
369 | if (!rendererWidget)
|
---|
370 | {
|
---|
371 | if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers())
|
---|
372 | {
|
---|
373 | cerr << "this system does not support OpenGL/pbuffers" << endl;
|
---|
374 |
|
---|
375 | QMessageBox::information(0, "OpenGL pbuffers",
|
---|
376 | "This system does not support OpenGL/pbuffers.",
|
---|
377 | QMessageBox::Ok);
|
---|
378 |
|
---|
379 | return NULL;
|
---|
380 | }
|
---|
381 |
|
---|
382 | rendererWidget =
|
---|
383 | new QtGlRendererWidget(preprocessor->mSceneGraph,
|
---|
384 | preprocessor->mViewCellsManager,
|
---|
385 | preprocessor->mKdTree);
|
---|
386 |
|
---|
387 | rendererWidget->Show();
|
---|
388 |
|
---|
389 | // hack matt
|
---|
390 | preprocessor->mRendererWidget = rendererWidget;
|
---|
391 | //rendererWidget->SetWindowTitle("Global Visualization");
|
---|
392 |
|
---|
393 | if (1 && !rendererWidget->GetUseVbos()) // viewer not working with vbo
|
---|
394 | {
|
---|
395 | cout << "starting the qt viewer" << endl;
|
---|
396 | QtGlViewer *viewer =
|
---|
397 | new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
|
---|
398 | viewer->show();
|
---|
399 | }
|
---|
400 |
|
---|
401 | guiSupported = true;
|
---|
402 | }
|
---|
403 |
|
---|
404 | qApp->exec();
|
---|
405 | }
|
---|
406 | #else // USE_QT
|
---|
407 |
|
---|
408 | /*#if USE_THREADS
|
---|
409 | pt = new BoostPreprocessorThread(preprocessor);
|
---|
410 | #else
|
---|
411 | // use a dummy thread
|
---|
412 | pt = new DummyPreprocessorThread(preprocessor);
|
---|
413 | #endif
|
---|
414 | */
|
---|
415 | #endif
|
---|
416 |
|
---|
417 | preprocessor->SetThread(pt);
|
---|
418 |
|
---|
419 | // no gui available
|
---|
420 | if (!guiSupported)
|
---|
421 | {
|
---|
422 | if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
|
---|
423 | cout << "warning: gui not supported!" << endl;
|
---|
424 |
|
---|
425 | preprocessor->mUseGlRenderer = false;
|
---|
426 | preprocessor->mUseGlDebugger = false;
|
---|
427 | }
|
---|
428 |
|
---|
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 | }
|
---|
435 |
|
---|
436 | // release memory
|
---|
437 | Cleanup();
|
---|
438 | delete pt;
|
---|
439 |
|
---|
440 | return returnCode;
|
---|
441 | }
|
---|
442 |
|
---|