math.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef __MATH_H__
  6. #define __MATH_H__
  7. #define _USE_MATH_DEFINES
  8. #include <math.h>
  9. #include <string.h>
  10. inline void vec3Add(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  11. {
  12. _result[0] = _a[0] + _b[0];
  13. _result[1] = _a[1] + _b[1];
  14. _result[2] = _a[2] + _b[2];
  15. }
  16. inline void vec3Sub(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  17. {
  18. _result[0] = _a[0] - _b[0];
  19. _result[1] = _a[1] - _b[1];
  20. _result[2] = _a[2] - _b[2];
  21. }
  22. inline void vec3Mul(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  23. {
  24. _result[0] = _a[0] * _b[0];
  25. _result[1] = _a[1] * _b[1];
  26. _result[2] = _a[2] * _b[2];
  27. }
  28. inline float vec3Dot(const float* __restrict _a, const float* __restrict _b)
  29. {
  30. return _a[0]*_b[0] + _a[1]*_b[1] + _a[2]*_b[2];
  31. }
  32. inline void vec3Cross(float* __restrict _result, const float* __restrict _a, const float* __restrict _b)
  33. {
  34. _result[0] = _a[1]*_b[2] - _a[2]*_b[1];
  35. _result[1] = _a[2]*_b[0] - _a[0]*_b[2];
  36. _result[2] = _a[0]*_b[1] - _a[1]*_b[0];
  37. }
  38. inline void vec3Norm(float* __restrict _result, const float* __restrict _a)
  39. {
  40. float scale = 1.0f/sqrtf(vec3Dot(_a, _a) );
  41. _result[0] = _a[0] * scale;
  42. _result[1] = _a[1] * scale;
  43. _result[2] = _a[2] * scale;
  44. }
  45. inline void mtxIdentity(float* _result)
  46. {
  47. memset(_result, 0, sizeof(float)*16);
  48. _result[0] = _result[5] = _result[10] = _result[15] = 1.0f;
  49. }
  50. inline void mtxLookAt(float* __restrict _result, const float* __restrict _eye, const float* __restrict _at)
  51. {
  52. float tmp[4];
  53. vec3Sub(tmp, _at, _eye);
  54. float view[4];
  55. vec3Norm(view, tmp);
  56. float up[3] = { 0.0f, 1.0f, 0.0f };
  57. vec3Cross(tmp, up, view);
  58. float right[4];
  59. vec3Norm(right, tmp);
  60. vec3Cross(up, view, right);
  61. memset(_result, 0, sizeof(float)*16);
  62. _result[ 0] = right[0];
  63. _result[ 1] = up[0];
  64. _result[ 2] = view[0];
  65. _result[ 4] = right[1];
  66. _result[ 5] = up[1];
  67. _result[ 6] = view[1];
  68. _result[ 8] = right[2];
  69. _result[ 9] = up[2];
  70. _result[10] = view[2];
  71. _result[12] = -vec3Dot(right, _eye);
  72. _result[13] = -vec3Dot(up, _eye);
  73. _result[14] = -vec3Dot(view, _eye);
  74. _result[15] = 1.0f;
  75. }
  76. inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
  77. {
  78. float height = 1.0f/tanf(_fovy*( (float)M_PI/180.0f)*0.5f);
  79. float width = height * 1.0f/_aspect;
  80. float aa = _far/(_far-_near);
  81. float bb = -_near * aa;
  82. memset(_result, 0, sizeof(float)*16);
  83. _result[0] = width;
  84. _result[5] = height;
  85. _result[10] = aa;
  86. _result[11] = 1.0f;
  87. _result[14] = bb;
  88. }
  89. void mtxRotateXY(float* _result, float _ax, float _ay)
  90. {
  91. float sinx = sinf(_ax);
  92. float cosx = cosf(_ax);
  93. float siny = sinf(_ay);
  94. float cosy = cosf(_ay);
  95. memset(_result, 0, sizeof(float)*16);
  96. _result[ 0] = cosy;
  97. _result[ 2] = -siny;
  98. _result[ 4] = -sinx * siny;
  99. _result[ 5] = cosx;
  100. _result[ 6] = -sinx * cosy;
  101. _result[ 8] = cosx * siny;
  102. _result[ 9] = sinx;
  103. _result[10] = cosx * cosy;
  104. _result[15] = 1.0f;
  105. }
  106. #endif // __MATH_H__