Equalizer  1.4.1
application.cpp
00001 
00002 /* Copyright (c) 2011-2012, Stefan Eilemann <eile@eyescale.ch>
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *
00007  * - Redistributions of source code must retain the above copyright notice, this
00008  *   list of conditions and the following disclaimer.
00009  * - Redistributions in binary form must reproduce the above copyright notice,
00010  *   this list of conditions and the following disclaimer in the documentation
00011  *   and/or other materials provided with the distribution.
00012  * - Neither the name of Eyescale Software GmbH nor the names of its
00013  *   contributors may be used to endorse or promote products derived from this
00014  *   software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026  * POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00029 #include "application.h"
00030 
00031 #include "renderer.h"
00032 
00033 #ifndef MIN
00034 #  define MIN LB_MIN
00035 #endif
00036 #include <tclap/CmdLine.h>
00037 
00038 namespace seqPly
00039 {
00040 
00041 bool Application::init( const int argc, char** argv )
00042 {
00043     const eq::Strings& models = _parseArguments( argc, argv );
00044     if( !seq::Application::init( argc, argv, 0 ))
00045         return false;
00046 
00047     _loadModel( models );
00048     return true;
00049 }
00050 
00051 bool Application::run()
00052 {
00053     return seq::Application::run( &_frameData );
00054 }
00055 
00056 bool Application::exit()
00057 {
00058     _unloadModel();
00059     return seq::Application::exit();
00060 }
00061 
00062 seq::Renderer* Application::createRenderer()
00063 {
00064     return new Renderer( *this );
00065 }
00066 
00067 co::Object* Application::createObject( const uint32_t type )
00068 {
00069     switch( type )
00070     {
00071       case seq::OBJECTTYPE_FRAMEDATA:
00072           return new eqPly::FrameData;
00073 
00074       default:
00075           return seq::Application::createObject( type );
00076     }
00077 }
00078 
00079 namespace
00080 {
00081 static bool _isPlyfile( const std::string& filename )
00082 {
00083     const size_t size = filename.length();
00084     if( size < 5 )
00085         return false;
00086 
00087     if( filename[size-4] != '.' || filename[size-3] != 'p' ||
00088         filename[size-2] != 'l' || filename[size-1] != 'y' )
00089     {
00090         return false;
00091     }
00092     return true;
00093 }
00094 }
00095 
00096 eq::Strings Application::_parseArguments( const int argc, char** argv )
00097 {
00098     TCLAP::CmdLine command( "seqPly - Sequel polygonal rendering example", ' ',
00099                             eq::Version::getString( ));
00100     TCLAP::ValueArg<std::string> modelArg( "m", "model", "ply model file name",
00101                                            false, "", "string", command );
00102     TCLAP::VariableSwitchArg ignoreEqArgs( "eq", "Ignored Equalizer options",
00103                                            command );
00104     TCLAP::UnlabeledMultiArg< std::string >
00105         ignoreArgs( "ignore", "Ignored unlabeled arguments", false, "any",
00106                     command );
00107     command.parse( argc, argv );
00108 
00109     eq::Strings filenames;
00110 #ifdef EQ_RELEASE
00111 #  ifdef _WIN32 // final INSTALL_DIR is not known at compile time
00112     filenames.push_back( "../share/Equalizer/data" );
00113 #  else
00114     filenames.push_back( std::string( EQ_INSTALL_DIR ) +
00115                          std::string( "share/Equalizer/data" ));
00116     filenames.push_back( std::string( "/usr/share/Equalizer/data" ));
00117 #  endif
00118 #else
00119     filenames.push_back( std::string( EQ_SOURCE_DIR ) +
00120                          std::string( "examples/eqPly" ));
00121 #endif
00122 
00123     if( modelArg.isSet( ))
00124     {
00125         filenames.clear();
00126         filenames.push_back( modelArg.getValue( ));
00127     }
00128     return filenames;
00129 }
00130 
00131 void Application::_loadModel( const eq::Strings& models )
00132 {
00133     eq::Strings files = models;
00134     while( !files.empty( ))
00135     {
00136         const std::string filename = files.back();
00137         files.pop_back();
00138 
00139         if( _isPlyfile( filename ))
00140         {
00141             _model = new Model;
00142             if( _model->readFromFile( filename.c_str( )))
00143             {
00144                 _modelDist = new ModelDist( _model );
00145                 _modelDist->registerTree( this );
00146                 _frameData.setModelID( _modelDist->getID( ));
00147                 return;
00148             }
00149             delete _model;
00150             _model = 0;
00151         }
00152         else
00153         {
00154             const std::string basename = lunchbox::getFilename( filename );
00155             if( basename == "." || basename == ".." )
00156                 continue;
00157 
00158             // recursively search directories
00159             const eq::Strings subFiles = lunchbox::searchDirectory( filename,
00160                                                                     "*" );
00161 
00162             for(eq::StringsCIter i = subFiles.begin(); i != subFiles.end(); ++i)
00163                 files.push_back( filename + '/' + *i );
00164         }
00165     }
00166 }
00167 
00168 void Application::_unloadModel()
00169 {
00170     if( !_modelDist )
00171         return;
00172 
00173     _modelDist->deregisterTree();
00174     delete _modelDist;
00175     _modelDist = 0;
00176 
00177     delete _model;
00178     _model = 0;
00179 }
00180 
00181 const Model* Application::getModel( const eq::uint128_t& modelID )
00182 {
00183     if( modelID == eq::UUID::ZERO )
00184         return 0;
00185     if( _model )
00186         return _model;
00187     lunchbox::memoryBarrier();
00188 
00189     // Accessed concurrently from render threads
00190     lunchbox::ScopedMutex<> mutex( _modelLock );
00191     if( _model )
00192         return _model;
00193 
00194     LBASSERT( !_modelDist );
00195     _modelDist = new ModelDist;
00196     Model* model = _modelDist->loadModel( getMasterNode(), this, modelID );
00197     LBASSERT( model );
00198     _model = model;
00199 
00200     return model;
00201 }
00202 
00203 }
00204 
Generated on Mon Nov 26 2012 14:41:48 for Equalizer 1.4.1 by  doxygen 1.7.6.1