vertexBufferData.h

00001 /*  
00002  *  Copyright (c) 2007, Tobias Wolf <twolf@access.unizh.ch>
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 
00019 #ifndef MESH_VERTEXBUFFERDATA_H
00020 #define MESH_VERTEXBUFFERDATA_H
00021 
00022 
00023 #include "typedefs.h"
00024 #include <vector>
00025 #include <fstream>
00026 
00027 
00028 namespace mesh 
00029 {
00030     
00031     
00032     /*  Holds the final kd-tree data, sorted and reindexed.  */
00033     class VertexBufferData
00034     {
00035     public:
00036         void clear()
00037         {
00038             vertices.clear();
00039             colors.clear();
00040             normals.clear();
00041             indices.clear();
00042         }
00043         
00044         /*  Write the vectors' sizes and contents to the given stream.  */
00045         void toStream( std::ostream& os )
00046         {
00047             writeVector( os, vertices );
00048             writeVector( os, colors );
00049             writeVector( os, normals );
00050             writeVector( os, indices );
00051         }
00052         
00053         /*  Read the vectors' sizes and contents from the given MMF address.  */
00054         void fromMemory( char** addr )
00055         {
00056             clear();
00057             readVector( addr, vertices );
00058             readVector( addr, colors );
00059             readVector( addr, normals );
00060             readVector( addr, indices );
00061         }
00062         
00063         std::vector< Vertex >       vertices;
00064         std::vector< Color >        colors;
00065         std::vector< Normal >       normals;
00066         std::vector< ShortIndex >   indices;
00067         
00068     private:
00069         /*  Helper function to write a vector to output stream.  */
00070         template< class T >
00071         void writeVector( std::ostream& os, std::vector< T >& v )
00072         {
00073             size_t length = v.size();
00074             os.write( reinterpret_cast< char* >( &length ), 
00075                       sizeof( size_t ) );
00076             if( length > 0 )
00077                 os.write( reinterpret_cast< char* >( &v[0] ), 
00078                           length * sizeof( T ) );
00079         }
00080         
00081         /*  Helper function to read a vector from the MMF address.  */
00082         template< class T >
00083         void readVector( char** addr, std::vector< T >& v )
00084         {
00085             size_t length;
00086             memRead( reinterpret_cast< char* >( &length ), addr, 
00087                      sizeof( size_t ) );
00088             if( length > 0 )
00089             {
00090                 v.resize( length );
00091                 memRead( reinterpret_cast< char* >( &v[0] ), addr, 
00092                          length * sizeof( T ) );
00093             }
00094         }
00095     };
00096     
00097     
00098 }
00099 
00100 
00101 #endif // MESH_VERTEXBUFFERDATA_H
Generated on Mon Aug 10 18:58:41 2009 for Equalizer 0.9 by  doxygen 1.5.8