eng_import.hh

Go to the documentation of this file.
00001 /*
00009  * LEGAL:   COPYRIGHT (C) JIM E. BROOKS 2005
00010  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00011  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00012  *****************************************************************************/
00013 
00014 #ifndef ENG_IMPORT_HH
00015 #define ENG_IMPORT_HH 1
00016 
00017 #include "eng_node.hh"
00018 #include "eng_graph.hh"
00019 #include "eng_graph_maker.hh"
00020 
00021 namespace eng {
00022 
00023 // Interface to import is a friend function:
00024 SharedPtr<Graph>
00025 ImportGraph( const string& fnameModel,
00026              const Vector3 vertexScale = Vector3(1,1,1),
00027              const Vector3 vertexOffset = Vector3(0,0,0) );
00028 
00045 class GraphImporter
00046 {
00047 
00048 
00049 // Client interface is a friend function:
00050     friend SharedPtr<Graph>
00051     ImportGraph( const string& fnameModel,
00052                  const Vector3 vertexScale,    // default: (1,1,1)
00053                  const Vector3 vertexOffset )  // default: (0,0,0)
00054     {
00055         GraphImporter importer( fnameModel, vertexScale, vertexOffset );
00056         return importer.GetGraph();
00057     }
00058 
00059 private:
00060     // Constructor imports the 3D model as a Graph object
00061     // which can be obtained by GetGraph().
00062                     GraphImporter( const string& fnameModel,
00063                                    const Vector3& vectorScale,
00064                                    const Vector3& vectorOffset );
00065                     ~GraphImporter();
00066     SharedPtr<Graph>    GetGraph( void );
00067     SharedPtr<Graph>    Import( void );
00068     void            ImportBeginPartition( const bool parsedRootPartition );
00069     void            ImportEndPartition( void );
00070     void            ImportVertexs( void );
00071     void            ImportPolygonBegin( void );
00072     void            ImportPolygonEnd( void );
00073     void            ImportPolygonColor( void );
00074     void            ImportPolygonTexture( void );
00075     void            ImportPolygonVixs( void );
00076     void            CheckStream( void );
00077     void            Error( void );
00078     // To scale a model.
00079     LocalVertex     TransformVertex( LocalVertex v )
00080     {
00081         v *= mVertexScale;  // apply scale first
00082         v += mVertexOffset;
00083         return v;
00084     }
00085 
00086 private:
00090     class Polygon
00091     {
00092     public:
00093         Polygon( void )
00094         {
00095             mNum = 0;
00096             Reset();
00097             mNum = 0;  // twice
00098         }
00099 
00102         void Reset( void )
00103         {
00104             Reset( mNum );  // continue num by default
00105         }
00106 
00107         void Reset( uint num )
00108         {
00109             mNum = num;
00110 
00111             // Clear other members.
00112             mNodePredecessors.Clear();
00113             mVixs.clear();
00114             mNix = mNum;  // assign, don't zero
00115             mTexCoords.clear();
00116             mModes = ModesNode::Modes::MODE_DEFAULT;
00117         #if DEBUG
00118             mDebugImportedColor = false;
00119             mDebugImportedVixs = false;
00120         #endif
00121 
00122             // Increment polygon number.  Determines polygon's index into normal vectors.
00123             ++mNum;
00124             ASSERT( mNum < MAX_POLYGONS );
00125         }
00126 
00127         bool IfTextured( void ) const { return not mTexCoords.empty(); }
00128 
00129     private:
00130         uint                            mNum;
00131     public:
00132         GraphMaker::NodePredecessors    mNodePredecessors;
00133         Vixs                            mVixs;  
00134         Nix                             mNix;   
00135         vector<TexCoord>                mTexCoords;
00136         ModesNode::Modes                mModes;
00137         #if DEBUG
00138         bool                            mDebugImportedColor;
00139         bool                            mDebugImportedVixs;
00140         #endif
00141     };
00142 
00143 // Persistent/const data members:
00144 private:
00145     typedef map<string,SharedPtr<Graph> > GraphMap;
00146     CLASS_VAR GraphMap  mGraphMap;      
00147 
00148     // The magic RGB (0.42,0.42,0.42) in a Blender model means translucency.
00149     CLASS_CONST uint MODEL_COLOR_TRANSLUCENT = 107;  // 107 = 0.42 * 256
00150     CLASS_CONST uint COLOR_TRANSLUCENT_R     = uint(0.40 * 256.0);
00151     CLASS_CONST uint COLOR_TRANSLUCENT_G     = uint(0.40 * 256.0);
00152     CLASS_CONST uint COLOR_TRANSLUCENT_B     = uint(0.45 * 256.0);
00153     CLASS_CONST uint COLOR_TRANSLUCENT_A     = uint(0.45 * 256.0);
00154 
00155 // Instance data members:
00156 private:
00157     const string                 mFnameModel;       
00158     const Vector3                mVertexScale;
00159     const Vector3                mVertexOffset;
00160     string                       mFileBuf;
00161     istringstream                mIstream;          
00162     GraphMaker                   mGraphMaker;       
00163     shptr<GraphMaker::Partition> mGraphPartition;   
00164     Stack<SharedPtr<GraphMaker::Partition> > mGraphPartitionStack;
00165     Polygon                      mPolygon;          
00166 #if DEBUG
00167     uint                         mDebugVertexsCnt;
00168     bool                         mDebugImportedPolygon;
00169     bool                         mDebugReturnedGraph;
00170     uint                         mDebugBeginPartition;  
00171     uint                         mDebugEndPartition;
00172 #endif
00173 };
00174 
00175 } // namespace eng
00176 
00177 #endif // ENG_IMPORT_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:08 2007