lua_matrix4x4.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "matrix4x4.h"
  24. #include "vector3.h"
  25. #include "lua_stack.h"
  26. #include "lua_environment.h"
  27. namespace crown
  28. {
  29. using namespace matrix4x4;
  30. static int matrix4x4_new(lua_State* L)
  31. {
  32. LuaStack stack(L);
  33. float m0 = stack.get_float(1);
  34. float m1 = stack.get_float(2);
  35. float m2 = stack.get_float(3);
  36. float m3 = stack.get_float(4);
  37. float m4 = stack.get_float(5);
  38. float m5 = stack.get_float(6);
  39. float m6 = stack.get_float(7);
  40. float m7 = stack.get_float(8);
  41. float m8 = stack.get_float(9);
  42. float m9 = stack.get_float(10);
  43. float m10 = stack.get_float(11);
  44. float m11 = stack.get_float(12);
  45. float m12 = stack.get_float(13);
  46. float m13 = stack.get_float(14);
  47. float m14 = stack.get_float(15);
  48. float m15 = stack.get_float(16);
  49. stack.push_matrix4x4(Matrix4x4(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15));
  50. return 1;
  51. }
  52. static int matrix4x4_ctor(lua_State* L)
  53. {
  54. LuaStack stack(L);
  55. stack.remove(1); // Remove table
  56. return matrix4x4_new(L);
  57. }
  58. static int matrix4x4_from_quaternion(lua_State* L)
  59. {
  60. LuaStack stack(L);
  61. stack.push_matrix4x4(Matrix4x4(stack.get_quaternion(1), Vector3(0, 0, 0)));
  62. return 1;
  63. }
  64. static int matrix4x4_from_translation(lua_State* L)
  65. {
  66. LuaStack stack(L);
  67. stack.push_matrix4x4(Matrix4x4(quaternion::IDENTITY, stack.get_vector3(1)));
  68. return 1;
  69. }
  70. static int matrix4x4_from_quaternion_translation(lua_State* L)
  71. {
  72. LuaStack stack(L);
  73. stack.push_matrix4x4(Matrix4x4(stack.get_quaternion(1), stack.get_vector3(2)));
  74. return 1;
  75. }
  76. static int matrix4x4_from_axes(lua_State* L)
  77. {
  78. LuaStack stack(L);
  79. stack.push_matrix4x4(Matrix4x4(stack.get_vector3(1), stack.get_vector3(2), stack.get_vector3(3), stack.get_vector3(4)));
  80. return 1;
  81. }
  82. static int matrix4x4_add(lua_State* L)
  83. {
  84. LuaStack stack(L);
  85. stack.push_matrix4x4(stack.get_matrix4x4(1) + stack.get_matrix4x4(2));
  86. return 1;
  87. }
  88. static int matrix4x4_subtract(lua_State* L)
  89. {
  90. LuaStack stack(L);
  91. stack.push_matrix4x4(stack.get_matrix4x4(1) - stack.get_matrix4x4(2));
  92. return 1;
  93. }
  94. static int matrix4x4_multiply(lua_State* L)
  95. {
  96. LuaStack stack(L);
  97. stack.push_matrix4x4(stack.get_matrix4x4(1) * stack.get_matrix4x4(2));
  98. return 1;
  99. }
  100. static int matrix4x4_transpose(lua_State* L)
  101. {
  102. LuaStack stack(L);
  103. stack.push_matrix4x4(transpose(stack.get_matrix4x4(1)));
  104. return 1;
  105. }
  106. static int matrix4x4_determinant(lua_State* L)
  107. {
  108. LuaStack stack(L);
  109. stack.push_float(determinant(stack.get_matrix4x4(1)));
  110. return 1;
  111. }
  112. static int matrix4x4_invert(lua_State* L)
  113. {
  114. LuaStack stack(L);
  115. stack.push_matrix4x4(invert(stack.get_matrix4x4(1)));
  116. return 1;
  117. }
  118. static int matrix4x4_x(lua_State* L)
  119. {
  120. LuaStack stack(L);
  121. stack.push_vector3(x(stack.get_matrix4x4(1)));
  122. return 1;
  123. }
  124. static int matrix4x4_y(lua_State* L)
  125. {
  126. LuaStack stack(L);
  127. stack.push_vector3(y(stack.get_matrix4x4(1)));
  128. return 1;
  129. }
  130. static int matrix4x4_z(lua_State* L)
  131. {
  132. LuaStack stack(L);
  133. stack.push_vector3(z(stack.get_matrix4x4(1)));
  134. return 1;
  135. }
  136. static int matrix4x4_set_x(lua_State* L)
  137. {
  138. LuaStack stack(L);
  139. set_x(stack.get_matrix4x4(1), stack.get_vector3(2));
  140. return 0;
  141. }
  142. static int matrix4x4_set_y(lua_State* L)
  143. {
  144. LuaStack stack(L);
  145. set_y(stack.get_matrix4x4(1), stack.get_vector3(2));
  146. return 0;
  147. }
  148. static int matrix4x4_set_z(lua_State* L)
  149. {
  150. LuaStack stack(L);
  151. set_z(stack.get_matrix4x4(1), stack.get_vector3(2));
  152. return 0;
  153. }
  154. static int matrix4x4_translation(lua_State* L)
  155. {
  156. LuaStack stack(L);
  157. stack.push_vector3(translation(stack.get_matrix4x4(1)));
  158. return 1;
  159. }
  160. static int matrix4x4_set_translation(lua_State* L)
  161. {
  162. LuaStack stack(L);
  163. set_translation(stack.get_matrix4x4(1), stack.get_vector3(2));
  164. return 0;
  165. }
  166. static int matrix4x4_identity(lua_State* L)
  167. {
  168. LuaStack stack(L);
  169. stack.push_matrix4x4(matrix4x4::IDENTITY);
  170. return 1;
  171. }
  172. static int matrix4x4_to_string(lua_State* L)
  173. {
  174. LuaStack stack(L);
  175. Matrix4x4& a = stack.get_matrix4x4(1);
  176. stack.push_fstring("%.1f, %.1f, %.1f, %.1f\n%.1f, %.1f, %.1f, %.1f\n%.1f, %.1f, %.1f, %.1f\n%.1f, %.1f, %.1f, %.1f\n",
  177. a[0], a[4], a[8], a[12], a[1], a[5], a[9], a[13], a[2], a[6], a[10], a[14], a[3], a[7], a[11], a[15]);
  178. return 1;
  179. }
  180. void load_matrix4x4(LuaEnvironment& env)
  181. {
  182. env.load_module_function("Matrix4x4", "new", matrix4x4_new);
  183. env.load_module_function("Matrix4x4", "from_quaternion", matrix4x4_from_quaternion);
  184. env.load_module_function("Matrix4x4", "from_translation", matrix4x4_from_translation);
  185. env.load_module_function("Matrix4x4", "from_quaternion_translation", matrix4x4_from_quaternion_translation);
  186. env.load_module_function("Matrix4x4", "from_axes", matrix4x4_from_axes);
  187. env.load_module_function("Matrix4x4", "add", matrix4x4_add);
  188. env.load_module_function("Matrix4x4", "subtract", matrix4x4_subtract);
  189. env.load_module_function("Matrix4x4", "multiply", matrix4x4_multiply);
  190. env.load_module_function("Matrix4x4", "transpose", matrix4x4_transpose);
  191. env.load_module_function("Matrix4x4", "determinant", matrix4x4_determinant);
  192. env.load_module_function("Matrix4x4", "invert", matrix4x4_invert);
  193. env.load_module_function("Matrix4x4", "x", matrix4x4_x);
  194. env.load_module_function("Matrix4x4", "y", matrix4x4_y);
  195. env.load_module_function("Matrix4x4", "z", matrix4x4_z);
  196. env.load_module_function("Matrix4x4", "set_x", matrix4x4_set_x);
  197. env.load_module_function("Matrix4x4", "set_y", matrix4x4_set_y);
  198. env.load_module_function("Matrix4x4", "set_z", matrix4x4_set_z);
  199. env.load_module_function("Matrix4x4", "translation", matrix4x4_translation);
  200. env.load_module_function("Matrix4x4", "set_translation", matrix4x4_set_translation);
  201. env.load_module_function("Matrix4x4", "identity", matrix4x4_identity);
  202. env.load_module_function("Matrix4x4", "to_string", matrix4x4_to_string);
  203. env.load_module_constructor("Matrix4x4", matrix4x4_ctor);
  204. }
  205. } //namespace crown