2
0

LuaMat4.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 "Mat4.h"
  24. #include "Vec3.h"
  25. #include "LuaStack.h"
  26. #include "LuaEnvironment.h"
  27. #include "OS.h"
  28. namespace crown
  29. {
  30. //-----------------------------------------------------------------------------
  31. CE_EXPORT int32_t mat4(lua_State* L)
  32. {
  33. LuaStack stack(L);
  34. float m0 = stack.get_float(1);
  35. float m1 = stack.get_float(2);
  36. float m2 = stack.get_float(3);
  37. float m3 = stack.get_float(4);
  38. float m4 = stack.get_float(5);
  39. float m5 = stack.get_float(6);
  40. float m6 = stack.get_float(7);
  41. float m7 = stack.get_float(8);
  42. float m8 = stack.get_float(9);
  43. float m9 = stack.get_float(10);
  44. float m10 = stack.get_float(11);
  45. float m11 = stack.get_float(12);
  46. float m12 = stack.get_float(13);
  47. float m13 = stack.get_float(14);
  48. float m14 = stack.get_float(15);
  49. float m15 = stack.get_float(16);
  50. stack.push_mat4(Mat4(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15));
  51. return 1;
  52. }
  53. //-----------------------------------------------------------------------------
  54. CE_EXPORT int32_t mat4_add(lua_State* L)
  55. {
  56. LuaStack stack(L);
  57. Mat4& a = stack.get_mat4(1);
  58. Mat4& b = stack.get_mat4(2);
  59. stack.push_mat4(a + b);
  60. return 1;
  61. }
  62. //-----------------------------------------------------------------------------
  63. CE_EXPORT int32_t mat4_subtract(lua_State* L)
  64. {
  65. LuaStack stack(L);
  66. Mat4& a = stack.get_mat4(1);
  67. Mat4& b = stack.get_mat4(2);
  68. stack.push_mat4(a - b);
  69. return 1;
  70. }
  71. //-----------------------------------------------------------------------------
  72. CE_EXPORT int32_t mat4_multiply(lua_State* L)
  73. {
  74. LuaStack stack(L);
  75. Mat4& a = stack.get_mat4(1);
  76. Mat4& b = stack.get_mat4(2);
  77. stack.push_mat4(a * b);
  78. return 1;
  79. }
  80. //-----------------------------------------------------------------------------
  81. CE_EXPORT int32_t mat4_multiply_by_scalar(lua_State* L)
  82. {
  83. LuaStack stack(L);
  84. Mat4& a = stack.get_mat4(1);
  85. float k = stack.get_float(2);
  86. stack.push_mat4(a * k);
  87. return 1;
  88. }
  89. //-----------------------------------------------------------------------------
  90. CE_EXPORT int32_t mat4_divide_by_scalar(lua_State* L)
  91. {
  92. LuaStack stack(L);
  93. Mat4& a = stack.get_mat4(1);
  94. float k = stack.get_float(2);
  95. stack.push_mat4(a / k);
  96. return 1;
  97. }
  98. //-----------------------------------------------------------------------------
  99. CE_EXPORT int32_t mat4_build_rotation_x(lua_State* L)
  100. {
  101. LuaStack stack(L);
  102. Mat4& a = stack.get_mat4(1);
  103. float k = stack.get_float(2);
  104. a.build_rotation_x(k);
  105. return 0;
  106. }
  107. //-----------------------------------------------------------------------------
  108. CE_EXPORT int32_t mat4_build_rotation_y(lua_State* L)
  109. {
  110. LuaStack stack(L);
  111. Mat4& a = stack.get_mat4(1);
  112. float k = stack.get_float(2);
  113. a.build_rotation_y(k);
  114. return 0;
  115. }
  116. //-----------------------------------------------------------------------------
  117. CE_EXPORT int32_t mat4_build_rotation_z(lua_State* L)
  118. {
  119. LuaStack stack(L);
  120. Mat4& a = stack.get_mat4(1);
  121. float k = stack.get_float(2);
  122. a.build_rotation_z(k);
  123. return 0;
  124. }
  125. //-----------------------------------------------------------------------------
  126. CE_EXPORT int32_t mat4_build_rotation(lua_State* L)
  127. {
  128. LuaStack stack(L);
  129. Mat4& a = stack.get_mat4(1);
  130. Vec3& d = stack.get_vec3(2);
  131. float k = stack.get_float(3);
  132. a.build_rotation(d, k);
  133. return 0;
  134. }
  135. //-----------------------------------------------------------------------------
  136. CE_EXPORT int32_t mat4_build_projection_perspective_rh(lua_State* L)
  137. {
  138. LuaStack stack(L);
  139. Mat4& a = stack.get_mat4(1);
  140. float fovy = stack.get_float(2);
  141. float aspect = stack.get_float(3);
  142. float near = stack.get_float(4);
  143. float far = stack.get_float(5);
  144. a.build_projection_perspective_rh(fovy, aspect, near, far);
  145. return 0;
  146. }
  147. //-----------------------------------------------------------------------------
  148. CE_EXPORT int32_t mat4_build_projection_perspective_lh(lua_State* L)
  149. {
  150. LuaStack stack(L);
  151. Mat4& a = stack.get_mat4(1);
  152. float fovy = stack.get_float(2);
  153. float aspect = stack.get_float(3);
  154. float near = stack.get_float(4);
  155. float far = stack.get_float(5);
  156. a.build_projection_perspective_lh(fovy, aspect, near, far);
  157. return 0;
  158. }
  159. //-----------------------------------------------------------------------------
  160. CE_EXPORT int32_t mat4_build_projection_ortho_rh(lua_State* L)
  161. {
  162. LuaStack stack(L);
  163. Mat4& a = stack.get_mat4(1);
  164. float width = stack.get_float(2);
  165. float height = stack.get_float(3);
  166. float near = stack.get_float(4);
  167. float far = stack.get_float(5);
  168. a.build_projection_ortho_rh(width, height, near, far);
  169. return 0;
  170. }
  171. //-----------------------------------------------------------------------------
  172. CE_EXPORT int32_t mat4_build_projection_ortho_lh(lua_State* L)
  173. {
  174. LuaStack stack(L);
  175. Mat4& a = stack.get_mat4(1);
  176. float width = stack.get_float(2);
  177. float height = stack.get_float(3);
  178. float near = stack.get_float(4);
  179. float far = stack.get_float(5);
  180. a.build_projection_ortho_lh(width, height, near, far);
  181. return 0;
  182. }
  183. //-----------------------------------------------------------------------------
  184. CE_EXPORT int32_t mat4_build_projection_ortho_2d_rh(lua_State* L)
  185. {
  186. LuaStack stack(L);
  187. Mat4& a = stack.get_mat4(1);
  188. float width = stack.get_float(2);
  189. float height = stack.get_float(3);
  190. float near = stack.get_float(4);
  191. float far = stack.get_float(5);
  192. a.build_projection_ortho_2d_rh(width, height, near, far);
  193. return 0;
  194. }
  195. //-----------------------------------------------------------------------------
  196. CE_EXPORT int32_t mat4_build_look_at_rh(lua_State* L)
  197. {
  198. LuaStack stack(L);
  199. Mat4& a = stack.get_mat4(1);
  200. Vec3& pos = stack.get_vec3(2);
  201. Vec3& target = stack.get_vec3(3);
  202. Vec3& up = stack.get_vec3(4);
  203. a.build_look_at_rh(pos, target, up);
  204. return 0;
  205. }
  206. //-----------------------------------------------------------------------------
  207. CE_EXPORT int32_t mat4_build_look_at_lh(lua_State* L)
  208. {
  209. LuaStack stack(L);
  210. Mat4& a = stack.get_mat4(1);
  211. Vec3& pos = stack.get_vec3(2);
  212. Vec3& target = stack.get_vec3(3);
  213. Vec3& up = stack.get_vec3(4);
  214. a.build_look_at_lh(pos, target, up);
  215. return 0;
  216. }
  217. //-----------------------------------------------------------------------------
  218. CE_EXPORT int32_t mat4_build_viewpoint_billboard(lua_State* L)
  219. {
  220. LuaStack stack(L);
  221. Mat4& a = stack.get_mat4(1);
  222. Vec3& pos = stack.get_vec3(2);
  223. Vec3& target = stack.get_vec3(3);
  224. Vec3& up = stack.get_vec3(4);
  225. a.build_viewpoint_billboard(pos, target, up);
  226. return 0;
  227. }
  228. //-----------------------------------------------------------------------------
  229. CE_EXPORT int32_t mat4_build_axis_billboard(lua_State* L)
  230. {
  231. LuaStack stack(L);
  232. Mat4& a = stack.get_mat4(1);
  233. Vec3& pos = stack.get_vec3(2);
  234. Vec3& target = stack.get_vec3(3);
  235. Vec3& up = stack.get_vec3(4);
  236. a.build_axis_billboard(pos, target, up);
  237. return 0;
  238. }
  239. //-----------------------------------------------------------------------------
  240. CE_EXPORT int32_t mat4_transpose(lua_State* L)
  241. {
  242. LuaStack stack(L);
  243. Mat4& a = stack.get_mat4(1);
  244. stack.push_mat4(a.transpose());
  245. return 1;
  246. }
  247. //-----------------------------------------------------------------------------
  248. CE_EXPORT int32_t mat4_determinant(lua_State* L)
  249. {
  250. LuaStack stack(L);
  251. Mat4& a = stack.get_mat4(1);
  252. stack.push_float(a.get_determinant());
  253. return 1;
  254. }
  255. //-----------------------------------------------------------------------------
  256. CE_EXPORT int32_t mat4_invert(lua_State* L)
  257. {
  258. LuaStack stack(L);
  259. Mat4& a = stack.get_mat4(1);
  260. stack.push_mat4(a.invert());
  261. return 1;
  262. }
  263. //-----------------------------------------------------------------------------
  264. CE_EXPORT int32_t mat4_load_identity(lua_State* L)
  265. {
  266. LuaStack stack(L);
  267. Mat4& a = stack.get_mat4(1);
  268. a.load_identity();
  269. return 0;
  270. }
  271. //-----------------------------------------------------------------------------
  272. CE_EXPORT int32_t mat4_x(lua_State* L)
  273. {
  274. LuaStack stack(L);
  275. Mat4& a = stack.get_mat4(1);
  276. stack.push_vec3(a.x());
  277. return 1;
  278. }
  279. //-----------------------------------------------------------------------------
  280. CE_EXPORT int32_t mat4_y(lua_State* L)
  281. {
  282. LuaStack stack(L);
  283. Mat4& a = stack.get_mat4(1);
  284. stack.push_vec3(a.y());
  285. return 1;
  286. }
  287. //-----------------------------------------------------------------------------
  288. CE_EXPORT int32_t mat4_z(lua_State* L)
  289. {
  290. LuaStack stack(L);
  291. Mat4& a = stack.get_mat4(1);
  292. stack.push_vec3(a.z());
  293. return 1;
  294. }
  295. //-----------------------------------------------------------------------------
  296. CE_EXPORT int32_t mat4_set_x(lua_State* L)
  297. {
  298. LuaStack stack(L);
  299. Mat4& a = stack.get_mat4(1);
  300. Vec3& x = stack.get_vec3(2);
  301. a.set_x(x);
  302. return 0;
  303. }
  304. //-----------------------------------------------------------------------------
  305. CE_EXPORT int32_t mat4_set_y(lua_State* L)
  306. {
  307. LuaStack stack(L);
  308. Mat4& a = stack.get_mat4(1);
  309. Vec3& y = stack.get_vec3(2);
  310. a.set_y(y);
  311. return 0;
  312. }
  313. //-----------------------------------------------------------------------------
  314. CE_EXPORT int32_t mat4_set_z(lua_State* L)
  315. {
  316. LuaStack stack(L);
  317. Mat4& a = stack.get_mat4(1);
  318. Vec3& z = stack.get_vec3(2);
  319. a.set_z(z);
  320. return 0;
  321. }
  322. //-----------------------------------------------------------------------------
  323. CE_EXPORT int32_t mat4_translation(lua_State* L)
  324. {
  325. LuaStack stack(L);
  326. Mat4& a = stack.get_mat4(1);
  327. stack.push_vec3(a.translation());
  328. return 1;
  329. }
  330. //-----------------------------------------------------------------------------
  331. CE_EXPORT int32_t mat4_set_translation(lua_State* L)
  332. {
  333. LuaStack stack(L);
  334. Mat4& a = stack.get_mat4(1);
  335. Vec3& trans = stack.get_vec3(2);
  336. a.set_translation(trans);
  337. return 0;
  338. }
  339. //-----------------------------------------------------------------------------
  340. CE_EXPORT int32_t mat4_get_scale(lua_State* L)
  341. {
  342. LuaStack stack(L);
  343. Mat4& a = stack.get_mat4(1);
  344. stack.push_vec3(a.get_scale());
  345. return 1;
  346. }
  347. //-----------------------------------------------------------------------------
  348. CE_EXPORT int32_t mat4_set_scale(lua_State* L)
  349. {
  350. LuaStack stack(L);
  351. Mat4& a = stack.get_mat4(1);
  352. Vec3& scale = stack.get_vec3(2);
  353. a.set_scale(scale);
  354. return 0;
  355. }
  356. //-----------------------------------------------------------------------------
  357. CE_EXPORT int32_t mat4_print(lua_State* L)
  358. {
  359. LuaStack stack(L);
  360. Mat4& a = stack.get_mat4(1);
  361. os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a.m[0], a.m[4], a.m[8], a.m[12]);
  362. os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a.m[1], a.m[5], a.m[9], a.m[13]);
  363. os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a.m[2], a.m[6], a.m[10], a.m[14]);
  364. os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a.m[3], a.m[7], a.m[11], a.m[15]);
  365. return 0;
  366. }
  367. //-----------------------------------------------------------------------------
  368. void load_mat4(LuaEnvironment& env)
  369. {
  370. env.load_module_function("Mat4", "new", mat4);
  371. env.load_module_function("Mat4", "add", mat4_add);
  372. env.load_module_function("Mat4", "sub", mat4_subtract);
  373. env.load_module_function("Mat4", "mul", mat4_multiply);
  374. env.load_module_function("Mat4", "muls", mat4_multiply_by_scalar);
  375. env.load_module_function("Mat4", "divs", mat4_divide_by_scalar);
  376. env.load_module_function("Mat4", "build_rotation_x", mat4_build_rotation_x);
  377. env.load_module_function("Mat4", "build_rotation_y", mat4_build_rotation_y);
  378. env.load_module_function("Mat4", "build_rotation_z", mat4_build_rotation_z);
  379. env.load_module_function("Mat4", "build_rotation", mat4_build_rotation);
  380. env.load_module_function("Mat4", "build_projection_perspective_rh", mat4_build_projection_perspective_rh);
  381. env.load_module_function("Mat4", "build_projection_perspective_lh", mat4_build_projection_perspective_lh);
  382. env.load_module_function("Mat4", "build_projection_ortho_rh", mat4_build_projection_ortho_rh);
  383. env.load_module_function("Mat4", "build_projection_ortho_lh", mat4_build_projection_ortho_lh);
  384. env.load_module_function("Mat4", "build_projection_ortho_2d_rh", mat4_build_projection_ortho_2d_rh);
  385. env.load_module_function("Mat4", "build_look_at_rh", mat4_build_look_at_rh);
  386. env.load_module_function("Mat4", "build_look_at_lh", mat4_build_look_at_rh);
  387. env.load_module_function("Mat4", "build_viewpoint_billboard", mat4_build_viewpoint_billboard);
  388. env.load_module_function("Mat4", "build_axis_billboard", mat4_build_axis_billboard);
  389. env.load_module_function("Mat4", "transpose", mat4_transpose);
  390. env.load_module_function("Mat4", "determinant", mat4_determinant);
  391. env.load_module_function("Mat4", "invert", mat4_invert);
  392. env.load_module_function("Mat4", "load_identity", mat4_load_identity);
  393. env.load_module_function("Mat4", "x", mat4_x);
  394. env.load_module_function("Mat4", "y", mat4_y);
  395. env.load_module_function("Mat4", "z", mat4_z);
  396. env.load_module_function("Mat4", "set_x", mat4_set_x);
  397. env.load_module_function("Mat4", "set_y", mat4_set_y);
  398. env.load_module_function("Mat4", "set_z", mat4_set_z);
  399. env.load_module_function("Mat4", "translation", mat4_translation);
  400. env.load_module_function("Mat4", "set_translation", mat4_set_translation);
  401. env.load_module_function("Mat4", "get_scale", mat4_get_scale);
  402. env.load_module_function("Mat4", "set_scale", mat4_set_scale);
  403. env.load_module_function("Mat4", "print", mat4_print);
  404. }
  405. } //namespace crown