glgeometry.h

00001 /*
00002 Copyright (c) 2000-2007 Lee Thomason (www.grinninglizard.com)
00003 Grinning Lizard Utilities.
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any 
00010 purpose, including commercial applications, and to alter it and 
00011 redistribute it freely, subject to the following restrictions:
00012 
00013 1. The origin of this software must not be misrepresented; you must 
00014 not claim that you wrote the original software. If you use this 
00015 software in a product, an acknowledgment in the product documentation 
00016 would be appreciated but is not required.
00017 
00018 2. Altered source versions must be plainly marked as such, and 
00019 must not be misrepresented as being the original software.
00020 
00021 3. This notice may not be removed or altered from any source 
00022 distribution.
00023 */
00024 
00025 
00026 
00027 #ifndef GRINLIZ_GEOMETRY_INCLUDED
00028 #define GRINLIZ_GEOMETRY_INCLUDED
00029 
00030 #include "glvector.h"
00031 #include "glrectangle.h"
00032 #include "glmatrix.h"
00033 
00034 #include <vector>
00035 
00036 namespace grinliz {
00037 
00039 template< class T>
00040 inline void CrossProduct( const Vector2<T>& u, const Vector2<T>& v, Vector3<T>* w )
00041 {
00042         w->x = 0.0f;
00043         w->y = 0.0f;
00044         w->z = u.x * v.y - u.y * v.x;
00045 }
00046 
00048 template< class T>
00049 inline T CrossProductZ( const Vector2<T>& u, const Vector2<T>& v )
00050 {
00051         return u.x * v.y - u.y * v.x;
00052 }
00053 
00054 
00056 template< class T>
00057 inline T DotProduct( const Vector2<T>& v0, const Vector2<T>& v1 )
00058 {
00059         return v0.x*v1.x + v0.y*v1.y;
00060 }
00061 
00062 
00064 template< class T>
00065 inline void CrossProduct( const Vector3<T>& u, const Vector3<T>& v, Vector3<T>* w )
00066 {
00067         GLASSERT( &u != w && &v != w );
00068 
00069         w->x = u.y * v.z - u.z * v.y;
00070         w->y = u.z * v.x - u.x * v.z;
00071         w->z = u.x * v.y - u.y * v.x;
00072 }
00073 
00074 
00076 template< class T>
00077 inline Vector3<T> CrossProduct( const Vector3<T>& u, const Vector3<T>& v )
00078 {
00079         Vector3<T> w;
00080 
00081         w.x = u.y * v.z - u.z * v.y;
00082         w.y = u.z * v.x - u.x * v.z;
00083         w.z = u.x * v.y - u.y * v.x;
00084         return w;
00085 }
00086 
00087 
00089 template< class T>
00090 inline T DotProduct( const Vector3<T>& v0, const Vector3<T>& v1 )
00091 {
00092         return v0.x*v1.x + v0.y*v1.y + v0.z*v1.z;
00093 }
00094 
00095 
00096 struct BoundingSphere
00097 {
00098         Vector3F        point;
00099         float           rad;
00100 
00101 };
00102 
00103 
00106 struct Ray
00107 {
00108         Vector3F        origin;
00109         Vector3F        direction;      // should always have a length of 1.0f
00110         float           length;
00111 };
00112 
00117 struct Plane
00118 {
00120         static void CreatePlane( const Vector3F* vertices, Plane* plane );
00121 
00122         Vector3F        n;      
00123         float           d;      
00124 
00125         float Z( float x, float y ) const { return -( d + (n.x * x) + (n.y * y) ) / ( n.z ); }
00126 
00127         void Normalize()
00128         {
00129                 float div = 1.0f / sqrtf( ( n.x * n.x ) + ( n.y * n.y ) + ( n.z * n.z ) );
00130                 n.x *= div;
00131                 n.y *= div;
00132                 n.z *= div;
00133                 d   *= div;
00134         }
00135 
00137         void ProjectToPlane( const Vector3F& vector, Vector3F* projected ) const;
00138 };
00139 
00140 float CalcSphereTexture( float x, float y, bool roundHigh );
00141 
00142 enum TessellateSphereMode
00143 {
00144         TESS_SPHERE_NORMAL,
00145         TESS_SPHERE_TOPHALF,
00146         TESS_SPHERE_MIRROR,
00147 };
00148 
00162 void TessellateSphere( int iterations, float radius, bool innerNormals, 
00163                                           std::vector< Vector3F >* _vertex, 
00164                                           std::vector< U32 >* _index, 
00165                                           std::vector< Vector3F >* _normal = 0,
00166                                           std::vector< Vector2F >* _texture = 0,
00167                                           TessellateSphereMode mode = TESS_SPHERE_NORMAL );
00168 
00169 
00172 struct LineNode
00173 {
00174         grinliz::Vector2F       point;          // the point on the line
00175         LineNode*       prev;
00176         LineNode*       next;
00177         float           value;                          // optional interpolation info
00178 
00179         LineNode( const grinliz::Vector2F& p ) : point( p ), prev( 0 ), next( 0 ), value( 0.0f ) {}
00180         LineNode( float x, float y ) : prev( 0 ), next( 0 ), value( 0.0f ) { point.Set( x, y ); }
00181         LineNode( float x, float y, float val ) : prev( 0 ), next( 0 ), value( val ) { point.Set( x, y ); }
00182         ~LineNode() {}
00183 };
00184 
00185 
00191 class LineLoop
00192 {
00193 public:
00195         LineLoop()      : first( 0 )    {}
00196         ~LineLoop()     { Clear(); }
00197 
00199         void Clear();
00201         void Delete( LineNode* del );
00212         void AddAtEnd( LineNode* node );
00214         void AddAfter( LineNode* me, LineNode* add );
00216         LineNode* First()       { return first; }
00218         const LineNode* First() const   { return first; }
00223         void SortToTop();
00225         void Bounds( grinliz::Rectangle2F* bounds );
00226 
00231         void Render( float* surface, int width, int height, bool fill=false );
00232 
00233         #ifdef DEBUG
00234         void Dump();
00235         #endif
00236  
00237 private:
00238         LineNode* first;
00239 };
00240 
00241 
00247 template< int X0, int X1, int XC >
00248 struct Quad3F
00249 {
00250         enum { NUM_VERTEX = 4 };
00251         Vector3F vertex[NUM_VERTEX];
00252 
00253         Quad3F( float x0, float y0, float x1, float y1, float c ) 
00254         {
00255                 vertex[0].X(X0) = x0;   vertex[0].X(X1) = y0;   vertex[0].X(XC) = c;
00256                 vertex[1].X(X0) = x1;   vertex[1].X(X1) = y0;   vertex[1].X(XC) = c;
00257                 vertex[2].X(X0) = x1;   vertex[2].X(X1) = y1;   vertex[2].X(XC) = c;
00258                 vertex[3].X(X0) = x0;   vertex[3].X(X1) = y1;   vertex[3].X(XC) = c;
00259         }
00260 };
00261 
00262 typedef Quad3F< 0, 1, 2 > Quad3Fz;
00263 
00264 
00270 template< int X0, int X1 >
00271 struct Quad2F
00272 {
00273         enum { NUM_VERTEX = 4 };
00274         Vector2F vertex[NUM_VERTEX];
00275 
00276         Quad2F( float x0, float y0, float x1, float y1 ) 
00277         {
00278                 vertex[0].X(X0) = x0;   vertex[0].X(X1) = y0;
00279                 vertex[1].X(X0) = x1;   vertex[1].X(X1) = y0;
00280                 vertex[2].X(X0) = x1;   vertex[2].X(X1) = y1;
00281                 vertex[3].X(X0) = x0;   vertex[3].X(X1) = y1;
00282         }
00283 };
00284 
00285 typedef Quad2F< 0, 1 > Quad2Fz;
00286 
00287 
00288 
00293 class Quaternion
00294 {
00295 public:
00296         Quaternion() : x( 0.0f ), y( 0.0f ), z( 0.0f ), w( 1.0f )       {}
00297 
00298         void Normalize();
00299 
00300         const void ToAxisAngle( Vector3F* axis, float* angleOut ) const;
00301         const void ToMatrix( Matrix4* matrix ) const;
00302         void FromRotationMatrix( const Matrix4& matrix );
00303         void FromAxisAngle( const Vector3F& axis, float angle );
00304 
00305         static void SLERP( const Quaternion& start, const Quaternion& end, float t, Quaternion* result );
00306         static void Multiply( const Quaternion& a, const Quaternion& b, Quaternion* result );
00307 
00308         float x, y, z, w;
00309 };
00310 
00311 
00312 enum
00313 {
00314         REJECT = 0,             // No intersection.
00315         INTERSECT,              // Intersected
00316         POSITIVE,               // All comparison positive
00317         NEGATIVE,               // All Comparison negative
00318         INSIDE,                 // An intersection, expected to be outside of the object, is originating inside
00319 };
00320 
00321 enum 
00322 {
00323         // Reordering would be bad. x=0, y=1, z=2
00324         YZ_PLANE,
00325         XZ_PLANE,
00326         XY_PLANE
00327 };
00328 
00329 
00351 int ComparePlaneAABB( const Plane&, const Rectangle3F& aabb );
00352 
00356 int ComparePlanePoint( const Plane&, const Vector3F& p );
00357 
00361 inline int IntersectPlaneAABB( const Plane& p, const Rectangle3F& aabb )
00362 {
00363         return ( ComparePlaneAABB( p, aabb ) == INTERSECT ) ? INTERSECT : REJECT; 
00364 }
00365 
00366 
00371 int IntersectRayTri(    const Vector3F& point, const Vector3F& dir,
00372                                                 const Vector3F& vert0, const Vector3F& vert1, const Vector3F& vert2,
00373                                                 Vector3F* intersect );
00374 
00379 int IntersectRayAAPlane(        const Vector3F& point, const Vector3F& dir,
00380                                                         int planeType, float planeLoc,
00381                                                         Vector3F* intersect,
00382                                                         float* t );
00383                                                  
00388 int IntersectRayAABB(   const Vector3F& origin, const Vector3F& dir,
00389                                                 const Rectangle3F& aabb,
00390                                                 Vector3F* intersect,
00391                                                 float* t );
00392 
00398 int IntersectRayAABB(   const Vector2F& origin, const Vector2F& dir,
00399                                                 const Rectangle2F& aabb,
00400                                                 Vector2F* intersect,
00401                                                 float* t, int* planeIndex=0, int* quadrant=0 );
00402 
00408 int IntersectionRayAABB(        const Ray& ray,
00409                                                         const Rectangle3F& aabb,
00410                                                         Rectangle3F* result );
00411 
00415 int IntersectRayZPlane( const Vector3F& origin, const Vector3F& dir,
00416                                                 float z,
00417                                                 Vector3F* intersect );
00418 
00423 int IntersectLinePlane( const Vector3F& a0, const Vector3F& a1,
00424                                                 const Plane& plane, 
00425                                                 Vector3F* out,
00426                                                 float* t );
00427 
00433 int IntersectLineLine(  const Vector3F& a0, const Vector3F& a1,
00434                                                 const Vector3F& b0, const Vector3F& b1,
00435                                                 Vector3F* out0, 
00436                                                 Vector3F* out1 );
00437 
00443 int IntersectLineLine(  const Vector2F& a0, const Vector2F& a1,
00444                                                 const Vector2F& b0, const Vector2F& b1,
00445                                                 Vector2F* out,
00446                                                 float* t0,
00447                                                 float* t1 );
00448 
00449 
00453 int PointInPolygon( const Vector2F& p, const Vector2F* vertex, int numVertex );
00454 
00458 float PointBetweenPoints( const Vector3F& p0, const Vector3F& p1, const Vector3F& pi );
00459 
00464 int Intersect3Planes( const Plane& p0, const Plane& p1, const Plane& p2, Vector3F* intersection );
00465 
00466 };      // namespace grinliz
00467 #endif
00468 

Generated on Fri Mar 23 19:36:21 2007 for Lilith3D by  doxygen 1.5.1-p1