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