dump.h 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /*
  2. * m3dconv/dump.h
  3. *
  4. * Copyright (C) 2019 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief dump a Model 3D (file or in-memory) to stdout
  27. * https://gitlab.com/bztsrc/model3d
  28. *
  29. */
  30. #include <string.h>
  31. #ifndef dumplog
  32. #define dumplog printf
  33. #endif
  34. #ifndef dumperr
  35. #define dumperr fprintf
  36. #endif
  37. /**
  38. * Dump and validate binary model file byte-by-byte
  39. */
  40. int dump_file(unsigned char *data, unsigned int size, int dump)
  41. {
  42. unsigned char *out = NULL, *end = NULL, *zip = NULL, *chend, weights[8];
  43. char *str;
  44. static char *cn[] = { "int8_t", "int16_t", "float", "double" };
  45. static char *in[] = { "uint8_t", "uint16_t", "uint32_t", "(none)" };
  46. static char *pf[] = { "color ", "uint8 ", "uint16", "uint32", "float ", "map " };
  47. unsigned int len, vc_s, vi_s, si_s, ci_s, ti_s, bi_s, nb_s, sk_s, fc_s, hi_s, fi_s, vd_s, vp_s, *cmap = NULL;
  48. int i, j, k, l, m, n, d, nv = 0, nt = 0, np = 0, nb = 0, nc = 0, nvt = 0;
  49. m3dcd_t *cd;
  50. if(!data || !size) return 1;
  51. if(M3D_CHUNKMAGIC(data, '3','D','M','O')) {
  52. dumplog("Model 3D File Header\n"
  53. " Magic: 3DMO\n"
  54. " Length: %u\n", ((m3dchunk_t*)data)->length);
  55. if(((m3dchunk_t*)data)->length != size) {
  56. dumperr(stderr, "ERROR: Invalid 3DMO chunk length %u (should be %u)\n",((m3dchunk_t*)data)->length,size);
  57. return 1;
  58. }
  59. if(M3D_CHUNKMAGIC(data, 'P','R','V','W')) {
  60. dumplog("\nPreview Chunk\n Magic: PRVW\n"
  61. " Length: %u\n\n",
  62. ((m3dhdr_t*)data)->length);
  63. size -= ((m3dhdr_t*)data)->length;
  64. data += ((m3dhdr_t*)data)->length;
  65. }
  66. dumplog(" Data: %scompressed", M3D_CHUNKMAGIC(data + 8, 'H','E','A','D') ? "un":"stream ");
  67. if(!M3D_CHUNKMAGIC(data + 8, 'H','E','A','D')) {
  68. zip = (unsigned char *)stbi_zlib_decode_malloc_guesssize_headerflag((const char*)data+8,
  69. size-8, 16384, (int*)&size, 1);
  70. if(!zip || !size || !M3D_CHUNKMAGIC(zip, 'H','E','A','D')) {
  71. dumperr(stderr, "m3dconv: uncompression error\n");
  72. return 1;
  73. }
  74. dumplog(" (inflated %u bytes)", size + 8);
  75. data = zip;
  76. } else {
  77. data += 8;
  78. size -= 8;
  79. }
  80. dumplog("\n\n");
  81. end = data + size;
  82. while(data < end) {
  83. if(M3D_CHUNKMAGIC(data, 'O','M','D','3')) {
  84. dumplog("End Chunk\n Magic: OMD3\n\n");
  85. if(data + 4 < end) { dumperr(stderr, "ERROR: Garbage at the end of the file\n"); return 1; }
  86. break;
  87. }
  88. out = data + 8;
  89. len = *((uint32_t*)(data + 4));
  90. if(len < 8) {
  91. dumperr(stderr, "ERROR: Invalid %c%c%c%c chunk length %u\n",data[0],data[1],data[2],data[3],len);
  92. return 1;
  93. }
  94. if(M3D_CHUNKMAGIC(data, 'H','E','A','D')) {
  95. dumplog("Model Header Chunk\n Magic: HEAD\n"
  96. " Length: %u\n"
  97. " Scale: %g meter(s)\n"
  98. " Types: 0x%08x\n",
  99. ((m3dhdr_t*)data)->length, ((m3dhdr_t*)data)->scale, ((m3dhdr_t*)data)->types);
  100. i = ((m3dhdr_t*)data)->types;
  101. vc_s = 1 << ((i >> 0) & 3); /* vertex coordinate size */
  102. vi_s = 1 << ((i >> 2) & 3); /* vertex index size */
  103. si_s = 1 << ((i >> 4) & 3); /* string offset size */
  104. ci_s = 1 << ((i >> 6) & 3); /* color index size */
  105. ti_s = 1 << ((i >> 8) & 3); /* tmap index size */
  106. bi_s = 1 << ((i >>10) & 3); /* bone index size */
  107. nb_s = 1 << ((i >>12) & 3); /* number of bones per vertex */
  108. sk_s = 1 << ((i >>14) & 3); /* skin index size */
  109. fc_s = 1 << ((i >>16) & 3); /* frame counter size */
  110. hi_s = 1 << ((i >>18) & 3); /* shape index size */
  111. fi_s = 1 << ((i >>20) & 3); /* face index size */
  112. vd_s = 1 << ((i >>22) & 3); /* voxel dimension size */
  113. vp_s = 1 << ((i >>24) & 3); /* voxel pixel size */
  114. if(dump > 1) {
  115. dumplog(" vc_t %d %-10s (vertex coordinate)\n", i & 3, cn[i & 3]); i >>= 2;
  116. dumplog(" vi_t %d %-10s (vertex index)\n", i & 3, in[i & 3]); i >>= 2;
  117. dumplog(" si_t %d %-10s (string offset)\n", i & 3, in[i & 3]); i >>= 2;
  118. dumplog(" ci_t %d %-10s (color index)\n", i & 3, in[i & 3]); i >>= 2;
  119. dumplog(" ti_t %d %-10s (texture uv index)\n", i & 3, in[i & 3]); i >>= 2;
  120. dumplog(" bi_t %d %-10s (bone index)\n", i & 3, in[i & 3]); i >>= 2;
  121. dumplog(" nb_t %d %d/vertex (number of bones per vertex)\n", i & 3, nb_s); i >>= 2;
  122. dumplog(" sk_t %d %-10s (skin index)\n", i & 3, in[i & 3]); i >>= 2;
  123. dumplog(" fc_t %d %-10s (frame counter)\n", i & 3, in[i & 3]); i >>= 2;
  124. dumplog(" hi_t %d %-10s (shape index)\n", i & 3, in[i & 3]); i >>= 2;
  125. dumplog(" fi_t %d %-10s (face index)\n", i & 3, in[i & 3]); i >>= 2;
  126. dumplog(" vd_t %d %-10s (voxel dim)\n", i & 3, in[i & 3] + 1); i >>= 2;
  127. dumplog(" vp_t %d %-10s (voxel type)\n", i & 3, in[i & 3]); i >>= 2;
  128. }
  129. if(data[((m3dhdr_t*)data)->length - 1]) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  130. out = data + sizeof(m3dhdr_t);
  131. chend = data + ((m3dhdr_t*)data)->length;
  132. str = (char*)out;
  133. dumplog(" Model: '%s'\n", out);
  134. out += strlen((char*)out) + 1;
  135. dumplog(" License: '%s'\n", out);
  136. out += strlen((char*)out) + 1;
  137. dumplog(" Author: '%s'\n", out);
  138. out += strlen((char*)out) + 1;
  139. if(dump > 1) {
  140. dumplog(" Description:\n%s\n String Table:\n", out);
  141. out += strlen((char*)out) + 1;
  142. while(out < chend) {
  143. dumplog(si_s == 1 ? " %02x. '%s'\n" : (si_s == 2 ? " %04x. '%s'\n" : " %08x. '%s'\n"),
  144. (uint32_t)((uintptr_t)out - (uintptr_t)data - 16), out);
  145. out += strlen((char*)out) + 1;
  146. }
  147. if(out != chend) { dumperr(stderr, "ERROR: Bad string table size\n"); return 1; }
  148. }
  149. dumplog("\n");
  150. } else
  151. if(M3D_CHUNKMAGIC(data, 'C','M','A','P')) {
  152. cmap = (uint32_t*)out;
  153. dumplog("Color Map Chunk\n Magic: CMAP\n"
  154. " Length: %u\n",
  155. ((m3dhdr_t*)data)->length);
  156. if(ci_s >= 4) { dumperr(stderr, "ERROR: There should be no color map\n"); }
  157. if(((m3dhdr_t*)data)->length & 3) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  158. nc = ((m3dhdr_t*)data)->length / 4;
  159. if(dump > 1) {
  160. dumplog(" Data: ");
  161. for(i = 0; i < (int)(((m3dhdr_t*)data)->length/4) - 2; i++, out += 4)
  162. dumplog(" #%08x", *((uint32_t*)out));
  163. dumplog("\n");
  164. }
  165. dumplog("\n");
  166. } else
  167. if(M3D_CHUNKMAGIC(data, 'T','M','A','P')) {
  168. dumplog("Texture Map Chunk\n Magic: TMAP\n"
  169. " Length: %u\n",
  170. ((m3dhdr_t*)data)->length);
  171. i = 2 * vc_s;
  172. nt = (((m3dhdr_t*)data)->length-8)/i;
  173. if(dump > 1) dumplog(" NumUV: %u, reclen %u (use -ddd to dump all)\n", nt, i);
  174. if((unsigned int)nt*i != (((m3dhdr_t*)data)->length-8))
  175. { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  176. if(dump > 2) {
  177. for(i = 0; i < (int)((((m3dhdr_t*)data)->length - 8)/(2*vc_s)); i++) {
  178. dumplog(" %5u. ", i);
  179. switch(vc_s) {
  180. case 1:
  181. dumplog("%02x (u=%2g) %02x (u=%2g) ",
  182. out[0], (float)((uint8_t)out[0]) / 255.0,
  183. out[1], (float)((uint8_t)out[1]) / 255.0);
  184. out += 2;
  185. break;
  186. case 2:
  187. dumplog("%04x (u=%2g) %04x (v=%2g) ",
  188. *((uint16_t*)(out+0)), (float)(*((uint16_t*)(out+0))) / 65535.0,
  189. *((uint16_t*)(out+2)), (float)(*((uint16_t*)(out+2))) / 65535.0);
  190. out += 4;
  191. break;
  192. case 4:
  193. dumplog("%08x (u=%2g) %08x (v=%2g) ",
  194. *((uint32_t*)(out+0)), *((float*)(out+0)),
  195. *((uint32_t*)(out+4)), *((float*)(out+4)));
  196. out += 8;
  197. break;
  198. case 8:
  199. dumplog("(u=%2g) (v=%2g)", *((double*)(out+0)), *((double*)(out+8)));
  200. out += 16;
  201. break;
  202. }
  203. dumplog("\n");
  204. }
  205. }
  206. dumplog("\n");
  207. } else
  208. if(M3D_CHUNKMAGIC(data, 'V','R','T','S')) {
  209. chend = data + ((m3dhdr_t*)data)->length;
  210. dumplog("Vertex Chunk\n Magic: VRTS\n"
  211. " Length: %u\n",
  212. ((m3dhdr_t*)data)->length);
  213. if(ci_s < 4 && !cmap) { dumperr(stderr, "ERROR: there should be a color map\n"); }
  214. i = 4*vc_s + (ci_s!=8 ? ci_s : 0) + (sk_s!=8 ? sk_s : 0);
  215. nv = (((m3dhdr_t*)data)->length-8)/i;
  216. if(dump > 1) dumplog(" Numvertex: %u, reclen %u (use -ddd to dump all)\n", nv, i);
  217. if((unsigned int)(nv*i) != (((m3dhdr_t*)data)->length-8)) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  218. if(dump > 2) {
  219. for(i = 0; out < chend && i < nv; i++) {
  220. dumplog(" %5u. ", i);
  221. switch(vc_s) {
  222. case 1:
  223. dumplog("%02x (x=%2g) %02x (y=%2g) %02x (z=%2g) %02x (w=%2g) ",
  224. out[0], (float)((int8_t)out[0]) / 127,
  225. out[1], (float)((int8_t)out[1]) / 127,
  226. out[2], (float)((int8_t)out[2]) / 127,
  227. out[3], (float)((int8_t)out[3]) / 127);
  228. out += 4;
  229. break;
  230. case 2:
  231. dumplog("%04x (x=%2g) %04x (y=%2g) %04x (z=%2g) %04x (w=%2g) ",
  232. *((int16_t*)(out+0)), (float)(*((int16_t*)(out+0))) / 32767,
  233. *((int16_t*)(out+2)), (float)(*((int16_t*)(out+2))) / 32767,
  234. *((int16_t*)(out+4)), (float)(*((int16_t*)(out+4))) / 32767,
  235. *((int16_t*)(out+6)), (float)(*((int16_t*)(out+6))) / 32767);
  236. out += 8;
  237. break;
  238. case 4:
  239. dumplog("%08x (x=%2g) %08x (y=%2g) %08x (z=%2g) %08x (w=%2g) ",
  240. *((int32_t*)(out+0)), *((float*)(out+0)),
  241. *((int32_t*)(out+4)), *((float*)(out+4)),
  242. *((int32_t*)(out+8)), *((float*)(out+8)),
  243. *((int32_t*)(out+12)), *((float*)(out+12)));
  244. out += 16;
  245. break;
  246. case 8:
  247. dumplog("(x=%2g) (y=%2g) (z=%2g) (w=%2g) ",
  248. *((double*)(out+0)), *((double*)(out+8)),
  249. *((double*)(out+16)), *((double*)(out+24)));
  250. out += 32;
  251. break;
  252. }
  253. m = 0;
  254. switch(ci_s) {
  255. case 1: dumplog("%02x (#%08x) ", out[0], cmap ? cmap[out[0]] : 0); m = out[0]; out++; break;
  256. case 2: dumplog("%04x (#%08x) ", *((uint16_t*)out), cmap ? cmap[*((uint16_t*)out)] : 0); m = *((uint16_t*)out); out += 2; break;
  257. case 4: dumplog("%08x (#%08x) ", *((uint32_t*)out), *((uint32_t*)out)); out += 4; break;
  258. }
  259. if(m && m >= nc) { dumplog("\n"); dumperr(stderr, "ERROR: cmap index out of bound\n"); return 1; }
  260. switch(sk_s) {
  261. case 1: dumplog("%02x (skin=%d) ", out[0], (int8_t)out[0]); out++; break;
  262. case 2: dumplog("%04x (skin=%d) ", *((uint16_t*)out), *((int16_t*)out)); out += 2; break;
  263. case 4: dumplog("%08x (skin=%d) ", *((uint32_t*)out), *((int32_t*)out)); out += 4; break;
  264. }
  265. dumplog("\n");
  266. }
  267. if(out != chend) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  268. }
  269. dumplog("\n");
  270. } else
  271. if(M3D_CHUNKMAGIC(data, 'B','O','N','E')) {
  272. chend = data + ((m3dhdr_t*)data)->length;
  273. nb = 0;
  274. switch(bi_s) {
  275. case 1: nb = out[0]; out++; break;
  276. case 2: nb = *((uint16_t*)out); out += 2; break;
  277. case 4: nb = *((uint32_t*)out); out += 4; break;
  278. }
  279. l = 0;
  280. switch(sk_s) {
  281. case 1: l = out[0]; out++; break;
  282. case 2: l = *((uint16_t*)out); out += 2; break;
  283. case 4: l = *((uint32_t*)out); out += 4; break;
  284. }
  285. dumplog("Skeleton Chunk\n Magic: BONE\n"
  286. " Length: %u\n"
  287. " #Bones: %u\n"
  288. " #Skins: %u\n",
  289. ((m3dhdr_t*)data)->length, nb, l);
  290. if(bi_s >= 4) { dumperr(stderr, "ERROR: there should be no skeleton\n"); }
  291. if(!nv) { dumperr(stderr, "ERROR: bones without vertex list\n"); return 1; }
  292. if(((m3dhdr_t*)data)->length < 8 + nb*(bi_s + si_s + 2*vi_s) + l*bi_s)
  293. { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  294. if(dump > 1) {
  295. dumplog(" Bone Hierarchy\n");
  296. for(i = 0; i < nb; i++) {
  297. dumplog(" %5u. ", i);
  298. j = nb;
  299. switch(bi_s) {
  300. case 1: dumplog("%02x (parent=%d) ", out[0], (int8_t)out[0]); j = (int8_t)out[0]; out++; break;
  301. case 2: dumplog("%04x (parent=%d) ", *((uint16_t*)out), *((int16_t*)out)); j = *((int16_t*)out); out += 2; break;
  302. case 4: dumplog("%08x (parent=%d) ", *((uint32_t*)out), *((int32_t*)out)); j = *((int32_t*)out); out += 4; break;
  303. }
  304. if(j != -1 && j >= nb) { dumplog("\n"); dumperr(stderr, "ERROR: bone index out of bound\n"); return 1; }
  305. j = 0;
  306. switch(si_s) {
  307. case 1: j = out[0]; out++; break;
  308. case 2: j = *((uint16_t*)out); out += 2; break;
  309. case 4: j = *((uint32_t*)out); out += 4; break;
  310. }
  311. dumplog(si_s == 1 ? "%02x '%s' " : (si_s == 2 ? "%04x '%s' " : "%08x '%s' "),
  312. j, j ? str + j : "");
  313. j = nv;
  314. switch(vi_s) {
  315. case 1: dumplog("%02x (pos=%u) ", out[0], (int8_t)out[0]); j = out[0]; out++; break;
  316. case 2: dumplog("%04x (pos=%u) ", *((uint16_t*)out), *((int16_t*)out)); j = *((uint16_t*)out); out += 2; break;
  317. case 4: dumplog("%08x (pos=%u) ", *((uint32_t*)out), *((int32_t*)out)); j = *((uint32_t*)out); out += 4; break;
  318. }
  319. if(j >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  320. j = nv;
  321. switch(vi_s) {
  322. case 1: dumplog("%02x (ori=%u) ", out[0], (int8_t)out[0]); j = out[0]; out++; break;
  323. case 2: dumplog("%04x (ori=%u) ", *((uint16_t*)out), *((int16_t*)out)); j = *((uint16_t*)out); out += 2; break;
  324. case 4: dumplog("%08x (ori=%u) ", *((uint32_t*)out), *((int32_t*)out)); j = *((uint32_t*)out); out += 4; break;
  325. }
  326. if(j >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  327. dumplog("\n");
  328. }
  329. dumplog(" Skin (bone id and weight pairs for vertices)\n");
  330. for(i = 0; i < l && out < chend; i++) {
  331. dumplog(" %5u.", i);
  332. memcpy(&weights, out, nb_s);
  333. if(nb_s == 1) {
  334. dumplog(" (implicit w=1.0) ");
  335. weights[0] = 255;
  336. } else
  337. for(j = 0; j < (int)nb_s; j++) { dumplog(" %02x (w=%g) ", out[0], ((float)(out[0])) / 255); out++; }
  338. for(j = 0; j < (int)nb_s && weights[j]; j++) {
  339. m = nb;
  340. switch(bi_s) {
  341. case 1: dumplog("%02x (bone=%u) ", out[0], out[0]); m = out[0]; out++; break;
  342. case 2: dumplog("%04x (bone=%u) ", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  343. case 4: dumplog("%08x (bone=%u) ", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4; break;
  344. }
  345. if(m >= nb) { dumplog("\n"); dumperr(stderr, "ERROR: bone index out of bound\n"); return 1; }
  346. }
  347. dumplog("\n");
  348. }
  349. if(i != l || out != chend) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  350. }
  351. dumplog("\n");
  352. } else
  353. if(M3D_CHUNKMAGIC(data, 'M','T','R','L')) {
  354. chend = data + ((m3dhdr_t*)data)->length;
  355. i = 0;
  356. switch(si_s) {
  357. case 1: i = out[0]; out++; break;
  358. case 2: i = *((uint16_t*)out); out += 2; break;
  359. case 4: i = *((uint32_t*)out); out += 4; break;
  360. }
  361. dumplog("Material Definiton Chunk\n Magic: MTRL\n"
  362. " Length: %u\n"
  363. " Name: ",
  364. ((m3dhdr_t*)data)->length);
  365. if(dump > 1)
  366. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), i);
  367. dumplog("'%s'\n", i ? str + i : "");
  368. if(((m3dhdr_t*)data)->length < 8 + si_s + 2) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  369. if(dump > 1) {
  370. while(out < chend) {
  371. for(i = 0, j = 256; i < (int)(sizeof(m3d_propertytypes)/sizeof(m3d_propertytypes[0])); i++)
  372. if(m3d_propertytypes[i].id == out[0] ||
  373. m3d_propertytypes[i].id + 128 == out[0]) { j = i; break; }
  374. if(j == 256) { dumplog(" %02x unknown property?\n", out[0]); break; }
  375. k = out[0] >= 128 ? m3dpf_map : m3d_propertytypes[j].format;
  376. dumplog(" %02x m3dpf_%s ", out[0], pf[k]);
  377. #ifdef M3D_ASCII
  378. dumplog("m3dp_%s%-4s%s ", out[0] >= 128 ? "map_" : "", m3d_propertytypes[j].key,
  379. out[0] >= 128 ? "" : " ");
  380. #endif
  381. out++;
  382. switch(k) {
  383. case m3dpf_color:
  384. if(!cmap && (ci_s==1 || ci_s==2)) { dumplog("\n"); dumperr(stderr, "ERROR: missing color map\n"); return 1; }
  385. m = 0;
  386. switch(ci_s) {
  387. case 1: dumplog("%02x (#%08x)", out[0], cmap[out[0]]); m = out[0]; out++; break;
  388. case 2: dumplog("%04x (#%08x)", *((uint16_t*)out), cmap[*((uint16_t*)out)]); m = *((uint16_t*)out); out += 2; break;
  389. case 4: dumplog("%08x (#%08x)", *((uint32_t*)out), *((uint32_t*)out)); out += 4; break;
  390. }
  391. if(m && m >= nc) { dumplog("\n"); dumperr(stderr, "ERROR: cmap index out of bound\n"); return 1; }
  392. break;
  393. case m3dpf_uint8: dumplog("%02x (%u)", out[0], out[0]); out++; break;
  394. case m3dpf_uint16: dumplog("%04x (%u)", *((uint16_t*)out), *((uint16_t*)out)); out += 2; break;
  395. case m3dpf_uint32: dumplog("%08x (%u)", *((uint32_t*)out), *((uint32_t*)out)); out += 4; break;
  396. case m3dpf_float: dumplog("%08x (%g)", *((uint32_t*)out), *((float*)out)); out += 4; break;
  397. case m3dpf_map:
  398. l = 0;
  399. switch(si_s) {
  400. case 1: l = out[0]; out++; break;
  401. case 2: l = *((uint16_t*)out); out += 2; break;
  402. case 4: l = *((uint32_t*)out); out += 4; break;
  403. }
  404. dumplog(si_s == 1 ? "%02x '%s'" : (si_s == 2 ? "%04x '%s'" : "%08x '%s'"),
  405. l, l ? str + l : "");
  406. break;
  407. }
  408. dumplog("\n");
  409. }
  410. if(out != chend) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  411. }
  412. dumplog("\n");
  413. } else
  414. if(M3D_CHUNKMAGIC(data, 'P','R','O','C')) {
  415. i = 0;
  416. switch(si_s) {
  417. case 1: i = out[0]; out++; break;
  418. case 2: i = *((uint16_t*)out); out += 2; break;
  419. case 4: i = *((uint32_t*)out); out += 4; break;
  420. }
  421. dumplog("Procedural Surface Chunk\n Magic: PROC\n"
  422. " Length: %u\n"
  423. " Script: ",
  424. ((m3dhdr_t*)data)->length);
  425. if(dump > 1)
  426. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), i);
  427. dumplog("'%s'\n", i ? str + i : "");
  428. if(((m3dhdr_t*)data)->length != 8 + si_s) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  429. dumplog("\n");
  430. } else
  431. if(M3D_CHUNKMAGIC(data, 'M','E','S','H')) {
  432. chend = data + ((m3dhdr_t*)data)->length;
  433. dumplog("Polygon Mesh Chunk\n Magic: MESH\n"
  434. " Length: %u\n",
  435. ((m3dhdr_t*)data)->length);
  436. if(((m3dhdr_t*)data)->length < 9 + 3 * vi_s) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  437. if(!nv) { dumperr(stderr, "ERROR: mesh without vertex list\n"); return 1; }
  438. if(dump > 1) {
  439. d = 0;
  440. for(i = 0; out < chend; i++) {
  441. if(!out[0]) {
  442. if(dump>2)dumplog(" %02x", out[0]);
  443. out++;
  444. l = 0;
  445. switch(si_s) {
  446. case 1: l = out[0]; out++; break;
  447. case 2: l = *((uint16_t*)out); out += 2; break;
  448. case 4: l = *((uint32_t*)out); out += 4; break;
  449. }
  450. dumplog(" use material ");
  451. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), l);
  452. dumplog("'%s'\n", l ? str + l : "");
  453. } else
  454. if(out[0] == 1) {
  455. if(dump>2)dumplog(" %02x", out[0]);
  456. out++;
  457. l = 0;
  458. switch(si_s) {
  459. case 1: l = out[0]; out++; break;
  460. case 2: l = *((uint16_t*)out); out += 2; break;
  461. case 4: l = *((uint32_t*)out); out += 4; break;
  462. }
  463. dumplog(" use parameter ");
  464. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), l);
  465. dumplog("'%s'\n", l ? str + l : "");
  466. np++;
  467. } else {
  468. if(dump>2)dumplog(" %5u. %02x", d, out[0]);
  469. d++;
  470. k = *out++;
  471. l = k >> 4;
  472. if(dump>2)dumplog(" (edge=%u%s%s%s) ", l, k & 1 ? ",uv" : "", k & 2 ? ",n" : "", k & 4 ? ",m" : "");
  473. if(!nt && (k & 1)) { dumperr(stderr, "ERROR: UV index without texture map\n"); return 1; }
  474. if(!np && (k & 4)) { dumperr(stderr, "ERROR: maximum vertex without parameter\n"); return 1; }
  475. for(j = 0; j < l; j++) {
  476. if(dump > 2) {
  477. if(j) dumplog(", ");
  478. m = nv;
  479. switch(vi_s) {
  480. case 1: dumplog("%02x (p=%u)", out[0], (uint8_t)out[0]); m = out[0]; out++; break;
  481. case 2: dumplog("%04x (p=%u)", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  482. case 4: dumplog("%08x (p=%u)", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4; break;
  483. }
  484. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  485. } else out += vi_s;
  486. if(k & 1) {
  487. if(dump > 2) {
  488. m = nt;
  489. switch(ti_s) {
  490. case 1: dumplog(" %02x (uv=%u)", out[0], (uint8_t)out[0]); m = out[0]; out++; break;
  491. case 2: dumplog(" %04x (uv=%u)", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  492. case 4: dumplog(" %08x (uv=%u)", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4; break;
  493. }
  494. if(m >= nt) { dumplog("\n"); dumperr(stderr, "ERROR: UV index out of bound\n"); return 1; }
  495. } else if(ti_s != 8) out += ti_s;
  496. }
  497. if(k & 2) {
  498. if(dump > 2) {
  499. m = nv;
  500. switch(vi_s) {
  501. case 1: dumplog(" %02x (n=%u)", out[0], (uint8_t)out[0]); m = out[0]; out++; break;
  502. case 2: dumplog(" %04x (n=%u)", *((uint16_t*)out), (int)*((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  503. case 4: dumplog(" %08x (n=%u)", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4; break;
  504. }
  505. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  506. } else out += vi_s;
  507. }
  508. if(k & 4) {
  509. if(dump > 2) {
  510. m = nv;
  511. switch(vi_s) {
  512. case 1: dumplog(" %02x (m=%u)", out[0], (uint8_t)out[0]); m = out[0]; out++; break;
  513. case 2: dumplog(" %04x (m=%u)", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  514. case 4: dumplog(" %08x (m=%u)", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4; break;
  515. }
  516. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  517. } else out += vi_s;
  518. }
  519. }
  520. if(dump > 2) dumplog("\n");
  521. }
  522. }
  523. if(dump == 2) dumplog(" NumFace %u (use -ddd to dump all)\n", d);
  524. if(out != chend) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  525. }
  526. dumplog("\n");
  527. } else
  528. if(M3D_CHUNKMAGIC(data, 'V','O','X','T')) {
  529. chend = data + ((m3dhdr_t*)data)->length;
  530. dumplog("Voxel Type Chunk\n Magic: VOXT\n"
  531. " Length: %u\n",
  532. ((m3dhdr_t*)data)->length);
  533. if(!cmap && (ci_s==1 || ci_s==2)) { dumplog("\n"); dumperr(stderr, "ERROR: missing color map\n"); return 1; }
  534. for(i = 0; out < chend; i++) {
  535. if(dump>1) {
  536. dumplog(" ");
  537. m = 0;
  538. switch(ci_s) {
  539. case 1: dumplog("%02x (#%08x) ", out[0], cmap[out[0]]); m = out[0]; out++; break;
  540. case 2: dumplog("%04x (#%08x) ", *((uint16_t*)out), cmap[*((uint16_t*)out)]); m = *((uint16_t*)out); out += 2; break;
  541. case 4: dumplog("%08x (#%08x) ", *((uint32_t*)out), *((uint32_t*)out)); out += 4; break;
  542. }
  543. if(m && m >= nc) { dumplog("\n"); dumperr(stderr, "ERROR: cmap index out of bound\n"); return 1; }
  544. } else
  545. out += ci_s == 8 ? 0 : ci_s;
  546. if(dump>1) {
  547. m = 0;
  548. switch(si_s) {
  549. case 1: m = out[0]; out++; break;
  550. case 2: m = *((uint16_t*)out); out += 2; break;
  551. case 4: m = *((uint32_t*)out); out += 4; break;
  552. }
  553. if(dump > 1)
  554. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), m);
  555. dumplog("'%s' ", m ? str + m : "");
  556. } else
  557. out += si_s;
  558. if(dump>1) {
  559. dumplog("%02x (", out[0]);
  560. if(out[0] & 0x80) dumplog("height=%u/64", 1+ (out[0] & 0x3F));
  561. else dumplog("rotate y=%u z=%u x=%u deg", (out[0] & 3) * 90, ((out[0]>>2) & 3) * 90, ((out[0]>>4) & 3) * 90);
  562. dumplog(") %02x (", out[1]);
  563. if(!out[1]) dumplog("full cube");
  564. else if(out[1] < 8) dumplog("partial cube");
  565. else if(out[1] == 8) dumplog("coloumn");
  566. else if(out[1] == 9) dumplog("cross");
  567. else if(out[1] == 10) dumplog("cylinder");
  568. else if(out[1] == 11) dumplog("cylinder cross");
  569. else if(out[1] == 12) dumplog("lego top");
  570. else if(out[1] == 13) dumplog("lego bottom");
  571. else if(out[1] < 32) dumplog("reserved");
  572. else dumplog("user defined shape");
  573. dumplog(") %02x (numitem=%u) ", out[2], out[2]);
  574. }
  575. l = out[2];
  576. out += 3;
  577. if(dump>1) {
  578. switch(sk_s) {
  579. case 1: dumplog("%02x (skin=%d)", out[0], (int8_t)out[0]); out++; break;
  580. case 2: dumplog("%04x (skin=%d)", *((uint16_t*)out), *((int16_t*)out)); out += 2; break;
  581. case 4: dumplog("%08x (skin=%d)", *((uint32_t*)out), *((int32_t*)out)); out += 4; break;
  582. }
  583. } else
  584. out += sk_s == 8 ? 0 : sk_s;
  585. if(dump>2) {
  586. for(j = 0; j < l; j++) {
  587. dumplog(" %04x (count=%u)", *((uint16_t*)out), *((uint16_t*)out)); out += 2;
  588. m = 0;
  589. switch(si_s) {
  590. case 1: m = out[0]; out++; break;
  591. case 2: m = *((uint16_t*)out); out += 2; break;
  592. case 4: m = *((uint32_t*)out); out += 4; break;
  593. }
  594. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), m);
  595. dumplog("'%s'\n", m ? str + m : "");
  596. }
  597. } else
  598. out += l * (2 + si_s);
  599. if(dump>1) dumplog("\n");
  600. }
  601. if(dump < 2) dumplog(" #VoxelType %u (use -ddd to dump all)\n", i);
  602. if(out != chend) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  603. nvt = i;
  604. dumplog("\n");
  605. } else
  606. if(M3D_CHUNKMAGIC(data, 'V','O','X','D')) {
  607. chend = data + ((m3dhdr_t*)data)->length;
  608. i = 0;
  609. switch(si_s) {
  610. case 1: i = out[0]; out++; break;
  611. case 2: i = *((uint16_t*)out); out += 2; break;
  612. case 4: i = *((uint32_t*)out); out += 4; break;
  613. }
  614. dumplog("Voxel Data Chunk\n Magic: VOXD\n"
  615. " Length: %u\n"
  616. " Name: ",
  617. ((m3dhdr_t*)data)->length);
  618. if(dump > 1)
  619. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), i);
  620. dumplog("'%s'\n", i ? str + i : "");
  621. if(vd_s > 4 || vp_s >= 4) { dumperr(stderr, "ERROR: there should be no voxel data\n"); }
  622. if(!nvt) { dumplog("\n"); dumperr(stderr, "ERROR: voxel data without voxel type list\n"); return 1; }
  623. if(dump>1) {
  624. dumplog(" Pos: ");
  625. i = j = k = 0;
  626. switch(vd_s) {
  627. case 1:
  628. dumplog("%02x %02x %02x", (int8_t)out[0], (int8_t)out[1], (int8_t)out[2]);
  629. i = (int8_t)out[0]; out++;
  630. j = (int8_t)out[0]; out++;
  631. k = (int8_t)out[0]; out++;
  632. break;
  633. case 2:
  634. dumplog("%04x %04x %04x", *((int16_t*)(out+0)), *((int16_t*)(out+2)), *((int16_t*)(out+4)));
  635. i = *((int16_t*)out); out += 2;
  636. j = *((int16_t*)out); out += 2;
  637. k = *((int16_t*)out); out += 2;
  638. break;
  639. case 4:
  640. dumplog("%08x %08x %08x", *((int32_t*)(out+0)), *((int32_t*)(out+4)), *((int32_t*)(out+8)));
  641. i = *((int32_t*)out); out += 4;
  642. j = *((int32_t*)out); out += 4;
  643. k = *((int32_t*)out); out += 4;
  644. break;
  645. }
  646. dumplog(" (x=%d) (y=%d) (z=%d)\n Size: ", i, j, k);
  647. } else
  648. out += 3 * vd_s;
  649. i = j = k = 0;
  650. switch(vd_s) {
  651. case 1:
  652. if(dump>1) dumplog("%02x %02x %02x", (int8_t)out[0], (int8_t)out[1], (int8_t)out[2]);
  653. i = (int8_t)out[0]; out++;
  654. j = (int8_t)out[0]; out++;
  655. k = (int8_t)out[0]; out++;
  656. break;
  657. case 2:
  658. if(dump>1) dumplog("%04x %04x %04x", *((int16_t*)(out+0)), *((int16_t*)(out+2)), *((int16_t*)(out+4)));
  659. i = *((int16_t*)out); out += 2;
  660. j = *((int16_t*)out); out += 2;
  661. k = *((int16_t*)out); out += 2;
  662. break;
  663. case 4:
  664. if(dump>1) dumplog("%08x %08x %08x", *((int32_t*)(out+0)), *((int32_t*)(out+4)), *((int32_t*)(out+8)));
  665. i = *((int32_t*)out); out += 4;
  666. j = *((int32_t*)out); out += 4;
  667. k = *((int32_t*)out); out += 4;
  668. break;
  669. }
  670. if(dump>1) {
  671. dumplog(" (width=%u) (height=%u) (depth=%u)\n Group: %02x (uncertain=%u) %02x (groupid=%u)\n", i, j, k,
  672. out[0], out[0], out[1], out[1]);
  673. if(dump>2) dumplog(" ");
  674. }
  675. out += 2;
  676. l = i * j * k;
  677. k = vp_s == 1 ? 0xFE : 0xFFFE;
  678. for(i = 0; i < l && out < chend;) {
  679. j = *out++;
  680. i += (j & 0x7F) + 1;
  681. if(j & 0x80) {
  682. if(dump>2) {
  683. dumplog("%u*", (j & 0x7F)+1);
  684. m = nvt;
  685. switch(vp_s) {
  686. case 1: dumplog("%02x ", out[0]); m = out[0]; out++; break;
  687. case 2: dumplog("%04x ", *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  688. }
  689. if(m >= nvt && m < k) { dumplog("\n"); dumperr(stderr, "ERROR: voxel type index out of bound\n"); return 1; }
  690. } else
  691. out += vp_s;
  692. } else {
  693. if(dump>2) {
  694. for(j++; j; j--) {
  695. m = nvt;
  696. switch(vp_s) {
  697. case 1: dumplog("%02x ", out[0]); m = out[0]; out++; break;
  698. case 2: dumplog("%04x ", *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  699. }
  700. if(m >= nvt && m < k) { dumplog("\n"); dumperr(stderr, "ERROR: voxel type index out of bound\n"); return 1; }
  701. }
  702. } else
  703. out += (j+1) * vp_s;
  704. }
  705. }
  706. if(dump>2) dumplog("\n");
  707. dumplog("\n");
  708. if(out != chend || i != l) { dumperr(stderr, "\nERROR: Bad chunk size\n"); return 1; }
  709. } else
  710. if(M3D_CHUNKMAGIC(data, 'S','H','P','E')) {
  711. chend = data + ((m3dhdr_t*)data)->length;
  712. i = 0;
  713. switch(si_s) {
  714. case 1: i = out[0]; out++; break;
  715. case 2: i = *((uint16_t*)out); out += 2; break;
  716. case 4: i = *((uint32_t*)out); out += 4; break;
  717. }
  718. j = -1;
  719. switch(bi_s) {
  720. case 1: j = (int8_t)out[0]; out++; break;
  721. case 2: j = *((int16_t*)out); out += 2; break;
  722. case 4: j = *((int32_t*)out); out += 4; break;
  723. }
  724. if(j != -1 && j >= nb) { dumplog("\n"); dumperr(stderr, "ERROR: bone index out of bound\n"); return 1; }
  725. dumplog("Shape Chunk\n Magic: SHPE\n"
  726. " Length: %u\n"
  727. " Group: %d\n"
  728. " Name: ",
  729. ((m3dhdr_t*)data)->length, j);
  730. if(dump > 1)
  731. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), i);
  732. dumplog("'%s'\n", i ? str + i : "");
  733. if(((m3dhdr_t*)data)->length < 8 + si_s + 1) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  734. if(dump > 1) {
  735. while(out < chend) {
  736. for(i = 0, j = 256; i < (int)(sizeof(m3d_propertytypes)/sizeof(m3d_propertytypes[0])); i++)
  737. if(m3d_propertytypes[i].id == out[0] ||
  738. m3d_propertytypes[i].id + 128 == out[0]) { j = i; break; }
  739. k = *out++;
  740. if(k & 0x80) { k &= 0x7F; k |= (*out++ << 7); }
  741. if(k >= (int)(sizeof(m3d_commandtypes)/sizeof(m3d_commandtypes[0])))
  742. { dumplog(" %04x unknown command?\n", k); break; }
  743. cd = &m3d_commandtypes[k];
  744. if(k < 128) dumplog(" %02x ", out[-1]);
  745. else dumplog(" %02x %02x ", out[-2], out[-1]);
  746. #ifdef M3D_ASCII
  747. dumplog("m3dc_%s ", cd->key);
  748. #endif
  749. for(k = n = 0, l = cd->p; k < l; k++)
  750. switch(cd->a[((k - n) % (cd->p - n)) + n]) {
  751. case m3dcp_mi_t:
  752. i = 0;
  753. switch(si_s) {
  754. case 1: i = out[0]; out++; break;
  755. case 2: i = *((uint16_t*)out); out += 2; break;
  756. case 4: i = *((uint32_t*)out); out += 4; break;
  757. }
  758. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), i);
  759. dumplog("(material='%s') ", i ? str + i : "");
  760. break;
  761. case m3dcp_hi_t:
  762. switch(hi_s) {
  763. case 1: dumplog("%02x (shape=%u) ", out[0], out[0]); out++; break;
  764. case 2: dumplog("%04x (shape=%u) ", *((uint16_t*)out), *((uint16_t*)out)); out += 2; break;
  765. case 4: dumplog("%08x (shape=%u) ", *((uint32_t*)out), *((uint32_t*)out)); out += 4; break;
  766. }
  767. break;
  768. case m3dcp_fi_t:
  769. switch(fi_s) {
  770. case 1: dumplog("%02x (face=%u) ", out[0], out[0]); out++; break;
  771. case 2: dumplog("%04x (face=%u) ", *((uint16_t*)out), *((uint16_t*)out)); out += 2; break;
  772. case 4: dumplog("%08x (face=%u) ", *((uint32_t*)out), *((uint32_t*)out)); out += 4; break;
  773. }
  774. break;
  775. case m3dcp_vi_t:
  776. m = nv;
  777. switch(vi_s) {
  778. case 1: dumplog("%02x (pos=%u) ", out[0], out[0]); m = out[0]; out++; break;
  779. case 2: dumplog("%04x (pos=%u) ", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  780. case 4: dumplog("%08x (pos=%u) ", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4; break;
  781. }
  782. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  783. break;
  784. case m3dcp_qi_t:
  785. m = nv;
  786. switch(vi_s) {
  787. case 1: dumplog("%02x (ori=%u) ", out[0], out[0]); m = out[0]; out++; break;
  788. case 2: dumplog("%04x (ori=%u) ", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  789. case 4: dumplog("%08x (ori=%u) ", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4; break;
  790. }
  791. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  792. break;
  793. case m3dcp_ti_t:
  794. m = nt;
  795. switch(ti_s) {
  796. case 1: dumplog("%02x (uv=%u) ", out[0], out[0]); m = out[0]; out++; break;
  797. case 2: dumplog("%04x (uv=%u) ", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  798. case 4: dumplog("%08x (uv=%u) ", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4; break;
  799. }
  800. if(m >= nt) { dumplog("\n"); dumperr(stderr, "ERROR: tmap index out of bound\n"); return 1; }
  801. break;
  802. case m3dcp_vc_t:
  803. switch(vc_s) {
  804. case 1: dumplog("%02x (%g) ", out[0], (float)((int8_t)out[0]) / 127); break;
  805. case 2:
  806. dumplog("%04x (%g) ", *((int16_t*)(out+0)), (float)(*((int16_t*)(out+0))) / 32767);
  807. break;
  808. case 4: dumplog("%08x (%g) ", *((int32_t*)(out+0)), *((float*)(out+0))); break;
  809. case 8: dumplog("(%g) ", *((double*)(out+0))); break;
  810. }
  811. out += vc_s;
  812. break;
  813. case m3dcp_i1_t: dumplog("%02x (%d) ", *((uint8_t*)out), *((int8_t*)out)); out++; break;
  814. case m3dcp_i2_t: dumplog("%04x (%d) ", *((uint16_t*)out), *((int16_t*)out)); out += 2; break;
  815. case m3dcp_i4_t: dumplog("%08x (%d) ", *((uint32_t*)out), *((int32_t*)out)); out += 4; break;
  816. case m3dcp_va_t: dumplog("%08x (va_args=%u) ", *((uint32_t*)out), *((int32_t*)out));
  817. m = *((int32_t*)out); out += 4; n = k + 1; l += (m - 1) * (cd->p - k - 1);
  818. break;
  819. default: dumplog("\n"); dumperr(stderr, "ERROR: unknown command argument type\n"); return 1;
  820. }
  821. dumplog("\n");
  822. }
  823. }
  824. dumplog("\n");
  825. } else
  826. if(M3D_CHUNKMAGIC(data, 'L','B','L','S')) {
  827. chend = data + ((m3dhdr_t*)data)->length;
  828. i = 0;
  829. switch(si_s) {
  830. case 1: i = out[0]; out++; break;
  831. case 2: i = *((uint16_t*)out); out += 2; break;
  832. case 4: i = *((uint32_t*)out); out += 4; break;
  833. }
  834. dumplog("Labels Chunk\n Magic: LBLS\n"
  835. " Length: %u\n"
  836. " Group: ",
  837. ((m3dhdr_t*)data)->length);
  838. if(dump > 1)
  839. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), i);
  840. dumplog("'%s'\n", i ? str + i : "");
  841. i = 0;
  842. switch(si_s) {
  843. case 1: i = out[0]; out++; break;
  844. case 2: i = *((uint16_t*)out); out += 2; break;
  845. case 4: i = *((uint32_t*)out); out += 4; break;
  846. }
  847. dumplog(" Lang: ");
  848. if(dump > 1)
  849. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), i);
  850. dumplog("'%s'\n", i ? str + i : "");
  851. if(!cmap && (ci_s==1 || ci_s==2)) { dumplog("\n"); dumperr(stderr, "ERROR: missing color map\n"); return 1; }
  852. m = 0;
  853. switch(ci_s) {
  854. case 1: dumplog(" Color: %02x (#%08x)\n", out[0], cmap[out[0]]); m = out[0]; out++; break;
  855. case 2: dumplog(" Color: %04x (#%08x)\n", *((uint16_t*)out), cmap[*((uint16_t*)out)]); m = *((uint16_t*)out); out += 2; break;
  856. case 4: dumplog(" Color: %08x (#%08x)\n", *((uint32_t*)out), *((uint32_t*)out)); out += 4; break;
  857. }
  858. if(m && m >= nc) { dumplog("\n"); dumperr(stderr, "ERROR: cmap index out of bound\n"); return 1; }
  859. i = vi_s + si_s;
  860. j = (((m3dhdr_t*)data)->length-8)/i;
  861. if(dump == 2) dumplog(" Numlabel: %u, reclen %u (use -ddd to dump all)\n", j, i);
  862. if(((m3dhdr_t*)data)->length != 8 + 2 * si_s + (ci_s!=8 ? ci_s : 0) + j * i)
  863. { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  864. if(dump > 2) {
  865. while(out < chend) {
  866. dumplog(" ");
  867. m = nv;
  868. switch(vi_s) {
  869. case 1: dumplog("%02x (vertex=%u) ", out[0], out[0]); m = out[0]; out++; break;
  870. case 2: dumplog("%04x (vertex=%u) ", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  871. case 4: dumplog("%08x (vertex=%u) ", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4; break;
  872. }
  873. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  874. i = 0;
  875. switch(si_s) {
  876. case 1: i = out[0]; out++; break;
  877. case 2: i = *((uint16_t*)out); out += 2; break;
  878. case 4: i = *((uint32_t*)out); out += 4; break;
  879. }
  880. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), i);
  881. dumplog("'%s'\n", i ? str + i : "");
  882. }
  883. }
  884. dumplog("\n");
  885. } else
  886. if(M3D_CHUNKMAGIC(data, 'A','C','T','N')) {
  887. chend = data + ((m3dhdr_t*)data)->length;
  888. i = 0;
  889. switch(si_s) {
  890. case 1: i = out[0]; out++; break;
  891. case 2: i = *((uint16_t*)out); out += 2; break;
  892. case 4: i = *((uint32_t*)out); out += 4; break;
  893. }
  894. j = *((uint16_t*)out); out += 2;
  895. d = *((uint32_t*)out); out += 4;
  896. dumplog("Action Chunk\n Magic: ACTN\n"
  897. " Length: %u\n"
  898. " Name: ",
  899. ((m3dhdr_t*)data)->length);
  900. if(dump > 1)
  901. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), i);
  902. dumplog("'%s'\n"
  903. " #Frames: %u\n"
  904. " Duration: %g sec\n", i ? str + i : "", j, ((float)d)/1000);
  905. if(j < 1 || d < 10) { dumperr(stderr, "ERROR: Bad duration or bad number of frames\n"); return 1; }
  906. if(bi_s >= 4 || fc_s >= 4) { dumperr(stderr, "ERROR: there should be no action chunk\n"); }
  907. if(!nv) { dumperr(stderr, "ERROR: action chunk without vertex list\n"); return 1; }
  908. if(((m3dhdr_t*)data)->length < 8 + si_s + 6 + j * (4 + fc_s + 2 * vi_s))
  909. { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  910. if(dump > 1) {
  911. for(i = 0; i < j; i++) {
  912. l = *((uint32_t*)out); out += 4;
  913. k = 0;
  914. switch(fc_s) {
  915. case 1: k = out[0]; out++; break;
  916. case 2: k = *((uint16_t*)out); out += 2; break;
  917. case 4: k = *((uint32_t*)out); out += 4; break;
  918. }
  919. dumplog(" Frame #%u at %08x (%g sec) ", i+1, l, ((float)l)/1000);
  920. dumplog(fc_s == 1 ? "%02x " : (fc_s == 2 ? "%04x " : "%08x "), k);
  921. dumplog("(%u transforms)\n", k);
  922. if(l > d) { dumperr(stderr, "ERROR: invalid frame timestamp\n"); return 1; }
  923. if(k == 0 || out + k * (fc_s + 2 * vi_s) > chend)
  924. { dumperr(stderr, "ERROR: invalid number of transforms\n"); return 1; }
  925. for(l = 0; l < k; l++) {
  926. dumplog(" ");
  927. m = nb;
  928. switch(bi_s) {
  929. case 1: dumplog("%02x (bone=%u) ", out[0], out[0]); m = out[0]; out++; break;
  930. case 2: dumplog("%04x (bone=%u) ", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2; break;
  931. case 4: dumplog("%08x (bone=%u) ", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4; break;
  932. }
  933. if(m >= nb) { dumplog("\n"); dumperr(stderr, "ERROR: bone index out of bound\n"); return 1; }
  934. switch(vi_s) {
  935. case 1:
  936. dumplog("%02x (pos=%u) ", out[0], out[0]); m = out[0]; out++;
  937. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  938. dumplog("%02x (ori=%u)\n", out[0], out[0]); m = out[0]; out++;
  939. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  940. break;
  941. case 2:
  942. dumplog("%04x (pos=%u) ", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2;
  943. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  944. dumplog("%04x (ori=%u)\n", *((uint16_t*)out), *((uint16_t*)out)); m = *((uint16_t*)out); out += 2;
  945. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  946. break;
  947. case 4:
  948. dumplog("%08x (pos=%u) ", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4;
  949. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  950. dumplog("%08x (ori=%u)\n", *((uint32_t*)out), *((uint32_t*)out)); m = *((uint32_t*)out); out += 4;
  951. if(m >= nv) { dumplog("\n"); dumperr(stderr, "ERROR: vertex index out of bound\n"); return 1; }
  952. break;
  953. }
  954. }
  955. }
  956. if(out != chend) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  957. }
  958. dumplog("\n");
  959. } else
  960. if(M3D_CHUNKMAGIC(data, 'A','S','E','T')) {
  961. i = 0;
  962. switch(si_s) {
  963. case 1: i = out[0]; out++; break;
  964. case 2: i = *((uint16_t*)out); out += 2; break;
  965. case 4: i = *((uint32_t*)out); out += 4; break;
  966. }
  967. dumplog("Inlined Asset Chunk\n Magic: ASET\n"
  968. " Length: %u\n"
  969. " Name: ",
  970. ((m3dhdr_t*)data)->length);
  971. if(dump > 1)
  972. dumplog(si_s == 1 ? "%02x " : (si_s == 2 ? "%04x " : "%08x "), i);
  973. dumplog("'%s'\n", i ? str + i : "");
  974. if(((m3dhdr_t*)data)->length < 8 + si_s + 1) { dumperr(stderr, "ERROR: Bad chunk size\n"); return 1; }
  975. dumplog("\n");
  976. } else
  977. dumplog("Extra Chunk\n Magic: %c%c%c%c\n"
  978. " Length: %u\n\n",
  979. data[0], data[1], data[2], data[3], ((m3dhdr_t*)data)->length);
  980. data += len;
  981. }
  982. } else {
  983. dumperr(stderr, "ERROR: not a valid Model 3D binary\n");
  984. return 1;
  985. }
  986. dumperr(stderr, "Model parsed OK.\n");
  987. if(zip) free(zip);
  988. return 0;
  989. }
  990. /**
  991. * Dump in-memory representation
  992. */
  993. void dump_cstruct(m3d_t *model)
  994. {
  995. unsigned int i, j, k, l, n;
  996. m3dcd_t *cd;
  997. if(!model) {
  998. dumplog("m3d_load() returned NULL, probably memory allocation error\n");
  999. return;
  1000. }
  1001. dumplog("m3d_t = {\n"
  1002. " raw:0x%lx, flags:0x%x, errcode:%d (fatal:%s),\n"
  1003. , (uintptr_t)model->raw, model->flags, model->errcode, M3D_ERR_ISFATAL(model->errcode) ? "true" : "false");
  1004. dumplog(" vc_s:%d, vi_s:%d, si_s:%d, ci_s:%d, ti_s:%d, bi_s:%d, nb_s:%d, sk_s:%d, fc_s:%d, hi_s:%d, fi_s:%d\n",
  1005. model->vc_s, model->vi_s, model->si_s, model->ci_s, model->ti_s, model->bi_s,
  1006. model->nb_s, model->sk_s, model->fc_s, model->hi_s, model->fi_s);
  1007. dumplog(" name:\"%s\"\n license:\"%s\"\n author:\"%s\"\n desc:\"%s\"\n scale:%g\n",
  1008. model->name, model->license, model->author, model->desc, model->scale);
  1009. dumplog(" numcmap:%d, cmap:0x%lx,\n numtmap:%d, tmap:0x%lx,\n numtexture:%d, texture:0x%lx,\n numbone:%d, bone:0x%lx,\n",
  1010. model->numcmap, (uintptr_t)model->cmap, model->numtmap, (uintptr_t)model->tmap,
  1011. model->numtexture, (uintptr_t)model->texture, model->numbone, (uintptr_t)model->bone);
  1012. dumplog(" numvertex:%d, vertex:0x%lx,\n numskin:%d, skin:0x%lx,\n nummaterial:%d, material:0x%lx,\n"
  1013. " numface:%d, face:0x%lx,\n", model->numvertex, (uintptr_t)model->vertex, model->numskin, (uintptr_t)model->skin,
  1014. model->nummaterial, (uintptr_t)model->material, model->numface, (uintptr_t)model->face);
  1015. dumplog(" numvoxtype:%d, voxtype:0x%lx,\n numvoxel:%d, voxel:0x%lx,\n",
  1016. model->numvoxtype, (uintptr_t)model->voxtype, model->numvoxel, (uintptr_t)model->voxel);
  1017. dumplog(" numshape:%d, shape:0x%lx,\n numlabel:%d, label:0x%lx,\n numaction:%d, action:0x%lx,"
  1018. "\n numinlined:%d, inlined:0x%lx,\n numextra:%d, extra:0x%lx,\n preview.length:%d, preview.data:0x%lx\n",
  1019. model->numshape, (uintptr_t)model->shape, model->numlabel, (uintptr_t)model->label, model->numaction,
  1020. (uintptr_t)model->action, model->numinlined, (uintptr_t)model->inlined, model->numextra, (uintptr_t)model->extra,
  1021. model->preview.length, (uintptr_t)model->preview.data);
  1022. dumplog("}\n\n");
  1023. if(model->preview.length && model->preview.data) {
  1024. dumplog("m3d_t.preview.data[%d] = {", model->preview.length);
  1025. for(i = 0; i < 8; i++) dumplog("%s0x%02x(%c)", i ? ", ":" ", model->preview.data[i], model->preview.data[i] < 32 ||
  1026. model->preview.data[i] > 127 ? '.' : model->preview.data[i]);
  1027. dumplog("... }\n\n");
  1028. }
  1029. if(model->numcmap && model->cmap && model->ci_s != 8) {
  1030. dumplog("m3d_t.cmap[%d] = {", model->numcmap);
  1031. for(i = 0; i < model->numcmap; i++) dumplog("%s0x%08x", i ? ", ":" ", model->cmap[i]);
  1032. dumplog(" }\n\n");
  1033. }
  1034. if(model->numtmap && model->tmap) {
  1035. dumplog("m3d_t.tmap[%d] = {", model->numtmap);
  1036. for(i = 0; i < model->numtmap; i++)
  1037. dumplog("%s\n /* %d. */ { u:%g, v:%g }", i ? ", ":" ", i, model->tmap[i].u, model->tmap[i].v);
  1038. dumplog("\n}\n\n");
  1039. }
  1040. if(model->numvertex && model->vertex) {
  1041. dumplog("m3d_t.vertex[%d] = {", model->numvertex);
  1042. for(i = 0; i < model->numvertex; i++)
  1043. dumplog("%s\n /* %d. */ { x:%g, y:%g, z:%g, w:%g, color:0x%08x, skinid:%d }", i ? ", ":" ", i,
  1044. model->vertex[i].x, model->vertex[i].y, model->vertex[i].z, model->vertex[i].w,
  1045. model->vertex[i].color, model->vertex[i].skinid);
  1046. dumplog("\n}\n\n");
  1047. }
  1048. if(model->numface && model->face) {
  1049. dumplog("m3d_t.face[%d] = {", model->numface);
  1050. for(i = 0; i < model->numface; i++)
  1051. #ifdef M3D_VERTEXMAX
  1052. dumplog("%s\n { materialid:%d, paramid:%d, vertex[3]={%d,%d,%d}, vertmax[3]={%d,%d,%d}, normal[3]={%d,%d,%d}, "
  1053. "texcoord[3]={%d,%d,%d} }", i ? ", ":" ",
  1054. model->face[i].materialid, model->face[i].paramid,
  1055. model->face[i].vertex[0], model->face[i].vertex[1], model->face[i].vertex[2],
  1056. model->face[i].vertmax[0], model->face[i].vertmax[1], model->face[i].vertmax[2],
  1057. model->face[i].normal[0], model->face[i].normal[1], model->face[i].normal[2],
  1058. model->face[i].texcoord[0], model->face[i].texcoord[1], model->face[i].texcoord[2]);
  1059. #else
  1060. dumplog("%s\n { materialid:%d, vertex[3]={%d,%d,%d}, normal[3]={%d,%d,%d}, texcoord[3]={%d,%d,%d} }", i ? ", ":" ",
  1061. model->face[i].materialid, model->face[i].vertex[0], model->face[i].vertex[1], model->face[i].vertex[2],
  1062. model->face[i].normal[0], model->face[i].normal[1], model->face[i].normal[2],
  1063. model->face[i].texcoord[0], model->face[i].texcoord[1], model->face[i].texcoord[2]);
  1064. #endif
  1065. dumplog("\n}\n\n");
  1066. }
  1067. if(model->numvoxtype && model->voxtype) {
  1068. dumplog("m3d_t.voxtype[%d] = {", model->numvoxtype);
  1069. for(i = 0; i < model->numvoxtype; i++) {
  1070. dumplog("%s\n /* %d. */ { name:\"%s\", rotation:%02x, voxshape:%03x, materialid:%d, color:0x%08x, skinid:%d, "
  1071. "item[%d] = {", i ? ", ":" ", i, model->voxtype[i].name, model->voxtype[i].rotation,
  1072. model->voxtype[i].voxshape, model->voxtype[i].materialid, model->voxtype[i].color, model->voxtype[i].skinid,
  1073. model->voxtype[i].numitem);
  1074. if(model->voxtype[i].numitem && model->voxtype[i].item) {
  1075. for(j = 0; j < model->voxtype[i].numitem; j++)
  1076. dumplog("%s{count:%d, name:\"%s\"}", j ? ", ":" ",model->voxtype[i].item[j].count,
  1077. model->voxtype[i].item[j].name);
  1078. }
  1079. dumplog("} }");
  1080. }
  1081. dumplog("\n}\n\n");
  1082. }
  1083. if(model->numvoxel && model->voxel) {
  1084. dumplog("m3d_t.voxel[%d] = {", model->numvoxel);
  1085. for(i = 0; i < model->numvoxel; i++)
  1086. dumplog("%s\n { name:\"%s\", x:%d, y:%d, z:%d, w:%d, h:%d, d:%d, uncertain:%d, groupid:%d, data:%ld bytes }",
  1087. i ? ", ":" ", model->voxel[i].name, model->voxel[i].x, model->voxel[i].y, model->voxel[i].z,
  1088. model->voxel[i].w, model->voxel[i].h, model->voxel[i].d, model->voxel[i].uncertain, model->voxel[i].groupid,
  1089. model->voxel[i].w * model->voxel[i].h * model->voxel[i].d * sizeof(M3D_VOXEL));
  1090. dumplog("\n}\n\n");
  1091. }
  1092. if(model->numshape && model->shape) {
  1093. dumplog("m3d_t.shape[%d] = {", model->numshape);
  1094. for(i = 0; i < model->numshape; i++) {
  1095. dumplog("%s\n /* %d. */ { name:\"%s\", group:%d, numcmd:%d, cmd[%d] = {", i ? ", ":" ", i,
  1096. model->shape[i].name, model->shape[i].group, model->shape[i].numcmd, model->shape[i].numcmd);
  1097. for(j = 0; j < model->shape[i].numcmd; j++) {
  1098. dumplog("%s\n { type:%d", j ? ", ":" ", model->shape[i].cmd[j].type);
  1099. if(model->shape[i].cmd[j].type >= (unsigned int)(sizeof(m3d_commandtypes)/sizeof(m3d_commandtypes[0])))
  1100. { dumplog(", unknown command type? }"); continue; }
  1101. if(!model->shape[i].cmd[j].arg) { dumplog("arg = NULL? }"); continue; }
  1102. cd = &m3d_commandtypes[model->shape[i].cmd[j].type];
  1103. #ifdef M3D_ASCII
  1104. dumplog(" m3dc_%s", cd->key);
  1105. #endif
  1106. dumplog(", arg[%d] = { ", cd->p);
  1107. for(k = n = 0, l = cd->p; k < l; k++) {
  1108. if(j) dumplog(", ");
  1109. if(cd->a[k < cd->p ? k : (k % (cd->p - 1)) + n] == m3dcp_vc_t)
  1110. dumplog("%g", *((float*)&model->shape[i].cmd[j].arg[k]));
  1111. else
  1112. dumplog("%d", model->shape[i].cmd[j].arg[k]);
  1113. if(cd->a[k < cd->p ? k : (k % (cd->p - 1)) + n] == m3dcp_va_t) {
  1114. n = k + 1; l += model->shape[i].cmd[j].arg[k] * (cd->p - k - 1) - 1;
  1115. }
  1116. }
  1117. dumplog(" }");
  1118. }
  1119. dumplog("\n }\n }");
  1120. }
  1121. dumplog("\n}\n\n");
  1122. }
  1123. if(model->numlabel && model->label) {
  1124. dumplog("m3d_t.label[%d] = {", model->numlabel);
  1125. for(i = 0; i < model->numlabel; i++)
  1126. dumplog("%s\n { name:\"%s\", lang:\"%s\", color:0x%08x, vertexid:%d, text:\"%s\" }", i ? ", ":" ",
  1127. model->label[i].name, model->label[i].lang, model->label[i].color, model->label[i].vertexid, model->label[i].text);
  1128. dumplog("\n}\n\n");
  1129. }
  1130. if(model->numtexture && model->texture) {
  1131. dumplog("m3d_t.texture[%d] = {", model->numtexture);
  1132. for(i = 0; i < model->numtexture; i++)
  1133. dumplog("%s\n /* %d. */ { name:\"%s\", w:%d, h:%d, d:%lx }", i ? ", ":" ", i,
  1134. model->texture[i].name, model->texture[i].w, model->texture[i].h, (uintptr_t)model->texture[i].d);
  1135. dumplog("\n}\n\n");
  1136. }
  1137. if(model->numbone && model->bone) {
  1138. dumplog("m3d_t.bone[%d] = {", model->numbone);
  1139. for(i = 0; i < model->numbone; i++)
  1140. dumplog("%s\n /* %d. */ { parent:%d, name:\"%s\", pos:%d, ori:%d }", i ? ", ":" ", i,
  1141. model->bone[i].parent, model->bone[i].name, model->bone[i].pos, model->bone[i].ori);
  1142. dumplog("\n}\n\n");
  1143. }
  1144. if(model->numskin && model->skin) {
  1145. dumplog("m3d_t.skin[%d] = {", model->numskin);
  1146. for(i = 0; i < model->numskin; i++) {
  1147. dumplog("%s\n /* %d. */ { boneid[%d] = { ", i ? ", ":" ", i, M3D_NUMBONE);
  1148. for(j = 0; j < M3D_NUMBONE; j++) dumplog("%s%d", j ? ", ":" ", model->skin[i].boneid[j]);
  1149. dumplog(" }, weight[%d] = {", M3D_NUMBONE);
  1150. for(j = 0; j < M3D_NUMBONE; j++) dumplog("%s%g", j ? ", ":" ", model->skin[i].weight[j]);
  1151. dumplog(" } }");
  1152. }
  1153. dumplog("\n}\n\n");
  1154. }
  1155. if(model->nummaterial && model->material) {
  1156. dumplog("m3d_t.material[%d] = {", model->nummaterial);
  1157. for(i = 0; i < model->nummaterial; i++) {
  1158. dumplog("%s\n /* %d. */ { name:\"%s\", numprop:%d, prop[%d] = {", i ? ", ":" ", i,
  1159. model->material[i].name, model->material[i].numprop, model->material[i].numprop);
  1160. for(j = 0; j < model->material[i].numprop; j++) {
  1161. dumplog("%s\n { type:%d ", j ? ", ":" ", model->material[i].prop[j].type);
  1162. for(k = 0, l = 256; k < (int)(sizeof(m3d_propertytypes)/sizeof(m3d_propertytypes[0])); k++)
  1163. if(m3d_propertytypes[k].id == model->material[i].prop[j].type ||
  1164. m3d_propertytypes[k].id + 128 == model->material[i].prop[j].type) { l = k; break; }
  1165. if(l == 256) { dumplog("unknown property? value.num:0x%x }", model->material[i].prop[j].value.num); break; }
  1166. k = model->material[i].prop[j].type >= 128 ? m3dpf_map : m3d_propertytypes[l].format;
  1167. #ifdef M3D_ASCII
  1168. dumplog("m3dp_%s%s, value.", model->material[i].prop[j].type >= 128 ? "map_" : "", m3d_propertytypes[l].key);
  1169. #else
  1170. dumplog(", value.");
  1171. #endif
  1172. switch(k) {
  1173. case m3dpf_color: dumplog("color:0x%08x }", model->material[i].prop[j].value.color); break;
  1174. case m3dpf_float: dumplog("fnum:%g }", model->material[i].prop[j].value.fnum); break;
  1175. case m3dpf_map: dumplog("textureid:%d }", model->material[i].prop[j].value.textureid); break;
  1176. default: dumplog("num:%d }", model->material[i].prop[j].value.num); break;
  1177. }
  1178. }
  1179. dumplog("\n }\n }");
  1180. }
  1181. dumplog("\n}\n\n");
  1182. }
  1183. if(model->numaction && model->action) {
  1184. dumplog("m3d_t.action[%d] = {", model->numaction);
  1185. for(i = 0; i < model->numaction; i++) {
  1186. dumplog("%s\n /* %d. */ { name:\"%s\", durationmsec:%d, numframe:%d, frame[%d] = {", i ? ", ":" ", i,
  1187. model->action[i].name, model->action[i].durationmsec, model->action[i].numframe, model->action[i].numframe);
  1188. for(j = 0; j < model->action[i].numframe; j++) {
  1189. dumplog("%s\n { msec:%d, numtransform:%d, transform[%d] = {", j ? ", ":" ", model->action[i].frame[j].msec,
  1190. model->action[i].frame[j].numtransform, model->action[i].frame[j].numtransform);
  1191. for(k = 0; k < model->action[i].frame[j].numtransform; k++)
  1192. dumplog("%s\n { boneid:%d, pos:%d, ori:%d }", k ? ", ":" ",
  1193. model->action[i].frame[j].transform[k].boneid,
  1194. model->action[i].frame[j].transform[k].pos, model->action[i].frame[j].transform[k].ori);
  1195. dumplog("\n }\n }");
  1196. }
  1197. dumplog("\n }\n }");
  1198. }
  1199. dumplog("\n}\n\n");
  1200. }
  1201. }