typedefs.h

00001 /*  
00002     typedefs.h
00003     Copyright (c) 2007, Tobias Wolf <twolf@access.unizh.ch>
00004     Copyright (c) 2009, Cedric Stalder <cedric.stalder@gmail.com>
00005   *
00006  * This library is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License version 2.1 as published
00008  * by the Free Software Foundation.
00009  *  
00010  * This library is distributed in the hope that it will be useful, but WITHOUT
00011  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00012  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00013  * details.
00014  * 
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this library; if not, write to the Free Software Foundation, Inc.,
00017  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018   
00019     
00020     Type definitions for the mesh classes.
00021 */
00022 
00023 
00024 #ifndef MESH_TYPEDEFS_H
00025 #define MESH_TYPEDEFS_H
00026 
00027 #define EQUALIZER  1
00028 
00029 #ifdef EQUALIZER
00030 #   include <eq/eq.h>
00031 #   define MESHASSERT  EQASSERT
00032 #   define MESHERROR   EQERROR
00033 #   define MESHWARN    EQWARN
00034 #   define MESHINFO    EQINFO
00035 #else
00036 #   ifdef _WIN32
00037 #      include <Winsock2.h>
00038 #      include <Windows.h>
00039 #   endif
00040 #   ifdef __APPLE__
00041 #      include <OpenGL/gl.h>
00042 #   else
00043 #      include <GL/gl.h>
00044 #   endif
00045 #   include <cassert>
00046 #   define MESHASSERT  assert
00047 #   define MESHERROR   std::cerr
00048 #   define MESHWARN    std::cout
00049 #   define MESHINFO    std::cout
00050 #endif
00051 
00052 #include <vmmlib/vmmlib.hpp>
00053 
00054 #include <exception>
00055 #include <iostream>
00056 #include <string>
00057 
00058 namespace mesh 
00059 {
00060     
00061     
00062     // basic type definitions   
00063     typedef vmml::vector< 3, GLfloat >    Vertex;
00064     typedef vmml::vector< 4, GLubyte >    Color;
00065     typedef vmml::vector< 3, GLfloat >    Normal;
00066     typedef size_t                        Index;
00067     typedef GLushort                      ShortIndex;
00068     
00069     
00070     // mesh exception
00071     struct MeshException : public std::exception
00072     {
00073         explicit MeshException( const std::string& msg ) : _message( msg ) {}
00074         virtual ~MeshException() throw() {}
00075         virtual const char* what() const throw() { return _message.c_str(); }
00076     private:
00077         std::string _message;
00078     };
00079     
00080     // null output stream that discards everything written to it
00081     struct NullOStream : std::ostream
00082     {
00083         struct NullStreamBuf : std::streambuf
00084         {
00085             int overflow( int c ) { return traits_type::not_eof( c ); }
00086         } _nullBuf;
00087         
00088         NullOStream() : std::ios( &_nullBuf ), std::ostream( &_nullBuf ) {}
00089     };
00090     
00091     // wrapper to enable array use where arrays would not be allowed otherwise
00092     template< class T, size_t d >
00093     struct ArrayWrapper
00094     {
00095         T& operator[]( const size_t i )
00096         {
00097             MESHASSERT( i < d );
00098             return data[i];
00099         }
00100         
00101         const T& operator[]( const size_t i ) const
00102         {
00103             MESHASSERT( i < d );
00104             return data[i];
00105         }
00106         
00107     private:
00108         T data[d];
00109     };
00110     
00111     
00112     // compound type definitions
00113     typedef vmml::vector< 3, Index >      Triangle;
00114     typedef ArrayWrapper< Vertex, 2 >   BoundingBox;
00115     typedef vmml::vector< 4, float >    BoundingSphere;
00116     typedef ArrayWrapper< float, 2 >    Range;
00117     
00118     
00119     // maximum triangle count per leaf node (keep in mind that the number of
00120     // different vertices per leaf must stay below ShortIndex range; usually
00121     // #vertices ~ #triangles/2, but max #vertices = #triangles * 3)
00122     const Index             LEAF_SIZE( 21845 );
00123     
00124     // binary mesh file version, increment if changing the file format
00125     const unsigned short    FILE_VERSION ( 0x0115 );
00126     
00127     
00128     // enumeration for the sort axis
00129     enum Axis
00130     {
00131         AXIS_X,
00132         AXIS_Y,
00133         AXIS_Z
00134     };
00135     inline std::ostream& operator << ( std::ostream& os, const Axis axis )
00136     {
00137         os << ( axis == AXIS_X ? "x axis" : axis == AXIS_Y ? "y axis" :
00138                 axis == AXIS_Z ? "z axis" : "ERROR" );
00139         return os;
00140     }
00141     
00142     // enumeration for the buffer objects
00143     enum BufferObject
00144     {
00145         VERTEX_OBJECT,
00146         NORMAL_OBJECT,
00147         COLOR_OBJECT,
00148         INDEX_OBJECT
00149     };
00150     
00151     // enumeration for the render modes
00152     enum RenderMode
00153     {
00154         RENDER_MODE_IMMEDIATE = 0,
00155         RENDER_MODE_DISPLAY_LIST,
00156         RENDER_MODE_BUFFER_OBJECT,
00157         RENDER_MODE_ALL // must be last
00158     };
00159     inline std::ostream& operator << ( std::ostream& os, const RenderMode mode )
00160     {
00161         os << ( mode == RENDER_MODE_IMMEDIATE     ? "immediate mode" : 
00162                 mode == RENDER_MODE_DISPLAY_LIST  ? "display list mode" : 
00163                 mode == RENDER_MODE_BUFFER_OBJECT ? "VBO mode" : "ERROR" );
00164         return os;
00165     }
00166     
00167     // enumeration for kd-tree node types
00168     enum NodeType
00169     {
00170         ROOT_TYPE = 0x07,
00171         NODE_TYPE = 0xde,
00172         LEAF_TYPE = 0xef
00173     };
00174     
00175     
00176     // helper function for MMF (memory mapped file) reading
00177     inline
00178     void memRead( char* destination, char** source, size_t length )
00179     {
00180         memcpy( destination, *source, length );
00181         *source += length;
00182     }
00183     
00184     
00185     // internally linked null stream, every translation unit gets a copy
00186     static mesh::NullOStream cnul;
00187 }
00188 
00189 
00190 #endif // MESH_TYPEDEFS_H
Generated on Mon Aug 10 18:58:41 2009 for Equalizer 0.9 by  doxygen 1.5.8