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