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