2
0

lua_vector3.cpp 7.6 KB

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