00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef GRINLIZ_GEOMETRY_INCLUDED
00027 #define GRINLIZ_GEOMETRY_INCLUDED
00028
00029 #include "glvector.h"
00030 #include "glrectangle.h"
00031 #include "glmatrix.h"
00032
00033 namespace grinliz {
00034
00035 template< class T>
00036 inline T DotProduct( const Vector2<T>& v0, const Vector2<T>& v1 )
00037 {
00038 return v0.x*v1.x + v0.y*v1.y;
00039 }
00040
00041 template< class T>
00042 inline void CrossProduct( const Vector3<T>& u, const Vector3<T>& v, Vector3<T>* w )
00043 {
00044 GLASSERT( &u != w && &v != w );
00045
00046 w->x = u.y * v.z - u.z * v.y;
00047 w->y = u.z * v.x - u.x * v.z;
00048 w->z = u.x * v.y - u.y * v.x;
00049 }
00050
00051 template< class T>
00052 inline T DotProduct( const Vector3<T>& v0, const Vector3<T>& v1 )
00053 {
00054 return v0.x*v1.x + v0.y*v1.y + v0.z*v1.z;
00055 }
00056
00057
00058 struct BoundingSphere
00059 {
00060 Vector3F point;
00061 float rad;
00062
00063 };
00064
00065
00071 struct Plane
00072 {
00074 static void CreatePlane( const Vector3F* vertices, Plane* plane );
00075
00076 Vector3F n;
00077 float d;
00078
00079 float Z( float x, float y ) { return -( d + (n.x * x) + (n.y * y) ) / ( n.z ); }
00080
00081 void Normalize()
00082 {
00083 float div = 1.0f / sqrtf( ( n.x * n.x ) + ( n.y * n.y ) + ( n.z * n.z ) );
00084 n.x *= div;
00085 n.y *= div;
00086 n.z *= div;
00087 d *= div;
00088 }
00089 };
00090
00091
00096 class Quaternion
00097 {
00098 public:
00099 Quaternion() { x = y = z = 0.0f; w = 1.0f; }
00100
00101 void Normalize();
00102
00103 const void ToAxisAngle( Vector3F* axis, float* angleOut );
00104 const void ToMatrix( Matrix4* matrix );
00105 void FromRotationMatrix( const Matrix4& matrix );
00106 void FromAxisAngle( const Vector3F& axis, float angle );
00107
00108 static void SLERP( const Quaternion& start, const Quaternion& end, float t, Quaternion* result );
00109 static void Multiply( const Quaternion& a, const Quaternion& b, Quaternion* result );
00110
00111 float x, y, z, w;
00112 };
00113
00114
00115 enum
00116 {
00117 REJECT = 0,
00118 INTERSECT,
00119 POSITIVE,
00120 NEGATIVE,
00121 INSIDE,
00122 };
00123
00124 enum
00125 {
00126
00127 YZ_PLANE,
00128 XZ_PLANE,
00129 XY_PLANE
00130 };
00131
00132
00154 int ComparePlaneAABB( const Plane&, const Rectangle3F& aabb );
00155
00156
00157
00158
00159
00163 inline int IntersectPlaneAABB( const Plane& p, const Rectangle3F& aabb )
00164 {
00165 return ( ComparePlaneAABB( p, aabb ) == INTERSECT ) ? INTERSECT : REJECT;
00166 }
00167
00168
00172 int IntersectRayTri( const Vector3F& point, const Vector3F& dir,
00173 const Vector3F& vert0, const Vector3F& vert1, const Vector3F& vert2,
00174 Vector3F* intersect );
00175
00180 int IntersectRayAAPlane( const Vector3F& point, const Vector3F& dir,
00181 int planeType, float planeLoc,
00182 Vector3F* intersect,
00183 float* t );
00184
00189 int IntersectRayAABB( const Vector3F& origin, const Vector3F& dir,
00190 const Rectangle3F& aabb,
00191 Vector3F* intersect,
00192 float* t );
00193
00194
00198 int IntersectRayZPlane( const Vector3F& origin, const Vector3F& dir,
00199 float z,
00200 Vector3F* intersect );
00201
00202 };
00203 #endif
00204