1 | #define USE_THREADS 0
|
---|
2 |
|
---|
3 | #ifdef UNICODE
|
---|
4 | #undef UNICODE
|
---|
5 | #endif
|
---|
6 |
|
---|
7 | #define NOMINMAX
|
---|
8 | #ifdef __WINDOWS__
|
---|
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 "vlastimil/testrt.h"
|
---|
29 |
|
---|
30 | #include "ViewCellsManager.h"
|
---|
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 |
|
---|
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 | #if 0
|
---|
135 | // Test code by VH
|
---|
136 | //TestRTcamera(argc, argv);
|
---|
137 | //TestRTfromFile(argc, argv);
|
---|
138 | TestRTfromFilePackets(argc, argv);
|
---|
139 | return 0;
|
---|
140 | #endif
|
---|
141 |
|
---|
142 | int returnCode = 0;
|
---|
143 |
|
---|
144 | //Now just call this function at the start of your program and if you're
|
---|
145 | //compiling in debug mode (F5), any leaks will be displayed in the Output
|
---|
146 | //window when the program shuts down. If you're not in debug mode this will
|
---|
147 | //be ignored. Use it as you will!
|
---|
148 | //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
|
---|
149 |
|
---|
150 | //#define _CRT_SET
|
---|
151 |
|
---|
152 | #ifdef _CRT_SET
|
---|
153 | _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
|
---|
154 | _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
|
---|
155 | _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | InitTiming();
|
---|
159 | Debug.open("debug.log");
|
---|
160 |
|
---|
161 | Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
|
---|
162 | MeshKdTree::ParseEnvironment();
|
---|
163 |
|
---|
164 | char buff[128];
|
---|
165 | Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
|
---|
166 | string preprocessorType(buff);
|
---|
167 |
|
---|
168 | if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
|
---|
169 | {
|
---|
170 | Environment::DelSingleton();
|
---|
171 | cerr << "Unknown preprocessor type" << endl;
|
---|
172 | exit(1);
|
---|
173 | }
|
---|
174 |
|
---|
175 | Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
|
---|
176 | string filename(buff);
|
---|
177 |
|
---|
178 | const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
|
---|
179 | const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ?
|
---|
180 | ".kdm" : ".kdt");
|
---|
181 |
|
---|
182 | if (preprocessor->InitRayCast(externKdTree, internKdTree))
|
---|
183 | {
|
---|
184 | cout << "ray casting initialized!" << endl;
|
---|
185 | }
|
---|
186 | else
|
---|
187 | {
|
---|
188 | cout << "ray casting initialization failed!" << endl;
|
---|
189 | Cleanup();
|
---|
190 | exit(1);
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | //Debug << "using pvs type " << PVS_TYPE << endl;
|
---|
195 |
|
---|
196 | /////////////
|
---|
197 | //-- load scene
|
---|
198 |
|
---|
199 | if (!preprocessor->LoadScene(filename))
|
---|
200 | {
|
---|
201 | cout << "loading file " << filename << " failed" << endl;
|
---|
202 | Cleanup();
|
---|
203 | exit(1);
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | ////////////
|
---|
208 | //-- initialize external ray caster
|
---|
209 |
|
---|
210 | if (preprocessor->LoadInternKdTree(internKdTree))
|
---|
211 | {
|
---|
212 | cout << "intern kd tree loaded!" << endl;
|
---|
213 | }
|
---|
214 | else
|
---|
215 | {
|
---|
216 | cout << "loading intern kd tree failed!" << endl;
|
---|
217 | Cleanup();
|
---|
218 | exit(1);
|
---|
219 | }
|
---|
220 |
|
---|
221 | // export objects as obj
|
---|
222 | if (preprocessor->mExportObj)
|
---|
223 | {
|
---|
224 | if (strstr(filename.c_str(), ".obj"))
|
---|
225 | {
|
---|
226 | cerr << "already in obj format" << endl;
|
---|
227 | if (0) preprocessor->ExportObj("test.obj", preprocessor->mObjects);
|
---|
228 | }
|
---|
229 | else
|
---|
230 | {
|
---|
231 |
|
---|
232 | const string objname = GetInternFilename(filename, ".obj");
|
---|
233 |
|
---|
234 | cout << "exporting scene to " << objname << endl;
|
---|
235 | bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
|
---|
236 |
|
---|
237 |
|
---|
238 | if (success)
|
---|
239 | cout << "finished exporting obj" << endl;
|
---|
240 | else
|
---|
241 | cerr << "error exporting " << objname << endl;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | // parse view cells related options
|
---|
246 | if (!preprocessor->PrepareViewCells())
|
---|
247 | {
|
---|
248 | cerr << "error: view cells could not be loaded" << endl;
|
---|
249 |
|
---|
250 | Cleanup();
|
---|
251 | exit(1);
|
---|
252 | }
|
---|
253 |
|
---|
254 | string viewCellPointsFile;
|
---|
255 |
|
---|
256 | if (strstr(filename.c_str(), ".obj"))
|
---|
257 | viewCellPointsFile = ReplaceSuffix(filename, ".obj", ".vc");
|
---|
258 | else if (strstr(filename.c_str(), ".dat"))
|
---|
259 | viewCellPointsFile = ReplaceSuffix(filename, ".dat", ".vc");
|
---|
260 | else if (strstr(filename.c_str(), ".x3d"))
|
---|
261 | viewCellPointsFile = ReplaceSuffix(filename, ".x3d", ".vc");
|
---|
262 |
|
---|
263 | bool importRandomViewCells;
|
---|
264 | Environment::GetSingleton()->GetBoolValue("ViewCells.importRandomViewCells",
|
---|
265 | importRandomViewCells);
|
---|
266 |
|
---|
267 | if (importRandomViewCells)
|
---|
268 | {
|
---|
269 | cout << "importing random view cells" << endl;
|
---|
270 | preprocessor->mViewCellsManager->ImportViewCellsList(viewCellPointsFile);
|
---|
271 | cout << "finished" << endl;
|
---|
272 | }
|
---|
273 |
|
---|
274 | bool useHwGlobalLines;
|
---|
275 | Environment::GetSingleton()->GetBoolValue("Preprocessor.useHwGlobalLines",
|
---|
276 | useHwGlobalLines);
|
---|
277 |
|
---|
278 | if (useHwGlobalLines)
|
---|
279 | preprocessor->PrepareHwGlobalLines();
|
---|
280 |
|
---|
281 | // create a preprocessor thread (note: capsulates calls to boost fuctions!)
|
---|
282 | PreprocessorThread *pt = NULL;
|
---|
283 |
|
---|
284 | //preprocessor->PrepareHwGlobalLines();
|
---|
285 | //globalLinesRenderer->Run();
|
---|
286 |
|
---|
287 |
|
---|
288 | bool guiSupported = false;
|
---|
289 | const bool useRendererBuffer = true;
|
---|
290 |
|
---|
291 | int frames;
|
---|
292 | Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
|
---|
293 |
|
---|
294 | #ifdef USE_QT
|
---|
295 |
|
---|
296 | // create a qt application first (must be created before any opengl widget ...)
|
---|
297 | QApplication *app = new QApplication(argc, NULL);
|
---|
298 | pt = new QtPreprocessorThread(preprocessor);
|
---|
299 |
|
---|
300 | if (0 && preprocessor->mUseGlRenderer && (importRandomViewCells || frames))
|
---|
301 | {
|
---|
302 | QGLFormat f;
|
---|
303 | f.setStencil(true);
|
---|
304 | QGLFormat::setDefaultFormat(f);
|
---|
305 |
|
---|
306 | // NOTE: render texture should be power of 2 and square
|
---|
307 | // renderer must be initialised
|
---|
308 | // $$matt
|
---|
309 | preprocessor->renderer =
|
---|
310 | new QtGlRendererBuffer(512, 512,
|
---|
311 | preprocessor->mSceneGraph,
|
---|
312 | preprocessor->mViewCellsManager,
|
---|
313 | preprocessor->mKdTree);
|
---|
314 | }
|
---|
315 |
|
---|
316 | if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
|
---|
317 | {
|
---|
318 | ////////
|
---|
319 | //-- create and run the preprocessor application in a parallel thread
|
---|
320 |
|
---|
321 | cout << "using gl widget" << endl;
|
---|
322 |
|
---|
323 | pt->InitThread();
|
---|
324 | pt->RunThread();
|
---|
325 |
|
---|
326 | // display the render widget
|
---|
327 | if (!rendererWidget)
|
---|
328 | {
|
---|
329 | if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
|
---|
330 | cout << "damn" << endl;
|
---|
331 | QMessageBox::information(0, "OpenGL pbuffers",
|
---|
332 | "This system does not support OpenGL/pbuffers.",
|
---|
333 | QMessageBox::Ok);
|
---|
334 | return NULL;
|
---|
335 | }
|
---|
336 |
|
---|
337 | rendererWidget =
|
---|
338 | new QtGlRendererWidget(preprocessor->mSceneGraph,
|
---|
339 | preprocessor->mViewCellsManager,
|
---|
340 | preprocessor->mKdTree);
|
---|
341 |
|
---|
342 | rendererWidget->Show();
|
---|
343 |
|
---|
344 | if (1) // not working with vbo
|
---|
345 | {
|
---|
346 | cout << "starting the qt viewer" << endl;
|
---|
347 | QtGlViewer *viewer =
|
---|
348 | new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
|
---|
349 | viewer->show();
|
---|
350 | }
|
---|
351 |
|
---|
352 | guiSupported = true;
|
---|
353 | }
|
---|
354 |
|
---|
355 | bool exportRandomViewCells;
|
---|
356 | Environment::GetSingleton()->GetBoolValue("ViewCells.exportRandomViewCells",
|
---|
357 | exportRandomViewCells);
|
---|
358 |
|
---|
359 | if (exportRandomViewCells)
|
---|
360 | {
|
---|
361 | cout << "exporting random view cells" << endl;
|
---|
362 | preprocessor->mViewCellsManager->ExportRandomViewCells(viewCellPointsFile);
|
---|
363 | cout << "finished" << endl;
|
---|
364 | }
|
---|
365 |
|
---|
366 | /*bool evaluatePixelError;
|
---|
367 | Environment::GetSingleton()->GetBoolValue("Preprocessor.evaluatePixelError", evaluatePixelError);
|
---|
368 |
|
---|
369 | if (evaluatePixelError)
|
---|
370 | {
|
---|
371 | cout << "evaluating pixel error" << endl;
|
---|
372 | preprocessor->ComputeRenderError();
|
---|
373 | }*/
|
---|
374 |
|
---|
375 | qApp->exec();
|
---|
376 | }
|
---|
377 | #else // USE_QUT
|
---|
378 |
|
---|
379 | //#if USE_THREADS
|
---|
380 | // pt = new BoostPreprocessorThread(preprocessor);
|
---|
381 | //#else
|
---|
382 | // use a dummy thread
|
---|
383 | pt = new DummyPreprocessorThread(preprocessor);
|
---|
384 | //#endif
|
---|
385 | #endif
|
---|
386 |
|
---|
387 | preprocessor->SetThread(pt);
|
---|
388 |
|
---|
389 | // no gui available
|
---|
390 | if (!guiSupported)
|
---|
391 | {
|
---|
392 | if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
|
---|
393 | cout << "warning: gui not supported!" << endl;
|
---|
394 |
|
---|
395 | preprocessor->mUseGlRenderer = false;
|
---|
396 | preprocessor->mUseGlDebugger = false;
|
---|
397 | }
|
---|
398 |
|
---|
399 | if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
|
---|
400 | {
|
---|
401 | cout << "executing main thread" << endl;
|
---|
402 | // just call the mail method -> will be executed in the main thread
|
---|
403 | pt->Main();
|
---|
404 | }
|
---|
405 |
|
---|
406 | // release memory
|
---|
407 | Cleanup();
|
---|
408 | delete pt;
|
---|
409 |
|
---|
410 | return returnCode;
|
---|
411 | }
|
---|
412 |
|
---|