wrap_Mesh.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /**
  2. * Copyright (c) 2006-2014 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. #include "wrap_Texture.h"
  25. // C++
  26. #include <typeinfo>
  27. namespace love
  28. {
  29. namespace graphics
  30. {
  31. namespace opengl
  32. {
  33. Mesh *luax_checkmesh(lua_State *L, int idx)
  34. {
  35. return luax_checktype<Mesh>(L, idx, "Mesh", GRAPHICS_MESH_T);
  36. }
  37. int w_Mesh_setVertex(lua_State *L)
  38. {
  39. Mesh *t = luax_checkmesh(L, 1);
  40. size_t i = size_t(luaL_checkinteger(L, 2) - 1);
  41. Vertex v;
  42. if (lua_istable(L, 3))
  43. {
  44. for (int i = 1; i <= 8; i++)
  45. lua_rawgeti(L, 3, i);
  46. v.x = luaL_checknumber(L, -8);
  47. v.y = luaL_checknumber(L, -7);
  48. v.s = luaL_optnumber(L, -6, 0.0);
  49. v.t = luaL_optnumber(L, -5, 0.0);
  50. v.r = luaL_optinteger(L, -4, 255);
  51. v.g = luaL_optinteger(L, -3, 255);
  52. v.b = luaL_optinteger(L, -2, 255);
  53. v.a = luaL_optinteger(L, -1, 255);
  54. lua_pop(L, 8);
  55. }
  56. else
  57. {
  58. v.x = luaL_checknumber(L, 3);
  59. v.y = luaL_checknumber(L, 4);
  60. v.s = luaL_optnumber(L, 5, 0.0);
  61. v.t = luaL_optnumber(L, 6, 0.0);
  62. v.r = luaL_optinteger(L, 7, 255);
  63. v.g = luaL_optinteger(L, 8, 255);
  64. v.b = luaL_optinteger(L, 9, 255);
  65. v.a = luaL_optinteger(L, 10, 255);
  66. }
  67. luax_catchexcept(L, [&](){ t->setVertex(i, v); });
  68. return 0;
  69. }
  70. int w_Mesh_getVertex(lua_State *L)
  71. {
  72. Mesh *t = luax_checkmesh(L, 1);
  73. size_t i = (size_t) (luaL_checkinteger(L, 2) - 1);
  74. Vertex v;
  75. luax_catchexcept(L, [&](){ v = t->getVertex(i); });
  76. lua_pushnumber(L, v.x);
  77. lua_pushnumber(L, v.y);
  78. lua_pushnumber(L, v.s);
  79. lua_pushnumber(L, v.t);
  80. lua_pushnumber(L, v.r);
  81. lua_pushnumber(L, v.g);
  82. lua_pushnumber(L, v.b);
  83. lua_pushnumber(L, v.a);
  84. return 8;
  85. }
  86. int w_Mesh_setVertices(lua_State *L)
  87. {
  88. Mesh *t = luax_checkmesh(L, 1);
  89. size_t vertex_count = lua_objlen(L, 2);
  90. std::vector<Vertex> vertices;
  91. vertices.reserve(vertex_count);
  92. // Get the vertices from the table.
  93. for (size_t i = 1; i <= vertex_count; i++)
  94. {
  95. lua_rawgeti(L, 2, i);
  96. if (lua_type(L, -1) != LUA_TTABLE)
  97. return luax_typerror(L, 2, "table of tables");
  98. for (int j = 1; j <= 8; j++)
  99. lua_rawgeti(L, -j, j);
  100. Vertex v;
  101. v.x = (float) luaL_checknumber(L, -8);
  102. v.y = (float) luaL_checknumber(L, -7);
  103. v.s = (float) luaL_optnumber(L, -6, 0.0);
  104. v.t = (float) luaL_optnumber(L, -5, 0.0);
  105. v.r = (unsigned char) luaL_optinteger(L, -4, 255);
  106. v.g = (unsigned char) luaL_optinteger(L, -3, 255);
  107. v.b = (unsigned char) luaL_optinteger(L, -2, 255);
  108. v.a = (unsigned char) luaL_optinteger(L, -1, 255);
  109. lua_pop(L, 9);
  110. vertices.push_back(v);
  111. }
  112. luax_catchexcept(L, [&](){ t->setVertices(vertices); });
  113. return 0;
  114. }
  115. int w_Mesh_getVertices(lua_State *L)
  116. {
  117. Mesh *t = luax_checkmesh(L, 1);
  118. const Vertex *vertices = t->getVertices();
  119. size_t count = t->getVertexCount();
  120. lua_createtable(L, count, 0);
  121. if (count == 0 || vertices == nullptr)
  122. return 1;
  123. for (size_t i = 0; i < count; i++)
  124. {
  125. // Create vertex table.
  126. lua_createtable(L, 8, 0);
  127. lua_pushnumber(L, vertices[i].x);
  128. lua_rawseti(L, -2, 1);
  129. lua_pushnumber(L, vertices[i].y);
  130. lua_rawseti(L, -2, 2);
  131. lua_pushnumber(L, vertices[i].s);
  132. lua_rawseti(L, -2, 3);
  133. lua_pushnumber(L, vertices[i].t);
  134. lua_rawseti(L, -2, 4);
  135. lua_pushnumber(L, vertices[i].r);
  136. lua_rawseti(L, -2, 5);
  137. lua_pushnumber(L, vertices[i].g);
  138. lua_rawseti(L, -2, 6);
  139. lua_pushnumber(L, vertices[i].b);
  140. lua_rawseti(L, -2, 7);
  141. lua_pushnumber(L, vertices[i].a);
  142. lua_rawseti(L, -2, 8);
  143. // Insert vertex table into vertices table.
  144. lua_rawseti(L, -2, i + 1);
  145. }
  146. // Return vertices table.
  147. return 1;
  148. }
  149. int w_Mesh_getVertexCount(lua_State *L)
  150. {
  151. Mesh *t = luax_checkmesh(L, 1);
  152. lua_pushinteger(L, t->getVertexCount());
  153. return 1;
  154. }
  155. int w_Mesh_setVertexMap(lua_State *L)
  156. {
  157. Mesh *t = luax_checkmesh(L, 1);
  158. bool is_table = lua_istable(L, 2);
  159. int nargs = is_table ? lua_objlen(L, 2) : lua_gettop(L) - 1;
  160. std::vector<uint32> vertexmap;
  161. vertexmap.reserve(nargs);
  162. for (int i = 0; i < nargs; i++)
  163. {
  164. if (is_table)
  165. {
  166. lua_rawgeti(L, 2, i + 1);
  167. vertexmap.push_back(uint32(luaL_checkinteger(L, -1) - 1));
  168. lua_pop(L, 1);
  169. }
  170. else
  171. vertexmap.push_back(uint32(luaL_checkinteger(L, i + 2) - 1));
  172. }
  173. luax_catchexcept(L, [&](){ t->setVertexMap(vertexmap); });
  174. return 0;
  175. }
  176. int w_Mesh_getVertexMap(lua_State *L)
  177. {
  178. Mesh *t = luax_checkmesh(L, 1);
  179. std::vector<uint32> vertex_map;
  180. luax_catchexcept(L, [&](){ t->getVertexMap(vertex_map); });
  181. size_t element_count = vertex_map.size();
  182. lua_createtable(L, element_count, 0);
  183. for (size_t i = 0; i < element_count; i++)
  184. {
  185. lua_pushinteger(L, lua_Integer(vertex_map[i]) + 1);
  186. lua_rawseti(L, -2, i + 1);
  187. }
  188. return 1;
  189. }
  190. int w_Mesh_setTexture(lua_State *L)
  191. {
  192. Mesh *t = luax_checkmesh(L, 1);
  193. if (lua_isnoneornil(L, 2))
  194. t->setTexture();
  195. else
  196. {
  197. Texture *tex = luax_checktexture(L, 2);
  198. t->setTexture(tex);
  199. }
  200. return 0;
  201. }
  202. int w_Mesh_getTexture(lua_State *L)
  203. {
  204. Mesh *t = luax_checkmesh(L, 1);
  205. Texture *tex = t->getTexture();
  206. if (tex == nullptr)
  207. return 0;
  208. tex->retain();
  209. // FIXME: big hack right here.
  210. if (typeid(*tex) == typeid(Image))
  211. luax_pushtype(L, "Image", GRAPHICS_IMAGE_T, tex);
  212. else if (typeid(*tex) == typeid(Canvas))
  213. luax_pushtype(L, "Canvas", GRAPHICS_CANVAS_T, tex);
  214. else
  215. {
  216. tex->release();
  217. return luaL_error(L, "Unable to determine texture type.");
  218. }
  219. return 1;
  220. }
  221. int w_Mesh_setDrawMode(lua_State *L)
  222. {
  223. Mesh *t = luax_checkmesh(L, 1);
  224. const char *str = luaL_checkstring(L, 2);
  225. Mesh::DrawMode mode;
  226. if (!Mesh::getConstant(str, mode))
  227. return luaL_error(L, "Invalid mesh draw mode: %s", str);
  228. t->setDrawMode(mode);
  229. return 0;
  230. }
  231. int w_Mesh_getDrawMode(lua_State *L)
  232. {
  233. Mesh *t = luax_checkmesh(L, 1);
  234. Mesh::DrawMode mode = t->getDrawMode();
  235. const char *str;
  236. if (!Mesh::getConstant(mode, str))
  237. return luaL_error(L, "Unknown mesh draw mode.");
  238. lua_pushstring(L, str);
  239. return 1;
  240. }
  241. int w_Mesh_setDrawRange(lua_State *L)
  242. {
  243. Mesh *t = luax_checkmesh(L, 1);
  244. if (lua_isnoneornil(L, 2))
  245. t->setDrawRange();
  246. else
  247. {
  248. int rangemin = luaL_checkint(L, 2) - 1;
  249. int rangemax = luaL_checkint(L, 3) - 1;
  250. luax_catchexcept(L, [&](){ t->setDrawRange(rangemin, rangemax); });
  251. }
  252. return 0;
  253. }
  254. int w_Mesh_getDrawRange(lua_State *L)
  255. {
  256. Mesh *t = luax_checkmesh(L, 1);
  257. int rangemin = -1;
  258. int rangemax = -1;
  259. t->getDrawRange(rangemin, rangemax);
  260. if (rangemin < 0 || rangemax < 0)
  261. return 0;
  262. lua_pushinteger(L, rangemin + 1);
  263. lua_pushinteger(L, rangemax + 1);
  264. return 2;
  265. }
  266. int w_Mesh_setVertexColors(lua_State *L)
  267. {
  268. Mesh *t = luax_checkmesh(L, 1);
  269. t->setVertexColors(luax_toboolean(L, 2));
  270. return 0;
  271. }
  272. int w_Mesh_hasVertexColors(lua_State *L)
  273. {
  274. Mesh *t = luax_checkmesh(L, 1);
  275. luax_pushboolean(L, t->hasVertexColors());
  276. return 1;
  277. }
  278. static const luaL_Reg functions[] =
  279. {
  280. { "setVertex", w_Mesh_setVertex },
  281. { "getVertex", w_Mesh_getVertex },
  282. { "setVertices", w_Mesh_setVertices },
  283. { "getVertices", w_Mesh_getVertices },
  284. { "getVertexCount", w_Mesh_getVertexCount },
  285. { "setVertexMap", w_Mesh_setVertexMap },
  286. { "getVertexMap", w_Mesh_getVertexMap },
  287. { "setTexture", w_Mesh_setTexture },
  288. { "getTexture", w_Mesh_getTexture },
  289. { "setDrawMode", w_Mesh_setDrawMode },
  290. { "getDrawMode", w_Mesh_getDrawMode },
  291. { "setDrawRange", w_Mesh_setDrawRange },
  292. { "getDrawRange", w_Mesh_getDrawRange },
  293. { "setVertexColors", w_Mesh_setVertexColors },
  294. { "hasVertexColors", w_Mesh_hasVertexColors },
  295. { 0, 0 }
  296. };
  297. extern "C" int luaopen_mesh(lua_State *L)
  298. {
  299. return luax_register_type(L, "Mesh", functions);
  300. }
  301. } // opengl
  302. } // graphics
  303. } // love