wrap_Mesh.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /**
  2. * Copyright (c) 2006-2016 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 "graphics/wrap_Texture.h"
  25. // C++
  26. #include <typeinfo>
  27. #include <algorithm>
  28. namespace love
  29. {
  30. namespace graphics
  31. {
  32. namespace opengl
  33. {
  34. Mesh *luax_checkmesh(lua_State *L, int idx)
  35. {
  36. return luax_checktype<Mesh>(L, idx, GRAPHICS_MESH_ID);
  37. }
  38. static inline size_t writeByteData(lua_State *L, int startidx, int components, char *data)
  39. {
  40. uint8 *componentdata = (uint8 *) data;
  41. for (int i = 0; i < components; i++)
  42. componentdata[i] = (uint8) (luaL_optnumber(L, startidx + i, 1.0) * 255.0);
  43. return sizeof(uint8) * components;
  44. }
  45. static inline size_t writeFloatData(lua_State *L, int startidx, int components, char *data)
  46. {
  47. float *componentdata = (float *) data;
  48. for (int i = 0; i < components; i++)
  49. componentdata[i] = (float) luaL_optnumber(L, startidx + i, 0);
  50. return sizeof(float) * components;
  51. }
  52. char *luax_writeAttributeData(lua_State *L, int startidx, Mesh::DataType type, int components, char *data)
  53. {
  54. switch (type)
  55. {
  56. case Mesh::DATA_BYTE:
  57. return data + writeByteData(L, startidx, components, data);
  58. case Mesh::DATA_FLOAT:
  59. return data + writeFloatData(L, startidx, components, data);
  60. default:
  61. return data;
  62. }
  63. }
  64. static inline size_t readByteData(lua_State *L, int components, const char *data)
  65. {
  66. const uint8 *componentdata = (const uint8 *) data;
  67. for (int i = 0; i < components; i++)
  68. lua_pushnumber(L, (lua_Number) componentdata[i] / 255.0);
  69. return sizeof(uint8) * components;
  70. }
  71. static inline size_t readFloatData(lua_State *L, int components, const char *data)
  72. {
  73. const float *componentdata = (const float *) data;
  74. for (int i = 0; i < components; i++)
  75. lua_pushnumber(L, componentdata[i]);
  76. return sizeof(float) * components;
  77. }
  78. const char *luax_readAttributeData(lua_State *L, Mesh::DataType type, int components, const char *data)
  79. {
  80. switch (type)
  81. {
  82. case Mesh::DATA_BYTE:
  83. return data + readByteData(L, components, data);
  84. case Mesh::DATA_FLOAT:
  85. return data + readFloatData(L, components, data);
  86. default:
  87. return data;
  88. }
  89. }
  90. int w_Mesh_setVertices(lua_State *L)
  91. {
  92. Mesh *t = luax_checkmesh(L, 1);
  93. size_t vertoffset = (size_t) luaL_optnumber(L, 3, 1) - 1;
  94. if (vertoffset >= t->getVertexCount())
  95. return luaL_error(L, "Invalid vertex start index (must be between 1 and %d)", (int) t->getVertexCount());
  96. size_t stride = t->getVertexStride();
  97. size_t byteoffset = vertoffset * stride;
  98. if (luax_istype(L, 2, DATA_ID))
  99. {
  100. Data *d = luax_checktype<Data>(L, 2, DATA_ID);
  101. size_t datasize = std::min(d->getSize(), (t->getVertexCount() - vertoffset) * stride);
  102. char *bytedata = (char *) t->mapVertexData() + byteoffset;
  103. memcpy(bytedata, d->getData(), datasize);
  104. t->unmapVertexData(byteoffset, datasize);
  105. return 0;
  106. }
  107. luaL_checktype(L, 2, LUA_TTABLE);
  108. size_t nvertices = luax_objlen(L, 2);
  109. if (vertoffset + nvertices > t->getVertexCount())
  110. return luaL_error(L, "Too many vertices (expected at most %d, got %d)", (int) t->getVertexCount() - (int) vertoffset, (int) nvertices);
  111. const std::vector<Mesh::AttribFormat> &vertexformat = t->getVertexFormat();
  112. int ncomponents = 0;
  113. for (const Mesh::AttribFormat &format : vertexformat)
  114. ncomponents += format.components;
  115. char *data = (char *) t->mapVertexData() + byteoffset;
  116. for (size_t i = 0; i < nvertices; i++)
  117. {
  118. // get vertices[vertindex]
  119. lua_rawgeti(L, 2, i + 1);
  120. luaL_checktype(L, -1, LUA_TTABLE);
  121. // get vertices[vertindex][j]
  122. for (int j = 1; j <= ncomponents; j++)
  123. lua_rawgeti(L, -j, j);
  124. int idx = -ncomponents;
  125. for (const Mesh::AttribFormat &format : vertexformat)
  126. {
  127. // Fetch the values from Lua and store them in data buffer.
  128. data = luax_writeAttributeData(L, idx, format.type, format.components, data);
  129. idx += format.components;
  130. }
  131. lua_pop(L, ncomponents + 1);
  132. }
  133. t->unmapVertexData(byteoffset, nvertices * stride);
  134. return 0;
  135. }
  136. int w_Mesh_setVertex(lua_State *L)
  137. {
  138. Mesh *t = luax_checkmesh(L, 1);
  139. size_t index = (size_t) luaL_checkinteger(L, 2) - 1;
  140. bool istable = lua_istable(L, 3);
  141. const std::vector<Mesh::AttribFormat> &vertexformat = t->getVertexFormat();
  142. char *data = (char *) t->getVertexScratchBuffer();
  143. char *writtendata = data;
  144. int idx = istable ? 1 : 3;
  145. if (istable)
  146. {
  147. for (const Mesh::AttribFormat &format : vertexformat)
  148. {
  149. for (int i = idx; i < idx + format.components; i++)
  150. lua_rawgeti(L, 3, i);
  151. // Fetch the values from Lua and store them in data buffer.
  152. writtendata = luax_writeAttributeData(L, -format.components, format.type, format.components, writtendata);
  153. idx += format.components;
  154. lua_pop(L, format.components);
  155. }
  156. }
  157. else
  158. {
  159. for (const Mesh::AttribFormat &format : vertexformat)
  160. {
  161. // Fetch the values from Lua and store them in data buffer.
  162. writtendata = luax_writeAttributeData(L, idx, format.type, format.components, writtendata);
  163. idx += format.components;
  164. }
  165. }
  166. luax_catchexcept(L, [&](){ t->setVertex(index, data, t->getVertexStride()); });
  167. return 0;
  168. }
  169. int w_Mesh_getVertex(lua_State *L)
  170. {
  171. Mesh *t = luax_checkmesh(L, 1);
  172. size_t index = (size_t) luaL_checkinteger(L, 2) - 1;
  173. const std::vector<Mesh::AttribFormat> &vertexformat = t->getVertexFormat();
  174. char *data = (char *) t->getVertexScratchBuffer();
  175. const char *readdata = data;
  176. luax_catchexcept(L, [&](){ t->getVertex(index, data, t->getVertexStride()); });
  177. int n = 0;
  178. for (const Mesh::AttribFormat &format : vertexformat)
  179. {
  180. readdata = luax_readAttributeData(L, format.type, format.components, readdata);
  181. n += format.components;
  182. }
  183. return n;
  184. }
  185. int w_Mesh_setVertexAttribute(lua_State *L)
  186. {
  187. Mesh *t = luax_checkmesh(L, 1);
  188. size_t vertindex = (size_t) luaL_checkinteger(L, 2) - 1;
  189. int attribindex = (int) luaL_checkinteger(L, 3) - 1;
  190. Mesh::DataType type;
  191. int components;
  192. luax_catchexcept(L, [&](){ type = t->getAttributeInfo(attribindex, components); });
  193. // Maximum possible size for a single vertex attribute.
  194. char data[sizeof(float) * 4];
  195. // Fetch the values from Lua and store them in the data buffer.
  196. luax_writeAttributeData(L, 4, type, components, data);
  197. luax_catchexcept(L, [&](){ t->setVertexAttribute(vertindex, attribindex, data, sizeof(float) * 4); });
  198. return 0;
  199. }
  200. int w_Mesh_getVertexAttribute(lua_State *L)
  201. {
  202. Mesh *t = luax_checkmesh(L, 1);
  203. size_t vertindex = (size_t) luaL_checkinteger(L, 2) - 1;
  204. int attribindex = (int) luaL_checkinteger(L, 3) - 1;
  205. Mesh::DataType type;
  206. int components;
  207. luax_catchexcept(L, [&](){ type = t->getAttributeInfo(attribindex, components); });
  208. // Maximum possible size for a single vertex attribute.
  209. char data[sizeof(float) * 4];
  210. luax_catchexcept(L, [&](){ t->getVertexAttribute(vertindex, attribindex, data, sizeof(float) * 4); });
  211. luax_readAttributeData(L, type, components, data);
  212. return components;
  213. }
  214. int w_Mesh_getVertexCount(lua_State *L)
  215. {
  216. Mesh *t = luax_checkmesh(L, 1);
  217. lua_pushinteger(L, t->getVertexCount());
  218. return 1;
  219. }
  220. int w_Mesh_getVertexFormat(lua_State *L)
  221. {
  222. Mesh *t = luax_checkmesh(L, 1);
  223. const std::vector<Mesh::AttribFormat> &vertexformat = t->getVertexFormat();
  224. lua_createtable(L, (int) vertexformat.size(), 0);
  225. const char *tname = nullptr;
  226. for (size_t i = 0; i < vertexformat.size(); i++)
  227. {
  228. if (!Mesh::getConstant(vertexformat[i].type, tname))
  229. return luaL_error(L, "Unknown vertex attribute data type.");
  230. lua_createtable(L, 3, 0);
  231. lua_pushstring(L, vertexformat[i].name.c_str());
  232. lua_rawseti(L, -2, 1);
  233. lua_pushstring(L, tname);
  234. lua_rawseti(L, -2, 2);
  235. lua_pushinteger(L, vertexformat[i].components);
  236. lua_rawseti(L, -2, 3);
  237. // format[i] = {name, type, components}
  238. lua_rawseti(L, -2, (int) i + 1);
  239. }
  240. return 1;
  241. }
  242. int w_Mesh_setAttributeEnabled(lua_State *L)
  243. {
  244. Mesh *t = luax_checkmesh(L, 1);
  245. const char *name = luaL_checkstring(L, 2);
  246. bool enable = luax_toboolean(L, 3);
  247. luax_catchexcept(L, [&](){ t->setAttributeEnabled(name, enable); });
  248. return 0;
  249. }
  250. int w_Mesh_isAttributeEnabled(lua_State *L)
  251. {
  252. Mesh *t = luax_checkmesh(L, 1);
  253. const char *name = luaL_checkstring(L, 2);
  254. bool enabled = false;
  255. luax_catchexcept(L, [&](){ enabled = t->isAttributeEnabled(name); });
  256. lua_pushboolean(L, enabled);
  257. return 1;
  258. }
  259. int w_Mesh_attachAttribute(lua_State *L)
  260. {
  261. Mesh *t = luax_checkmesh(L, 1);
  262. const char *name = luaL_checkstring(L, 2);
  263. Mesh *mesh = luax_checkmesh(L, 3);
  264. luax_catchexcept(L, [&](){ t->attachAttribute(name, mesh); });
  265. return 0;
  266. }
  267. int w_Mesh_flush(lua_State *L)
  268. {
  269. Mesh *t = luax_checkmesh(L, 1);
  270. t->flush();
  271. return 0;
  272. }
  273. int w_Mesh_setVertexMap(lua_State *L)
  274. {
  275. Mesh *t = luax_checkmesh(L, 1);
  276. if (lua_isnoneornil(L, 2))
  277. {
  278. // Disable the vertex map / index buffer.
  279. luax_catchexcept(L, [&](){ t->setVertexMap(); });
  280. return 0;
  281. }
  282. bool is_table = lua_istable(L, 2);
  283. int nargs = is_table ? (int) luax_objlen(L, 2) : lua_gettop(L) - 1;
  284. std::vector<uint32> vertexmap;
  285. vertexmap.reserve(nargs);
  286. if (is_table)
  287. {
  288. for (int i = 0; i < nargs; i++)
  289. {
  290. lua_rawgeti(L, 2, i + 1);
  291. vertexmap.push_back(uint32(luaL_checkinteger(L, -1) - 1));
  292. lua_pop(L, 1);
  293. }
  294. }
  295. else
  296. {
  297. for (int i = 0; i < nargs; i++)
  298. vertexmap.push_back(uint32(luaL_checkinteger(L, i + 2) - 1));
  299. }
  300. luax_catchexcept(L, [&](){ t->setVertexMap(vertexmap); });
  301. return 0;
  302. }
  303. int w_Mesh_getVertexMap(lua_State *L)
  304. {
  305. Mesh *t = luax_checkmesh(L, 1);
  306. std::vector<uint32> vertex_map;
  307. bool has_vertex_map = false;
  308. luax_catchexcept(L, [&](){ has_vertex_map = t->getVertexMap(vertex_map); });
  309. if (!has_vertex_map)
  310. {
  311. lua_pushnil(L);
  312. return 1;
  313. }
  314. int element_count = (int) vertex_map.size();
  315. lua_createtable(L, element_count, 0);
  316. for (int i = 0; i < element_count; i++)
  317. {
  318. lua_pushinteger(L, lua_Integer(vertex_map[i]) + 1);
  319. lua_rawseti(L, -2, i + 1);
  320. }
  321. return 1;
  322. }
  323. int w_Mesh_setTexture(lua_State *L)
  324. {
  325. Mesh *t = luax_checkmesh(L, 1);
  326. if (lua_isnoneornil(L, 2))
  327. t->setTexture();
  328. else
  329. {
  330. Texture *tex = luax_checktexture(L, 2);
  331. t->setTexture(tex);
  332. }
  333. return 0;
  334. }
  335. int w_Mesh_getTexture(lua_State *L)
  336. {
  337. Mesh *t = luax_checkmesh(L, 1);
  338. Texture *tex = t->getTexture();
  339. if (tex == nullptr)
  340. return 0;
  341. // FIXME: big hack right here.
  342. if (typeid(*tex) == typeid(Image))
  343. luax_pushtype(L, GRAPHICS_IMAGE_ID, tex);
  344. else if (typeid(*tex) == typeid(Canvas))
  345. luax_pushtype(L, GRAPHICS_CANVAS_ID, tex);
  346. else
  347. return luaL_error(L, "Unable to determine texture type.");
  348. return 1;
  349. }
  350. int w_Mesh_setDrawMode(lua_State *L)
  351. {
  352. Mesh *t = luax_checkmesh(L, 1);
  353. const char *str = luaL_checkstring(L, 2);
  354. Mesh::DrawMode mode;
  355. if (!Mesh::getConstant(str, mode))
  356. return luaL_error(L, "Invalid mesh draw mode: %s", str);
  357. t->setDrawMode(mode);
  358. return 0;
  359. }
  360. int w_Mesh_getDrawMode(lua_State *L)
  361. {
  362. Mesh *t = luax_checkmesh(L, 1);
  363. Mesh::DrawMode mode = t->getDrawMode();
  364. const char *str;
  365. if (!Mesh::getConstant(mode, str))
  366. return luaL_error(L, "Unknown mesh draw mode.");
  367. lua_pushstring(L, str);
  368. return 1;
  369. }
  370. int w_Mesh_setDrawRange(lua_State *L)
  371. {
  372. Mesh *t = luax_checkmesh(L, 1);
  373. if (lua_isnoneornil(L, 2))
  374. t->setDrawRange();
  375. else
  376. {
  377. int rangemin = (int) luaL_checknumber(L, 2) - 1;
  378. int rangemax = (int) luaL_checknumber(L, 3) - 1;
  379. luax_catchexcept(L, [&](){ t->setDrawRange(rangemin, rangemax); });
  380. }
  381. return 0;
  382. }
  383. int w_Mesh_getDrawRange(lua_State *L)
  384. {
  385. Mesh *t = luax_checkmesh(L, 1);
  386. int rangemin = -1;
  387. int rangemax = -1;
  388. t->getDrawRange(rangemin, rangemax);
  389. if (rangemin < 0 || rangemax < 0)
  390. return 0;
  391. lua_pushinteger(L, rangemin + 1);
  392. lua_pushinteger(L, rangemax + 1);
  393. return 2;
  394. }
  395. static const luaL_Reg w_Mesh_functions[] =
  396. {
  397. { "setVertices", w_Mesh_setVertices },
  398. { "setVertex", w_Mesh_setVertex },
  399. { "getVertex", w_Mesh_getVertex },
  400. { "setVertexAttribute", w_Mesh_setVertexAttribute },
  401. { "getVertexAttribute", w_Mesh_getVertexAttribute },
  402. { "getVertexCount", w_Mesh_getVertexCount },
  403. { "getVertexFormat", w_Mesh_getVertexFormat },
  404. { "setAttributeEnabled", w_Mesh_setAttributeEnabled },
  405. { "isAttributeEnabled", w_Mesh_isAttributeEnabled },
  406. { "attachAttribute", w_Mesh_attachAttribute },
  407. { "flush", w_Mesh_flush },
  408. { "setVertexMap", w_Mesh_setVertexMap },
  409. { "getVertexMap", w_Mesh_getVertexMap },
  410. { "setTexture", w_Mesh_setTexture },
  411. { "getTexture", w_Mesh_getTexture },
  412. { "setDrawMode", w_Mesh_setDrawMode },
  413. { "getDrawMode", w_Mesh_getDrawMode },
  414. { "setDrawRange", w_Mesh_setDrawRange },
  415. { "getDrawRange", w_Mesh_getDrawRange },
  416. { 0, 0 }
  417. };
  418. extern "C" int luaopen_mesh(lua_State *L)
  419. {
  420. return luax_register_type(L, GRAPHICS_MESH_ID, "Mesh", w_Mesh_functions, nullptr);
  421. }
  422. } // opengl
  423. } // graphics
  424. } // love