source: GTP/trunk/Lib/Vis/Preprocessing/src/main.cpp @ 1627

Revision 1627, 5.7 KB checked in by mattausch, 18 years ago (diff)
Line 
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
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
41using namespace GtpVisibilityPreprocessor;
42
43Preprocessor *preprocessor = NULL;
44GlRendererWidget *rendererWidget = NULL;
45
46// DLL function signature
47typedef GlRendererWidget *(*importFunction)(Preprocessor *);
48
49
50
51
52void Cleanup()
53{
54        DEL_PTR(rendererWidget);
55        DEL_PTR(preprocessor);
56
57        Environment::DelSingleton();
58        MeshManager::DelSingleton();
59        MaterialManager::DelSingleton();
60}
61
62
63static string ReplaceSuffix(string filename, string a, string b)
64{
65        string result = filename;
66
67        int pos = (int)filename.rfind(a, (int)filename.size() - 1);
68        if (pos == filename.size() - a.size()) {
69                result.replace(pos, a.size(), b);
70        }
71        return result;
72}
73
74
75static string GetInternKdTreeName(string &filename)
76{
77        //Environment::GetSingleton()->GetStringValue("Preprocessor.kdTreeFilename", internKdTree);
78       
79        // hack! should take any extension
80    if (strstr(filename.c_str(), ".x3d"))
81        {
82                return ReplaceSuffix(filename, ".x3d", ".kd");
83        }
84        else if (strstr(filename.c_str(), ".dat"))
85        {
86                return ReplaceSuffix(filename, ".dat", ".kd");
87        }
88        else if (strstr(filename.c_str(), ".obj"))
89        {
90                return ReplaceSuffix(filename, ".dat", ".kd");
91        }
92
93        cerr << "Error: Currently unsupported format for kd, filename " << filename << endl;
94
95        // return empty string
96        return string();
97}
98
99
100int
101main(int argc, char **argv)
102{
103
104        //Now just call this function at the start of your program and if you're
105        //compiling in debug mode (F5), any leaks will be displayed in the Output
106        //window when the program shuts down. If you're not in debug mode this will
107        //be ignored. Use it as you will!
108        //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
109#if 0
110  _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
111  _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
112  _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);
113#endif
114
115        int returnCode = 0;
116       
117        InitTiming();
118
119        Debug.open("debug.log");
120 
121        Environment::GetSingleton()->Parse(argc, argv, USE_EXE_PATH);
122        MeshKdTree::ParseEnvironment();
123
124       
125        char buff[128];
126        Environment::GetSingleton()->GetStringValue("Preprocessor.type", buff);
127        string preprocessorType(buff);
128
129        if (!(preprocessor = PreprocessorFactory::CreatePreprocessor(preprocessorType)))
130        {
131                Environment::DelSingleton();
132                cerr << "Unknown preprocessor type" << endl;
133                exit(1);
134        }
135
136
137        /////////////
138        //-- load scene
139
140        Environment::GetSingleton()->GetStringValue("Scene.filename", buff);
141        string filename(buff);
142
143        if (!preprocessor->LoadScene(filename))
144        {
145                cout << "loading file " << filename << " failed" << endl;
146                Cleanup();
147                exit(1);
148        }
149
150        const string externKdTree = ReplaceSuffix(filename, ".obj", ".kdf");
151        const string internKdTree = GetInternKdTreeName(filename);
152
153        //-- initialize external ray casters
154        if (preprocessor->InitRayCast(externKdTree, internKdTree))
155        {
156                cout << "ray casting initialized!" << endl;
157        }
158        else
159        {
160                cout << "ray casting initialization failed" << endl;
161                Cleanup();
162                exit(1);
163        }
164
165        // parse view cells related options
166        if (!preprocessor->PrepareViewCells())
167        {
168                Cleanup();
169                exit(1);
170        }
171
172        if (0)
173        {
174                preprocessor->Export(filename + "-out.x3d", true, false);
175                preprocessor->Export(filename + "-kdtree.x3d", false, true);   
176        }
177
178        // create a preprocessor thread (note: capsulates calls to boost fuctions!)
179        //PreprocessorThread *pt = PreprocessorThreadFactory::Create(preprocessor);
180        PreprocessorThread *pt;
181
182#if USE_QT
183        pt = new QtPreprocessorThread(preprocessor);
184#else
185#if USE_THREADS
186        pt = new BoostPreprocessorThread(preprocessor);
187#else
188        pt = new PreprocessorThread(preprocessor);
189#endif
190#endif
191
192        bool guiSupported = false;
193
194        if (preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)
195          {
196                cout << "using gl widget" << endl;
197                // create and run the preprocessor application in a parallel thread
198#if USE_QT             
199#if USE_THREADS
200                pt->InitThread();
201                if (!preprocessor->mDelayVisibilityComputation)
202                  pt->RunThread();
203#endif
204               
205                // display the render widget
206                if (!rendererWidget)
207                {
208                  // create a qt application first (must be created before any opengl widget ...)
209                  QApplication *app = new QApplication(argc, NULL);
210                 
211                  if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) {
212                        QMessageBox::information(0, "OpenGL pbuffers",
213                                                                         "This system does not support OpenGL/pbuffers.",
214                                                                         QMessageBox::Ok);
215                        return NULL;
216                  }
217                 
218                 
219                  rendererWidget = new QtGlRendererWidget(preprocessor->mSceneGraph,
220                                                                                                  preprocessor->mViewCellsManager,
221                                                                                                  preprocessor->mKdTree);
222
223                  ((QtGlRendererWidget *)rendererWidget)->SetThread((QtPreprocessorThread *)pt);
224                  rendererWidget->Show();
225                  guiSupported = true;
226                }
227
228                qApp->exec();   
229#endif
230          }
231       
232        if (!guiSupported) {
233          preprocessor->mUseGlRenderer = false;
234          preprocessor->mUseGlDebugger = false;
235        }
236       
237        if (!(preprocessor->mUseGlRenderer || preprocessor->mUseGlDebugger)) {
238          // just call the mail method -> will be executed in the main thread
239          pt->Main();
240        }
241       
242        // release memory
243        Cleanup();
244       
245        return returnCode;
246}
247
Note: See TracBrowser for help on using the repository browser.