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

Revision 1832, 8.1 KB checked in by bittner, 18 years ago (diff)

gl render updates - separate gl viewer widget

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