LuaVector3.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 "Vector3.h"
  24. #include "LuaStack.h"
  25. #include "LuaEnvironment.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. CE_EXPORT int vector3(lua_State* L)
  30. {
  31. LuaStack stack(L);
  32. float x = stack.get_float(1);
  33. float y = stack.get_float(2);
  34. float z = stack.get_float(3);
  35. stack.push_vector3(Vector3(x, y, z));
  36. return 1;
  37. }
  38. //-----------------------------------------------------------------------------
  39. CE_EXPORT int vector3_values(lua_State* L)
  40. {
  41. LuaStack stack(L);
  42. Vector3& a = stack.get_vector3(1);
  43. stack.push_float(a.x);
  44. stack.push_float(a.y);
  45. stack.push_float(a.z);
  46. return 3;
  47. }
  48. //-----------------------------------------------------------------------------
  49. CE_EXPORT int vector3_add(lua_State* L)
  50. {
  51. LuaStack stack(L);
  52. Vector3& a = stack.get_vector3(1);
  53. Vector3& b = stack.get_vector3(2);
  54. stack.push_vector3(a + b);
  55. return 1;
  56. }
  57. //-----------------------------------------------------------------------------
  58. CE_EXPORT int vector3_subtract(lua_State* L)
  59. {
  60. LuaStack stack(L);
  61. Vector3& a = stack.get_vector3(1);
  62. Vector3& b = stack.get_vector3(2);
  63. stack.push_vector3(a - b);
  64. return 1;
  65. }
  66. //-----------------------------------------------------------------------------
  67. CE_EXPORT int vector3_multiply(lua_State* L)
  68. {
  69. LuaStack stack(L);
  70. Vector3& a = stack.get_vector3(1);
  71. float b = stack.get_float(2);
  72. stack.push_vector3(a * b);
  73. return 1;
  74. }
  75. //-----------------------------------------------------------------------------
  76. CE_EXPORT int vector3_divide(lua_State* L)
  77. {
  78. LuaStack stack(L);
  79. Vector3& a = stack.get_vector3(1);
  80. float b = stack.get_float(2);
  81. stack.push_vector3(a / b);
  82. return 1;
  83. }
  84. //-----------------------------------------------------------------------------
  85. CE_EXPORT int vector3_dot(lua_State* L)
  86. {
  87. LuaStack stack(L);
  88. Vector3& a = stack.get_vector3(1);
  89. Vector3& b = stack.get_vector3(2);
  90. stack.push_float(a.dot(b));
  91. return 1;
  92. }
  93. //-----------------------------------------------------------------------------
  94. CE_EXPORT int vector3_cross(lua_State* L)
  95. {
  96. LuaStack stack(L);
  97. Vector3& a = stack.get_vector3(1);
  98. Vector3& b = stack.get_vector3(2);
  99. stack.push_vector3(a.cross(b));
  100. return 1;
  101. }
  102. //-----------------------------------------------------------------------------
  103. CE_EXPORT int vector3_equals(lua_State* L)
  104. {
  105. LuaStack stack(L);
  106. Vector3& a = stack.get_vector3(1);
  107. Vector3& b = stack.get_vector3(2);
  108. stack.push_bool(a == b);
  109. return 1;
  110. }
  111. //-----------------------------------------------------------------------------
  112. CE_EXPORT int vector3_lower(lua_State* L)
  113. {
  114. LuaStack stack(L);
  115. Vector3& a = stack.get_vector3(1);
  116. Vector3& b = stack.get_vector3(2);
  117. stack.push_bool(a < b);
  118. return 1;
  119. }
  120. //-----------------------------------------------------------------------------
  121. CE_EXPORT int vector3_greater(lua_State* L)
  122. {
  123. LuaStack stack(L);
  124. Vector3& a = stack.get_vector3(1);
  125. Vector3& b = stack.get_vector3(2);
  126. stack.push_bool(a > b);
  127. return 1;
  128. }
  129. //-----------------------------------------------------------------------------
  130. CE_EXPORT int vector3_length(lua_State* L)
  131. {
  132. LuaStack stack(L);
  133. Vector3& a = stack.get_vector3(1);
  134. stack.push_float(a.length());
  135. return 1;
  136. }
  137. //-----------------------------------------------------------------------------
  138. CE_EXPORT int vector3_squared_length(lua_State* L)
  139. {
  140. LuaStack stack(L);
  141. Vector3& a = stack.get_vector3(1);
  142. stack.push_float(a.squared_length());
  143. return 1;
  144. }
  145. //-----------------------------------------------------------------------------
  146. CE_EXPORT int vector3_set_length(lua_State* L)
  147. {
  148. LuaStack stack(L);
  149. Vector3& a = stack.get_vector3(1);
  150. float len = stack.get_float(2);
  151. a.set_length(len);
  152. return 0;
  153. }
  154. //-----------------------------------------------------------------------------
  155. CE_EXPORT int vector3_normalize(lua_State* L)
  156. {
  157. LuaStack stack(L);
  158. Vector3& a = stack.get_vector3(1);
  159. a.normalize();
  160. return 0;
  161. }
  162. //-----------------------------------------------------------------------------
  163. CE_EXPORT int vector3_negate(lua_State* L)
  164. {
  165. LuaStack stack(L);
  166. Vector3& a = stack.get_vector3(1);
  167. stack.push_vector3(-a);
  168. return 1;
  169. }
  170. //-----------------------------------------------------------------------------
  171. CE_EXPORT int vector3_get_distance_to(lua_State* L)
  172. {
  173. LuaStack stack(L);
  174. Vector3& a = stack.get_vector3(1);
  175. Vector3& b = stack.get_vector3(2);
  176. stack.push_float(a.get_distance_to(b));
  177. return 1;
  178. }
  179. //-----------------------------------------------------------------------------
  180. CE_EXPORT int vector3_get_angle_between(lua_State* L)
  181. {
  182. LuaStack stack(L);
  183. Vector3& a = stack.get_vector3(1);
  184. Vector3& b = stack.get_vector3(2);
  185. stack.push_float(a.get_angle_between(b));
  186. return 1;
  187. }
  188. //-----------------------------------------------------------------------------
  189. CE_EXPORT int vector3_zero(lua_State* L)
  190. {
  191. LuaStack stack(L);
  192. Vector3& a = stack.get_vector3(1);
  193. a.zero();
  194. return 0;
  195. }
  196. //-----------------------------------------------------------------------------
  197. void load_vector3(LuaEnvironment& env)
  198. {
  199. env.load_module_function("Vector3", "new", vector3);
  200. env.load_module_function("Vector3", "val", vector3_values);
  201. env.load_module_function("Vector3", "add", vector3_add);
  202. env.load_module_function("Vector3", "sub", vector3_subtract);
  203. env.load_module_function("Vector3", "mul", vector3_multiply);
  204. env.load_module_function("Vector3", "div", vector3_divide);
  205. env.load_module_function("Vector3", "dot", vector3_dot);
  206. env.load_module_function("Vector3", "cross", vector3_cross);
  207. env.load_module_function("Vector3", "equals", vector3_equals);
  208. env.load_module_function("Vector3", "lower", vector3_lower);
  209. env.load_module_function("Vector3", "greater", vector3_greater);
  210. env.load_module_function("Vector3", "length", vector3_length);
  211. env.load_module_function("Vector3", "squared_length", vector3_squared_length);
  212. env.load_module_function("Vector3", "set_length", vector3_set_length);
  213. env.load_module_function("Vector3", "normalize", vector3_normalize);
  214. env.load_module_function("Vector3", "negate", vector3_negate);
  215. env.load_module_function("Vector3", "get_distance_to", vector3_get_distance_to);
  216. env.load_module_function("Vector3", "get_angle_between",vector3_get_angle_between);
  217. env.load_module_function("Vector3", "zero", vector3_zero);
  218. }
  219. } // namespace crown