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 | //cout << "here6 " << currentFile << endl;
|
---|
109 | string strippedFilename;
|
---|
110 |
|
---|
111 | if (i == 0)
|
---|
112 | {
|
---|
113 | strippedFilename = currentFile;
|
---|
114 | }
|
---|
115 | else
|
---|
116 | {
|
---|
117 | char *str = StripPath(currentFile.c_str());
|
---|
118 | strippedFilename = string(str);
|
---|
119 |
|
---|
120 | delete [] str;
|
---|
121 | }
|
---|
122 |
|
---|
123 | string suffix("_");
|
---|
124 |
|
---|
125 | if (i == (int)filenames.size() - 1)
|
---|
126 | {
|
---|
127 | suffix = newSuffix;
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (strstr(strippedFilename.c_str(), ".x3d"))
|
---|
131 | {
|
---|
132 | kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
|
---|
133 | }
|
---|
134 | else if (strstr(strippedFilename.c_str(), ".dat"))
|
---|
135 | {
|
---|
136 | kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
|
---|
137 | }
|
---|
138 | else if (strstr(strippedFilename.c_str(), ".obj"))
|
---|
139 | {
|
---|
140 | kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
|
---|
141 | }
|
---|
142 | else
|
---|
143 | {
|
---|
144 | cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | //cout << "kdfilename: " << kdFilename << endl;
|
---|
149 | return kdFilename;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | int
|
---|
154 | main(int argc, char **argv)
|
---|
155 | {
|
---|
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 |
|
---|
172 | Debug.open("debug.log");
|
---|
173 |
|
---|
174 | Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
|
---|
175 | MeshKdTree::ParseEnvironment();
|
---|
176 |
|
---|
177 |
|
---|
178 | char buff[128];
|
---|
179 | Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
|
---|
180 | string preprocessorType(buff);
|
---|
181 |
|
---|
182 | if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
|
---|
183 | {
|
---|
184 | Environment::DelSingleton();
|
---|
185 | cerr << "Unknown preprocessor type" << endl;
|
---|
186 | exit(1);
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | /////////////
|
---|
191 | //-- load scene
|
---|
192 |
|
---|
193 | Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
|
---|
194 | string filename(buff);
|
---|
195 |
|
---|
196 |
|
---|
197 | if (!preprocessor->LoadScene(filename))
|
---|
198 | {
|
---|
199 | cout << "loading file " << filename << " failed" << endl;
|
---|
200 | Cleanup();
|
---|
201 | exit(1);
|
---|
202 | }
|
---|
203 |
|
---|
204 | const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
|
---|
205 | const string internKdTree = GetInternFilename(filename, preprocessor->mLoadMeshes ? ".kdm" : ".kdt");
|
---|
206 |
|
---|
207 | ////////////
|
---|
208 | //-- initialize external ray caster
|
---|
209 |
|
---|
210 | if (preprocessor->InitRayCast(externKdTree, internKdTree))
|
---|
211 | {
|
---|
212 | cout << "ray casting initialized!" << endl;
|
---|
213 | }
|
---|
214 | else
|
---|
215 | {
|
---|
216 | cout << "ray casting initialization failed" << endl;
|
---|
217 | Cleanup();
|
---|
218 | exit(1);
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | // export objects as obj
|
---|
223 | if (preprocessor->mExportObj)
|
---|
224 | {
|
---|
225 | if (strstr(filename.c_str(), ".obj"))
|
---|
226 | {
|
---|
227 | cerr << "already in obj format" << endl;
|
---|
228 | if (0) preprocessor->ExportObj("test.obj", preprocessor->mObjects);
|
---|
229 | }
|
---|
230 | else
|
---|
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 | if (success)
|
---|
238 | {
|
---|
239 | cout << "finished exporting obj" << endl;
|
---|
240 | }
|
---|
241 | else
|
---|
242 | {
|
---|
243 | cerr << "error exporting " << objname << endl;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 | // Exporter *exporter= Exporter::GetExporter("test1.wrl");
|
---|
248 | // AxisAlignedBox3 bbox = preprocessor->mKdTree->GetBox();
|
---|
249 | // bbox.Scale(Vector3(1, 1, 0.5));
|
---|
250 | //exporter->ExportGeometry(preprocessor->mObjects);cout<<"here2"<<endl;
|
---|
251 |
|
---|
252 | // parse view cells related options
|
---|
253 | if (!preprocessor->PrepareViewCells())
|
---|
254 | {
|
---|
255 | Cleanup();
|
---|
256 | exit(1);
|
---|
257 | }
|
---|
258 |
|
---|
259 | if (0)
|
---|
260 | {
|
---|
261 | preprocessor->Export(filename + "-out.x3d", true, false);
|
---|
262 | preprocessor->Export(filename + "-kdtree.x3d", false, true);
|
---|
263 | }
|
---|
264 |
|
---|
265 | // create a preprocessor thread (note: capsulates calls to boost fuctions!)
|
---|
266 | //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
|
---|
267 | PreprocessorThread *pt;
|
---|
268 |
|
---|
269 | #if USE_QT
|
---|
270 | pt = new QtPreprocessorThread(preprocessor);
|
---|
271 | #else
|
---|
272 | #if USE_THREADS
|
---|
273 | pt = new BoostPreprocessorThread(preprocessor);
|
---|
274 | #else
|
---|
275 | pt = new PreprocessorThread(preprocessor);
|
---|
276 | #endif
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | bool guiSupported = false;
|
---|
280 |
|
---|
281 | if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
|
---|
282 | {
|
---|
283 | cout << "using gl widget" << endl;
|
---|
284 | // create and run the preprocessor application in a parallel thread
|
---|
285 | #if USE_QT
|
---|
286 | #if USE_THREADS
|
---|
287 | pt->InitThread();
|
---|
288 | if (!preprocessor->mDelayVisibilityComputation)
|
---|
289 | pt->RunThread();
|
---|
290 | #endif
|
---|
291 |
|
---|
292 | // display the render widget
|
---|
293 | if (!rendererWidget)
|
---|
294 | {
|
---|
295 | // create a qt application first (must be created before any opengl widget ...)
|
---|
296 | QApplication *app = new QApplication(argc, NULL);
|
---|
297 |
|
---|
298 | if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
|
---|
299 | QMessageBox::information(0, "OpenGL pbuffers",
|
---|
300 | "This system does not support OpenGL/pbuffers.",
|
---|
301 | QMessageBox::Ok);
|
---|
302 | return NULL;
|
---|
303 | }
|
---|
304 |
|
---|
305 |
|
---|
306 | rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
|
---|
307 | preprocessor->mViewCellsManager,
|
---|
308 | preprocessor->mKdTree);
|
---|
309 |
|
---|
310 | ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
|
---|
311 | rendererWidget->Show();
|
---|
312 | guiSupported = true;
|
---|
313 | }
|
---|
314 |
|
---|
315 | qApp->exec();
|
---|
316 | #endif
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (!guiSupported) {
|
---|
320 | preprocessor->mUseGlRenderer = false;
|
---|
321 | preprocessor->mUseGlDebugger = false;
|
---|
322 | }
|
---|
323 |
|
---|
324 | if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
|
---|
325 | // just call the mail method -> will be executed in the main thread
|
---|
326 | pt->Main();
|
---|
327 | }
|
---|
328 |
|
---|
329 | // release memory
|
---|
330 | Cleanup();
|
---|
331 |
|
---|
332 | return returnCode;
|
---|
333 | }
|
---|
334 |
|
---|