xfile.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. #include "pch.h"
  2. //////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Tokenizer for text X-Files
  5. //
  6. //////////////////////////////////////////////////////////////////////////////
  7. class XToken : public IObjectSingle {
  8. public:
  9. virtual bool EnterSection(ZString& strType, ZString& strName) = 0;
  10. virtual bool LeaveSection() = 0;
  11. virtual bool ReadFloat(float& value) = 0;
  12. virtual bool ReadInt(int& value) = 0;
  13. virtual bool ReadString(ZString& str) = 0;
  14. virtual bool ReadReference(ZString& str) = 0;
  15. virtual bool Error() = 0;
  16. virtual void SetError(const ZString& str) = 0;
  17. };
  18. //////////////////////////////////////////////////////////////////////////////
  19. //
  20. // Tokenizer for text X-Files
  21. //
  22. //////////////////////////////////////////////////////////////////////////////
  23. const WORD TOKEN_TEMPLATE = 31;
  24. const WORD TOKEN_NAME = 1;
  25. const WORD TOKEN_INTEGER_LIST = 6;
  26. const WORD TOKEN_REALNUM_LIST = 7;
  27. const WORD TOKEN_OBRACE = 10;
  28. const WORD TOKEN_CBRACE = 11;
  29. class XBinaryToken : public XToken {
  30. private:
  31. PCC m_p;
  32. PCC m_pend;
  33. int m_intCount;
  34. int m_floatCount;
  35. ZString m_strError;
  36. bool MoreTokens()
  37. {
  38. return m_p < m_pend;
  39. }
  40. bool Is(WORD id)
  41. {
  42. if (*(WORD*)m_p == id) {
  43. m_p += sizeof(WORD);
  44. return true;
  45. }
  46. return false;
  47. }
  48. int GetInt()
  49. {
  50. int value = *((int*)m_p);
  51. m_p += sizeof(int);
  52. return value;
  53. }
  54. bool ReadString(ZString& str)
  55. {
  56. if (Is(TOKEN_NAME)) {
  57. int length = GetInt();
  58. str = ZString(m_p, length);
  59. m_p += length;
  60. return true;
  61. }
  62. return false;
  63. }
  64. public:
  65. XBinaryToken(PCC p, int length) :
  66. m_p(p),
  67. m_pend(p + length),
  68. m_intCount(0),
  69. m_floatCount(0)
  70. {
  71. }
  72. bool EnterSection(ZString& strType, ZString& strName)
  73. {
  74. // skip any templates
  75. while (Is(TOKEN_TEMPLATE)) {
  76. while (!Is(TOKEN_CBRACE)) m_p += 2;
  77. }
  78. if (ReadString(strType)) {
  79. ReadString(strName);
  80. return Is(TOKEN_OBRACE);
  81. }
  82. return false;
  83. }
  84. bool LeaveSection()
  85. {
  86. int level = 1;
  87. ZString str;
  88. while (MoreTokens() && level > 0) {
  89. while (ReadString(str));
  90. if (Is(TOKEN_REALNUM_LIST)) {
  91. m_p += sizeof(float) * GetInt();
  92. } else if (Is(TOKEN_INTEGER_LIST)) {
  93. m_p += sizeof(int) * GetInt();
  94. } else if (Is(TOKEN_OBRACE)) {
  95. level++;
  96. } else if (Is(TOKEN_CBRACE)) {
  97. level--;
  98. } else {
  99. m_p += 2;
  100. }
  101. }
  102. return level == 0;
  103. }
  104. bool ReadFloat(float& value)
  105. {
  106. ZAssert(m_intCount == 0);
  107. if (m_floatCount == 0) {
  108. if (Is(TOKEN_REALNUM_LIST)) {
  109. m_floatCount = GetInt();
  110. } else {
  111. return false;
  112. }
  113. }
  114. m_floatCount--;
  115. value = *((float*)m_p);
  116. m_p += sizeof(float);
  117. return true;
  118. }
  119. bool ReadInt(int& value)
  120. {
  121. ZAssert(m_floatCount == 0);
  122. if (m_intCount == 0) {
  123. if (Is(TOKEN_INTEGER_LIST)) {
  124. m_intCount = GetInt();
  125. } else {
  126. return false;
  127. }
  128. }
  129. m_intCount--;
  130. value = *((int*)m_p);
  131. m_p += sizeof(int);
  132. return true;
  133. }
  134. bool ReadReference(ZString& str)
  135. {
  136. ZUnimplemented();
  137. return false;
  138. }
  139. bool Error()
  140. {
  141. return !m_strError.IsEmpty();
  142. }
  143. void SetError(const ZString& str)
  144. {
  145. m_strError = str;
  146. }
  147. };
  148. //////////////////////////////////////////////////////////////////////////////
  149. //
  150. // Tokenizer for text X-Files
  151. //
  152. //////////////////////////////////////////////////////////////////////////////
  153. class XTextToken : public XToken, public TextTokenImpl {
  154. private:
  155. int Comma ;
  156. int SemiColon ;
  157. int LeftParen ;
  158. int RightParen ;
  159. int LeftBracket ;
  160. int RightBracket;
  161. int LeftBrace ;
  162. int RightBrace ;
  163. int Minus ;
  164. int Dummy ;
  165. public:
  166. XTextToken(PCC pcc, int length) :
  167. TextTokenImpl(pcc, length)
  168. {
  169. Comma = AddToken("','");
  170. SemiColon = AddToken("';'");
  171. LeftParen = AddToken("'('");
  172. RightParen = AddToken("')'");
  173. LeftBracket = AddToken("'['");
  174. RightBracket = AddToken("']'");
  175. LeftBrace = AddToken("'{'");
  176. RightBrace = AddToken("'}'");
  177. Minus = AddToken("'-'");
  178. Dummy = AddToken("dummy");
  179. Next();
  180. }
  181. int ParseToken(PCC& pcc)
  182. {
  183. switch (pcc[0]) {
  184. case '[': pcc++; return LeftBracket;
  185. case ']': pcc++; return RightBracket;
  186. case '{': pcc++; return LeftBrace;
  187. case '}': pcc++; return RightBrace;
  188. case '(': pcc++; return LeftParen;
  189. case ')': pcc++; return RightParen;
  190. case ',': pcc++; return Comma;
  191. case ';': pcc++; return SemiColon;
  192. case '-': pcc++; return Minus;
  193. case '<':
  194. case '>': pcc++; return Dummy;
  195. }
  196. return 0;
  197. }
  198. bool IsSymbolChar(char ch)
  199. {
  200. return TextTokenImpl::IsSymbolChar(ch) || ch == '-' || ch == '.';
  201. }
  202. bool Error()
  203. {
  204. return TextTokenImpl::Error();
  205. }
  206. void SetError(const ZString& str)
  207. {
  208. TextTokenImpl::SetError(str);
  209. }
  210. bool EnterSection(ZString& strType, ZString& strName)
  211. {
  212. if (IsSymbol(strType, false)) {
  213. IsSymbol(strName, false);
  214. return Is(LeftBrace, true);
  215. }
  216. return false;
  217. }
  218. bool LeaveSection()
  219. {
  220. int level = 1;
  221. while (MoreTokens() && level > 0) {
  222. if (Is(LeftBrace, false)) {
  223. level++;
  224. } else if (Is(RightBrace, false)) {
  225. level--;
  226. } else {
  227. Next();
  228. }
  229. }
  230. return level == 0;
  231. }
  232. bool ReadFloat(float& value)
  233. {
  234. bool fMinus = Is(Minus, false);
  235. if (IsNumber(value, true)) {
  236. while (Is(Comma, false) || Is(SemiColon, false));
  237. if (fMinus) {
  238. value = -value;
  239. }
  240. return true;
  241. }
  242. return false;
  243. }
  244. bool ReadInt(int& i)
  245. {
  246. float value;
  247. if (ReadFloat(value)) {
  248. i = (int)value;
  249. return true;
  250. }
  251. return false;
  252. }
  253. bool ReadString(ZString& str)
  254. {
  255. return IsString(str, true);
  256. }
  257. bool ReadReference(ZString& str)
  258. {
  259. if (Is(TOKEN_OBRACE, true)) {
  260. if (IsSymbol(str, true)) {
  261. if (Is(TOKEN_CBRACE, true)) {
  262. return true;
  263. }
  264. }
  265. }
  266. return false;
  267. }
  268. };
  269. //////////////////////////////////////////////////////////////////////////////
  270. //
  271. // Parser for X-Files
  272. //
  273. //////////////////////////////////////////////////////////////////////////////
  274. class XParser {
  275. TRef<Modeler> m_pmodeler;
  276. TRef<XToken> m_ptoken;
  277. TMap<ZString, TRef<TransformGeo> > m_namedFrames;
  278. int m_countMesh;
  279. int m_countFrame;
  280. public:
  281. XParser(
  282. Modeler* pmodeler,
  283. PCC pcc,
  284. int length
  285. ) :
  286. m_pmodeler(pmodeler),
  287. m_countMesh(0),
  288. m_countFrame(0)
  289. {
  290. if (pcc[8] == 't') {
  291. m_ptoken = new XTextToken(pcc + 17, length - 17);
  292. } else if (pcc[8] == 'b') {
  293. m_ptoken = new XBinaryToken(pcc + 16, length - 16);
  294. }
  295. }
  296. ~XParser()
  297. {
  298. }
  299. bool ReadTransform(TRef<Transform>& ptrans)
  300. {
  301. ZString strType;
  302. ZString strName;
  303. if (m_ptoken->EnterSection(strType, strName)) {
  304. float m00, m01, m02, m03;
  305. float m10, m11, m12, m13;
  306. float m20, m21, m22, m23;
  307. float m30, m31, m32, m33;
  308. if (
  309. m_ptoken->ReadFloat(m00)
  310. && m_ptoken->ReadFloat(m10)
  311. && m_ptoken->ReadFloat(m20)
  312. && m_ptoken->ReadFloat(m30)
  313. && m_ptoken->ReadFloat(m01)
  314. && m_ptoken->ReadFloat(m11)
  315. && m_ptoken->ReadFloat(m21)
  316. && m_ptoken->ReadFloat(m31)
  317. && m_ptoken->ReadFloat(m02)
  318. && m_ptoken->ReadFloat(m12)
  319. && m_ptoken->ReadFloat(m22)
  320. && m_ptoken->ReadFloat(m32)
  321. && m_ptoken->ReadFloat(m03)
  322. && m_ptoken->ReadFloat(m13)
  323. && m_ptoken->ReadFloat(m23)
  324. && m_ptoken->ReadFloat(m33)
  325. ) {
  326. Matrix
  327. mat(
  328. m00, m01, m02, m03,
  329. m10, m11, m12, m13,
  330. m20, m21, m22, m23,
  331. m30, m31, m32, m33
  332. );
  333. /* , this should change a LH Transform to a RH Transform
  334. mat.PreScale(Vector(1, 1, -1));
  335. mat.Scale(Vector(1, 1, -1));
  336. */
  337. ptrans = new Transform(mat);
  338. m_ptoken->LeaveSection();
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. bool ReadMesh(const ZString strMeshName, TRef<Geo>& pgeo)
  345. {
  346. m_countMesh++;
  347. //
  348. // Read in the vertices
  349. //
  350. int vcount;
  351. if (!m_ptoken->ReadInt(vcount)) {
  352. return false;
  353. }
  354. TVector<Vertex> ppositions(vcount, vcount);
  355. int index;
  356. for(index = 0; index < vcount; index++) {
  357. Vertex& vertex = ppositions.Get(index);
  358. float z;
  359. if (
  360. !m_ptoken->ReadFloat(vertex.x)
  361. || !m_ptoken->ReadFloat(vertex.y)
  362. || !m_ptoken->ReadFloat(z)
  363. ) {
  364. return false;
  365. }
  366. vertex.z = -z;
  367. vertex.u = 0;
  368. vertex.v = 0;
  369. }
  370. //
  371. // Read in the vertices for the faces
  372. //
  373. int countFace;
  374. if (!m_ptoken->ReadInt(countFace)) {
  375. return false;
  376. }
  377. TVector<WORD> pvi;
  378. TVector<int> pfaceVCounts(countFace, countFace);
  379. for(index = 0; index < countFace; index++) {
  380. int firstIndex ;
  381. int secondIndex;
  382. if (
  383. !m_ptoken->ReadInt(pfaceVCounts.Get(index))
  384. || !m_ptoken->ReadInt(firstIndex )
  385. || !m_ptoken->ReadInt(secondIndex)
  386. ) {
  387. return false;
  388. }
  389. for (int faceVIndex = 0; faceVIndex < pfaceVCounts[index] - 2; faceVIndex++) {
  390. int thirdIndex;
  391. if (!m_ptoken->ReadInt(thirdIndex)) {
  392. return false;
  393. }
  394. pvi.PushEnd(firstIndex);
  395. pvi.PushEnd(secondIndex);
  396. pvi.PushEnd(thirdIndex);
  397. secondIndex = thirdIndex;
  398. }
  399. }
  400. //
  401. // Read in the materials
  402. //
  403. int mcount = 0;
  404. int matCountFace = 0;
  405. TVector<TRef<Material> > pmaterials;
  406. TVector<TRef<Image> > pimageTextures;
  407. TVector<WORD> pmi;
  408. int ncount = 0;
  409. TVector<Vector> pnormals;
  410. TVector<WORD> pni;
  411. ZString strType;
  412. ZString strName;
  413. while (m_ptoken->EnterSection(strType, strName)) {
  414. if (strType == "MeshMaterialList") {
  415. if (
  416. !m_ptoken->ReadInt(mcount)
  417. || !m_ptoken->ReadInt(matCountFace)
  418. ) {
  419. return false;
  420. }
  421. ZAssert(matCountFace == 1 || countFace == matCountFace);
  422. //
  423. // read in per face material indicies
  424. //
  425. for(index = 0; index < matCountFace; index++) {
  426. int mi;
  427. if (!m_ptoken->ReadInt(mi)) {
  428. return false;
  429. }
  430. pmi.PushEnd(mi);
  431. }
  432. //
  433. // read in the materials
  434. //
  435. for (index = 0; index < mcount; index++) {
  436. ZVerify(m_ptoken->EnterSection(strType, strName));
  437. ZAssert(strType == "Material");
  438. float rd, gd, bd, ad;
  439. float power;
  440. float rs, gs, bs;
  441. float re, ge, be;
  442. if (
  443. !m_ptoken->ReadFloat(rd)
  444. || !m_ptoken->ReadFloat(gd)
  445. || !m_ptoken->ReadFloat(bd)
  446. || !m_ptoken->ReadFloat(ad)
  447. || !m_ptoken->ReadFloat(power)
  448. || !m_ptoken->ReadFloat(rs)
  449. || !m_ptoken->ReadFloat(gs)
  450. || !m_ptoken->ReadFloat(bs)
  451. || !m_ptoken->ReadFloat(re)
  452. || !m_ptoken->ReadFloat(ge)
  453. || !m_ptoken->ReadFloat(be)
  454. ) {
  455. return false;
  456. }
  457. Color colorDiffuse(rd, gd, bd, ad);
  458. Color colorSpecular(rs, gs, bs);
  459. Color colorEmissive(re, ge, be);
  460. TRef<Image> pimageTexture;
  461. if (m_ptoken->EnterSection(strType, strName)) {
  462. ZAssert(strType == "TextureFilename");
  463. PathString strFilename;
  464. if (!m_ptoken->ReadString(strFilename)) {
  465. return false;
  466. }
  467. ZAssert(
  468. strFilename.GetExtension() == "ppm"
  469. || strFilename.GetExtension() == "bmp"
  470. );
  471. strFilename = strFilename.LeftOf(4).ToLower() + "bmp";
  472. pimageTexture = m_pmodeler->LoadImage(strFilename, false);
  473. m_ptoken->LeaveSection();
  474. } else if (m_ptoken->Error()) {
  475. return false;
  476. }
  477. pmaterials.PushEnd(
  478. CreateMaterial(
  479. colorDiffuse,
  480. colorSpecular,
  481. colorEmissive,
  482. power
  483. )
  484. );
  485. pimageTextures.PushEnd(pimageTexture);
  486. m_ptoken->LeaveSection();
  487. }
  488. } else if (strType == "MeshVertexColors" ) {
  489. // , parse this?
  490. } else if (strType == "MeshFaceWraps" ) {
  491. // , parse this?
  492. } else if (strType == "MeshTextureCoords") {
  493. int vcountTextureCoords;
  494. if (!m_ptoken->ReadInt(vcountTextureCoords)) {
  495. return false;
  496. }
  497. ZAssert(vcount == vcountTextureCoords);
  498. for(index = 0; index < vcount; index++) {
  499. Vertex& vertex = ppositions.Get(index);
  500. if (
  501. !m_ptoken->ReadFloat(vertex.u)
  502. || !m_ptoken->ReadFloat(vertex.v)
  503. ) {
  504. return false;
  505. }
  506. }
  507. } else if (strType == "MeshNormals") {
  508. //
  509. // Read in the normals
  510. //
  511. if (!m_ptoken->ReadInt(ncount)) {
  512. return false;
  513. }
  514. pnormals.SetCount(ncount);
  515. for(index = 0; index < ncount; index++) {
  516. float x, y, z;
  517. if (
  518. !m_ptoken->ReadFloat(x)
  519. || !m_ptoken->ReadFloat(y)
  520. || !m_ptoken->ReadFloat(z)
  521. ) {
  522. return false;
  523. }
  524. pnormals.Get(index) = Vector(x, y, -z);
  525. }
  526. //
  527. // Read in the normals for the faces
  528. //
  529. int countFaceNormals;
  530. if (!m_ptoken->ReadInt(countFaceNormals)) {
  531. return false;
  532. }
  533. ZAssert(countFace == countFaceNormals);
  534. for(index = 0; index < countFace; index++) {
  535. int faceVCount ;
  536. int firstIndex ;
  537. int secondIndex;
  538. if (
  539. !m_ptoken->ReadInt(faceVCount )
  540. || !m_ptoken->ReadInt(firstIndex )
  541. || !m_ptoken->ReadInt(secondIndex)
  542. ) {
  543. return false;
  544. }
  545. for (int faceVIndex = 0; faceVIndex < faceVCount - 2; faceVIndex++) {
  546. int thirdIndex;
  547. if (!m_ptoken->ReadInt(thirdIndex)) {
  548. return false;
  549. }
  550. pni.PushEnd(firstIndex);
  551. pni.PushEnd(secondIndex);
  552. pni.PushEnd(thirdIndex);
  553. secondIndex = thirdIndex;
  554. }
  555. }
  556. }
  557. m_ptoken->LeaveSection();
  558. }
  559. if (m_ptoken->Error()) {
  560. return false;
  561. }
  562. //
  563. // Create unique Position Normal pairs
  564. //
  565. TVector<Vertex> vertices;
  566. TVector<WORD> indices;
  567. if (ncount > 0) {
  568. TVector<TList<WORD> > vertexTree(vcount, vcount);
  569. int count = pvi.GetCount();
  570. for (index = 0; index < count; index++) {
  571. int iVertex = pvi[index];
  572. int iNormal = pni[index];
  573. TList<WORD>::Iterator iter(vertexTree.Get(iVertex));
  574. do {
  575. if (iter.End()) {
  576. // create a new vertex
  577. Vertex vertex;
  578. vertex.x = ppositions[iVertex].x;
  579. vertex.y = ppositions[iVertex].y;
  580. vertex.z = ppositions[iVertex].z;
  581. vertex.u = ppositions[iVertex].u;
  582. vertex.v = ppositions[iVertex].v;
  583. vertex.nx = pnormals[iNormal].X();
  584. vertex.ny = pnormals[iNormal].Y();
  585. vertex.nz = pnormals[iNormal].Z();
  586. vertices.PushEnd(vertex);
  587. indices.PushEnd(vertices.GetCount() - 1);
  588. vertexTree.Get(iVertex).PushFront(vertices.GetCount() - 1);
  589. break;
  590. }
  591. Vertex& vertex = vertices.Get(iter.Value());
  592. if (
  593. vertex.nx == pnormals[iNormal].X()
  594. && vertex.ny == pnormals[iNormal].Y()
  595. && vertex.nz == pnormals[iNormal].Z()
  596. ) {
  597. indices.PushEnd(iter.Value());
  598. break;
  599. }
  600. iter.Next();
  601. } while (true);
  602. }
  603. } else {
  604. vertices = ppositions;
  605. indices = pvi;
  606. }
  607. //
  608. // Split up the mesh based on materials
  609. //
  610. if (mcount == 0) {
  611. pgeo = Geo::CreateMesh(vertices, indices);
  612. return true;
  613. } else if (matCountFace == 1 || mcount == 1) {
  614. if (pimageTextures[0]) {
  615. pgeo =
  616. new TextureGeo(
  617. new MaterialGeo(
  618. Geo::CreateMesh(vertices, indices),
  619. pmaterials[0]
  620. ),
  621. pimageTextures[0]
  622. );
  623. return true;
  624. } else {
  625. pgeo =
  626. new MaterialGeo(
  627. Geo::CreateMesh(vertices, indices),
  628. pmaterials[0]
  629. );
  630. return true;
  631. }
  632. }
  633. //
  634. // Multiple Materials
  635. //
  636. TRef<GroupGeo> pgroup = GroupGeo::Create();
  637. TVector<Vertex> componentVertices;
  638. TVector<WORD> componentIndices;
  639. TVector<WORD> map(vertices.GetCount(), vertices.GetCount());
  640. //
  641. // for each material create a smaller mesh for all the faces that have that material
  642. //
  643. for (int indexMaterial = 0; indexMaterial < mcount; indexMaterial++) {
  644. for (index = 0; index < map.GetCount(); index++) {
  645. map.Set(index, 0xffff);
  646. }
  647. //
  648. // set the small mesh to empty
  649. //
  650. componentVertices.SetCount(0);
  651. componentIndices.SetCount(0);
  652. //
  653. // add all of the verticies and faces for which the material matches
  654. //
  655. int indexStart = 0;
  656. for (int indexFace = 0; indexFace < countFace; indexFace++) {
  657. int indexEnd = indexStart + (pfaceVCounts[indexFace] - 2) * 3;
  658. if (pmi[indexFace] == indexMaterial) {
  659. for (
  660. index = indexStart;
  661. index < indexEnd;
  662. index++
  663. ) {
  664. int indexVertex = indices[index];
  665. if (map[indexVertex] == 0xffff) {
  666. map.Set(indexVertex, componentVertices.GetCount());
  667. componentVertices.PushEnd(vertices[indexVertex]);
  668. }
  669. componentIndices.PushEnd(map[indexVertex]);
  670. }
  671. }
  672. indexStart = indexEnd;
  673. }
  674. //
  675. // create the mesh and stick it into the group
  676. //
  677. if (pimageTextures[indexMaterial]) {
  678. pgroup->AddGeo(
  679. new TextureGeo(
  680. new MaterialGeo(
  681. Geo::CreateMesh(componentVertices, componentIndices),
  682. pmaterials[indexMaterial]
  683. ),
  684. pimageTextures[indexMaterial]
  685. )
  686. );
  687. } else {
  688. pgroup->AddGeo(
  689. new MaterialGeo(
  690. Geo::CreateMesh(componentVertices, componentIndices),
  691. pmaterials[indexMaterial]
  692. )
  693. );
  694. }
  695. }
  696. pgeo = pgroup;
  697. return true;
  698. }
  699. bool ReadFrame(const ZString& strFrameName, TRef<Geo>& pgeo)
  700. {
  701. m_countFrame++;
  702. //
  703. // Create a group
  704. //
  705. TRef<GroupGeo> pgeoGroup = GroupGeo::Create();
  706. pgeoGroup->SetName(strFrameName);
  707. //
  708. // Create the group transform
  709. //
  710. TRef<Transform> ptrans;
  711. if (!ReadTransform(ptrans)) {
  712. if (m_ptoken->Error()) {
  713. return false;
  714. }
  715. ptrans = new Transform();
  716. }
  717. TRef<TransformGeo> ptransformGeo = new TransformGeo(pgeoGroup, ptrans);
  718. //
  719. // return the transformGeo
  720. //
  721. pgeo = ptransformGeo;
  722. //
  723. // remember the the name of the transform so we can replace it
  724. // if it is animated
  725. //
  726. m_namedFrames.Set(strFrameName, ptransformGeo);
  727. //
  728. // read the children of this frame
  729. //
  730. ZString strType;
  731. ZString strName;
  732. while (m_ptoken->EnterSection(strType, strName)) {
  733. TRef<Geo> pgeoChild;
  734. if (strType == "Frame") {
  735. if (!ReadFrame(strName, pgeoChild)) {
  736. return false;
  737. }
  738. } else if (strType == "Mesh") {
  739. if (!ReadMesh(strName, pgeoChild)) {
  740. return false;
  741. }
  742. } else {
  743. m_ptoken->SetError("Unknown Section Type");
  744. return false;
  745. }
  746. pgeoGroup->AddGeo(pgeoChild);
  747. m_ptoken->LeaveSection();
  748. }
  749. return !m_ptoken->Error();
  750. }
  751. bool ReadAnimationKey(
  752. Number* pnumberFrame,
  753. TRef<Transform>& ptransformTranslate,
  754. TRef<Transform>& ptransformScale,
  755. TRef<Transform>& ptransformRotate
  756. ) {
  757. int type ;
  758. int count;
  759. if (
  760. !m_ptoken->ReadInt(type )
  761. || !m_ptoken->ReadInt(count)
  762. ) {
  763. return false;
  764. }
  765. switch (type) {
  766. case 0:
  767. {
  768. // rotation keys
  769. TRef<KeyFramedRotateTransform> ptrans = CreateKeyFramedRotateTransform(pnumberFrame);
  770. ptransformRotate = ptrans;
  771. for (int index = 0; index < count; index++) {
  772. int time;
  773. int count;
  774. float s;
  775. float x;
  776. float y;
  777. float z;
  778. if (
  779. !m_ptoken->ReadInt(time)
  780. || !m_ptoken->ReadInt(count)
  781. || !m_ptoken->ReadFloat(s)
  782. || !m_ptoken->ReadFloat(x)
  783. || !m_ptoken->ReadFloat(y)
  784. || !m_ptoken->ReadFloat(z)
  785. ) {
  786. return false;
  787. }
  788. ZAssert(count == 4);
  789. ptrans->AddKey((float)time, Quaternion(s, x, y, -z));
  790. }
  791. }
  792. break;
  793. case 1:
  794. {
  795. // scale keys
  796. TRef<KeyFramedScaleTransform> ptrans = CreateKeyFramedScaleTransform(pnumberFrame);
  797. ptransformScale = ptrans;
  798. for (int index = 0; index < count; index++) {
  799. int time;
  800. int count;
  801. float x;
  802. float y;
  803. float z;
  804. if (
  805. !m_ptoken->ReadInt(time)
  806. || !m_ptoken->ReadInt(count)
  807. || !m_ptoken->ReadFloat(x)
  808. || !m_ptoken->ReadFloat(y)
  809. || !m_ptoken->ReadFloat(z)
  810. ) {
  811. return false;
  812. }
  813. ZAssert(count == 3);
  814. ptrans->AddKey((float)time, Vector(x, y, z));
  815. }
  816. }
  817. break;
  818. case 2:
  819. {
  820. // Translate keys
  821. TRef<KeyFramedTranslateTransform> ptrans = CreateKeyFramedTranslateTransform(pnumberFrame);
  822. ptransformTranslate = ptrans;
  823. for (int index = 0; index < count; index++) {
  824. int time;
  825. int count;
  826. float x;
  827. float y;
  828. float z;
  829. if (
  830. !m_ptoken->ReadInt(time)
  831. || !m_ptoken->ReadInt(count)
  832. || !m_ptoken->ReadFloat(x)
  833. || !m_ptoken->ReadFloat(y)
  834. || !m_ptoken->ReadFloat(z)
  835. ) {
  836. return false;
  837. }
  838. ZAssert(count == 3);
  839. ptrans->AddKey((float)time, Vector(x, y, -z));
  840. }
  841. }
  842. break;
  843. default:
  844. m_ptoken->SetError("Invalid key type");
  845. return false;
  846. }
  847. return true;
  848. }
  849. bool ReadAnimation(Number* pnumberFrame)
  850. {
  851. ZString strFrameName;
  852. if (m_ptoken->ReadReference(strFrameName)) {
  853. //
  854. // Read in any Animation Keys
  855. //
  856. TRef<Transform> ptransformTranslate;
  857. TRef<Transform> ptransformRotate;
  858. TRef<Transform> ptransformScale;
  859. ZString strType;
  860. ZString strName;
  861. while (m_ptoken->EnterSection(strType, strName)) {
  862. if (strType == "AnimationKey") {
  863. if (!ReadAnimationKey(pnumberFrame, ptransformTranslate, ptransformScale, ptransformRotate)) {
  864. return false;
  865. }
  866. } else if (strType == "AnimationOptions") {
  867. ZError("Not Implemented");
  868. }
  869. m_ptoken->LeaveSection();
  870. }
  871. //
  872. // if there were any transforms replace the old transform
  873. //
  874. if (!m_ptoken->Error()) {
  875. if (ptransformRotate || ptransformScale || ptransformTranslate) {
  876. //
  877. // Find the frame we will be animating
  878. //
  879. TRef<TransformGeo> ptransformGeo;
  880. ZVerify(m_namedFrames.Find(strFrameName, ptransformGeo));
  881. //
  882. // build the new transformed geo
  883. //
  884. TRef<Geo> pgeo = ptransformGeo->GetGeo();
  885. if (ptransformRotate) {
  886. pgeo = new TransformGeo(pgeo, ptransformRotate);
  887. }
  888. if (ptransformScale) {
  889. pgeo = new TransformGeo(pgeo, ptransformScale);
  890. }
  891. if (ptransformTranslate) {
  892. pgeo = new TransformGeo(pgeo, ptransformTranslate);
  893. }
  894. ptransformGeo->ChangeTo(pgeo);
  895. }
  896. return true;
  897. }
  898. }
  899. return false;
  900. }
  901. bool ReadAnimationSet(Number* pnumberFrame)
  902. {
  903. ZString strType;
  904. ZString strName;
  905. while (m_ptoken->EnterSection(strType, strName)) {
  906. if (strType == "Animation") {
  907. if (!ReadAnimation(pnumberFrame)) {
  908. return false;
  909. }
  910. }
  911. m_ptoken->LeaveSection();
  912. }
  913. return !m_ptoken->Error();
  914. }
  915. TRef<Geo> ReadFile(Number* pnumberFrame, bool& bAnimation)
  916. {
  917. TRef<GroupGeo> pgroup = GroupGeo::Create();
  918. ZString strType;
  919. ZString strName;
  920. while (m_ptoken->EnterSection(strType, strName)) {
  921. TRef<Geo> pgeoChild;
  922. if (strType == "Frame") {
  923. if (!ReadFrame(strName, pgeoChild)) {
  924. return false;
  925. }
  926. } else if (strType == "Mesh") {
  927. if (!ReadMesh(strName, pgeoChild)) {
  928. return false;
  929. }
  930. } else if (strType == "AnimationSet") {
  931. ZAssert(!bAnimation);
  932. bAnimation = true;
  933. if (!ReadAnimationSet(pnumberFrame)) {
  934. return false;
  935. }
  936. }
  937. if (pgeoChild) {
  938. pgroup->AddGeo(pgeoChild);
  939. }
  940. m_ptoken->LeaveSection();
  941. }
  942. if (m_ptoken->Error()) {
  943. return NULL;
  944. } else {
  945. return pgroup;
  946. }
  947. }
  948. };
  949. //////////////////////////////////////////////////////////////////////////////
  950. //
  951. // Parse Animation Data in X-File
  952. //
  953. //////////////////////////////////////////////////////////////////////////////
  954. TRef<Geo> ImportXFile(Modeler* pmodeler, ZFile* pfile, Number* pnumberFrame, bool& bAnimation)
  955. {
  956. XParser parser(pmodeler, (PCC)pfile->GetPointer(), pfile->GetLength());
  957. bAnimation = false;
  958. return parser.ReadFile(pnumberFrame, bAnimation);
  959. }