Equalizer 1.0

vertexBufferData.h

00001 
00002 /* Copyright (c) 2007, Tobias Wolf <twolf@access.unizh.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 
00030 #ifndef MESH_VERTEXBUFFERDATA_H
00031 #define MESH_VERTEXBUFFERDATA_H
00032 
00033 
00034 #include "typedefs.h"
00035 #include <vector>
00036 #include <fstream>
00037 
00038 
00039 namespace mesh 
00040 {    
00041     
00042     /*  Holds the final kd-tree data, sorted and reindexed.  */
00043     class VertexBufferData
00044     {
00045     public:
00046         void clear()
00047         {
00048             vertices.clear();
00049             colors.clear();
00050             normals.clear();
00051             indices.clear();
00052         }
00053         
00054         /*  Write the vectors' sizes and contents to the given stream.  */
00055         void toStream( std::ostream& os )
00056         {
00057             writeVector( os, vertices );
00058             writeVector( os, colors );
00059             writeVector( os, normals );
00060             writeVector( os, indices );
00061         }
00062         
00063         /*  Read the vectors' sizes and contents from the given MMF address.  */
00064         void fromMemory( char** addr )
00065         {
00066             clear();
00067             readVector( addr, vertices );
00068             readVector( addr, colors );
00069             readVector( addr, normals );
00070             readVector( addr, indices );
00071         }
00072         
00073         std::vector< Vertex >       vertices;
00074         std::vector< Color >        colors;
00075         std::vector< Normal >       normals;
00076         std::vector< ShortIndex >   indices;
00077         
00078     private:
00079         /*  Helper function to write a vector to output stream.  */
00080         template< class T >
00081         void writeVector( std::ostream& os, std::vector< T >& v )
00082         {
00083             size_t length = v.size();
00084             os.write( reinterpret_cast< char* >( &length ), 
00085                       sizeof( size_t ) );
00086             if( length > 0 )
00087                 os.write( reinterpret_cast< char* >( &v[0] ), 
00088                           length * sizeof( T ) );
00089         }
00090         
00091         /*  Helper function to read a vector from the MMF address.  */
00092         template< class T >
00093         void readVector( char** addr, std::vector< T >& v )
00094         {
00095             size_t length;
00096             memRead( reinterpret_cast< char* >( &length ), addr, 
00097                      sizeof( size_t ) );
00098             if( length > 0 )
00099             {
00100                 v.resize( length );
00101                 memRead( reinterpret_cast< char* >( &v[0] ), addr, 
00102                          length * sizeof( T ) );
00103             }
00104         }
00105     };
00106     
00107     
00108 }
00109 
00110 
00111 #endif // MESH_VERTEXBUFFERDATA_H
Generated on Sun May 8 2011 19:11:07 for Equalizer 1.0 by  doxygen 1.7.3