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

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