CVector.h
上传用户:liujun12jf
上传日期:2022-07-12
资源大小:638k
文件大小:10k
源码类别:

OpenGL

开发平台:

Visual C++

  1. /*
  2.    Class Name:
  3.       CVector3.
  4.    Created by:
  5.       Allen Sherrod (Programming Ace of www.UltimateGameProgramming.com).
  6.    Description:
  7.       This class represents a 3D point for a vertex's x, y, z.
  8. */
  9. #ifndef _CVECTOR_H_
  10. #define _CVECTOR_H_
  11. #include <math.h>
  12. #include <stdlib.h>
  13. #ifndef RAND01
  14. #define RAND01
  15. inline float rand01() { return ( (float)rand() ) / 32767; }
  16. #endif
  17. /*******************************************************************************
  18. * VECTOR
  19. *******************************************************************************/
  20. class CVector
  21. {
  22.    public:
  23.       CVector();                                        // Constructor.
  24.       CVector(float Q);              // Overloaded constructor.
  25.       CVector(float X, float Y, float Z);               // Overloaded constructor.
  26.       CVector(const CVector &v);                       // Another overloaded constructor.
  27.       void operator=(CVector v);                        // Overloaded = sign.
  28.       void operator=(float f);                           // Overloaded = sign.
  29.       CVector operator-(CVector v);                    // Overloaded - sign.
  30.       CVector operator+(CVector v);                    // Overloaded + sign.
  31.       CVector operator*(CVector v);                    // Overloaded * sign.
  32.       CVector operator/(CVector v);                    // Overloaded / sign.
  33.       CVector operator+(float f);                       // Overloaded + sign.
  34.       CVector operator-(float f);                       // Overloaded - sign.
  35.       CVector operator*(float f);                       // Overloaded * sign.
  36.       CVector operator/(float f);                       // Overloaded / sign.
  37.       void operator +=(CVector v);                      // Overloaded += sign.
  38.       void operator -=(CVector v);                      // Overloaded -= sign.
  39.       void operator *=(CVector v);                      // Overloaded *= sign.
  40.       void operator /=(CVector v);                      // Overloaded /= sign.
  41.       void operator +=(float f);                         // Overloaded += sign.
  42.       void operator -=(float f);                         // Overloaded -= sign.
  43.       void operator *=(float f);                         // Overloaded *= sign.
  44.       void operator /=(float f);                         // Overloaded /= sign.
  45.       bool operator >(CVector v);                      // Overloaded > sign.
  46.       bool operator <(CVector v);                      // Overloaded < sign.
  47.       bool operator >(float f);                      // Overloaded > sign.
  48.       bool operator <(float f);                      // Overloaded < sign.
  49.       bool operator ==(CVector v);                      // Overloaded == sign.
  50.       bool operator !=(CVector v);                      // Overloaded != sign.
  51.       void CrossProduct(CVector v1, CVector v2);       // Stores cross product of v1, v2.
  52.       void CrossProduct3(CVector v1, CVector v2,
  53.                          CVector v3);                   // Stores cross product of v1, v2, v3.
  54.       float DotProduct3(CVector v1);                    // Dot 3 on v1 and this.
  55.       float GetLength();                                 // Return this objects length.
  56.       float GetLength2();                                 // Return this objects length, but not sqrt-ed.
  57.       void Normal();                                     // Normalize this vector.
  58.       CVector Normalized();                          // Returns this vector normalized.
  59.       void Normalize(CVector Triangle[]);               // Find the normal of a triangle.
  60.       void Randomize();
  61.       /*
  62.       void CalculateTangentVector(CVector Point1,
  63.                                   CVector Point2,
  64.                                   CVector Point3,
  65.                                   CVector NormalOfA,
  66.                                   CTexCoord P1Tex,
  67.                                   CTexCoord P2Tex,
  68.                                   CTexCoord P3Tex);      // Cacluate a tagent vector & store here.
  69.       */
  70.       void ExtendVertexPos(CVector lightPos,            // Extend point based on light position
  71.                            float Extend);                // and currend point, saves here.
  72.       void ExtendVertexPos(CVector currentVertex,       // Takes current point and source and
  73.                            CVector lightPos,            // extend and saves here.
  74.                            float Extend);
  75.       CVector GetRotatedX(double angle);                // Rotate along x.
  76.       CVector GetRotatedY(double angle);                // Rotate alone y.
  77.       CVector GetRotatedZ(double angle);                // Rotate along z.
  78.       CVector GetRotatedAxis(double angle, CVector axis);// Rotate along an axis.
  79.       void CalculateBinormalVector(CVector tangent,
  80.                                    CVector normal);     // Calcluate a binormal.
  81.       void ClampTo01();                                  // Clamp this vector between 0 and 1.
  82.       void Set(CVector v)
  83.       { x = v.x; y = v.y; z = v.z; }
  84.       void Set(float X, float Y, float Z)
  85. { x=X; y=Y; z=Z; }
  86.       void Set(float X, float Y, float Z, float W)
  87. { x=X; y=Y; z=Z; w=W; }
  88. void NoiseAngle()
  89. {
  90. x += ((float)(rand()%50)-25)/50;
  91. y += ((float)(rand()%50)-25)/50;
  92. z += ((float)(rand()%50)-25)/50;
  93. }
  94.       float x, y, z;                                  // vertex's x, y, and z info.
  95. float w; // just so it work with some physic functions, etc.. and for matrix which must be array of 16..
  96. float& operator[] (int i)
  97. {
  98. return (&x)[i];
  99. }
  100. const float& operator[] (int i) const
  101. {
  102. return (&x)[i];
  103. }
  104. static CVector RandomizeAngle( CVector v, float f );
  105.   /*******************************************************/
  106.  /* this was copied from vector3f class by Kevin Harris */
  107. /* (some part of it was deleted)                       */
  108. //-----------------------------------------------------------------------------
  109. //           Name: vector3f.cpp
  110. //         Author: Kevin Harris
  111. //  Last Modified: 09/09/02
  112. //    Description: Source file for OpenGL compatible utility class for a 3  
  113. //                 component vector of floats.
  114. //
  115. //  NOTE: This class has been left unoptimized for readability.
  116. //-----------------------------------------------------------------------------
  117. static float dotProduct( const CVector &v1,  const CVector &v2 )
  118. {
  119. return( v1.x * v2.x + v1.y * v2.y + v1.z * v2.z  );
  120. }
  121. /*
  122. CVector operator + ( const CVector &other )
  123. {
  124.     CVector vResult(0.0f, 0.0f, 0.0f);
  125.     vResult.x = x + other.x;
  126.     vResult.y = y + other.y;
  127.     vResult.z = z + other.z;
  128.     return vResult;
  129. }
  130. CVector operator + ( void ) const
  131. {
  132.     return *this;
  133. }
  134. CVector operator - ( const CVector &other )
  135. {
  136.     CVector vResult(0.0f, 0.0f, 0.0f);
  137.     vResult.x = x - other.x;
  138.     vResult.y = y - other.y;
  139.     vResult.z = z - other.z;
  140.     return vResult;
  141. }
  142. CVector operator - ( void ) const
  143. {
  144.     CVector vResult(-x, -y, -z);
  145.     return vResult;
  146. }
  147. CVector operator * ( const CVector &other )
  148. {
  149.     CVector vResult(0.0f, 0.0f, 0.0f);
  150.     vResult.x = x * other.x;
  151.     vResult.y = y * other.y;
  152.     vResult.z = z * other.z;
  153.     return vResult;
  154. }
  155. CVector operator * ( const float scalar )
  156. {
  157.     CVector vResult(0.0f, 0.0f, 0.0f);
  158.     vResult.x = x * scalar;
  159.     vResult.y = y * scalar;
  160.     vResult.z = z * scalar;
  161.     return vResult;
  162. }
  163. CVector operator * ( const float scalar, const CVector &other )
  164. {
  165.     CVector vResult(0.0f, 0.0f, 0.0f);
  166.     vResult.x = other.x * scalar;
  167.     vResult.y = other.y * scalar;
  168.     vResult.z = other.z * scalar;
  169.     return vResult;
  170. }
  171. CVector operator / ( const CVector &other )
  172. {
  173.     CVector vResult(0.0f, 0.0f, 0.0f);
  174.     vResult.x = x / other.x;
  175.     vResult.y = y / other.y;
  176.     vResult.z = z / other.z;
  177.     return vResult;
  178. }
  179. CVector& operator = ( const CVector &other )
  180. {
  181. x = other.x;
  182. y = other.y;
  183. z = other.z;
  184.     return *this;
  185. }
  186. CVector& operator += ( const CVector &other )
  187. {
  188.     x += other.x;
  189.     y += other.y;
  190.     z += other.z;
  191.     return *this;
  192. }
  193. CVector& operator -= ( const CVector &other )
  194. {
  195.     x -= other.x;
  196.     y -= other.y;
  197.     z -= other.z;
  198.     return *this;
  199. }
  200. //
  201. // Static utility methods...
  202. //
  203. static float distance( const CVector &v1,  const CVector &v2  )
  204. {
  205. float dx = v1.x - v2.x;
  206. float dy = v1.y - v2.y;
  207. float dz = v1.z - v2.z;
  208. return (float)sqrt( dx * dx + dy * dy + dz * dz );
  209. }
  210. static CVector crossProduct( const CVector &v1,  const CVector &v2 )
  211. {
  212. CVector vCrossProduct;
  213.     vCrossProduct.x = v1.y * v2.z - v1.z * v2.y;
  214.     vCrossProduct.y = v1.z * v2.x - v1.x * v2.z;
  215.     vCrossProduct.z = v1.x * v2.y - v1.y * v2.x;
  216. return vCrossProduct;
  217. }
  218. */
  219. /*****************************************/
  220. };
  221. class CMatrix
  222. {
  223. public:
  224. CMatrix()
  225. {
  226. Reset();
  227. }
  228. void Reset()
  229. {
  230. m_front.Set(1, 0, 0, 0);
  231. m_up.Set  (0, 1, 0, 0);
  232. m_right.Set(0, 0, 1, 0);
  233. m_posit.Set(0, 0, 0, 1);
  234. }
  235. void ResetRotation()
  236. {
  237. m_front.Set(1, 0, 0, 0);
  238. m_up.Set  (0, 1, 0, 0);
  239. m_right.Set(0, 0, 1, 0);
  240. }
  241. void Randomize()
  242. {
  243. m_front.Randomize();
  244. m_up.Randomize();
  245. m_right.Randomize();
  246. m_posit.Randomize();
  247. }
  248. void ResetW()
  249. {
  250. m_front[3] = 0;
  251. m_up[3] = 0;
  252. m_right[3] = 0;
  253. m_posit[3] = 1;
  254. }
  255. CVector m_front;
  256. CVector m_up;
  257. CVector m_right;
  258. CVector m_posit;
  259. CVector& operator[] (int i)
  260. {
  261. return (&m_front)[i];
  262. }
  263. const CVector& operator[] (int i) const
  264. {
  265. return (&m_front)[i];
  266. }
  267. };
  268. #endif
  269. // Copyright September 2003
  270. // All Rights Reserved!
  271. // Allen Sherrod
  272. // ProgrammingAce@UltimateGameProgramming.com
  273. // www.UltimateGameProgramming.com