home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / GLENGINE / vector3.h < prev    next >
C/C++ Source or Header  |  2000-08-16  |  4KB  |  161 lines

  1. #ifndef __OGL2_VECTOR3_H__
  2. #define __OGL2_VECTOR3_H__
  3.  
  4. #if !defined (__GNUG__) && !defined (__attribute__)
  5. #define __attribute__(foo) /* Ignore.  */
  6. #endif
  7.  
  8. #include <iostream>
  9.  
  10. extern "C++" {
  11.  
  12. template <class __type>
  13. struct Vector3 {
  14.   __type x;
  15.   __type y;
  16.   __type z;
  17.  
  18.   Vector3() :
  19.     x(0), y(0), z(0) {}
  20.   Vector3(__type _x, __type _y, __type _z) :
  21.     x(_x), y(_y), z(_z) {}
  22.  
  23.   Vector3<__type>& operator () (__type _x, __type _y, __type _z) {
  24.     x = _x; y = _y; z = _z;
  25.     return *this;
  26.   }
  27.   Vector3<__type>& operator () (const Vector3<__type>& b) {
  28.     x = b.x;
  29.     y = b.y;
  30.     z = b.z;
  31.     return *this;
  32.   }
  33.   Vector3<__type>& operator += (const Vector3<__type>& b) {
  34.     x += b.x;
  35.     y += b.y;
  36.     z += b.z;
  37.     return *this;
  38.   }
  39.   Vector3<__type>& operator -= (const Vector3<__type>& b) {
  40.     x -= b.x;
  41.     y -= b.y;
  42.     z -= b.z;
  43.     return *this;
  44.   }
  45.   Vector3<__type>& operator *= (const Vector3<__type>& b) {
  46.     x *= b.x;
  47.     y *= b.y;
  48.     z *= b.z;
  49.     return *this;
  50.   }
  51.   Vector3<__type>& operator *= (__type s) {
  52.     x *= s;
  53.     y *= s;
  54.     z *= s;
  55.     return *this;
  56.   }
  57.  
  58.   __type Length () {
  59.     return sqrt(x*x + y*y + z*z);
  60.   }
  61.   __type Len () {
  62.     return Length();
  63.   }
  64.  
  65.   void Unpack(__type* u) {
  66.     u[0] = x;
  67.     u[1] = y;
  68.     u[2] = z;
  69.   }
  70. };
  71.  
  72. template struct Vector3<GLfloat>;
  73. template struct Vector3<GLdouble>;
  74.  
  75. typedef Vector3<GLfloat> Vector3f;
  76. typedef Vector3<GLdouble> Vector3d;
  77.  
  78. template <class __type>
  79. ostream& operator << (ostream& s, const Vector3<__type>& a) {
  80.   return s << "(" << a.x << "," << a.y << "," << a.z << ")";
  81. }
  82.  
  83. template <class __type> inline Vector3<__type>
  84. operator + (const Vector3<__type>& a) __attribute__ ((const));
  85.  
  86. template <class __type> inline Vector3<__type>
  87. operator + (const Vector3<__type>& a) {
  88.   return a;
  89. }
  90.  
  91. template <class __type> inline Vector3<__type>
  92. operator - (const Vector3<__type>& a) __attribute__ ((const));
  93.  
  94. template <class __type> inline Vector3<__type>
  95. operator - (const Vector3<__type>& a) {
  96.   return Vector3<__type> (-a.x, -a.y, -a.z);
  97. }
  98.  
  99. template <class __type> inline Vector3<__type>
  100. operator + (const Vector3<__type>& a, const Vector3<__type>& b) __attribute__ ((const));
  101.  
  102. template <class __type> inline Vector3<__type>
  103. operator + (const Vector3<__type>& a, const Vector3<__type>& b) {
  104.   return Vector3<__type> (a.x+b.x, a.y+b.y, a.z+b.z);
  105. }
  106.  
  107. template <class __type> inline Vector3<__type>
  108. operator - (const Vector3<__type>& a, const Vector3<__type>& b) __attribute__ ((const));
  109.  
  110. template <class __type> inline Vector3<__type>
  111. operator - (const Vector3<__type>& a, const Vector3<__type>& b) {
  112.   return Vector3<__type> (a.x-b.x, a.y-b.y, a.z-b.z);
  113. }
  114.  
  115. template <class __type> inline Vector3<__type>
  116. operator * (const Vector3<__type>& a, __type s) __attribute__ ((const));
  117.  
  118. template <class __type> inline Vector3<__type>
  119. operator * (const Vector3<__type>& a, __type s)
  120. {
  121.   return Vector3<__type> (a.x*s, a.y*s, a.z*s);
  122. }
  123.  
  124. template <class __type> inline Vector3<__type>
  125. operator * (__type s, const Vector3<__type>& a) __attribute__ ((const));
  126.  
  127. template <class __type> inline Vector3<__type>
  128. operator * (__type s, const Vector3<__type>& a)
  129. {
  130.   return Vector3<__type> (s*a.x, s*a.y, s*a.z);
  131. }
  132.  
  133. template <class __type> inline Vector3<__type>
  134. operator * (const Vector3<__type>& a, const Vector3<__type>& b) __attribute__ ((const));
  135.  
  136. template <class __type> inline Vector3<__type>
  137. operator * (const Vector3<__type>& a, const Vector3<__type>& b) {
  138.   return Vector3<__type> (a.x*b.x, a.y*b.y, a.z*b.z);
  139. }
  140.  
  141. template <class __type> inline __type
  142. Dot (const Vector3<__type>& a, const Vector3<__type>& b) __attribute__ ((const));
  143.  
  144. template <class __type> inline __type
  145. Dot (const Vector3<__type>& a, const Vector3<__type>& b) {
  146.   return (a.x*b.x + a.y*b.y + a.z*b.z);
  147. }
  148.  
  149. template <class __type> inline Vector3<__type>
  150. Cross (const Vector3<__type>& u, const Vector3<__type>& v) __attribute__ ((const));
  151.  
  152. template <class __type> inline Vector3<__type>
  153. Cross (const Vector3<__type>& u, const Vector3<__type>& v) {
  154.   return Vector3<__type> ( u.y*v.z-u.z*v.y, u.z*v.x-u.x*v.z, u.x*v.y-u.y*v.x);
  155. }
  156.  
  157.  
  158. } // extern "C++"
  159.  
  160. #endif 
  161.