Debian Codegraph @

viewer/viewer.cpp

raw symbols SCIP
1 //
2 // This code was created by Jeff Molofee '99
3 // (ported to SDL by Sam Lantinga '2000)
4 //
5 // If you've found this code useful, please let me know.
6 //
7 // Visit me at www.demonews.com/hosted/nehe
8 //
9 // Code modifed by Frederic Rodrigo 2005-2007 as viewer of object loaded by OpenCity
10 /*
11 TODO:
12 - add geometry commands line argument
13 - improve help display: key
14 - mine type for .ac
15 */
16
17 // Useful enumerations
18 #include "opencity_direction.h"
19 #include "opencity_structure_type.h"
20
21 // OpenCity headers
22 #include "buildinglayer.h"
23 #include "conf.h"
24 #include "macros.h"
25 #include "modelloader.h"
26 #include "model.h"
27 #include "structure.h"
28 #include "texture.h"
29 #include "property.h"
30
31 // Standard headers
32 #include <string>
33
34 // Libraries headers
35 #include "pngfuncs.h"
36 #include "SimpleOpt.h" // Simple command line argument parser
37
38
39 int W = 640;
40 int H = 480;
41
42 /* floats for x rotation, y rotation, z rotation */
43 float xrot=30, yrot=30, zrot=0, zoom=3;
44
45
46 /* A general OpenGL initialization function. Sets all of the initial parameters. */
47 int initGL( const int width, const int height ) // We call this right after our OpenGL window is created.
48 {
49 glViewport(0, 0, width, height);
50 glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
51 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
52 // glBlendFunc( GL_ONE, GL_ZERO );
53 glClearColor(0.5f, 0.5f, 0.8f, 1.0f); // Clear The Background Color To Blue
54 glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
55 glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
56 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
57 // glDepthMask(GL_FALSE); // Disable depth buffer writing
58 glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
59
60 glMatrixMode(GL_PROJECTION);
61 glLoadIdentity(); // Reset The Projection Matrix
62
63 gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window
64
65 glMatrixMode(GL_MODELVIEW);
66
67 // glEnable( GL_LIGHTING );
68 // glColor3f ( 1.0f, 0.0f, 0.0f ) ;
69 // glColorMaterial ( GL_FRONT_AND_BACK, GL_EMISSION ) ;
70 glEnable ( GL_COLOR_MATERIAL ) ;
71
72 return 0;
73 }
74
75
76 /* The main drawing function. */
77 void DrawGLScene( float width, float length, float height, const Model *model )
78 {
79 signed char tabY [] = { 0, 0, 0, 0};
80
81 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
82 glLoadIdentity(); // Reset The View
83
84 glTranslatef(0.0f,0.0f,-zoom); // move 5 units into the screen.
85
86 glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate On The X Axis
87 glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate On The Y Axis
88 glRotatef(zrot,0.0f,0.0f,1.0f); // Rotate On The Z Axis
89
90 model->DisplayList( -width/2, -length/2, tabY );
91
92 // swap buffers to display, since we're double buffered.
93 SDL_GL_SwapBuffers();
94 }
95
96
97 /* FIXME : Not sure about portability of this function */
98 void screenshot( const string name, int w, int h )
99 {
100 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT ); // save context
101
102 // make screenshot file name
103 string file = name.substr( 0, name.length()-3 ) + "_screenshot.png";
104
105 // Build the screen shot surface
106 unsigned int* someBuffer = new unsigned int[ w * h * 4 ];
107
108 Uint32 rmask, gmask, bmask, amask;
109 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
110 rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff;
111 #else
112 rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000;
113 #endif
114
115 SDL_Surface *shot = SDL_CreateRGBSurfaceFrom( someBuffer, w, h, 8*4, w*4, rmask, gmask, bmask, amask );
116 // SDL_Surface *shot = SDL_CreateRGBSurfaceFrom( someBuffer, w, h, 8*4, w*4, 0, 0, 0, 0 );
117
118 // set screenshot context
119 glPixelStorei( GL_PACK_ROW_LENGTH, 0 );
120 glPixelStorei( GL_PACK_ALIGNMENT, 1 );
121 glReadPixels( 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, someBuffer );
122
123 if( shot != NULL )
124 {
125 SDL_Surface *shotMirorred = Texture::HorizontalMirror( shot );
126 if( shotMirorred != NULL )
127 {
128 if( png_save_surface( file.c_str(), shotMirorred ) == 0 )
129 {
130 OPENCITY_INFO( "Capture saved under \"" << file << "\"" );
131 }
132 else
133 {
134 OPENCITY_ERROR( "Failing save capture under \"" << file << "\"" );
135 }
136 SDL_FreeSurface( shotMirorred );
137 }
138 else
139 {
140 OPENCITY_ERROR( "Failing working whith capture for \"" << file << "\"" );
141 }
142
143 SDL_FreeSurface( shot );
144 }
145 else
146 {
147 OPENCITY_ERROR( "Failling take a shot." );
148 }
149
150 glPopClientAttrib(); // restor context
151 delete [] someBuffer;
152 }
153
154
155 Conf *loadConf( const string &acFile )
156 {
157 string confFile = acFile.substr( 0, acFile.length()-3 ) + ".conf";
158
159 Conf* pConf = new Conf();
160
161 int openStatus = pConf->Open( confFile );
162
163 if( openStatus != OC_ERR_FREE )
164 {
165 OPENCITY_DEBUG( "fail load confFile = " << confFile );
166 delete pConf;
167 pConf = NULL;
168 }
169 else
170 {
171 OPENCITY_DEBUG( "load confFile = " << confFile );
172 }
173
174 return pConf;
175 }
176
177 Property *loadProperty( const string &acFile )
178 {
179 string propertyFile = acFile.substr( 0, acFile.length()-3 ) + ".xml";
180 Property* pProperty = Property::LoadProperties( propertyFile );
181 OPENCITY_DEBUG( "load propertyFile = " << propertyFile );
182 return pProperty;
183 }
184
185 string parseArgLine( int argc, char **argv, bool *shot )
186 {
187 enum {
188 OPT_SCREENSHOT,
189 OPT_HELP
190 };
191
192 CSimpleOpt::SOption g_rgOptions[] = {
193 { OPT_SCREENSHOT, (char*)"--screenshot", SO_NONE },
194 { OPT_SCREENSHOT, (char*)"-s", SO_NONE },
195 { OPT_HELP, (char*)"--help", SO_NONE },
196 { OPT_HELP, (char*)"-h", SO_NONE },
197 SO_END_OF_OPTIONS // END
198 };
199
200 CSimpleOpt args(argc, argv, g_rgOptions, SO_O_EXACT | SO_O_NOSLASH | SO_O_SHORTARG | SO_O_CLUMP );
201
202 while (args.Next()) {
203 switch (args.LastError()) {
204 case SO_OPT_INVALID:
205 cout << "<OPTION> " << args.OptionText() << " unrecognized" << endl;
206 break;
207 case SO_OPT_MULTIPLE:
208 cout << "<OPTION> " << args.OptionText() << " matched multiple options" << endl;
209 break;
210 case SO_ARG_INVALID:
211 cout << "<OPTION> " << args.OptionText() << " does not accept argument" << endl;
212 break;
213 case SO_ARG_INVALID_TYPE:
214 cout << "<OPTION> " << args.OptionText() << " has an invalid argument format" << endl;
215 break;
216 case SO_ARG_MISSING:
217 cout << "<OPTION> " << args.OptionText() << " require an argument" << endl;
218 break;
219 case SO_ARG_INVALID_DATA:
220 cout << "<OPTION> " << args.OptionText() << " has an invalid argument data" << endl;
221 break;
222 case SO_SUCCESS:
223 break;
224 }
225
226 switch (args.LastError()) {
227 case SO_SUCCESS:
228 cout << "<OPTION> " << args.OptionText() << " detected" << endl;
229 break;
230 default:
231 cout << "try " << argv[0] << " --help for usage information" << endl;
232 exit( -1 );
233 break;
234 }
235
236 switch (args.OptionId()) {
237
238 case OPT_SCREENSHOT:
239 *shot = true;
240 break;
241
242 case OPT_HELP:
243 cout << "Usage: " << argv[0]
244 << " [-s|--screenshot] model.ac" << endl << endl;
245 exit( -1 );
246 break;
247 }
248 }
249
250 if( args.FileCount() >= 1 ) {
251 return args.File(0);
252 }
253 else {
254 return "";
255 }
256 }
257
258
259 int initDisplay( const uint w, const uint h )
260 {
261 /* Create a OpenGL screen */
262 if ( SDL_SetVideoMode( w, h, 24, SDL_OPENGLBLIT | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE ) == NULL )
263 {
264 fprintf( stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError() );
265 SDL_Quit();
266 return -1;
267 }
268
269 return initGL( w, h );
270 }
271
272
273 Model *loadModel( const string &modelFile, float *width, float *length, float *height )
274 {
275 OPENCITY_INFO( "Open model \"" << modelFile << "\"" );
276 Model *model = ModelLoader::Load( modelFile );
277
278 if( model != NULL )
279 {
280 *width = 1;
281 *length = 1;
282 *height = 1;
283
284 Property *pProperty = loadProperty( modelFile );
285 if( pProperty != NULL )
286 {
287 *width = pProperty->uiWidth;
288 *length = pProperty->uiLength;
289 *height = pProperty->fHeight;
290 OPENCITY_DEBUG( "Model : " << *width << "x" << *length << "x" << *height );
291 delete pProperty;
292 }
293
294 // heuristic
295 zoom = ( (*width) + (*length) )/2*1.5 + .5;
296 OPENCITY_DEBUG( "Zoom : " << zoom );
297 if( zoom < (*height) * 3 )
298 {
299 zoom = (*height) * 3;
300 }
301
302 return model;
303 }
304 else
305 {
306 return NULL;
307 }
308 }
309
310
311 int main( int argc, char **argv )
312 {
313 bool shot = false;
314 string modelFile = parseArgLine( argc, argv, &shot );
315
316 if( modelFile == "" )
317 {
318 exit( 0 );
319 }
320
321 /* Initialize SDL for video output */
322 if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0 )
323 {
324 fprintf( stderr, "Unable to initialize SDL: %s\n", SDL_GetError() );
325 return -1;
326 }
327
328 int isInitDisplay = initDisplay( W, H );
329 if( isInitDisplay < 0 )
330 {
331 exit( isInitDisplay );
332 }
333
334 float width, length, height;
335 Model *model = loadModel( modelFile, &width, &length, &height );
336 if( model == NULL )
337 {
338 OPENCITY_ERROR( "Can't load model from file : \"" << modelFile << "\"" );
339 exit( -3 );
340 }
341
342 DrawGLScene( width, length, height, model );
343
344 if( shot )
345 {
346 screenshot( modelFile, W, H );
347 }
348 else
349 {
350 int done = 0;
351 while ( ! done )
352 {
353 bool redraw = false;
354
355 SDL_Event event;
356 while ( SDL_PollEvent(&event) )
357 {
358 switch( event.type )
359 {
360 case SDL_QUIT:
361 done = 1;
362 break;
363 case SDL_KEYDOWN:
364 redraw = true;
365 switch( event.key.keysym.sym )
366 {
367 case SDLK_ESCAPE:
368 done = 1;
369 redraw = false;
370 break;
371 case SDLK_SPACE:
372 break;
373 case SDLK_UP:
374 xrot -= 15;
375 break;
376 case SDLK_DOWN:
377 xrot += 15;
378 break;
379 case SDLK_PAGEUP:
380 case SDLK_LEFT:
381 yrot -= 15;
382 break;
383 case SDLK_PAGEDOWN:
384 case SDLK_RIGHT:
385 yrot += 15;
386 break;
387 case SDLK_INSERT:
388 zoom -= 1;
389 break;
390 case SDLK_DELETE:
391 zoom += 1;
392 break;
393 case SDLK_l:
394 zoom=3;
395 yrot=45;
396 xrot=30;
397 break;
398 case SDLK_m:
399 zoom=3;
400 yrot=45+90;
401 xrot=30;
402 break;
403 case SDLK_p:
404 zoom=3;
405 yrot=45+180;
406 xrot=30;
407 break;
408 case SDLK_o:
409 zoom=3;
410 yrot=45+270;
411 xrot=30;
412 break;
413 // Chose the wireframe polygon mode
414 case SDLK_f:
415 glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
416 break;
417 // Chose the textured polygon mode
418 case SDLK_t:
419 glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
420 break;
421 case SDLK_s:
422 screenshot( modelFile, W, H );
423 break;
424 default:
425 redraw=false;
426 };
427 break;
428 case SDL_VIDEOEXPOSE:
429 redraw = true;
430 break;
431 case SDL_VIDEORESIZE:
432 W = event.resize.w;
433 H = event.resize.h;
434 initDisplay( W, H );
435 default: ;
436 }
437 }
438
439 if( redraw == true )
440 {
441 DrawGLScene( width, length, height, model );
442 }
443
444 SDL_Delay( 50 );
445 }
446 } // shot
447
448 delete model;
449
450 SDL_Quit();
451 return 1;
452 }
453
454
455 string ocDataDirPrefix( const string& s )
456 {
457 return s;
458 }
459
460
461 string ocConfigDirPrefix( const string& s )
462 {
463 return s;
464 }