wrap_Mesh.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /**
  2. * Copyright (c) 2006-2013 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "wrap_Mesh.h"
  22. #include "Image.h"
  23. #include "Canvas.h"
  24. // C++
  25. #include <typeinfo>
  26. namespace love
  27. {
  28. namespace graphics
  29. {
  30. namespace opengl
  31. {
  32. Mesh *luax_checkmesh(lua_State *L, int idx)
  33. {
  34. return luax_checktype<Mesh>(L, idx, "Mesh", GRAPHICS_MESH_T);
  35. }
  36. int w_Mesh_setVertex(lua_State *L)
  37. {
  38. Mesh *t = luax_checkmesh(L, 1);
  39. size_t i = size_t(luaL_checkinteger(L, 2) - 1);
  40. Vertex v;
  41. if (lua_istable(L, 3))
  42. {
  43. for (int i = 1; i <= 8; i++)
  44. lua_rawgeti(L, 3, i);
  45. v.x = luaL_checknumber(L, -8);
  46. v.y = luaL_checknumber(L, -7);
  47. v.s = luaL_checknumber(L, -6);
  48. v.t = luaL_checknumber(L, -5);
  49. v.r = luaL_optinteger(L, -4, 255);
  50. v.g = luaL_optinteger(L, -3, 255);
  51. v.b = luaL_optinteger(L, -2, 255);
  52. v.a = luaL_optinteger(L, -1, 255);
  53. lua_pop(L, 8);
  54. }
  55. else
  56. {
  57. v.x = luaL_checknumber(L, 3);
  58. v.y = luaL_checknumber(L, 4);
  59. v.s = luaL_checknumber(L, 5);
  60. v.t = luaL_checknumber(L, 6);
  61. v.r = luaL_optinteger(L, 7, 255);
  62. v.g = luaL_optinteger(L, 8, 255);
  63. v.b = luaL_optinteger(L, 9, 255);
  64. v.a = luaL_optinteger(L, 10, 255);
  65. }
  66. EXCEPT_GUARD(t->setVertex(i, v);)
  67. return 0;
  68. }
  69. int w_Mesh_getVertex(lua_State *L)
  70. {
  71. Mesh *t = luax_checkmesh(L, 1);
  72. size_t i = (size_t) (luaL_checkinteger(L, 2) - 1);
  73. Vertex v;
  74. EXCEPT_GUARD(v = t->getVertex(i);)
  75. lua_pushnumber(L, v.x);
  76. lua_pushnumber(L, v.y);
  77. lua_pushnumber(L, v.s);
  78. lua_pushnumber(L, v.t);
  79. lua_pushnumber(L, v.r);
  80. lua_pushnumber(L, v.g);
  81. lua_pushnumber(L, v.b);
  82. lua_pushnumber(L, v.a);
  83. return 8;
  84. }
  85. int w_Mesh_setVertices(lua_State *L)
  86. {
  87. Mesh *t = luax_checkmesh(L, 1);
  88. size_t vertex_count = lua_objlen(L, 2);
  89. std::vector<Vertex> vertices;
  90. vertices.reserve(vertex_count);
  91. // Get the vertices from the table.
  92. for (size_t i = 1; i <= vertex_count; i++)
  93. {
  94. lua_rawgeti(L, 2, i);
  95. if (lua_type(L, -1) != LUA_TTABLE)
  96. return luax_typerror(L, 2, "table of tables");
  97. for (int j = 1; j <= 8; j++)
  98. lua_rawgeti(L, -j, j);
  99. Vertex v;
  100. v.x = (float) luaL_checknumber(L, -8);
  101. v.y = (float) luaL_checknumber(L, -7);
  102. v.s = (float) luaL_checknumber(L, -6);
  103. v.t = (float) luaL_checknumber(L, -5);
  104. v.r = (unsigned char) luaL_optinteger(L, -4, 255);
  105. v.g = (unsigned char) luaL_optinteger(L, -3, 255);
  106. v.b = (unsigned char) luaL_optinteger(L, -2, 255);
  107. v.a = (unsigned char) luaL_optinteger(L, -1, 255);
  108. lua_pop(L, 9);
  109. vertices.push_back(v);
  110. }
  111. EXCEPT_GUARD(t->setVertices(vertices);)
  112. return 0;
  113. }
  114. int w_Mesh_getVertices(lua_State *L)
  115. {
  116. Mesh *t = luax_checkmesh(L, 1);
  117. const Vertex *vertices = t->getVertices();
  118. size_t count = t->getVertexCount();
  119. lua_createtable(L, count, 0);
  120. for (size_t i = 0; i < count; i++)
  121. {
  122. // Create vertex table.
  123. lua_createtable(L, 8, 0);
  124. lua_pushnumber(L, vertices[i].x);
  125. lua_rawseti(L, -2, 1);
  126. lua_pushnumber(L, vertices[i].y);
  127. lua_rawseti(L, -2, 2);
  128. lua_pushnumber(L, vertices[i].s);
  129. lua_rawseti(L, -2, 3);
  130. lua_pushnumber(L, vertices[i].t);
  131. lua_rawseti(L, -2, 4);
  132. lua_pushnumber(L, vertices[i].r);
  133. lua_rawseti(L, -2, 5);
  134. lua_pushnumber(L, vertices[i].g);
  135. lua_rawseti(L, -2, 6);
  136. lua_pushnumber(L, vertices[i].b);
  137. lua_rawseti(L, -2, 7);
  138. lua_pushnumber(L, vertices[i].a);
  139. lua_rawseti(L, -2, 8);
  140. // Insert vertex table into vertices table.
  141. lua_rawseti(L, -2, i + 1);
  142. }
  143. // Return vertices table.
  144. return 1;
  145. }
  146. int w_Mesh_getVertexCount(lua_State *L)
  147. {
  148. Mesh *t = luax_checkmesh(L, 1);
  149. lua_pushinteger(L, t->getVertexCount());
  150. return 1;
  151. }
  152. int w_Mesh_setVertexMap(lua_State *L)
  153. {
  154. Mesh *t = luax_checkmesh(L, 1);
  155. bool is_table = lua_istable(L, 2);
  156. int nargs = is_table ? lua_objlen(L, 2) : lua_gettop(L) - 1;
  157. std::vector<uint32> vertexmap;
  158. vertexmap.reserve(nargs);
  159. for (int i = 0; i < nargs; i++)
  160. {
  161. if (is_table)
  162. {
  163. lua_rawgeti(L, 2, i + 1);
  164. vertexmap.push_back(uint32(luaL_checkinteger(L, -1) - 1));
  165. lua_pop(L, 1);
  166. }
  167. else
  168. vertexmap.push_back(uint32(luaL_checkinteger(L, i + 2) - 1));
  169. }
  170. EXCEPT_GUARD(t->setVertexMap(vertexmap);)
  171. return 0;
  172. }
  173. int w_Mesh_getVertexMap(lua_State *L)
  174. {
  175. Mesh *t = luax_checkmesh(L, 1);
  176. const uint32 *vertex_map = 0;
  177. EXCEPT_GUARD(vertex_map = t->getVertexMap();)
  178. size_t elements = t->getVertexMapCount();
  179. lua_createtable(L, elements, 0);
  180. for (size_t i = 0; i < elements; i++)
  181. {
  182. lua_pushinteger(L, lua_Integer(vertex_map[i]) + 1);
  183. lua_rawseti(L, -2, i + 1);
  184. }
  185. return 1;
  186. }
  187. int w_Mesh_setTexture(lua_State *L)
  188. {
  189. Mesh *t = luax_checkmesh(L, 1);
  190. if (lua_isnoneornil(L, 2))
  191. t->setTexture();
  192. else
  193. {
  194. Texture *tex = luax_checktype<Texture>(L, 2, "Texture", GRAPHICS_TEXTURE_T);
  195. t->setTexture(tex);
  196. }
  197. return 0;
  198. }
  199. int w_Mesh_getTexture(lua_State *L)
  200. {
  201. Mesh *t = luax_checkmesh(L, 1);
  202. Texture *tex = t->getTexture();
  203. if (tex == nullptr)
  204. return 0;
  205. tex->retain();
  206. // FIXME: big hack right here.
  207. if (typeid(*tex) == typeid(Image))
  208. luax_pushtype(L, "Image", GRAPHICS_IMAGE_T, tex);
  209. else if (typeid(*tex) == typeid(Canvas))
  210. luax_pushtype(L, "Canvas", GRAPHICS_CANVAS_T, tex);
  211. else
  212. {
  213. tex->release();
  214. return luaL_error(L, "Unable to determine texture type.");
  215. }
  216. return 1;
  217. }
  218. int w_Mesh_setDrawMode(lua_State *L)
  219. {
  220. Mesh *t = luax_checkmesh(L, 1);
  221. const char *str = luaL_checkstring(L, 2);
  222. Mesh::DrawMode mode;
  223. if (!Mesh::getConstant(str, mode))
  224. return luaL_error(L, "Invalid mesh draw mode: %s", str);
  225. t->setDrawMode(mode);
  226. return 0;
  227. }
  228. int w_Mesh_getDrawMode(lua_State *L)
  229. {
  230. Mesh *t = luax_checkmesh(L, 1);
  231. Mesh::DrawMode mode = t->getDrawMode();
  232. const char *str;
  233. if (!Mesh::getConstant(mode, str))
  234. return luaL_error(L, "Unknown mesh draw mode.");
  235. lua_pushstring(L, str);
  236. return 1;
  237. }
  238. int w_Mesh_setVertexColors(lua_State *L)
  239. {
  240. Mesh *t = luax_checkmesh(L, 1);
  241. t->setVertexColors(luax_toboolean(L, 2));
  242. return 0;
  243. }
  244. int w_Mesh_hasVertexColors(lua_State *L)
  245. {
  246. Mesh *t = luax_checkmesh(L, 1);
  247. luax_pushboolean(L, t->hasVertexColors());
  248. return 1;
  249. }
  250. int w_Mesh_setWireframe(lua_State *L)
  251. {
  252. Mesh *t = luax_checkmesh(L, 1);
  253. t->setWireframe(luax_toboolean(L, 2));
  254. return 0;
  255. }
  256. int w_Mesh_isWireframe(lua_State *L)
  257. {
  258. Mesh *t = luax_checkmesh(L, 1);
  259. luax_pushboolean(L, t->isWireframe());
  260. return 1;
  261. }
  262. static const luaL_Reg functions[] =
  263. {
  264. { "setVertex", w_Mesh_setVertex },
  265. { "getVertex", w_Mesh_getVertex },
  266. { "setVertices", w_Mesh_setVertices },
  267. { "getVertices", w_Mesh_getVertices },
  268. { "getVertexCount", w_Mesh_getVertexCount },
  269. { "setVertexMap", w_Mesh_setVertexMap },
  270. { "getVertexMap", w_Mesh_getVertexMap },
  271. { "setTexture", w_Mesh_setTexture },
  272. { "getTexture", w_Mesh_getTexture },
  273. { "setDrawMode", w_Mesh_setDrawMode },
  274. { "getDrawMode", w_Mesh_getDrawMode },
  275. { "setVertexColors", w_Mesh_setVertexColors },
  276. { "hasVertexColors", w_Mesh_hasVertexColors },
  277. { "setWireframe", w_Mesh_setWireframe },
  278. { "isWireframe", w_Mesh_isWireframe },
  279. // Deprecated since 0.9.1.
  280. { "setImage", w_Mesh_setTexture },
  281. { "getImage", w_Mesh_getTexture },
  282. { 0, 0 }
  283. };
  284. extern "C" int luaopen_mesh(lua_State *L)
  285. {
  286. return luax_register_type(L, "Mesh", functions);
  287. }
  288. } // opengl
  289. } // graphics
  290. } // love