1 | /*#ifdef GTP_INTERNAL
|
---|
2 | #ifndef USE_QT
|
---|
3 | #define USE_QT 1
|
---|
4 | #else
|
---|
5 | #define USE_QT 0
|
---|
6 | #endif
|
---|
7 | #endif
|
---|
8 | */
|
---|
9 |
|
---|
10 | #define USE_THREADS 0
|
---|
11 |
|
---|
12 | #ifdef UNICODE
|
---|
13 | #undef UNICODE
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include <windows.h>
|
---|
17 | #include <stdio.h>
|
---|
18 | #include <crtdbg.h>
|
---|
19 |
|
---|
20 | #include "PreprocessorFactory.h"
|
---|
21 | #include "Parser.h"
|
---|
22 | #include "Environment.h"
|
---|
23 | #include "MeshKdTree.h"
|
---|
24 | #include "Preprocessor.h"
|
---|
25 | #include "common.h"
|
---|
26 |
|
---|
27 | #include "PreprocessorThread.h"
|
---|
28 | #include "ObjExporter.h"
|
---|
29 | #include "SceneGraph.h"
|
---|
30 | //#include "GlobalLinesRenderer.h"
|
---|
31 |
|
---|
32 | #include "ViewCellsManager.h"
|
---|
33 |
|
---|
34 | #ifdef USE_QT
|
---|
35 | #include "QtPreprocessorThread.h"
|
---|
36 | #include "QtGlViewer.h"
|
---|
37 | #include "QtGlRenderer.h"
|
---|
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 | static string ReplaceSuffix(string filename, string a, string b)
|
---|
75 | {
|
---|
76 | string result = filename;
|
---|
77 |
|
---|
78 | int pos = (int)filename.rfind(a, (int)filename.size() - 1);
|
---|
79 | if (pos == filename.size() - a.size()) {
|
---|
80 | result.replace(pos, a.size(), b);
|
---|
81 | }
|
---|
82 | return result;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | static int SplitFilenames(const string str, vector<string> &filenames)
|
---|
87 | {
|
---|
88 | int pos = 0;
|
---|
89 |
|
---|
90 | while(1) {
|
---|
91 | int npos = (int)str.find(';', pos);
|
---|
92 |
|
---|
93 | if (npos < 0 || npos - pos < 1)
|
---|
94 | break;
|
---|
95 | filenames.push_back(string(str, pos, npos - pos));
|
---|
96 | pos = npos + 1;
|
---|
97 | }
|
---|
98 |
|
---|
99 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
100 | return (int)filenames.size();
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | static string GetInternFilename(const string &filename, const string newSuffix)
|
---|
105 | {
|
---|
106 | vector<string> filenames;
|
---|
107 | const int files = SplitFilenames(filename, filenames);
|
---|
108 |
|
---|
109 | vector<string>::const_iterator sit, sit_end = filenames.end();
|
---|
110 | string kdFilename;
|
---|
111 |
|
---|
112 | int i = 0;
|
---|
113 | for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
|
---|
114 | {
|
---|
115 | string currentFile = *sit;
|
---|
116 | string strippedFilename;
|
---|
117 |
|
---|
118 | if (i == 0)
|
---|
119 | {
|
---|
120 | strippedFilename = currentFile;
|
---|
121 | }
|
---|
122 | else
|
---|
123 | {
|
---|
124 | char *str = StripPath(currentFile.c_str());
|
---|
125 | strippedFilename = string(str);
|
---|
126 |
|
---|
127 | delete [] str;
|
---|
128 | }
|
---|
129 |
|
---|
130 | string suffix("_");
|
---|
131 |
|
---|
132 | if (i == (int)filenames.size() - 1)
|
---|
133 | {
|
---|
134 | suffix = newSuffix;
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (strstr(strippedFilename.c_str(), ".x3d"))
|
---|
138 | {
|
---|
139 | kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
|
---|
140 | }
|
---|
141 | else if (strstr(strippedFilename.c_str(), ".dat"))
|
---|
142 | {
|
---|
143 | kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
|
---|
144 | }
|
---|
145 | else if (strstr(strippedFilename.c_str(), ".obj"))
|
---|
146 | {
|
---|
147 | kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
|
---|
148 | }
|
---|
149 | else
|
---|
150 | {
|
---|
151 | cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | //cout << "kdfilename: " << kdFilename << endl;
|
---|
156 | return kdFilename;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | int
|
---|
161 | main(int argc, char **argv)
|
---|
162 | {
|
---|
163 | //Now just call this function at the start of your program and if you're
|
---|
164 | //compiling in debug mode (F5), any leaks will be displayed in the Output
|
---|
165 | //window when the program shuts down. If you're not in debug mode this will
|
---|
166 | //be ignored. Use it as you will!
|
---|
167 | //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
|
---|
168 | #if 1
|
---|
169 | _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
|
---|
170 | _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
|
---|
171 | _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | int returnCode = 0;
|
---|
175 |
|
---|
176 | InitTiming();
|
---|
177 |
|
---|
178 | Debug.open("debug.log");
|
---|
179 |
|
---|
180 | Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
|
---|
181 | MeshKdTree::ParseEnvironment();
|
---|
182 |
|
---|
183 | char buff[128];
|
---|
184 | Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
|
---|
185 | string preprocessorType(buff);
|
---|
186 |
|
---|
187 | if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
|
---|
188 | {
|
---|
189 | Environment::DelSingleton();
|
---|
190 | cerr << "Unknown preprocessor type" << endl;
|
---|
191 | exit(1);
|
---|
192 | }
|
---|
193 |
|
---|
194 | /////////////
|
---|
195 | //-- load scene
|
---|
196 |
|
---|
197 | Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
|
---|
198 | string filename(buff);
|
---|
199 |
|
---|
200 |
|
---|
201 | if (!preprocessor->LoadScene(filename))
|
---|
202 | {
|
---|
203 | cout << "loading file " << filename << " failed" << endl;
|
---|
204 | Cleanup();
|
---|
205 | exit(1);
|
---|
206 | }
|
---|
207 |
|
---|
208 | const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
|
---|
209 | const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
|
---|
210 |
|
---|
211 | ////////////
|
---|
212 | //-- initialize external ray caster
|
---|
213 |
|
---|
214 | if (preprocessor->InitRayCast(externKdTree, internKdTree))
|
---|
215 | {
|
---|
216 | cout << "ray casting initialized!" << endl;
|
---|
217 | }
|
---|
218 | else
|
---|
219 | {
|
---|
220 | cout << "ray casting initialization failed" << endl;
|
---|
221 | Cleanup();
|
---|
222 | exit(1);
|
---|
223 | }
|
---|
224 |
|
---|
225 | // export objects as obj
|
---|
226 | if (preprocessor->mExportObj)
|
---|
227 | {
|
---|
228 | if (strstr(filename.c_str(), ".obj"))
|
---|
229 | {
|
---|
230 | cerr << "already in obj format" << endl;
|
---|
231 | if (0) preprocessor->ExportObj("test.obj", preprocessor->mObjects);
|
---|
232 | }
|
---|
233 | else
|
---|
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 | if (success)
|
---|
241 | {
|
---|
242 | cout << "finished exporting obj" << endl;
|
---|
243 | }
|
---|
244 | else
|
---|
245 | {
|
---|
246 | cerr << "error exporting " << objname << endl;
|
---|
247 | }
|
---|
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 |
|
---|
256 | Cleanup();
|
---|
257 | exit(1);
|
---|
258 | }
|
---|
259 |
|
---|
260 | // create a preprocessor thread (note: capsulates calls to boost fuctions!)
|
---|
261 | PreprocessorThread *pt = NULL;
|
---|
262 |
|
---|
263 | #ifdef TRY_GLOBAL_LINES
|
---|
264 | GlRenderer *renderer = new GlRenderer(preprocessor->mSceneGraph,
|
---|
265 | preprocessor->mViewCellsManager,
|
---|
266 | preprocessor->mKdTree);
|
---|
267 |
|
---|
268 | globalLinesRenderer = new GlobalLinesRenderer(preprocessor, renderer);
|
---|
269 | globalLinesRenderer->InitGl();
|
---|
270 | globalLinesRenderer->Run();
|
---|
271 |
|
---|
272 | #else
|
---|
273 |
|
---|
274 | #ifdef USE_QT
|
---|
275 |
|
---|
276 | // create a qt application first (must be created before any opengl widget ...)
|
---|
277 | QApplication *app = new QApplication(argc, NULL);
|
---|
278 |
|
---|
279 | pt = new QtPreprocessorThread(preprocessor);
|
---|
280 |
|
---|
281 | #else
|
---|
282 |
|
---|
283 | //#if USE_THREADS
|
---|
284 | // pt = new BoostPreprocessorThread(preprocessor);
|
---|
285 | //#else
|
---|
286 | // use a dummy thread
|
---|
287 | pt = new DummyPreprocessorThread(preprocessor);
|
---|
288 | //#endif
|
---|
289 | #endif
|
---|
290 |
|
---|
291 | preprocessor->SetThread(pt);
|
---|
292 |
|
---|
293 | bool guiSupported = false;
|
---|
294 |
|
---|
295 | if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
|
---|
296 | {
|
---|
297 | #ifdef USE_QT
|
---|
298 | cout << "using gl widget" << endl;
|
---|
299 | // create and run the preprocessor application in a parallel thread
|
---|
300 |
|
---|
301 | pt->InitThread();
|
---|
302 | pt->RunThread();
|
---|
303 |
|
---|
304 | // display the render widget
|
---|
305 | if (!rendererWidget)
|
---|
306 | {
|
---|
307 | if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
|
---|
308 | QMessageBox::information(0, "OpenGL pbuffers",
|
---|
309 | "This system does not support OpenGL/pbuffers.",
|
---|
310 | QMessageBox::Ok);
|
---|
311 | return NULL;
|
---|
312 | }
|
---|
313 |
|
---|
314 | rendererWidget =
|
---|
315 | new QtGlRendererWidget(preprocessor->mSceneGraph,
|
---|
316 | preprocessor->mViewCellsManager,
|
---|
317 | preprocessor->mKdTree);
|
---|
318 |
|
---|
319 | rendererWidget->Show();
|
---|
320 |
|
---|
321 | QtGlViewer *viewer =
|
---|
322 | new QtGlViewer(NULL, (QtGlRendererWidget *)rendererWidget);
|
---|
323 |
|
---|
324 | viewer->show();
|
---|
325 | guiSupported = true;
|
---|
326 | }
|
---|
327 |
|
---|
328 | int frames;
|
---|
329 | Environment::GetSingleton()->GetIntValue("Preprocessor.pvsRenderErrorSamples", frames);
|
---|
330 |
|
---|
331 | if (frames)
|
---|
332 | {
|
---|
333 | // NOTE: render texture should be power of 2 and square
|
---|
334 | // renderer must be initialised
|
---|
335 | // $$matt
|
---|
336 | preprocessor->renderer =
|
---|
337 | new QtGlRendererBuffer(512, 512,
|
---|
338 | preprocessor->mSceneGraph,
|
---|
339 | preprocessor->mViewCellsManager,
|
---|
340 | preprocessor->mKdTree);
|
---|
341 | }
|
---|
342 |
|
---|
343 | qApp->exec();
|
---|
344 | #endif
|
---|
345 | }
|
---|
346 |
|
---|
347 | // no gui available
|
---|
348 | if (!guiSupported)
|
---|
349 | {
|
---|
350 | if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
|
---|
351 | cout << "warning: gui not supported!" << endl;
|
---|
352 |
|
---|
353 | preprocessor->mUseGlRenderer = false;
|
---|
354 | preprocessor->mUseGlDebugger = false;
|
---|
355 | }
|
---|
356 |
|
---|
357 | if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger))
|
---|
358 | {
|
---|
359 | cout << "executing main thread" << endl;
|
---|
360 | // just call the mail method -> will be executed in the main thread
|
---|
361 | pt->Main();
|
---|
362 | }
|
---|
363 | #endif
|
---|
364 | // release memory
|
---|
365 | Cleanup();
|
---|
366 |
|
---|
367 | return returnCode;
|
---|
368 | }
|
---|
369 |
|
---|