wrap_Mesh.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. int nvertices = (int) 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 (int 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. const char *attachname = name;
  264. Mesh *mesh = nullptr;
  265. if (lua_isnoneornil(L, 3))
  266. {
  267. mesh = luax_checkmesh(L, 3);
  268. attachname = luaL_optstring(L, 4, attachname);
  269. }
  270. luax_catchexcept(L, [&](){ t->attachAttribute(name, mesh, attachname); });
  271. return 0;
  272. }
  273. int w_Mesh_flush(lua_State *L)
  274. {
  275. Mesh *t = luax_checkmesh(L, 1);
  276. t->flush();
  277. return 0;
  278. }
  279. int w_Mesh_setVertexMap(lua_State *L)
  280. {
  281. Mesh *t = luax_checkmesh(L, 1);
  282. if (lua_isnoneornil(L, 2))
  283. {
  284. // Disable the vertex map / index buffer.
  285. luax_catchexcept(L, [&](){ t->setVertexMap(); });
  286. return 0;
  287. }
  288. bool is_table = lua_istable(L, 2);
  289. int nargs = is_table ? (int) luax_objlen(L, 2) : lua_gettop(L) - 1;
  290. std::vector<uint32> vertexmap;
  291. vertexmap.reserve(nargs);
  292. if (is_table)
  293. {
  294. for (int i = 0; i < nargs; i++)
  295. {
  296. lua_rawgeti(L, 2, i + 1);
  297. vertexmap.push_back(uint32(luaL_checkinteger(L, -1) - 1));
  298. lua_pop(L, 1);
  299. }
  300. }
  301. else
  302. {
  303. for (int i = 0; i < nargs; i++)
  304. vertexmap.push_back(uint32(luaL_checkinteger(L, i + 2) - 1));
  305. }
  306. luax_catchexcept(L, [&](){ t->setVertexMap(vertexmap); });
  307. return 0;
  308. }
  309. int w_Mesh_getVertexMap(lua_State *L)
  310. {
  311. Mesh *t = luax_checkmesh(L, 1);
  312. std::vector<uint32> vertex_map;
  313. bool has_vertex_map = false;
  314. luax_catchexcept(L, [&](){ has_vertex_map = t->getVertexMap(vertex_map); });
  315. if (!has_vertex_map)
  316. {
  317. lua_pushnil(L);
  318. return 1;
  319. }
  320. int element_count = (int) vertex_map.size();
  321. lua_createtable(L, element_count, 0);
  322. for (int i = 0; i < element_count; i++)
  323. {
  324. lua_pushinteger(L, lua_Integer(vertex_map[i]) + 1);
  325. lua_rawseti(L, -2, i + 1);
  326. }
  327. return 1;
  328. }
  329. int w_Mesh_setTexture(lua_State *L)
  330. {
  331. Mesh *t = luax_checkmesh(L, 1);
  332. if (lua_isnoneornil(L, 2))
  333. t->setTexture();
  334. else
  335. {
  336. Texture *tex = luax_checktexture(L, 2);
  337. t->setTexture(tex);
  338. }
  339. return 0;
  340. }
  341. int w_Mesh_getTexture(lua_State *L)
  342. {
  343. Mesh *t = luax_checkmesh(L, 1);
  344. Texture *tex = t->getTexture();
  345. if (tex == nullptr)
  346. return 0;
  347. // FIXME: big hack right here.
  348. if (typeid(*tex) == typeid(Image))
  349. luax_pushtype(L, GRAPHICS_IMAGE_ID, tex);
  350. else if (typeid(*tex) == typeid(Canvas))
  351. luax_pushtype(L, GRAPHICS_CANVAS_ID, tex);
  352. else
  353. return luaL_error(L, "Unable to determine texture type.");
  354. return 1;
  355. }
  356. int w_Mesh_setDrawMode(lua_State *L)
  357. {
  358. Mesh *t = luax_checkmesh(L, 1);
  359. const char *str = luaL_checkstring(L, 2);
  360. Mesh::DrawMode mode;
  361. if (!Mesh::getConstant(str, mode))
  362. return luaL_error(L, "Invalid mesh draw mode: %s", str);
  363. t->setDrawMode(mode);
  364. return 0;
  365. }
  366. int w_Mesh_getDrawMode(lua_State *L)
  367. {
  368. Mesh *t = luax_checkmesh(L, 1);
  369. Mesh::DrawMode mode = t->getDrawMode();
  370. const char *str;
  371. if (!Mesh::getConstant(mode, str))
  372. return luaL_error(L, "Unknown mesh draw mode.");
  373. lua_pushstring(L, str);
  374. return 1;
  375. }
  376. int w_Mesh_setDrawRange(lua_State *L)
  377. {
  378. Mesh *t = luax_checkmesh(L, 1);
  379. if (lua_isnoneornil(L, 2))
  380. t->setDrawRange();
  381. else
  382. {
  383. int start = (int) luaL_checknumber(L, 2) - 1;
  384. int count = (int) luaL_checknumber(L, 3);
  385. luax_catchexcept(L, [&](){ t->setDrawRange(start, count); });
  386. }
  387. return 0;
  388. }
  389. int w_Mesh_getDrawRange(lua_State *L)
  390. {
  391. Mesh *t = luax_checkmesh(L, 1);
  392. int start = 0;
  393. int count = 1;
  394. if (!t->getDrawRange(start, count))
  395. return 0;
  396. lua_pushinteger(L, start + 1);
  397. lua_pushinteger(L, count);
  398. return 2;
  399. }
  400. static const luaL_Reg w_Mesh_functions[] =
  401. {
  402. { "setVertices", w_Mesh_setVertices },
  403. { "setVertex", w_Mesh_setVertex },
  404. { "getVertex", w_Mesh_getVertex },
  405. { "setVertexAttribute", w_Mesh_setVertexAttribute },
  406. { "getVertexAttribute", w_Mesh_getVertexAttribute },
  407. { "getVertexCount", w_Mesh_getVertexCount },
  408. { "getVertexFormat", w_Mesh_getVertexFormat },
  409. { "setAttributeEnabled", w_Mesh_setAttributeEnabled },
  410. { "isAttributeEnabled", w_Mesh_isAttributeEnabled },
  411. { "attachAttribute", w_Mesh_attachAttribute },
  412. { "flush", w_Mesh_flush },
  413. { "setVertexMap", w_Mesh_setVertexMap },
  414. { "getVertexMap", w_Mesh_getVertexMap },
  415. { "setTexture", w_Mesh_setTexture },
  416. { "getTexture", w_Mesh_getTexture },
  417. { "setDrawMode", w_Mesh_setDrawMode },
  418. { "getDrawMode", w_Mesh_getDrawMode },
  419. { "setDrawRange", w_Mesh_setDrawRange },
  420. { "getDrawRange", w_Mesh_getDrawRange },
  421. { 0, 0 }
  422. };
  423. extern "C" int luaopen_mesh(lua_State *L)
  424. {
  425. return luax_register_type(L, GRAPHICS_MESH_ID, "Mesh", w_Mesh_functions, nullptr);
  426. }
  427. } // opengl
  428. } // graphics
  429. } // love