PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lua3d.c

http://luaplayereuphoria.googlecode.com/
C | 510 lines | 433 code | 41 blank | 36 comment | 67 complexity | 9ed48741af688d6b8a72777af8c858f2 MD5 | raw file
  1. /*
  2. * LuaPlayer Euphoria
  3. * ------------------------------------------------------------------------
  4. * Licensed under the BSD license, see LICENSE for details.
  5. *
  6. * Copyright (c) 2005 Frank Buss <fb@frank-buss.de> (aka Shine)
  7. * Copyright (c) 2009 Danny Glover <danny86@live.ie> (aka Zack)
  8. *
  9. * Official Forum : http://www.retroemu.com/forum/forumdisplay.php?f=148
  10. * For help using LuaPlayer, code help, tutorials etc please visit the official site : http://www.retroemu.com/forum/forumdisplay.php?f=148
  11. *
  12. * Credits:
  13. *
  14. * (from Shine/Zack)
  15. *
  16. * many thanks to the authors of the PSPSDK from http://forums.ps2dev.org
  17. * and to the hints and discussions from #pspdev on freenode.net
  18. *
  19. * (from Zack Only)
  20. *
  21. * Thanks to Brunni for the Swizzle/UnSwizzle code (taken from oslib).
  22. * Thanks to Arshia001 for AALIB. It is the sound engine used in LuaPlayer Euphoria.
  23. * Thanks to HardHat for being a supportive friend and advisor.
  24. * Thanks to Benhur for IntraFont.
  25. * Thanks to Jono for the moveToVram code.
  26. * Thanks to Raphael for the Vram manager code.
  27. * Thanks to Osgeld, Dan369 & Cmbeke for testing LuaPlayer Euphoria for me and coming up with some neat ideas for it.
  28. * Thanks to the entire LuaPlayer Euphoria userbase, for using it and for supporting it's development. You guys rock!
  29. *
  30. *
  31. */
  32. #include <malloc.h>
  33. #include <pspdisplay.h>
  34. #include <psputils.h>
  35. #include <pspgu.h>
  36. #include <pspgum.h>
  37. #include "include/luaplayer.h"
  38. #include "libs/graphics/graphics.h"
  39. PspGeContext __attribute__((aligned(16))) geContext;
  40. extern Color* toColor(lua_State *L, int arg);
  41. extern Image** toImage(lua_State *L, int arg);
  42. static int lua_sceGuClearColor(lua_State *L) {
  43. int argc = lua_gettop(L);
  44. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  45. sceGuClearColor(*toColor(L, 1));
  46. return 0;
  47. }
  48. static int lua_sceGuClearDepth(lua_State *L) {
  49. int argc = lua_gettop(L);
  50. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  51. sceGuClearDepth(luaL_checkint(L, 1));
  52. return 0;
  53. }
  54. static int lua_sceGuClear(lua_State *L) {
  55. int argc = lua_gettop(L);
  56. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  57. sceGuClear(luaL_checkint(L, 1));
  58. return 0;
  59. }
  60. static int lua_sceGumMatrixMode(lua_State *L) {
  61. int argc = lua_gettop(L);
  62. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  63. sceGumMatrixMode(luaL_checkint(L, 1));
  64. return 0;
  65. }
  66. static int lua_sceGumLoadIdentity(lua_State *L) {
  67. int argc = lua_gettop(L);
  68. if (argc != 0) return luaL_error(L, "wrong number of arguments");
  69. sceGumLoadIdentity();
  70. return 0;
  71. }
  72. static int lua_sceGumPerspective(lua_State *L) {
  73. int argc = lua_gettop(L);
  74. if (argc != 4) return luaL_error(L, "wrong number of arguments");
  75. sceGumPerspective(luaL_checknumber(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4));
  76. return 0;
  77. }
  78. static int lua_sceGumTranslate(lua_State *L) {
  79. int argc = lua_gettop(L);
  80. if (argc != 3) return luaL_error(L, "wrong number of arguments");
  81. ScePspFVector3 v;
  82. v.x = luaL_checknumber(L, 1);
  83. v.y = luaL_checknumber(L, 2);
  84. v.z = luaL_checknumber(L, 3);
  85. sceGumTranslate(&v);
  86. return 0;
  87. }
  88. static int lua_sceGumRotateXYZ(lua_State *L) {
  89. int argc = lua_gettop(L);
  90. if (argc != 3) return luaL_error(L, "wrong number of arguments");
  91. ScePspFVector3 v;
  92. v.x = luaL_checknumber(L, 1);
  93. v.y = luaL_checknumber(L, 2);
  94. v.z = luaL_checknumber(L, 3);
  95. sceGumRotateXYZ(&v);
  96. return 0;
  97. }
  98. static int lua_sceGuTexImage(lua_State *L) {
  99. int argc = lua_gettop(L);
  100. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  101. Image* image = *toImage(L, 1);
  102. sceGuTexImage(0, image->textureWidth, image->textureHeight, image->textureWidth, image->data);
  103. return 0;
  104. }
  105. static int lua_sceGuTexFunc(lua_State *L) {
  106. int argc = lua_gettop(L);
  107. if (argc != 2) return luaL_error(L, "wrong number of arguments");
  108. sceGuTexFunc(luaL_checkint(L, 1), luaL_checkint(L, 2));
  109. return 0;
  110. }
  111. static int lua_sceGuTexEnvColor(lua_State *L) {
  112. int argc = lua_gettop(L);
  113. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  114. sceGuTexEnvColor(*toColor(L, 1));
  115. return 0;
  116. }
  117. static int lua_sceGuTexFilter(lua_State *L) {
  118. int argc = lua_gettop(L);
  119. if (argc != 2) return luaL_error(L, "wrong number of arguments");
  120. sceGuTexFilter(luaL_checkint(L, 1), luaL_checkint(L, 2));
  121. return 0;
  122. }
  123. static int lua_sceGuTexScale(lua_State *L) {
  124. int argc = lua_gettop(L);
  125. if (argc != 2) return luaL_error(L, "wrong number of arguments");
  126. sceGuTexScale(luaL_checknumber(L, 1), luaL_checknumber(L, 2));
  127. return 0;
  128. }
  129. static int lua_sceGuTexOffset(lua_State *L) {
  130. int argc = lua_gettop(L);
  131. if (argc != 2) return luaL_error(L, "wrong number of arguments");
  132. sceGuTexOffset(luaL_checknumber(L, 1), luaL_checknumber(L, 2));
  133. return 0;
  134. }
  135. static int lua_sceGuAmbientColor(lua_State *L) {
  136. int argc = lua_gettop(L);
  137. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  138. sceGuAmbientColor(*toColor(L, 1));
  139. return 0;
  140. }
  141. static int lua_sceGuAmbient(lua_State *L) {
  142. int argc = lua_gettop(L);
  143. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  144. sceGuAmbient(*toColor(L, 1));
  145. return 0;
  146. }
  147. static int lua_sceGuEnable(lua_State *L) {
  148. int argc = lua_gettop(L);
  149. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  150. sceGuEnable(luaL_checkint(L, 1));
  151. return 0;
  152. }
  153. static int lua_sceGuDisable(lua_State *L) {
  154. int argc = lua_gettop(L);
  155. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  156. sceGuDisable(luaL_checkint(L, 1));
  157. return 0;
  158. }
  159. static int lua_sceGuBlendFunc(lua_State *L) {
  160. int argc = lua_gettop(L);
  161. if (argc != 5) return luaL_error(L, "wrong number of arguments");
  162. sceGuBlendFunc(luaL_checkint(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3), luaL_checkint(L, 4), luaL_checkint(L, 5));
  163. return 0;
  164. }
  165. static int lua_sceGuLight(lua_State *L) {
  166. int argc = lua_gettop(L);
  167. if (argc != 6) return luaL_error(L, "wrong number of arguments");
  168. ScePspFVector3 v;
  169. v.x = luaL_checknumber(L, 4);
  170. v.y = luaL_checknumber(L, 5);
  171. v.z = luaL_checknumber(L, 6);
  172. sceGuLight(luaL_checkint(L, 1), luaL_checkint(L, 2), luaL_checkint(L, 3), &v);
  173. return 0;
  174. }
  175. static int lua_sceGuLightAtt(lua_State *L) {
  176. int argc = lua_gettop(L);
  177. if (argc != 4) return luaL_error(L, "wrong number of arguments");
  178. sceGuLightAtt(luaL_checkint(L, 1), luaL_checknumber(L, 2), luaL_checknumber(L, 3), luaL_checknumber(L, 4));
  179. return 0;
  180. }
  181. static int lua_sceGuLightColor(lua_State *L) {
  182. int argc = lua_gettop(L);
  183. if (argc != 3) return luaL_error(L, "wrong number of arguments");
  184. sceGuLightColor(luaL_checkint(L, 1), luaL_checkint(L, 2), *toColor(L, 3));
  185. return 0;
  186. }
  187. static int lua_sceGuLightMode(lua_State *L) {
  188. int argc = lua_gettop(L);
  189. if (argc != 1) return luaL_error(L, "wrong number of arguments");
  190. sceGuLightMode(luaL_checkint(L, 1));
  191. return 0;
  192. }
  193. static int lua_sceGuLightSpot(lua_State *L) {
  194. int argc = lua_gettop(L);
  195. if (argc != 6) return luaL_error(L, "wrong number of arguments");
  196. ScePspFVector3 v;
  197. v.x = luaL_checknumber(L, 2);
  198. v.y = luaL_checknumber(L, 3);
  199. v.z = luaL_checknumber(L, 4);
  200. sceGuLightSpot(luaL_checkint(L, 1), &v, luaL_checknumber(L, 5), luaL_checknumber(L, 6));
  201. return 0;
  202. }
  203. static int lua_sceGumDrawArray(lua_State *L) {
  204. int argc = lua_gettop(L);
  205. if (argc != 3) return luaL_error(L, "wrong number of arguments");
  206. int prim = luaL_checkint(L, 1);
  207. int vtype = luaL_checkint(L, 2);
  208. if (lua_type(L, 3) != LUA_TTABLE) return luaL_error(L, "vertices table missing");
  209. int n = luaL_getn(L, 3);
  210. int quads = 0;
  211. int colorLuaIndex = -1;
  212. if (vtype & GU_TEXTURE_32BITF) quads += 2;
  213. if (vtype & GU_COLOR_8888) {
  214. quads++;
  215. colorLuaIndex = quads;
  216. }
  217. if (vtype & GU_NORMAL_32BITF) quads += 3;
  218. if (vtype & GU_VERTEX_32BITF) quads += 3;
  219. void* vertices = memalign(16, n * quads*4);
  220. float* vertex = (float*) vertices;
  221. int i;
  222. for (i = 1; i <= n; ++i) {
  223. // get vertice table
  224. lua_rawgeti(L, 3, i);
  225. int n2 = luaL_getn(L, -1);
  226. if (n2 != quads) {
  227. free(vertices);
  228. return luaL_error(L, "wrong number of vertex components");
  229. }
  230. int j;
  231. for (j = 1; j <= n2; ++j) {
  232. lua_rawgeti(L, -1, j);
  233. if (j != colorLuaIndex) {
  234. *vertex = luaL_checknumber(L, -1);
  235. } else {
  236. *((Color*) vertex) = *toColor(L, -1);
  237. }
  238. lua_pop(L, 1); // removes 'value'
  239. vertex++;
  240. }
  241. // remove vertice table
  242. lua_pop(L, 1);
  243. }
  244. sceKernelDcacheWritebackInvalidateAll();
  245. sceGumDrawArray(prim, vtype, n, NULL, vertices);
  246. free(vertices);
  247. return 0;
  248. }
  249. static int lua_start3d(lua_State *L) {
  250. int argc = lua_gettop(L);
  251. if (argc != 0) return luaL_error(L, "wrong number of arguments");
  252. sceGeSaveContext(&geContext);
  253. guStart();
  254. return 0;
  255. }
  256. static int lua_end3d(lua_State *L) {
  257. int argc = lua_gettop(L);
  258. if (argc != 0) return luaL_error(L, "wrong number of arguments");
  259. guEnd();
  260. sceGeRestoreContext(&geContext);
  261. return 0;
  262. }
  263. static const luaL_reg Gu_functions[] = {
  264. {"clearColor", lua_sceGuClearColor},
  265. {"clearDepth", lua_sceGuClearDepth},
  266. {"clear", lua_sceGuClear},
  267. {"texImage", lua_sceGuTexImage},
  268. {"texFunc", lua_sceGuTexFunc},
  269. {"texEnvColor", lua_sceGuTexEnvColor},
  270. {"texFilter", lua_sceGuTexFilter},
  271. {"texScale", lua_sceGuTexScale},
  272. {"texOffset", lua_sceGuTexOffset},
  273. {"ambientColor", lua_sceGuAmbientColor},
  274. {"ambient", lua_sceGuAmbient},
  275. {"enable", lua_sceGuEnable},
  276. {"disable", lua_sceGuDisable},
  277. {"blendFunc", lua_sceGuBlendFunc},
  278. {"light", lua_sceGuLight},
  279. {"lightAtt", lua_sceGuLightAtt},
  280. {"lightColor", lua_sceGuLightColor},
  281. {"lightMode", lua_sceGuLightMode},
  282. {"lightSpot", lua_sceGuLightSpot},
  283. {"start3d", lua_start3d},
  284. {"end3d", lua_end3d},
  285. {0, 0}
  286. };
  287. static const luaL_reg Gum_functions[] = {
  288. {"matrixMode", lua_sceGumMatrixMode},
  289. {"loadIdentity", lua_sceGumLoadIdentity},
  290. {"perspective", lua_sceGumPerspective},
  291. {"translate", lua_sceGumTranslate},
  292. {"rotateXYZ", lua_sceGumRotateXYZ},
  293. {"drawArray", lua_sceGumDrawArray},
  294. {0, 0}
  295. };
  296. void lua3D_init(lua_State *L) {
  297. luaL_openlib(L, "Gu", Gu_functions, 0);
  298. luaL_openlib(L, "Gum", Gum_functions, 0);
  299. #define GU_CONSTANT(name)\
  300. lua_pushstring(L, #name);\
  301. lua_pushnumber(L, GU_##name);\
  302. lua_settable(L, -3);
  303. lua_pushstring(L, "Gu");
  304. lua_gettable(L, LUA_GLOBALSINDEX);
  305. GU_CONSTANT(PI)
  306. GU_CONSTANT(FALSE)
  307. GU_CONSTANT(TRUE)
  308. GU_CONSTANT(POINTS)
  309. GU_CONSTANT(LINES)
  310. GU_CONSTANT(LINE_STRIP)
  311. GU_CONSTANT(TRIANGLES)
  312. GU_CONSTANT(TRIANGLE_STRIP)
  313. GU_CONSTANT(TRIANGLE_FAN)
  314. GU_CONSTANT(SPRITES)
  315. GU_CONSTANT(ALPHA_TEST)
  316. GU_CONSTANT(DEPTH_TEST)
  317. GU_CONSTANT(SCISSOR_TEST)
  318. GU_CONSTANT(STENCIL_TEST)
  319. GU_CONSTANT(BLEND)
  320. GU_CONSTANT(CULL_FACE)
  321. GU_CONSTANT(DITHER)
  322. GU_CONSTANT(FOG)
  323. GU_CONSTANT(CLIP_PLANES)
  324. GU_CONSTANT(TEXTURE_2D)
  325. GU_CONSTANT(LIGHTING)
  326. GU_CONSTANT(LIGHT0)
  327. GU_CONSTANT(LIGHT1)
  328. GU_CONSTANT(LIGHT2)
  329. GU_CONSTANT(LIGHT3)
  330. GU_CONSTANT(LINE_SMOOTH)
  331. GU_CONSTANT(PATCH_CULL_FACE)
  332. GU_CONSTANT(COLOR_TEST)
  333. GU_CONSTANT(COLOR_LOGIC_OP)
  334. GU_CONSTANT(FACE_NORMAL_REVERSE)
  335. GU_CONSTANT(PATCH_FACE)
  336. GU_CONSTANT(FRAGMENT_2X)
  337. GU_CONSTANT(PROJECTION)
  338. GU_CONSTANT(VIEW)
  339. GU_CONSTANT(MODEL)
  340. GU_CONSTANT(TEXTURE)
  341. GU_CONSTANT(TEXTURE_8BIT)
  342. GU_CONSTANT(TEXTURE_16BIT)
  343. GU_CONSTANT(TEXTURE_32BITF)
  344. GU_CONSTANT(TEXTURE_BITS)
  345. //GU_CONSTANT(COLOR_RES1)
  346. //GU_CONSTANT(COLOR_RES2)
  347. //GU_CONSTANT(COLOR_RES3)
  348. GU_CONSTANT(COLOR_5650)
  349. GU_CONSTANT(COLOR_5551)
  350. GU_CONSTANT(COLOR_4444)
  351. GU_CONSTANT(COLOR_8888)
  352. GU_CONSTANT(COLOR_BITS)
  353. GU_CONSTANT(NORMAL_8BIT)
  354. GU_CONSTANT(NORMAL_16BIT)
  355. GU_CONSTANT(NORMAL_32BITF)
  356. GU_CONSTANT(NORMAL_BITS)
  357. GU_CONSTANT(VERTEX_8BIT)
  358. GU_CONSTANT(VERTEX_16BIT)
  359. GU_CONSTANT(VERTEX_32BITF)
  360. GU_CONSTANT(VERTEX_BITS)
  361. GU_CONSTANT(WEIGHT_8BIT)
  362. GU_CONSTANT(WEIGHT_16BIT)
  363. GU_CONSTANT(WEIGHT_32BITF)
  364. GU_CONSTANT(WEIGHT_BITS)
  365. GU_CONSTANT(INDEX_8BIT)
  366. GU_CONSTANT(INDEX_16BIT)
  367. GU_CONSTANT(INDEX_BITS)
  368. GU_CONSTANT(WEIGHTS_BITS)
  369. GU_CONSTANT(VERTICES_BITS)
  370. GU_CONSTANT(TRANSFORM_3D)
  371. GU_CONSTANT(TRANSFORM_2D)
  372. GU_CONSTANT(TRANSFORM_BITS)
  373. GU_CONSTANT(PSM_5650)
  374. GU_CONSTANT(PSM_5551)
  375. GU_CONSTANT(PSM_4444)
  376. GU_CONSTANT(PSM_8888)
  377. GU_CONSTANT(PSM_T4)
  378. GU_CONSTANT(PSM_T8)
  379. GU_CONSTANT(PSM_T16)
  380. GU_CONSTANT(PSM_T32)
  381. GU_CONSTANT(FLAT)
  382. GU_CONSTANT(SMOOTH)
  383. GU_CONSTANT(CLEAR)
  384. GU_CONSTANT(AND)
  385. GU_CONSTANT(AND_REVERSE)
  386. GU_CONSTANT(COPY)
  387. GU_CONSTANT(AND_INVERTED)
  388. GU_CONSTANT(NOOP)
  389. GU_CONSTANT(XOR)
  390. GU_CONSTANT(OR)
  391. GU_CONSTANT(NOR)
  392. GU_CONSTANT(EQUIV)
  393. GU_CONSTANT(INVERTED)
  394. GU_CONSTANT(OR_REVERSE)
  395. GU_CONSTANT(COPY_INVERTED)
  396. GU_CONSTANT(OR_INVERTED)
  397. GU_CONSTANT(NAND)
  398. GU_CONSTANT(SET)
  399. GU_CONSTANT(NEAREST)
  400. GU_CONSTANT(LINEAR)
  401. GU_CONSTANT(NEAREST_MIPMAP_NEAREST)
  402. GU_CONSTANT(LINEAR_MIPMAP_NEAREST)
  403. GU_CONSTANT(NEAREST_MIPMAP_LINEAR)
  404. GU_CONSTANT(LINEAR_MIPMAP_LINEAR)
  405. GU_CONSTANT(TEXTURE_COORDS)
  406. GU_CONSTANT(TEXTURE_MATRIX)
  407. GU_CONSTANT(ENVIRONMENT_MAP)
  408. GU_CONSTANT(POSITION)
  409. GU_CONSTANT(UV)
  410. GU_CONSTANT(NORMALIZED_NORMAL)
  411. GU_CONSTANT(NORMAL)
  412. GU_CONSTANT(REPEAT)
  413. GU_CONSTANT(CLAMP)
  414. GU_CONSTANT(CW)
  415. GU_CONSTANT(CCW)
  416. GU_CONSTANT(NEVER)
  417. GU_CONSTANT(ALWAYS)
  418. GU_CONSTANT(EQUAL)
  419. GU_CONSTANT(NOTEQUAL)
  420. GU_CONSTANT(LESS)
  421. GU_CONSTANT(LEQUAL)
  422. GU_CONSTANT(GREATER)
  423. GU_CONSTANT(GEQUAL)
  424. GU_CONSTANT(COLOR_BUFFER_BIT)
  425. GU_CONSTANT(STENCIL_BUFFER_BIT)
  426. GU_CONSTANT(DEPTH_BUFFER_BIT)
  427. GU_CONSTANT(TFX_MODULATE)
  428. GU_CONSTANT(TFX_DECAL)
  429. GU_CONSTANT(TFX_BLEND)
  430. GU_CONSTANT(TFX_REPLACE)
  431. GU_CONSTANT(TFX_ADD)
  432. GU_CONSTANT(TCC_RGB)
  433. GU_CONSTANT(TCC_RGBA)
  434. GU_CONSTANT(ADD)
  435. GU_CONSTANT(SUBTRACT)
  436. GU_CONSTANT(REVERSE_SUBTRACT)
  437. GU_CONSTANT(MIN)
  438. GU_CONSTANT(MAX)
  439. GU_CONSTANT(ABS)
  440. GU_CONSTANT(SRC_COLOR)
  441. GU_CONSTANT(ONE_MINUS_SRC_COLOR)
  442. GU_CONSTANT(SRC_ALPHA)
  443. GU_CONSTANT(ONE_MINUS_SRC_ALPHA)
  444. GU_CONSTANT(DST_COLOR)
  445. GU_CONSTANT(ONE_MINUS_DST_COLOR)
  446. GU_CONSTANT(DST_ALPHA)
  447. GU_CONSTANT(ONE_MINUS_DST_ALPHA)
  448. GU_CONSTANT(FIX)
  449. GU_CONSTANT(KEEP)
  450. GU_CONSTANT(ZERO)
  451. GU_CONSTANT(REPLACE)
  452. GU_CONSTANT(INVERT)
  453. GU_CONSTANT(INCR)
  454. GU_CONSTANT(DECR)
  455. GU_CONSTANT(AMBIENT)
  456. GU_CONSTANT(DIFFUSE)
  457. GU_CONSTANT(SPECULAR)
  458. GU_CONSTANT(AMBIENT_AND_DIFFUSE)
  459. GU_CONSTANT(DIFFUSE_AND_SPECULAR)
  460. GU_CONSTANT(UNKNOWN_LIGHT_COMPONENT)
  461. GU_CONSTANT(DIRECTIONAL)
  462. GU_CONSTANT(POINTLIGHT)
  463. GU_CONSTANT(SPOTLIGHT)
  464. GU_CONSTANT(DIRECT)
  465. GU_CONSTANT(CALL)
  466. GU_CONSTANT(SEND)
  467. GU_CONSTANT(TAIL)
  468. GU_CONSTANT(HEAD)
  469. }