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 |
|
---|
26 | #if !USE_QT && USE_THREADS
|
---|
27 | #include "BoostPreprocessorThread.h"
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #include "ResourceManager.h"
|
---|
31 | #include "GlRenderer.h"
|
---|
32 |
|
---|
33 | #if USE_QT
|
---|
34 | #include "QtPreprocessorThread.h"
|
---|
35 | #include "QtGlRenderer.h"
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #define USE_EXE_PATH false
|
---|
39 |
|
---|
40 |
|
---|
41 | using namespace GtpVisibilityPreprocessor;
|
---|
42 |
|
---|
43 | Preprocessor *preprocessor = NULL;
|
---|
44 | GlRendererWidget *rendererWidget = NULL;
|
---|
45 |
|
---|
46 | // DLL function signature
|
---|
47 | typedef GlRendererWidget *(*importFunction)(Preprocessor *);
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 | void Cleanup()
|
---|
52 | {
|
---|
53 | DEL_PTR(rendererWidget);
|
---|
54 | DEL_PTR(preprocessor);
|
---|
55 |
|
---|
56 | Environment::DelSingleton();
|
---|
57 | MeshManager::DelSingleton();
|
---|
58 | MaterialManager::DelSingleton();
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | static string ReplaceSuffix(string filename, string a, string b)
|
---|
63 | {
|
---|
64 | string result = filename;
|
---|
65 |
|
---|
66 | int pos = (int)filename.rfind(a, (int)filename.size() - 1);
|
---|
67 | if (pos == filename.size() - a.size()) {
|
---|
68 | result.replace(pos, a.size(), b);
|
---|
69 | }
|
---|
70 | return result;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | static int SplitFilenames(const string str, vector<string> &filenames)
|
---|
75 | {
|
---|
76 | int pos = 0;
|
---|
77 |
|
---|
78 | while(1) {
|
---|
79 | int npos = (int)str.find(';', pos);
|
---|
80 |
|
---|
81 | if (npos < 0 || npos - pos < 1)
|
---|
82 | break;
|
---|
83 | filenames.push_back(string(str, pos, npos - pos));
|
---|
84 | pos = npos + 1;
|
---|
85 | }
|
---|
86 |
|
---|
87 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
88 | return (int)filenames.size();
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | static string GetInternKdTreeName(const string &filename)
|
---|
93 | {
|
---|
94 | vector<string> filenames;
|
---|
95 | const int files = SplitFilenames(filename, filenames);
|
---|
96 |
|
---|
97 | vector<string>::const_iterator sit, sit_end = filenames.end();
|
---|
98 | string kdFilename;
|
---|
99 |
|
---|
100 | int i = 0;
|
---|
101 | for (sit = filenames.begin(); sit != sit_end; ++ sit, ++ i)
|
---|
102 | {
|
---|
103 | string currentFile = *sit;
|
---|
104 | //cout << "here6 " << currentFile << endl;
|
---|
105 | string strippedFilename;
|
---|
106 |
|
---|
107 | if (i == 0)
|
---|
108 | {
|
---|
109 | strippedFilename = currentFile;
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | char *str = StripPath(currentFile.c_str());
|
---|
114 | strippedFilename = string(str);
|
---|
115 |
|
---|
116 | delete []str;
|
---|
117 | }
|
---|
118 |
|
---|
119 | string suffix("_");
|
---|
120 |
|
---|
121 | if (i == (int)filenames.size() - 1)
|
---|
122 | {
|
---|
123 | if (preprocessor->mLoadMeshes)
|
---|
124 | {
|
---|
125 | suffix = ".kdm";
|
---|
126 | }
|
---|
127 | else
|
---|
128 | {
|
---|
129 | suffix = ".kdt";
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (strstr(strippedFilename.c_str(), ".x3d"))
|
---|
134 | {
|
---|
135 | kdFilename += ReplaceSuffix(strippedFilename, ".x3d", suffix);
|
---|
136 | }
|
---|
137 | else if (strstr(strippedFilename.c_str(), ".dat"))
|
---|
138 | {
|
---|
139 | kdFilename += ReplaceSuffix(strippedFilename, ".dat", suffix);
|
---|
140 | }
|
---|
141 | else if (strstr(strippedFilename.c_str(), ".obj"))
|
---|
142 | {
|
---|
143 | kdFilename += ReplaceSuffix(strippedFilename, ".obj", suffix);
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {
|
---|
147 | cerr << "Error: Currently unsupported format for kd, filename " << currentFile << endl;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | //cout << "kdfilename: " << kdFilename << endl;
|
---|
152 | return kdFilename;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | int
|
---|
157 | main(int argc, char **argv)
|
---|
158 | {
|
---|
159 |
|
---|
160 | //Now just call this function at the start of your program and if you're
|
---|
161 | //compiling in debug mode (F5), any leaks will be displayed in the Output
|
---|
162 | //window when the program shuts down. If you're not in debug mode this will
|
---|
163 | //be ignored. Use it as you will!
|
---|
164 | //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
|
---|
165 | #if 0
|
---|
166 | _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
|
---|
167 | _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
|
---|
168 | _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | int returnCode = 0;
|
---|
172 |
|
---|
173 | InitTiming();
|
---|
174 |
|
---|
175 | Debug.open("debug.log");
|
---|
176 |
|
---|
177 | Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
|
---|
178 | MeshKdTree::ParseEnvironment();
|
---|
179 |
|
---|
180 |
|
---|
181 | char buff[128];
|
---|
182 | Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
|
---|
183 | string preprocessorType(buff);
|
---|
184 |
|
---|
185 | if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
|
---|
186 | {
|
---|
187 | Environment::DelSingleton();
|
---|
188 | cerr << "Unknown preprocessor type" << endl;
|
---|
189 | exit(1);
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /////////////
|
---|
194 | //-- load scene
|
---|
195 |
|
---|
196 | Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
|
---|
197 | string filename(buff);
|
---|
198 | const string dummyname = GetInternKdTreeName(filename);
|
---|
199 |
|
---|
200 | if (!preprocessor->LoadScene(filename))
|
---|
201 | {
|
---|
202 | cout << "loading file " << filename << " failed" << endl;
|
---|
203 | Cleanup();
|
---|
204 | exit(1);
|
---|
205 | }
|
---|
206 |
|
---|
207 | const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
|
---|
208 | const string internKdTree = GetInternKdTreeName(filename);
|
---|
209 |
|
---|
210 | //-- initialize external ray casters
|
---|
211 | if (preprocessor->InitRayCast(externKdTree, internKdTree))
|
---|
212 | {
|
---|
213 | cout << "ray casting initialized!" << endl;
|
---|
214 | }
|
---|
215 | else
|
---|
216 | {
|
---|
217 | cout << "ray casting initialization failed" << endl;
|
---|
218 | Cleanup();
|
---|
219 | exit(1);
|
---|
220 | }
|
---|
221 |
|
---|
222 | // parse view cells related options
|
---|
223 | if (!preprocessor->PrepareViewCells())
|
---|
224 | {
|
---|
225 | Cleanup();
|
---|
226 | exit(1);
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (0)
|
---|
230 | {
|
---|
231 | preprocessor->Export(filename + "-out.x3d", true, false);
|
---|
232 | preprocessor->Export(filename + "-kdtree.x3d", false, true);
|
---|
233 | }
|
---|
234 |
|
---|
235 | // create a preprocessor thread (note: capsulates calls to boost fuctions!)
|
---|
236 | //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
|
---|
237 | PreprocessorThread *pt;
|
---|
238 |
|
---|
239 | #if USE_QT
|
---|
240 | pt = new QtPreprocessorThread(preprocessor);
|
---|
241 | #else
|
---|
242 | #if USE_THREADS
|
---|
243 | pt = new BoostPreprocessorThread(preprocessor);
|
---|
244 | #else
|
---|
245 | pt = new PreprocessorThread(preprocessor);
|
---|
246 | #endif
|
---|
247 | #endif
|
---|
248 |
|
---|
249 | bool guiSupported = false;
|
---|
250 |
|
---|
251 | if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
|
---|
252 | {
|
---|
253 | cout << "using gl widget" << endl;
|
---|
254 | // create and run the preprocessor application in a parallel thread
|
---|
255 | #if USE_QT
|
---|
256 | #if USE_THREADS
|
---|
257 | pt->InitThread();
|
---|
258 | if (!preprocessor->mDelayVisibilityComputation)
|
---|
259 | pt->RunThread();
|
---|
260 | #endif
|
---|
261 |
|
---|
262 | // display the render widget
|
---|
263 | if (!rendererWidget)
|
---|
264 | {
|
---|
265 | // create a qt application first (must be created before any opengl widget ...)
|
---|
266 | QApplication *app = new QApplication(argc, NULL);
|
---|
267 |
|
---|
268 | if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
|
---|
269 | QMessageBox::information(0, "OpenGL pbuffers",
|
---|
270 | "This system does not support OpenGL/pbuffers.",
|
---|
271 | QMessageBox::Ok);
|
---|
272 | return NULL;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
|
---|
277 | preprocessor->mViewCellsManager,
|
---|
278 | preprocessor->mKdTree);
|
---|
279 |
|
---|
280 | ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
|
---|
281 | rendererWidget->Show();
|
---|
282 | guiSupported = true;
|
---|
283 | }
|
---|
284 |
|
---|
285 | qApp->exec();
|
---|
286 | #endif
|
---|
287 | }
|
---|
288 |
|
---|
289 | if (!guiSupported) {
|
---|
290 | preprocessor->mUseGlRenderer = false;
|
---|
291 | preprocessor->mUseGlDebugger = false;
|
---|
292 | }
|
---|
293 |
|
---|
294 | if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
|
---|
295 | // just call the mail method -> will be executed in the main thread
|
---|
296 | pt->Main();
|
---|
297 | }
|
---|
298 |
|
---|
299 | // release memory
|
---|
300 | Cleanup();
|
---|
301 |
|
---|
302 | return returnCode;
|
---|
303 | }
|
---|
304 |
|
---|