// // This code was created by Jeff Molofee '99 // (ported to SDL by Sam Lantinga '2000) // // If you've found this code useful, please let me know. // // Visit me at www.demonews.com/hosted/nehe // // Code modifed by Frederic Rodrigo 2005-2007 as viewer of object loaded by OpenCity /* TODO: - add geometry commands line argument - improve help display: key - mine type for .ac */ // Useful enumerations #include "opencity_direction.h" #include "opencity_structure_type.h" // OpenCity headers #include "buildinglayer.h" #include "conf.h" #include "macros.h" #include "modelloader.h" #include "model.h" #include "structure.h" #include "texture.h" #include "property.h" // Standard headers #include // Libraries headers #include "pngfuncs.h" #include "SimpleOpt.h" // Simple command line argument parser int W = 640; int H = 480; /* floats for x rotation, y rotation, z rotation */ float xrot=30, yrot=30, zrot=0, zoom=3; /* A general OpenGL initialization function. Sets all of the initial parameters. */ int initGL( const int width, const int height ) // We call this right after our OpenGL window is created. { glViewport(0, 0, width, height); glEnable(GL_TEXTURE_2D); // Enable Texture Mapping glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); // glBlendFunc( GL_ONE, GL_ZERO ); glClearColor(0.5f, 0.5f, 0.8f, 1.0f); // Clear The Background Color To Blue glClearDepth(1.0); // Enables Clearing Of The Depth Buffer glDepthFunc(GL_LESS); // The Type Of Depth Test To Do glEnable(GL_DEPTH_TEST); // Enables Depth Testing // glDepthMask(GL_FALSE); // Disable depth buffer writing glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Reset The Projection Matrix gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window glMatrixMode(GL_MODELVIEW); // glEnable( GL_LIGHTING ); // glColor3f ( 1.0f, 0.0f, 0.0f ) ; // glColorMaterial ( GL_FRONT_AND_BACK, GL_EMISSION ) ; glEnable ( GL_COLOR_MATERIAL ) ; return 0; } /* The main drawing function. */ void DrawGLScene( float width, float length, float height, const Model *model ) { signed char tabY [] = { 0, 0, 0, 0}; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer glLoadIdentity(); // Reset The View glTranslatef(0.0f,0.0f,-zoom); // move 5 units into the screen. glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate On The X Axis glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate On The Y Axis glRotatef(zrot,0.0f,0.0f,1.0f); // Rotate On The Z Axis model->DisplayList( -width/2, -length/2, tabY ); // swap buffers to display, since we're double buffered. SDL_GL_SwapBuffers(); } /* FIXME : Not sure about portability of this function */ void screenshot( const string name, int w, int h ) { glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT ); // save context // make screenshot file name string file = name.substr( 0, name.length()-3 ) + "_screenshot.png"; // Build the screen shot surface unsigned int* someBuffer = new unsigned int[ w * h * 4 ]; Uint32 rmask, gmask, bmask, amask; #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; #else rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; #endif SDL_Surface *shot = SDL_CreateRGBSurfaceFrom( someBuffer, w, h, 8*4, w*4, rmask, gmask, bmask, amask ); // SDL_Surface *shot = SDL_CreateRGBSurfaceFrom( someBuffer, w, h, 8*4, w*4, 0, 0, 0, 0 ); // set screenshot context glPixelStorei( GL_PACK_ROW_LENGTH, 0 ); glPixelStorei( GL_PACK_ALIGNMENT, 1 ); glReadPixels( 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, someBuffer ); if( shot != NULL ) { SDL_Surface *shotMirorred = Texture::HorizontalMirror( shot ); if( shotMirorred != NULL ) { if( png_save_surface( file.c_str(), shotMirorred ) == 0 ) { OPENCITY_INFO( "Capture saved under \"" << file << "\"" ); } else { OPENCITY_ERROR( "Failing save capture under \"" << file << "\"" ); } SDL_FreeSurface( shotMirorred ); } else { OPENCITY_ERROR( "Failing working whith capture for \"" << file << "\"" ); } SDL_FreeSurface( shot ); } else { OPENCITY_ERROR( "Failling take a shot." ); } glPopClientAttrib(); // restor context delete [] someBuffer; } Conf *loadConf( const string &acFile ) { string confFile = acFile.substr( 0, acFile.length()-3 ) + ".conf"; Conf* pConf = new Conf(); int openStatus = pConf->Open( confFile ); if( openStatus != OC_ERR_FREE ) { OPENCITY_DEBUG( "fail load confFile = " << confFile ); delete pConf; pConf = NULL; } else { OPENCITY_DEBUG( "load confFile = " << confFile ); } return pConf; } Property *loadProperty( const string &acFile ) { string propertyFile = acFile.substr( 0, acFile.length()-3 ) + ".xml"; Property* pProperty = Property::LoadProperties( propertyFile ); OPENCITY_DEBUG( "load propertyFile = " << propertyFile ); return pProperty; } string parseArgLine( int argc, char **argv, bool *shot ) { enum { OPT_SCREENSHOT, OPT_HELP }; CSimpleOpt::SOption g_rgOptions[] = { { OPT_SCREENSHOT, (char*)"--screenshot", SO_NONE }, { OPT_SCREENSHOT, (char*)"-s", SO_NONE }, { OPT_HELP, (char*)"--help", SO_NONE }, { OPT_HELP, (char*)"-h", SO_NONE }, SO_END_OF_OPTIONS // END }; CSimpleOpt args(argc, argv, g_rgOptions, SO_O_EXACT | SO_O_NOSLASH | SO_O_SHORTARG | SO_O_CLUMP ); while (args.Next()) { switch (args.LastError()) { case SO_OPT_INVALID: cout << "