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