2
0

lua_math.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "math_utils.h"
  6. #include "math_types.h"
  7. #include "aabb.h"
  8. #include "intersection.h"
  9. #include "lua_stack.h"
  10. #include "lua_environment.h"
  11. namespace crown
  12. {
  13. static int math_to_rad(lua_State* L)
  14. {
  15. LuaStack stack(L);
  16. stack.push_float(to_rad(stack.get_float(1)));
  17. return 1;
  18. }
  19. static int math_to_deg(lua_State* L)
  20. {
  21. LuaStack stack(L);
  22. stack.push_float(to_deg(stack.get_float(1)));
  23. return 1;
  24. }
  25. static int math_next_pow_2(lua_State* L)
  26. {
  27. LuaStack stack(L);
  28. stack.push_uint32(next_pow_2(stack.get_int(1)));
  29. return 1;
  30. }
  31. static int math_is_pow_2(lua_State* L)
  32. {
  33. LuaStack stack(L);
  34. stack.push_bool(is_pow_2(stack.get_int(1)));
  35. return 1;
  36. }
  37. static int math_ceil(lua_State* L)
  38. {
  39. LuaStack stack(L);
  40. stack.push_float(ceil(stack.get_float(1)));
  41. return 1;
  42. }
  43. static int math_floor(lua_State* L)
  44. {
  45. LuaStack stack(L);
  46. stack.push_float(floor(stack.get_float(1)));
  47. return 1;
  48. }
  49. static int math_sqrt(lua_State* L)
  50. {
  51. LuaStack stack(L);
  52. stack.push_float(sqrt(stack.get_float(1)));
  53. return 1;
  54. }
  55. static int math_inv_sqrt(lua_State* L)
  56. {
  57. LuaStack stack(L);
  58. stack.push_float(inv_sqrt(stack.get_float(1)));
  59. return 1;
  60. }
  61. static int math_sin(lua_State* L)
  62. {
  63. LuaStack stack(L);
  64. stack.push_float(sin(stack.get_float(1)));
  65. return 1;
  66. }
  67. static int math_cos(lua_State* L)
  68. {
  69. LuaStack stack(L);
  70. stack.push_float(cos(stack.get_float(1)));
  71. return 1;
  72. }
  73. static int math_asin(lua_State* L)
  74. {
  75. LuaStack stack(L);
  76. stack.push_float(asin(stack.get_float(1)));
  77. return 1;
  78. }
  79. static int math_acos(lua_State* L)
  80. {
  81. LuaStack stack(L);
  82. stack.push_float(acos(stack.get_float(1)));
  83. return 1;
  84. }
  85. static int math_tan(lua_State* L)
  86. {
  87. LuaStack stack(L);
  88. stack.push_float(tan(stack.get_float(1)));
  89. return 1;
  90. }
  91. static int math_atan2(lua_State* L)
  92. {
  93. LuaStack stack(L);
  94. stack.push_float(atan2(stack.get_float(1), stack.get_float(2)));
  95. return 1;
  96. }
  97. static int math_abs(lua_State* L)
  98. {
  99. LuaStack stack(L);
  100. stack.push_float(abs(stack.get_float(1)));
  101. return 1;
  102. }
  103. static int math_fmod(lua_State* L)
  104. {
  105. LuaStack stack(L);
  106. stack.push_float(fmod(stack.get_float(1), stack.get_float(2)));
  107. return 1;
  108. }
  109. static int math_ray_oobb_intersection(lua_State* L)
  110. {
  111. LuaStack stack(L);
  112. OBB oobb;
  113. oobb.tm = stack.get_matrix4x4(3);
  114. oobb.aabb.min = stack.get_vector3(4) * -0.5;
  115. oobb.aabb.max = stack.get_vector3(4) * 0.5;
  116. stack.push_float(ray_oobb_intersection(stack.get_vector3(1), stack.get_vector3(2), oobb));
  117. return 1;
  118. }
  119. void load_math(LuaEnvironment& env)
  120. {
  121. env.load_module_function("Math", "to_rad", math_to_rad);
  122. env.load_module_function("Math", "to_deg", math_to_deg);
  123. env.load_module_function("Math", "next_pow_2", math_next_pow_2);
  124. env.load_module_function("Math", "is_pow_2", math_is_pow_2);
  125. env.load_module_function("Math", "ceil", math_ceil);
  126. env.load_module_function("Math", "floor", math_floor);
  127. env.load_module_function("Math", "sqrt", math_sqrt);
  128. env.load_module_function("Math", "inv_sqrt", math_inv_sqrt);
  129. env.load_module_function("Math", "sin", math_sin);
  130. env.load_module_function("Math", "cos", math_cos);
  131. env.load_module_function("Math", "asin", math_asin);
  132. env.load_module_function("Math", "acos", math_acos);
  133. env.load_module_function("Math", "tan", math_tan);
  134. env.load_module_function("Math", "atan2", math_atan2);
  135. env.load_module_function("Math", "abs", math_abs);
  136. env.load_module_function("Math", "fmod", math_fmod);
  137. env.load_module_function("Math", "ray_oobb_intersection", math_ray_oobb_intersection);
  138. }
  139. } // namespace crown