2
0

LuaVec3.cpp 7.0 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 "Vec3.h"
  24. #include "LuaStack.h"
  25. #include "LuaEnvironment.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. CE_EXPORT int vec3(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_vec3(Vec3(x, y, z));
  36. return 1;
  37. }
  38. //-----------------------------------------------------------------------------
  39. CE_EXPORT int vec3_values(lua_State* L)
  40. {
  41. LuaStack stack(L);
  42. Vec3& a = stack.get_vec3(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 vec3_add(lua_State* L)
  50. {
  51. LuaStack stack(L);
  52. Vec3& a = stack.get_vec3(1);
  53. Vec3& b = stack.get_vec3(2);
  54. stack.push_vec3(a + b);
  55. return 1;
  56. }
  57. //-----------------------------------------------------------------------------
  58. CE_EXPORT int vec3_subtract(lua_State* L)
  59. {
  60. LuaStack stack(L);
  61. Vec3& a = stack.get_vec3(1);
  62. Vec3& b = stack.get_vec3(2);
  63. stack.push_vec3(a - b);
  64. return 1;
  65. }
  66. //-----------------------------------------------------------------------------
  67. CE_EXPORT int vec3_multiply(lua_State* L)
  68. {
  69. LuaStack stack(L);
  70. Vec3& a = stack.get_vec3(1);
  71. float b = stack.get_float(2);
  72. stack.push_vec3(a * b);
  73. return 1;
  74. }
  75. //-----------------------------------------------------------------------------
  76. CE_EXPORT int vec3_divide(lua_State* L)
  77. {
  78. LuaStack stack(L);
  79. Vec3& a = stack.get_vec3(1);
  80. float b = stack.get_float(2);
  81. stack.push_vec3(a / b);
  82. return 1;
  83. }
  84. //-----------------------------------------------------------------------------
  85. CE_EXPORT int vec3_dot(lua_State* L)
  86. {
  87. LuaStack stack(L);
  88. Vec3& a = stack.get_vec3(1);
  89. Vec3& b = stack.get_vec3(2);
  90. stack.push_float(a.dot(b));
  91. return 1;
  92. }
  93. //-----------------------------------------------------------------------------
  94. CE_EXPORT int vec3_cross(lua_State* L)
  95. {
  96. LuaStack stack(L);
  97. Vec3& a = stack.get_vec3(1);
  98. Vec3& b = stack.get_vec3(2);
  99. stack.push_vec3(a.cross(b));
  100. return 1;
  101. }
  102. //-----------------------------------------------------------------------------
  103. CE_EXPORT int vec3_equals(lua_State* L)
  104. {
  105. LuaStack stack(L);
  106. Vec3& a = stack.get_vec3(1);
  107. Vec3& b = stack.get_vec3(2);
  108. stack.push_bool(a == b);
  109. return 1;
  110. }
  111. //-----------------------------------------------------------------------------
  112. CE_EXPORT int vec3_lower(lua_State* L)
  113. {
  114. LuaStack stack(L);
  115. Vec3& a = stack.get_vec3(1);
  116. Vec3& b = stack.get_vec3(2);
  117. stack.push_bool(a < b);
  118. return 1;
  119. }
  120. //-----------------------------------------------------------------------------
  121. CE_EXPORT int vec3_greater(lua_State* L)
  122. {
  123. LuaStack stack(L);
  124. Vec3& a = stack.get_vec3(1);
  125. Vec3& b = stack.get_vec3(2);
  126. stack.push_bool(a > b);
  127. return 1;
  128. }
  129. //-----------------------------------------------------------------------------
  130. CE_EXPORT int vec3_length(lua_State* L)
  131. {
  132. LuaStack stack(L);
  133. Vec3& a = stack.get_vec3(1);
  134. stack.push_float(a.length());
  135. return 1;
  136. }
  137. //-----------------------------------------------------------------------------
  138. CE_EXPORT int vec3_squared_length(lua_State* L)
  139. {
  140. LuaStack stack(L);
  141. Vec3& a = stack.get_vec3(1);
  142. stack.push_float(a.squared_length());
  143. return 1;
  144. }
  145. //-----------------------------------------------------------------------------
  146. CE_EXPORT int vec3_set_length(lua_State* L)
  147. {
  148. LuaStack stack(L);
  149. Vec3& a = stack.get_vec3(1);
  150. float len = stack.get_float(2);
  151. a.set_length(len);
  152. return 0;
  153. }
  154. //-----------------------------------------------------------------------------
  155. CE_EXPORT int vec3_normalize(lua_State* L)
  156. {
  157. LuaStack stack(L);
  158. Vec3& a = stack.get_vec3(1);
  159. a.normalize();
  160. return 0;
  161. }
  162. //-----------------------------------------------------------------------------
  163. CE_EXPORT int vec3_negate(lua_State* L)
  164. {
  165. LuaStack stack(L);
  166. Vec3& a = stack.get_vec3(1);
  167. a.negate();
  168. return 0;
  169. }
  170. //-----------------------------------------------------------------------------
  171. CE_EXPORT int vec3_get_distance_to(lua_State* L)
  172. {
  173. LuaStack stack(L);
  174. Vec3& a = stack.get_vec3(1);
  175. Vec3& b = stack.get_vec3(2);
  176. stack.push_float(a.get_distance_to(b));
  177. return 1;
  178. }
  179. //-----------------------------------------------------------------------------
  180. CE_EXPORT int vec3_get_angle_between(lua_State* L)
  181. {
  182. LuaStack stack(L);
  183. Vec3& a = stack.get_vec3(1);
  184. Vec3& b = stack.get_vec3(2);
  185. stack.push_float(a.get_angle_between(b));
  186. return 1;
  187. }
  188. //-----------------------------------------------------------------------------
  189. CE_EXPORT int vec3_zero(lua_State* L)
  190. {
  191. LuaStack stack(L);
  192. Vec3& a = stack.get_vec3(1);
  193. a.zero();
  194. return 0;
  195. }
  196. //-----------------------------------------------------------------------------
  197. void load_vec3(LuaEnvironment& env)
  198. {
  199. env.load_module_function("Vec3", "new", vec3);
  200. env.load_module_function("Vec3", "val", vec3_values);
  201. env.load_module_function("Vec3", "add", vec3_add);
  202. env.load_module_function("Vec3", "sub", vec3_subtract);
  203. env.load_module_function("Vec3", "mul", vec3_multiply);
  204. env.load_module_function("Vec3", "div", vec3_divide);
  205. env.load_module_function("Vec3", "dot", vec3_dot);
  206. env.load_module_function("Vec3", "cross", vec3_cross);
  207. env.load_module_function("Vec3", "equals", vec3_equals);
  208. env.load_module_function("Vec3", "lower", vec3_lower);
  209. env.load_module_function("Vec3", "greater", vec3_greater);
  210. env.load_module_function("Vec3", "length", vec3_length);
  211. env.load_module_function("Vec3", "squared_length", vec3_squared_length);
  212. env.load_module_function("Vec3", "set_length", vec3_set_length);
  213. env.load_module_function("Vec3", "normalize", vec3_normalize);
  214. env.load_module_function("Vec3", "negate", vec3_negate);
  215. env.load_module_function("Vec3", "get_distance_to", vec3_get_distance_to);
  216. env.load_module_function("Vec3", "get_angle_between", vec3_get_angle_between);
  217. env.load_module_function("Vec3", "zero", vec3_zero);
  218. }
  219. } // namespace crown