gfx_types_vertex.hh

Go to the documentation of this file.
00001 /*
00008  * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
00009  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00010  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00011  *****************************************************************************/
00012 
00013 #ifndef GFX_TYPES_VERTEX_HH
00014 #define GFX_TYPES_VERTEX_HH 1
00015 
00016 namespace gfx {
00017 
00018 // Basic types must be fast.  Virtual functions incur overhead from C++ vtable.
00019 #define virtual VIRTUAL_METHODS_IN_BASIC_TYPES_IS_TOO_SLOW
00020 
00024 typedef uint16 Vix;
00025 typedef uint16 Nix;  // index of a normal vector
00026 
00030 struct Vector2
00031 {
00032     Vector2( void )
00033   //:   x(0), y(0)  // would slow STL resize()
00034     {
00035         // NOP
00036     }
00037 
00038     Vector2( fp X, fp Y )
00039     :   x(X), y(Y)
00040     {
00041         // NOP
00042     }
00043 
00044     explicit Vector2( const fp f[2] )
00045     :   x(f[XX]), y(f[YY])
00046     {
00047         // NOP
00048     }
00049 
00050     void Set( fp X, fp Y )
00051     {
00052         x = X;
00053         y = Y;
00054     }
00055 
00056     void Set( const fp f[2] )
00057     {
00058         x = f[XX];
00059         y = f[YY];
00060     }
00061 
00062     bool operator==( const Vector2& src ) const
00063     {
00064         return x == src.x
00065             && y == src.y;
00066     }
00067 
00068     bool operator!=( const Vector2& src ) const
00069     {
00070         return x != src.x
00071             || y != src.y;
00072     }
00073 
00074     Vector2& operator+=( const Vector2& src )
00075     {
00076         x += src.x;
00077         y += src.y;
00078         return *this;
00079     }
00080 
00081     Vector2& operator-=( const Vector2& src )
00082     {
00083         x -= src.x;
00084         y -= src.y;
00085         return *this;
00086     }
00087 
00088     Vector2& operator*=( const Vector2& src )
00089     {
00090         x *= src.x;
00091         y *= src.y;
00092         return *this;
00093     }
00094 
00095     Vector2& operator/=( const Vector2& src )
00096     {
00097         x /= src.x;
00098         y /= src.y;
00099         return *this;
00100     }
00101 
00102     void Check( void ) const
00103     {
00104         // NOP (typesig omitted).
00105     }
00106 
00107     fp x;
00108     fp y;
00109 };
00110 
00114 INLINE Vector2 operator+( const Vector2& v1, const Vector2& v2 )
00115 {
00116     return Vector2( v1.x + v2.x,
00117                     v1.y + v2.y );
00118 }
00119 
00120 INLINE Vector2 operator-( const Vector2& v1, const Vector2& v2 )
00121 {
00122     return Vector2( v1.x - v2.x,
00123                     v1.y - v2.y );
00124 }
00125 
00126 INLINE Vector2 operator*( const Vector2& v1, const Vector2& v2 )
00127 {
00128     return Vector2( v1.x * v2.x,
00129                     v1.y * v2.y );
00130 }
00131 INLINE Vector2 operator/( const Vector2& v1, const Vector2& v2 )
00132 {
00133     return Vector2( v1.x / v2.x,
00134                     v1.y / v2.y );
00135 }
00136 
00140 INLINE Vector2 operator+( const Vector2& v, fp f )
00141 {
00142     return Vector2( v.x + f,
00143                     v.y + f );
00144 }
00145 
00146 INLINE Vector2 operator-( const Vector2& v, fp f )
00147 {
00148     return Vector2( v.x - f,
00149                     v.y - f );
00150 }
00151 
00152 INLINE Vector2 operator*( const Vector2& v, fp f )
00153 {
00154     return Vector2( v.x * f,
00155                     v.y * f );
00156 }
00157 
00158 INLINE Vector2 operator/( const Vector2& v, fp f )
00159 {
00160     return Vector2( v.x / f,
00161                     v.y / f );
00162 }
00163 
00167 // Vector2.
00168 INLINE ostream&
00169 operator<<( ostream& strm, const Vector2& v )
00170 {
00171     ios::fmtflags savedFlags = strm.flags();
00172     strm << '('
00173          << OMANIP_FIELD(8) << v.x << ','
00174          << OMANIP_FIELD(8) << v.y << ')';
00175     strm.flags(savedFlags);
00176     return strm;
00177 }
00178 
00188 struct Vector3
00189 {
00190     Vector3( void )
00191   //:   x(0), y(0), z(0)  // would slow STL resize()
00192     {
00193         // NOP
00194     }
00195 
00196     Vector3( fp X, fp Y, fp Z )
00197     :   x(X), y(Y), z(Z)
00198     {
00199         // NOP
00200     }
00201 
00202     explicit Vector3( const fp f[3] )
00203     :   x(f[XX]), y(f[YY]), z(f[ZZ])
00204     {
00205         // NOP
00206     }
00207 
00208     void Set( fp X, fp Y, fp Z )
00209     {
00210         x = X;
00211         y = Y;
00212         z = Z;
00213     }
00214 
00215     void Set( const fp f[3] )
00216     {
00217         x = f[XX];
00218         y = f[YY];
00219         z = f[ZZ];
00220     }
00221 
00222     bool operator==( const Vector3& src ) const
00223     {
00224         return x == src.x
00225             && y == src.y
00226             && z == src.z;
00227     }
00228 
00229     bool operator!=( const Vector3& src ) const
00230     {
00231         return x != src.x
00232             || y != src.y
00233             || z != src.z;
00234     }
00235 
00236     Vector3& operator+=( const Vector3& src )
00237     {
00238         x += src.x;
00239         y += src.y;
00240         z += src.z;
00241         return *this;
00242     }
00243 
00244     Vector3& operator-=( const Vector3& src )
00245     {
00246         x -= src.x;
00247         y -= src.y;
00248         z -= src.z;
00249         return *this;
00250     }
00251 
00252     Vector3& operator*=( const Vector3& src )
00253     {
00254         x *= src.x;
00255         y *= src.y;
00256         z *= src.z;
00257         return *this;
00258     }
00259 
00260     Vector3& operator/=( const Vector3& src )
00261     {
00262         x /= src.x;
00263         y /= src.y;
00264         z /= src.z;
00265         return *this;
00266     }
00267 
00268     fp& operator[]( uint i )
00269     {
00270     ASSERT( i < 3 );
00271         return reinterpret_cast<fp*>(&x)[i];
00272     }
00273 
00274     const fp& operator[]( uint i ) const
00275     {
00276     ASSERT( i < 3 );
00277         return reinterpret_cast<const fp*>(&x)[i];
00278     }
00279 
00280     // Useful with templates where a Vector3 is a template arg.
00281     // Check() isn't virtual since Vector3 should be POD.
00282     void Check( void ) const
00283     {
00284         // NOP (typesig omitted).
00285     }
00286 
00287     fp  x;
00288     fp  y;
00289     fp  z;
00290 
00291   //  // prevents EyeVertex being convertible to GLfloat[3]
00292 };
00293 
00294 #define VECTOR3 Vector3
00295 #include "gfx_types_vertex_ops.hh"
00296 #undef VECTOR3
00297 
00301 struct LocalVertex : public Vector3
00302 {
00303     LocalVertex( fp X, fp Y, fp Z )
00304     :   Vector3(X,Y,Z)
00305     {
00306         SET_TYPESIG(this,TYPESIG_VERTEX_LOCAL);
00307     }
00308 
00309     explicit LocalVertex( const fp f[3] ) 
00310     :   Vector3(f)
00311     {
00312         SET_TYPESIG(this,TYPESIG_VERTEX_LOCAL);
00313     }
00314 
00315     explicit LocalVertex( const Vector3& v )  // explicit should keep Vertex types distinct
00316     :   Vector3(v)
00317     {
00318         SET_TYPESIG(this,TYPESIG_VERTEX_LOCAL);
00319     }
00320 
00321   //LocalVertex& operator=( const Vector3& src )  // would erode distinct Vertex types
00322 
00323     void Check( void ) const
00324     {
00325         CHECK_TYPESIG(this,TYPESIG_VERTEX_LOCAL);
00326     }
00327 
00328     
00329 };
00330 
00331 #define VECTOR3 LocalVertex
00332 #include "gfx_types_vertex_ops.hh"
00333 #undef VECTOR3
00334 
00338 struct WorldVertex : public Vector3
00339 {
00340     WorldVertex( fp X, fp Y, fp Z )
00341     :   Vector3(X,Y,Z)
00342     {
00343         SET_TYPESIG(this,TYPESIG_VERTEX_WORLD);
00344     }
00345 
00346     explicit WorldVertex( const fp f[3] )
00347     :   Vector3(f)
00348     {
00349         SET_TYPESIG(this,TYPESIG_VERTEX_WORLD);
00350     }
00351 
00352     explicit WorldVertex( const Vector3& v )  // explicit should keep Vertex types distinct
00353     :   Vector3(v)
00354     {
00355         SET_TYPESIG(this,TYPESIG_VERTEX_WORLD);
00356     }
00357 
00358   //WorldVertex& operator=( const Vector3& src )  // would erode distinct Vertex types
00359 
00360     void Check( void ) const
00361     {
00362         CHECK_TYPESIG(this,TYPESIG_VERTEX_WORLD);
00363     }
00364 
00365     
00366 };
00367 
00368 #define VECTOR3 WorldVertex
00369 #include "gfx_types_vertex_ops.hh"
00370 #undef VECTOR3
00371 
00381 struct EyeVertex : public Vector3
00382 {
00383     EyeVertex( void )
00384   //:   x(0), y(0), z(0)  // would slow resize()
00385     {
00386     }
00387 
00388     EyeVertex( fp X, fp Y, fp Z )
00389     :   Vector3(X,Y,Z)
00390     {
00391     }
00392 
00393     explicit EyeVertex( const fp f[3] )
00394     :   Vector3(f)
00395     {
00396     }
00397 
00398     explicit EyeVertex( const Vector3& v )  // explicit should keep Vertex types distinct
00399     :   Vector3(v)
00400     {
00401     }
00402 
00403   //EyeVertex& operator=( const Vector3& src )  // would erode distinct Vertex types
00404 
00405     void Check( void ) const
00406     {
00407         // NOP (typesig omitted).
00408     }
00409 
00410   //  // EyeVertex be convertible to GLfloat[3]
00411 };
00412 
00413 #define VECTOR3 EyeVertex
00414 #include "gfx_types_vertex_ops.hh"
00415 #undef VECTOR3
00416 
00420 struct NormalVertex : public Vector3
00421 {
00422     NormalVertex( fp X, fp Y, fp Z )
00423     :   Vector3(X,Y,Z)
00424     {
00425         SET_TYPESIG(this,TYPESIG_VERTEX_NORMAL);
00426     }
00427 
00428     explicit NormalVertex( const fp f[3] )
00429     :   Vector3(f)
00430     {
00431         SET_TYPESIG(this,TYPESIG_VERTEX_NORMAL);
00432     }
00433 
00434     explicit NormalVertex( const Vector3& v )  // explicit should keep Vertex types distinct
00435     :   Vector3(v)
00436     {
00437         SET_TYPESIG(this,TYPESIG_VERTEX_NORMAL);
00438     }
00439 
00440     void Check( void ) const
00441     {
00442         CHECK_TYPESIG(this,TYPESIG_VERTEX_NORMAL);
00443     }
00444 
00445     
00446 };
00447 
00448 #define VECTOR3 NormalVertex
00449 #include "gfx_types_vertex_ops.hh"
00450 #undef VECTOR3
00451 
00453 // Array typedefs.
00454 // Plural "s" ending is terse albeit easy to mistype.
00455 //
00456 // [2007/03] Matrix class expects vertex arrays to be based on Array template class.
00457 // A possible future optimization is the template specialization Array<EyeVertexs>
00458 // which would reuse a pre-allocated array.  But it may cause trouble.
00459 //
00460 typedef Array<LocalVertex>  LocalVertexs;
00461 typedef Array<WorldVertex>  WorldVertexs;
00462 typedef Array<EyeVertex>    EyeVertexs;
00463 typedef Array<NormalVertex> NormalVertexs;
00464 typedef Array<Vix>          Vixs;  // array of vertex indexs
00465 
00466 #undef virtual
00467 
00468 } // namespace gfx
00469 
00470 #endif // GFX_TYPES_VERTEX_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:11 2007