1 | #ifdef GTP_INTERNAL
|
---|
2 | #define USE_QT 1
|
---|
3 | #else
|
---|
4 | #define USE_QT 0
|
---|
5 | #endif
|
---|
6 |
|
---|
7 | #define USE_THREADS 1
|
---|
8 |
|
---|
9 | #ifdef UNICODE
|
---|
10 | #undef UNICODE
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include <windows.h>
|
---|
14 | #include <stdio.h>
|
---|
15 | #include <crtdbg.h>
|
---|
16 |
|
---|
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 |
|
---|
24 | #include "PreprocessorThread.h"
|
---|
25 | #include "ObjExporter.h"
|
---|
26 | #include "SceneGraph.h"
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | #if !USE_QT && USE_THREADS
|
---|
31 | #include "BoostPreprocessorThread.h"
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include "ResourceManager.h"
|
---|
35 | #include "GlRenderer.h"
|
---|
36 |
|
---|
37 | #if USE_QT
|
---|
38 | #include "QtPreprocessorThread.h"
|
---|
39 | #include "QtGlRenderer.h"
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #define USE_EXE_PATH false
|
---|
43 |
|
---|
44 |
|
---|
45 | using namespace GtpVisibilityPreprocessor;
|
---|
46 |
|
---|
47 | Preprocessor *preprocessor = NULL;
|
---|
48 | GlRendererWidget *rendererWidget = NULL;
|
---|
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(string filename, string a, 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 | result.replace(pos, a.size(), b);
|
---|
73 | }
|
---|
74 | return result;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | static int SplitFilenames(const string str, vector<string> &filenames)
|
---|
79 | {
|
---|
80 | int pos = 0;
|
---|
81 |
|
---|
82 | while(1) {
|
---|
83 | int npos = (int)str.find(';', pos);
|
---|
84 |
|
---|
85 | if (npos < 0 || npos - pos < 1)
|
---|
86 | break;
|
---|
87 | filenames.push_back(string(str, pos, npos - pos));
|
---|
88 | pos = npos + 1;
|
---|
89 | }
|
---|
90 |
|
---|
91 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
92 | return (int)filenames.size();
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | static string GetInternFilename(const string &filename, const string newSuffix)
|
---|
97 | {
|
---|
98 | vector<string> filenames;
|
---|
99 | const int files = SplitFilenames(filename, filenames);
|
---|
100 |
|
---|
101 | vector<string>::const_iterator sit, sit_end = filenames.end();
|
---|
102 | string kdFilename;
|
---|
103 |
|
---|
104 | int i = 0;
|
---|
105 | for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
|
---|
106 | {
|
---|
107 | string currentFile = *sit;
|
---|
108 | string strippedFilename;
|
---|
109 |
|
---|
110 | if (i == 0)
|
---|
111 | {
|
---|
112 | strippedFilename = currentFile;
|
---|
113 | }
|
---|
114 | else
|
---|
115 | {
|
---|
116 | char *str = StripPath(currentFile.c_str());
|
---|
117 | strippedFilename = string(str);
|
---|
118 |
|
---|
119 | delete [] str;
|
---|
120 | }
|
---|
121 |
|
---|
122 | string suffix("_");
|
---|
123 |
|
---|
124 | if (i == (int)filenames.size() - 1)
|
---|
125 | {
|
---|
126 | suffix = newSuffix;
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (strstr(strippedFilename.c_str(), ".x3d"))
|
---|
130 | {
|
---|
131 | kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
|
---|
132 | }
|
---|
133 | else if (strstr(strippedFilename.c_str(), ".dat"))
|
---|
134 | {
|
---|
135 | kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
|
---|
136 | }
|
---|
137 | else if (strstr(strippedFilename.c_str(), ".obj"))
|
---|
138 | {
|
---|
139 | kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
|
---|
140 | }
|
---|
141 | else
|
---|
142 | {
|
---|
143 | cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | //cout << "kdfilename: " << kdFilename << endl;
|
---|
148 | return kdFilename;
|
---|
149 | }
|
---|
150 |
|
---|
151 | #include "ViewCellsManager.h"
|
---|
152 | int
|
---|
153 | main(int argc, char **argv)
|
---|
154 | {
|
---|
155 |
|
---|
156 | //Now just call this function at the start of your program and if you're
|
---|
157 | //compiling in debug mode (F5), any leaks will be displayed in the Output
|
---|
158 | //window when the program shuts down. If you're not in debug mode this will
|
---|
159 | //be ignored. Use it as you will!
|
---|
160 | //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
|
---|
161 | #if 1
|
---|
162 | _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
|
---|
163 | _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
|
---|
164 | _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
|
---|
165 | #endif
|
---|
166 |
|
---|
167 | int returnCode = 0;
|
---|
168 |
|
---|
169 | InitTiming();
|
---|
170 |
|
---|
171 | Debug.open("debug.log");
|
---|
172 |
|
---|
173 | Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
|
---|
174 | MeshKdTree::ParseEnvironment();
|
---|
175 |
|
---|
176 |
|
---|
177 | char buff[128];
|
---|
178 | Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
|
---|
179 | string preprocessorType(buff);
|
---|
180 |
|
---|
181 | if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
|
---|
182 | {
|
---|
183 | Environment::DelSingleton();
|
---|
184 | cerr << "Unknown preprocessor type" << endl;
|
---|
185 | exit(1);
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /////////////
|
---|
190 | //-- load scene
|
---|
191 |
|
---|
192 | Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
|
---|
193 | string filename(buff);
|
---|
194 |
|
---|
195 |
|
---|
196 | if (!preprocessor->LoadScene(filename))
|
---|
197 | {
|
---|
198 | cout << "loading file " << filename << " failed" << endl;
|
---|
199 | Cleanup();
|
---|
200 | exit(1);
|
---|
201 | }
|
---|
202 |
|
---|
203 | const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
|
---|
204 | const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
|
---|
205 |
|
---|
206 | ////////////
|
---|
207 | //-- initialize external ray caster
|
---|
208 |
|
---|
209 | if (preprocessor->InitRayCast(externKdTree, internKdTree))
|
---|
210 | {
|
---|
211 | cout << "ray casting initialized!" << endl;
|
---|
212 | }
|
---|
213 | else
|
---|
214 | {
|
---|
215 | cout << "ray casting initialization failed" << endl;
|
---|
216 | Cleanup();
|
---|
217 | exit(1);
|
---|
218 | }
|
---|
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 | const string objname = GetInternFilename(filename, ".obj");
|
---|
232 |
|
---|
233 | cout << "exporting scene to " << objname << endl;
|
---|
234 | bool success = preprocessor->ExportObj(objname, preprocessor->mObjects);
|
---|
235 |
|
---|
236 | if (success)
|
---|
237 | {
|
---|
238 | cout << "finished exporting obj" << endl;
|
---|
239 | }
|
---|
240 | else
|
---|
241 | {
|
---|
242 | cerr << "error exporting " << objname << endl;
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | // parse view cells related options
|
---|
249 | if (!preprocessor->PrepareViewCells())
|
---|
250 | {
|
---|
251 | Cleanup();
|
---|
252 | exit(1);
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (0){
|
---|
256 | Exporter *exporter= Exporter::GetExporter("test1.wrl");
|
---|
257 | AxisAlignedBox3 bbox = preprocessor->mViewCellsManager->GetViewSpaceBox();
|
---|
258 | exporter->SetWireframe();
|
---|
259 | exporter->ExportBox(bbox);
|
---|
260 | exporter->SetFilled();
|
---|
261 | exporter->ExportGeometry(preprocessor->mObjects, true);
|
---|
262 | delete exporter;
|
---|
263 | }
|
---|
264 |
|
---|
265 | if (0)
|
---|
266 | {
|
---|
267 | preprocessor->Export(filename + "-out.x3d", true, false);
|
---|
268 | preprocessor->Export(filename + "-kdtree.x3d", false, true);
|
---|
269 | }
|
---|
270 |
|
---|
271 | // create a preprocessor thread (note: capsulates calls to boost fuctions!)
|
---|
272 | //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
|
---|
273 | PreprocessorThread *pt;
|
---|
274 |
|
---|
275 |
|
---|
276 | #if USE_QT
|
---|
277 | // create a qt application first (must be created before any opengl widget ...)
|
---|
278 | QApplication *app = new QApplication(argc, NULL);
|
---|
279 |
|
---|
280 | pt = new QtPreprocessorThread(preprocessor);
|
---|
281 | #else
|
---|
282 | #if USE_THREADS
|
---|
283 | pt = new BoostPreprocessorThread(preprocessor);
|
---|
284 | #else
|
---|
285 | pt = new PreprocessorThread(preprocessor);
|
---|
286 | #endif
|
---|
287 | #endif
|
---|
288 |
|
---|
289 | bool guiSupported = false;
|
---|
290 |
|
---|
291 | if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
|
---|
292 | {
|
---|
293 | cout << "using gl widget" << endl;
|
---|
294 | // create and run the preprocessor application in a parallel thread
|
---|
295 | #if USE_QT
|
---|
296 | #if USE_THREADS
|
---|
297 | pt->InitThread();
|
---|
298 | if (!preprocessor->mDelayVisibilityComputation)
|
---|
299 | pt->RunThread();
|
---|
300 | #endif
|
---|
301 |
|
---|
302 | // display the render widget
|
---|
303 | if (!rendererWidget)
|
---|
304 | {
|
---|
305 |
|
---|
306 | if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
|
---|
307 | QMessageBox::information(0, "OpenGL pbuffers",
|
---|
308 | "This system does not support OpenGL/pbuffers.",
|
---|
309 | QMessageBox::Ok);
|
---|
310 | return NULL;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
|
---|
315 | preprocessor->mViewCellsManager,
|
---|
316 | preprocessor->mKdTree);
|
---|
317 |
|
---|
318 | ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
|
---|
319 | rendererWidget->Show();
|
---|
320 | guiSupported = true;
|
---|
321 | }
|
---|
322 |
|
---|
323 | qApp->exec();
|
---|
324 | #endif
|
---|
325 | }
|
---|
326 |
|
---|
327 | if (!guiSupported) {
|
---|
328 | preprocessor->mUseGlRenderer = false;
|
---|
329 | preprocessor->mUseGlDebugger = false;
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
|
---|
333 | // just call the mail method -> will be executed in the main thread
|
---|
334 | pt->Main();
|
---|
335 | }
|
---|
336 |
|
---|
337 | // release memory
|
---|
338 | Cleanup();
|
---|
339 |
|
---|
340 | return returnCode;
|
---|
341 | }
|
---|
342 |
|
---|