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