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