IcePoint.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains code for 3D vectors.
  4. * \file IcePoint.cpp
  5. * \author Pierre Terdiman
  6. * \date April, 4, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. /**
  11. * 3D point.
  12. *
  13. * The name is "Point" instead of "Vector" since a vector is N-dimensional, whereas a point is an implicit "vector of dimension 3".
  14. * So the choice was between "Point" and "Vector3", the first one looked better (IMHO).
  15. *
  16. * Some people, then, use a typedef to handle both points & vectors using the same class: typedef Point Vector3;
  17. * This is bad since it opens the door to a lot of confusion while reading the code. I know it may sounds weird but check this out:
  18. *
  19. * \code
  20. * Point P0,P1 = some 3D points;
  21. * Point Delta = P1 - P0;
  22. * \endcode
  23. *
  24. * This compiles fine, although you should have written:
  25. *
  26. * \code
  27. * Point P0,P1 = some 3D points;
  28. * Vector3 Delta = P1 - P0;
  29. * \endcode
  30. *
  31. * Subtle things like this are not caught at compile-time, and when you find one in the code, you never know whether it's a mistake
  32. * from the author or something you don't get.
  33. *
  34. * One way to handle it at compile-time would be to use different classes for Point & Vector3, only overloading operator "-" for vectors.
  35. * But then, you get a lot of redundant code in thoses classes, and basically it's really a lot of useless work.
  36. *
  37. * Another way would be to use homogeneous points: w=1 for points, w=0 for vectors. That's why the HPoint class exists. Now, to store
  38. * your model's vertices and in most cases, you really want to use Points to save ram.
  39. *
  40. * \class Point
  41. * \author Pierre Terdiman
  42. * \version 1.0
  43. */
  44. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46. #include "../Opcode.h"
  47. using namespace IceMaths;
  48. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. /**
  50. * Creates a positive unit random vector.
  51. * \return Self-reference
  52. */
  53. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. Point& Point::PositiveUnitRandomVector()
  55. {
  56. x = UnitRandomFloat();
  57. y = UnitRandomFloat();
  58. z = UnitRandomFloat();
  59. Normalize();
  60. return *this;
  61. }
  62. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. /**
  64. * Creates a unit random vector.
  65. * \return Self-reference
  66. */
  67. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. Point& Point::UnitRandomVector()
  69. {
  70. x = UnitRandomFloat() - 0.5f;
  71. y = UnitRandomFloat() - 0.5f;
  72. z = UnitRandomFloat() - 0.5f;
  73. Normalize();
  74. return *this;
  75. }
  76. // Cast operator
  77. // WARNING: not inlined
  78. Point::operator HPoint() const { return HPoint(x, y, z, 0.0f); }
  79. Point& Point::Refract(const Point& eye, const Point& n, float refractindex, Point& refracted)
  80. {
  81. // Point EyePt = eye position
  82. // Point p = current vertex
  83. // Point n = vertex normal
  84. // Point rv = refracted vector
  85. // Eye vector - doesn't need to be normalized
  86. Point Env;
  87. Env.x = eye.x - x;
  88. Env.y = eye.y - y;
  89. Env.z = eye.z - z;
  90. float NDotE = n|Env;
  91. float NDotN = n|n;
  92. NDotE /= refractindex;
  93. // Refracted vector
  94. refracted = n*NDotE - Env*NDotN;
  95. return *this;
  96. }
  97. Point& Point::ProjectToPlane(const Plane& p)
  98. {
  99. *this-= (p.d + (*this|p.n))*p.n;
  100. return *this;
  101. }
  102. void Point::ProjectToScreen(float halfrenderwidth, float halfrenderheight, const Matrix4x4& mat, HPoint& projected) const
  103. {
  104. projected = HPoint(x, y, z, 1.0f) * mat;
  105. projected.w = 1.0f / projected.w;
  106. projected.x*=projected.w;
  107. projected.y*=projected.w;
  108. projected.z*=projected.w;
  109. projected.x *= halfrenderwidth; projected.x += halfrenderwidth;
  110. projected.y *= -halfrenderheight; projected.y += halfrenderheight;
  111. }
  112. void Point::SetNotUsed()
  113. {
  114. // We use a particular integer pattern : 0xffffffff everywhere. This is a NAN.
  115. IR(x) = 0xffffffff;
  116. IR(y) = 0xffffffff;
  117. IR(z) = 0xffffffff;
  118. }
  119. BOOL Point::IsNotUsed() const
  120. {
  121. if(IR(x)!=0xffffffff) return FALSE;
  122. if(IR(y)!=0xffffffff) return FALSE;
  123. if(IR(z)!=0xffffffff) return FALSE;
  124. return TRUE;
  125. }
  126. Point& Point::Mult(const Matrix3x3& mat, const Point& a)
  127. {
  128. x = a.x * mat.m[0][0] + a.y * mat.m[0][1] + a.z * mat.m[0][2];
  129. y = a.x * mat.m[1][0] + a.y * mat.m[1][1] + a.z * mat.m[1][2];
  130. z = a.x * mat.m[2][0] + a.y * mat.m[2][1] + a.z * mat.m[2][2];
  131. return *this;
  132. }
  133. Point& Point::Mult2(const Matrix3x3& mat1, const Point& a1, const Matrix3x3& mat2, const Point& a2)
  134. {
  135. x = a1.x * mat1.m[0][0] + a1.y * mat1.m[0][1] + a1.z * mat1.m[0][2] + a2.x * mat2.m[0][0] + a2.y * mat2.m[0][1] + a2.z * mat2.m[0][2];
  136. y = a1.x * mat1.m[1][0] + a1.y * mat1.m[1][1] + a1.z * mat1.m[1][2] + a2.x * mat2.m[1][0] + a2.y * mat2.m[1][1] + a2.z * mat2.m[1][2];
  137. z = a1.x * mat1.m[2][0] + a1.y * mat1.m[2][1] + a1.z * mat1.m[2][2] + a2.x * mat2.m[2][0] + a2.y * mat2.m[2][1] + a2.z * mat2.m[2][2];
  138. return *this;
  139. }
  140. Point& Point::Mac(const Matrix3x3& mat, const Point& a)
  141. {
  142. x += a.x * mat.m[0][0] + a.y * mat.m[0][1] + a.z * mat.m[0][2];
  143. y += a.x * mat.m[1][0] + a.y * mat.m[1][1] + a.z * mat.m[1][2];
  144. z += a.x * mat.m[2][0] + a.y * mat.m[2][1] + a.z * mat.m[2][2];
  145. return *this;
  146. }
  147. Point& Point::TransMult(const Matrix3x3& mat, const Point& a)
  148. {
  149. x = a.x * mat.m[0][0] + a.y * mat.m[1][0] + a.z * mat.m[2][0];
  150. y = a.x * mat.m[0][1] + a.y * mat.m[1][1] + a.z * mat.m[2][1];
  151. z = a.x * mat.m[0][2] + a.y * mat.m[1][2] + a.z * mat.m[2][2];
  152. return *this;
  153. }
  154. Point& Point::Transform(const Point& r, const Matrix3x3& rotpos, const Point& linpos)
  155. {
  156. x = r.x * rotpos.m[0][0] + r.y * rotpos.m[0][1] + r.z * rotpos.m[0][2] + linpos.x;
  157. y = r.x * rotpos.m[1][0] + r.y * rotpos.m[1][1] + r.z * rotpos.m[1][2] + linpos.y;
  158. z = r.x * rotpos.m[2][0] + r.y * rotpos.m[2][1] + r.z * rotpos.m[2][2] + linpos.z;
  159. return *this;
  160. }
  161. Point& Point::InvTransform(const Point& r, const Matrix3x3& rotpos, const Point& linpos)
  162. {
  163. float sx = r.x - linpos.x;
  164. float sy = r.y - linpos.y;
  165. float sz = r.z - linpos.z;
  166. x = sx * rotpos.m[0][0] + sy * rotpos.m[1][0] + sz * rotpos.m[2][0];
  167. y = sx * rotpos.m[0][1] + sy * rotpos.m[1][1] + sz * rotpos.m[2][1];
  168. z = sx * rotpos.m[0][2] + sy * rotpos.m[1][2] + sz * rotpos.m[2][2];
  169. return *this;
  170. }