Mat4.inl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. Original file from GamePlay3D: http://gameplay3d.org
  13. This file was modified to fit the cocos2d-x project
  14. */
  15. #include "math/Mat4.h"
  16. NS_CC_MATH_BEGIN
  17. /* --- --- --- --- Add by JJ start --- --- --- --- */
  18. inline Vec3 Mat4::MultiplyPoint3 (const Vec3& v) const
  19. {
  20. Vec3 res;
  21. res.x = m[0] * v.x + m[4] * v.y + m[ 8] * v.z + m[12];
  22. res.y = m[1] * v.x + m[5] * v.y + m[ 9] * v.z + m[13];
  23. res.z = m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14];
  24. return res;
  25. }
  26. inline void Mat4::MultiplyPoint3 (const Vec3& v, Vec3& output) const
  27. {
  28. output.x = m[0] * v.x + m[4] * v.y + m[ 8] * v.z + m[12];
  29. output.y = m[1] * v.x + m[5] * v.y + m[ 9] * v.z + m[13];
  30. output.z = m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14];
  31. }
  32. inline Vec3 Mat4::MultiplyVector3 (const Vec3& v) const
  33. {
  34. Vec3 res;
  35. res.x = m[0] * v.x + m[4] * v.y + m[ 8] * v.z;
  36. res.y = m[1] * v.x + m[5] * v.y + m[ 9] * v.z;
  37. res.z = m[2] * v.x + m[6] * v.y + m[10] * v.z;
  38. return res;
  39. }
  40. inline void Mat4::MultiplyVector3 (const Vec3& v, Vec3& output) const
  41. {
  42. output.x = m[0] * v.x + m[4] * v.y + m[ 8] * v.z;
  43. output.y = m[1] * v.x + m[5] * v.y + m[ 9] * v.z;
  44. output.z = m[2] * v.x + m[6] * v.y + m[10] * v.z;
  45. }
  46. inline void Mat4::SetPosition( const Vec3& v ) {
  47. Get(0,3) = v.x; Get(1,3) = v.y; Get(2,3) = v.z;
  48. }
  49. /* --- --- --- --- Add by JJ end --- --- --- --- */
  50. inline Mat4 Mat4::operator+(const Mat4& mat) const
  51. {
  52. Mat4 result(*this);
  53. result.add(mat);
  54. return result;
  55. }
  56. inline Mat4& Mat4::operator+=(const Mat4& mat)
  57. {
  58. add(mat);
  59. return *this;
  60. }
  61. inline Mat4 Mat4::operator-(const Mat4& mat) const
  62. {
  63. Mat4 result(*this);
  64. result.subtract(mat);
  65. return result;
  66. }
  67. inline Mat4& Mat4::operator-=(const Mat4& mat)
  68. {
  69. subtract(mat);
  70. return *this;
  71. }
  72. inline Mat4 Mat4::operator-() const
  73. {
  74. Mat4 mat(*this);
  75. mat.negate();
  76. return mat;
  77. }
  78. inline Mat4 Mat4::operator*(const Mat4& mat) const
  79. {
  80. Mat4 result(*this);
  81. result.multiply(mat);
  82. return result;
  83. }
  84. inline Mat4& Mat4::operator*=(const Mat4& mat)
  85. {
  86. multiply(mat);
  87. return *this;
  88. }
  89. inline Vec3& operator*=(Vec3& v, const Mat4& m)
  90. {
  91. m.transformVector(&v);
  92. return v;
  93. }
  94. inline Vec3 operator*(const Mat4& m, const Vec3& v)
  95. {
  96. Vec3 x;
  97. m.transformVector(v, &x);
  98. return x;
  99. }
  100. inline Vec4& operator*=(Vec4& v, const Mat4& m)
  101. {
  102. m.transformVector(&v);
  103. return v;
  104. }
  105. inline Vec4 operator*(const Mat4& m, const Vec4& v)
  106. {
  107. Vec4 x;
  108. m.transformVector(v, &x);
  109. return x;
  110. }
  111. NS_CC_MATH_END