PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/DXBase/primitives.h

http://directxwinrt.codeplex.com
C Header | 434 lines | 373 code | 53 blank | 8 comment | 64 complexity | e97bab96300e24560bc94da65a51c453 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #pragma once
  2. #include <math.h>
  3. #include <memory>
  4. #ifndef PI_F
  5. #define PI_F (3.1415927f)
  6. #endif
  7. #ifndef DEG2RAD
  8. #define DEG2RAD(deg) (PI_F * (deg) / 180.0f)
  9. #endif
  10. #ifndef RAD2DEG
  11. #define RAD2DEG(rad) (180.0f * (rad) / PI_F)
  12. #endif
  13. namespace DXBase
  14. {
  15. // --- (WinRT) structs
  16. public value struct float2
  17. {
  18. float x,y;
  19. };
  20. public value struct float3
  21. {
  22. float x,y,z;
  23. };
  24. public value struct float4
  25. {
  26. float x,y,z,w;
  27. };
  28. public value struct quaternion
  29. {
  30. float x,y,z,w;
  31. };
  32. public value struct float4x4
  33. {
  34. float m00; float m01; float m02; float m03;
  35. float m10; float m11; float m12; float m13;
  36. float m20; float m21; float m22; float m23;
  37. float m30; float m31; float m32; float m33;
  38. };
  39. public value struct float3x2
  40. {
  41. float m11, m12;
  42. float m21, m22;
  43. float m31, m32;
  44. };
  45. public value struct USize { uint32 width; uint32 height; };
  46. public value struct Range
  47. {
  48. uint32 Start;
  49. uint32 Length;
  50. };
  51. public value struct box3d { float x, y, z, width, height, depth; };
  52. // pack all color in a single 32bit struct matching DXGI_FORMAT_B8G8R8A8_UNORM
  53. public value struct color32
  54. {
  55. byte b,g,r,a;
  56. };
  57. // --- functions to work with that stuff
  58. inline bool operator ==(const color32& s1, const color32& s2)
  59. {
  60. uint32 *p1 = (uint32*)&s1, *p2 = (uint32*)&s2;
  61. return *p1 == *p2;
  62. }
  63. inline bool operator ==(const USize& s1, const USize& s2) { return s1.width == s2.width && s1.height == s2.height; }
  64. inline bool operator !=(const USize& s1, const USize& s2) { return s1.width != s2.width || s1.height != s2.height; }
  65. inline bool operator==(const box3d& a, const box3d& b)
  66. {
  67. return
  68. a.x == b.x && a.width == b.width
  69. && a.y == b.y && a.height == b.height
  70. && a.z && b.z && a.depth == b.depth
  71. ;
  72. }
  73. float2 vector2(float x, float y);
  74. float3 vector3(float x, float y, float z);
  75. float4 vector4(float x, float y, float z, float w = 1);
  76. float4 vector4(float3& v, float w);
  77. inline float& at(float2& v, unsigned int index) { return ((float*)(&v))[index]; }
  78. inline float& at(float3& v, unsigned int index) { return ((float*)(&v))[index]; }
  79. inline float& at(float4& v, unsigned int index) { return ((float*)(&v))[index]; }
  80. inline float dot(float2& a, float2& b) { return a.x * b.x + a.y * b.y; }
  81. inline float dot(float3& a, float3& b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
  82. inline float dot(float4& a, float4& b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w + b.w; }
  83. inline float length(float2& a) { return sqrt(a.x * a.x + a.y * a.y); }
  84. inline float length(float3& a) { return sqrt(a.x * a.x + a.y * a.y + a.z * a.z); }
  85. inline float length(float4& a) { return sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w); }
  86. inline float3 cross(float3& a, float3& b) { return vector3((a.y*b.z)-(a.z*b.y),(a.z*b.x)-(a.x*b.z),(a.x*b.y)-(a.y*b.x)); }
  87. inline float2 operator+(float2& a, float2& b) { return vector2(a.x + b.x, a.y + b.y); }
  88. inline float2 operator-(float2& a, float2& b) { return vector2(a.x - b.x, a.y - b.y); }
  89. inline float2 operator-(float2& a) { return vector2(-a.x, -a.y); }
  90. inline float2 operator*(float2& a, float2& b) { return vector2(a.x * b.x, a.y * b.y); }
  91. inline float2 operator*(float2& a, float s) { return vector2(a.x * s, a.y * s); }
  92. inline float2 operator*(float s, float2& a) { return a * s; }
  93. inline float2 operator/(float2& a, float s) { return vector2(a.x / s, a.y / s); }
  94. inline float3 operator+(float3& a, float3& b) { return vector3(a.x + b.x, a.y + b.y, a.z + b.z); }
  95. inline float3 operator-(float3& a, float3& b) { return vector3(a.x - b.x, a.y - b.y, a.z - b.z); }
  96. inline float3 operator-(float3& a) { return vector3(-a.x, -a.y, -a.z); }
  97. inline float3 operator*(float3& a, float3& b) { return vector3(a.x * b.x, a.y * b.y, a.z * b.z); }
  98. inline float3 operator*(float3& a, float s) { return vector3(a.x * s, a.y * s, a.z * s); }
  99. inline float3 operator*(float s, float3& a) { return a * s; }
  100. inline float3 operator/(float3& a, float s) { return vector3(a.x / s, a.y / s, a.z / s); }
  101. inline float4 operator+(float4& a, float4& b) { return vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); }
  102. inline float4 operator-(float4& a, float4& b) { return vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); }
  103. inline float4 operator-(float4& a) { return vector4(-a.x, -a.y, -a.z, -a.w); }
  104. inline float4 operator*(float4& a, float4& b) { return vector4(a.x * b.x, a.y * b.y, a.w * b.z, a.w * b.w); }
  105. inline float4 operator*(float4& a, float s) { return vector4(a.x * s, a.y * s, a.z * s, a.w * s); }
  106. inline float4 operator*(float s, float4& a) { return a * s; }
  107. inline float4 operator/(float4& a, float s) { return vector4(a.x / s, a.y / s, a.z / s, a.w / s); }
  108. inline float2 normalize(float2& a) { return a / length(a); }
  109. inline float3 normalize(float3& a) { return a / length(a); }
  110. inline float4 normalize(float4& a) { return a / length(a); }
  111. inline bool operator==(const float2& a, const float2& b) { return a.x == b.x && a.y == b.y; }
  112. inline bool operator==(const float3& a, const float3& b) { return a.x == b.x && a.y == b.y && a.z == b.z; }
  113. inline bool operator==(const float4& a, const float4& b) { return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; }
  114. inline bool operator==(const quaternion& a, const quaternion& b)
  115. {
  116. int sig = a.w * b.w > 0 ? 1 : -1;
  117. return a.x == sig * b.x && a.y == sig * b.y && a.z == sig * b.z && a.w == sig * b.w;
  118. }
  119. float2 lerp(const float2& a, const float2& b, float t);
  120. float3 lerp(const float3& a, const float3& b, float t);
  121. float4 lerp(const float4& a, const float4& b, float t);
  122. // Template Matrix Operations
  123. inline float& at(float4x4& a, unsigned int row, unsigned col) { return ((float*)&a)[row*4+col]; }
  124. float4x4 matrix4x4(float value = 0);
  125. float4x4 matrix4x4(float3& vx, float3& vy, float3& vz);
  126. float4x4 matrix4x4(float3& vx, float3& vy, float3& vz, float3& origin);
  127. float4x4 matrix4x4(
  128. float i11, float i12, float i13, float i14,
  129. float i21, float i22, float i23, float i24,
  130. float i31, float i32, float i33, float i34,
  131. float i41, float i42, float i43, float i44
  132. );
  133. float4x4 transpose(float4x4& m);
  134. float4x4 mul(float4x4& m1,float4x4& m2);
  135. float4x4 identity();
  136. float4x4 translation(float x, float y, float z);
  137. float4x4 translation(float3& v);
  138. float4x4 scale(float x, float y, float z);
  139. float4x4 scale(float3& v);
  140. float4x4 rotationX(float degreeX);
  141. float4x4 rotationY(float degreeY);
  142. float4x4 rotationZ(float degreeZ);
  143. float4x4 rotation(float4& rotation);
  144. float4x4 rotation(float3& v0, float3& v1);
  145. // pass v0, v1, get axis and degree
  146. float4 getrotation(float3& v0, float3& v1);
  147. inline float4x4 operator*(float4x4& a,float4x4& b) { return mul(a,b); }
  148. float4 operator*(float4x4& a, float4& b);
  149. float4x4 transpose(float4x4& m);
  150. float determinant(const float4x4& m);
  151. float4x4 invert(const float4x4& m);
  152. bool operator==(const float4x4& a, const float4x4& b);
  153. bool decompose(float4x4& m, float3& translation, float4& rot, float3& scale);
  154. bool decompose(float4x4& m, float3& translation, quaternion& q, float3& scale);
  155. quaternion getquaternion(float3& v0, float3& v1);
  156. inline quaternion cquaternion(float x, float y, float z, float w)
  157. {
  158. quaternion q;
  159. q.x = x;
  160. q.y = y;
  161. q.z = z;
  162. q.w = w;
  163. return q;
  164. }
  165. inline quaternion cquaternion(float3 v, float w)
  166. {
  167. quaternion q;
  168. q.x = v.x;
  169. q.y = v.y;
  170. q.z = v.z;
  171. q.w = w;
  172. return q;
  173. }
  174. //http://www.idevgames.com/articles/quaternions
  175. //http://willperone.net/Code/quaternion.php
  176. class QuaternionOp
  177. {
  178. public:
  179. static float4x4 Rotation(quaternion q);
  180. static quaternion Extract(float4x4 m);
  181. static quaternion Normalize(quaternion q);
  182. static quaternion FromRotation(float4 rot);
  183. static float4 ToRotation(quaternion q);
  184. static quaternion Invert(quaternion q);
  185. static quaternion Mul(quaternion q1, quaternion q2);
  186. static float3 Mul(quaternion q, float3 p);
  187. static quaternion Slerp(quaternion q1, quaternion q2, float alpha);
  188. };
  189. template <class T>
  190. struct SquareMatrix
  191. {
  192. int size;
  193. T *data; // had strange trouble with std::vector<float> copy constructor
  194. SquareMatrix(int n) : size(n)
  195. {
  196. data = new T[n * n];
  197. memset(data, 0, n * n * sizeof(T));
  198. }
  199. SquareMatrix(SquareMatrix<T>& other)
  200. : size(other.size), data(other.data)
  201. {
  202. other.size = 0;
  203. other.data = NULL;
  204. }
  205. ~SquareMatrix()
  206. {
  207. if (data)
  208. delete[] data;
  209. data = NULL;
  210. }
  211. T& at(int i, int j) const { return data[i * size + j]; };
  212. void Transpose()
  213. {
  214. for(int i=0; i<size-1; i++)
  215. for(int j=i+1; j<size; j++)
  216. {
  217. auto tmp = at(i, j);
  218. at(i,j) = at(j,i);
  219. at(j,i) = tmp;
  220. }
  221. }
  222. SquareMatrix<T> Minor(int i, int j) const
  223. {
  224. if (size == 1)
  225. return *const_cast<SquareMatrix<T>*>(this);
  226. SquareMatrix<T> result(size - 1);
  227. for (int iSrc = 0; iSrc < size; iSrc++)
  228. for (int jSrc = 0; jSrc < size; jSrc++)
  229. {
  230. if (iSrc == i) continue;
  231. if (jSrc == j) continue;
  232. int iDst = iSrc<i ? iSrc : iSrc-1;
  233. int jDst = jSrc<j ? jSrc : jSrc-1;
  234. result.at(iDst,jDst) = at(iSrc,jSrc);
  235. }
  236. return result;
  237. }
  238. T Determinant() const
  239. {
  240. if(size == 1)
  241. return data[0];
  242. float result = 0;
  243. for (int i=0; i<size; i++)
  244. {
  245. auto minor = Minor(0, i);
  246. auto det = minor.Determinant();
  247. result += (i%2==0 ? 1 : -1) * at(0,i) * det;
  248. }
  249. return result;
  250. }
  251. SquareMatrix<T> Adjoint() const
  252. {
  253. SquareMatrix<T> result(size);
  254. for (int i = 0; i < size; i++)
  255. for (int j = 0; j < size; j++)
  256. {
  257. auto ij = i+j;
  258. result.at(i,j) = (ij % 2 == 0 ? 1 : -1) * Minor(i,j).Determinant();
  259. }
  260. return result;
  261. }
  262. SquareMatrix<T> Invert() const
  263. {
  264. SquareMatrix<T> result(size);
  265. T det = 0;
  266. for (int i=0; i<size; i++)
  267. {
  268. auto minor = Minor(0, i);
  269. auto num = (i % 2 == 0 ? 1 : -1) * minor.Determinant();
  270. det += at(0,i) * num;
  271. result.at(i,0) = num;
  272. }
  273. if (!det)
  274. return result;
  275. for (int i=0; i<size; i++)
  276. result.at(i,0) = result.at(i,0) / det;
  277. for (int i = 1; i < size; i++)
  278. for (int j = 0; j < size; j++)
  279. {
  280. auto ij = i+j;
  281. result.at(j,i) = (ij % 2 == 0 ? 1 : -1) * Minor(i,j).Determinant() / det;
  282. }
  283. return result;
  284. }
  285. };
  286. float3x2 matrix2d(float m11, float m12, float m21, float m22, float m31, float m32);
  287. float3x2 identity2d();
  288. float3x2 rotate2d(float degrees, const float2& center);
  289. inline float3x2 rotate2d(float degrees) { return rotate2d(degrees, vector2(0, 0)); }
  290. float3x2 scale2d(float sx, float sy, const float2& center);
  291. inline float3x2 scale2d(const float2& scale) { return scale2d(scale.x, scale.y, vector2(0, 0)); }
  292. inline float3x2 scale2d(const float2& scale, const float2& center) { return scale2d(scale.x, scale.y, center); }
  293. float3x2 skew2d(float degreesX, float degreesY, const float2& center);
  294. inline float3x2 skew2d(float degreesX, float degreesY) { return skew2d(degreesX, degreesY, vector2(0, 0)); }
  295. float3x2 translate2d(float dx, float dy);
  296. inline float3x2 translate2d(const float2& vector) { translate2d(vector.x, vector.y); }
  297. float3x2 mul(const float3x2& m1, const float3x2& m2);
  298. float2 mul(const float3x2& m1, const float2& p);
  299. inline float3x2 operator * (const float3x2& a, const float3x2& b) { return mul(a, b); }
  300. inline float2 operator * (const float3x2& a, const float2& b) { return mul(a, b); }
  301. float3x2 invert(const float3x2& m);
  302. //----- make shortcut methods (to turn into DirectX structs)
  303. inline D2D1_MATRIX_3X2_F makeD2D(const float3x2& m)
  304. {
  305. D2D1_MATRIX_3X2_F result = { m.m11, m.m12, m.m21, m.m22, m.m31, m.m32 };
  306. return result;
  307. }
  308. inline D2D1_POINT_2F makeD2D(const float2& p)
  309. {
  310. D2D1_POINT_2F result = { p.x, p.y };
  311. return result;
  312. }
  313. inline D2D1_POINT_2F makeD2D(const Windows::Foundation::Point& p)
  314. {
  315. D2D1_POINT_2F result = { p.X, p.Y };
  316. return result;
  317. }
  318. inline D2D1_RECT_F makeD2D(const Windows::Foundation::Rect& rect)
  319. {
  320. D2D1_RECT_F result = { rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height };
  321. return result;
  322. }
  323. inline D2D1_SIZE_F makeD2D(const Windows::Foundation::Size& size)
  324. {
  325. D2D1_SIZE_F result = { size.Width, size.Height };
  326. return result;
  327. }
  328. inline D2D1_COLOR_F makeD2D(const Windows::UI::Color& c)
  329. {
  330. D2D1_COLOR_F result = { c.R / 255.0f, c.G / 255.0f, c.B / 255.0f, c.A / 255.0f };
  331. return result;
  332. }
  333. inline void makeD3D(const Windows::UI::Color& c, float values[4])
  334. {
  335. values[0] = c.R / 255.0f;
  336. values[1] = c.G / 255.0f;
  337. values[2] = c.B / 255.0f;
  338. values[3] = c.A / 255.0f;
  339. }
  340. inline D2D1_MATRIX_4X4_F makeD2D(const float4x4& m)
  341. {
  342. D2D1_MATRIX_4X4_F result =
  343. {
  344. m.m00, m.m01, m.m02, m.m03,
  345. m.m10, m.m11, m.m12, m.m13,
  346. m.m20, m.m21, m.m22, m.m23,
  347. m.m30, m.m31, m.m32, m.m33
  348. };
  349. return result;
  350. }
  351. inline USize makeUSize(uint32 w, uint32 h)
  352. {
  353. USize result = { w, h };
  354. return result;
  355. }
  356. inline D2D_SIZE_U makeD2D(USize u)
  357. {
  358. D2D_SIZE_U result = { u.width, u.height };
  359. return result;
  360. }
  361. inline DWRITE_TEXT_RANGE makeDWrite(const Range& value)
  362. {
  363. DWRITE_TEXT_RANGE result = { value.Start, value.Length };
  364. return result;
  365. }
  366. inline Range fromDWrite(const DWRITE_TEXT_RANGE& value)
  367. {
  368. Range result = { value.startPosition, value.length };
  369. return result;
  370. }
  371. inline D3D11_VIEWPORT makeD3D(const box3d& box)
  372. {
  373. D3D11_VIEWPORT result = { box.x, box.y, box.width, box.height, box.z, box.z + box.depth };
  374. return result;
  375. }
  376. inline box3d fromD3D(const D3D11_VIEWPORT& box)
  377. {
  378. box3d result = { box.TopLeftX, box.TopLeftY, box.MinDepth, box.Width, box.Height, box.MaxDepth - box.MinDepth };
  379. return result;
  380. }
  381. }