MLR_I_C_PMesh.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "MLRHeaders.hpp"
  5. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  6. BitTrace *MLR_I_C_PMesh_Clip;
  7. #endif
  8. //#############################################################################
  9. //###### MLR_I_C_PMesh with color but no lighting one texture layer #####
  10. //#############################################################################
  11. MLR_I_C_PMesh::ClassData*
  12. MLR_I_C_PMesh::DefaultData = NULL;
  13. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14. //
  15. void
  16. MLR_I_C_PMesh::InitializeClass()
  17. {
  18. Verify(!DefaultData);
  19. Verify(gos_GetCurrentHeap() == StaticHeap);
  20. DefaultData =
  21. new ClassData(
  22. MLR_I_C_PMeshClassID,
  23. "MidLevelRenderer::MLR_I_C_PMesh",
  24. MLR_I_PMesh::DefaultData,
  25. (MLRPrimitiveBase::Factory)&Make
  26. );
  27. Register_Object(DefaultData);
  28. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  29. MLR_I_C_PMesh_Clip = new BitTrace("MLR_I_C_PMesh_Clip");
  30. Register_Object(MLR_I_C_PMesh_Clip);
  31. #endif
  32. }
  33. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  34. //
  35. void
  36. MLR_I_C_PMesh::TerminateClass()
  37. {
  38. Unregister_Object(DefaultData);
  39. delete DefaultData;
  40. DefaultData = NULL;
  41. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  42. Unregister_Object(MLR_I_C_PMesh_Clip);
  43. delete MLR_I_C_PMesh_Clip;
  44. #endif
  45. }
  46. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. //
  48. MLR_I_C_PMesh::MLR_I_C_PMesh(
  49. ClassData *class_data,
  50. MemoryStream *stream,
  51. int version
  52. ):
  53. MLR_I_PMesh(class_data, stream, version)
  54. {
  55. Verify(gos_GetCurrentHeap() == Heap);
  56. Check_Pointer(this);
  57. Check_Pointer(stream);
  58. switch(version)
  59. {
  60. case 1:
  61. case 2:
  62. {
  63. STOP(("This class got created only after version 2 !"));
  64. }
  65. break;
  66. default:
  67. {
  68. #if COLOR_AS_DWORD
  69. MemoryStreamIO_Read(stream, &colors);
  70. #else
  71. Stuff::DynamicArrayOf<DWORD> smallColors;
  72. MemoryStreamIO_Read(stream, &smallColors);
  73. int i, len = smallColors.GetLength();
  74. colors.SetLength(len);
  75. DWORD theColor;
  76. for(i=0;i<len;i++)
  77. {
  78. theColor = smallColors[i];
  79. colors[i].blue = (theColor & 0xff) * One_Over_256;
  80. theColor = theColor>>8;
  81. colors[i].green = (theColor & 0xff) * One_Over_256;
  82. theColor = theColor>>8;
  83. colors[i].red = (theColor & 0xff) * One_Over_256;
  84. theColor = theColor>>8;
  85. colors[i].alpha = (theColor & 0xff) * One_Over_256;
  86. }
  87. #endif
  88. }
  89. break;
  90. }
  91. }
  92. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. //
  94. MLR_I_C_PMesh::MLR_I_C_PMesh(ClassData *class_data):
  95. MLR_I_PMesh(class_data), colors(0)
  96. {
  97. Check_Pointer(this);
  98. Verify(gos_GetCurrentHeap() == Heap);
  99. }
  100. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  101. /*
  102. void
  103. MLR_I_C_PMesh::Copy(MLRIndexedPolyMesh *polyMesh)
  104. {
  105. Check_Pointer(this);
  106. int len;
  107. #if COLOR_AS_DWORD
  108. DWORD *_colors;
  109. #else
  110. RGBAColor *_colors;
  111. #endif
  112. MLR_I_PMesh::Copy(polyMesh);
  113. polyMesh->GetColorData(&_colors, &len);
  114. SetColorData(_colors, len);
  115. }
  116. */
  117. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118. //
  119. MLR_I_C_PMesh::~MLR_I_C_PMesh()
  120. {
  121. Check_Object(this);
  122. }
  123. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  124. //
  125. MLR_I_C_PMesh*
  126. MLR_I_C_PMesh::Make(
  127. MemoryStream *stream,
  128. int version
  129. )
  130. {
  131. Check_Object(stream);
  132. gos_PushCurrentHeap(Heap);
  133. MLR_I_C_PMesh *mesh = new MLR_I_C_PMesh(DefaultData, stream, version);
  134. gos_PopCurrentHeap();
  135. return mesh;
  136. }
  137. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  138. //
  139. void
  140. MLR_I_C_PMesh::Save(MemoryStream *stream)
  141. {
  142. Check_Object(this);
  143. Check_Object(stream);
  144. MLR_I_PMesh::Save(stream);
  145. #if COLOR_AS_DWORD
  146. MemoryStreamIO_Write(stream, &colors);
  147. #else
  148. Stuff::DynamicArrayOf<DWORD> smallColors;
  149. int i, len = colors.GetLength();
  150. const Stuff::RGBAColor *data = colors.GetData();
  151. smallColors.SetLength(len);
  152. for(i=0;i<len;i++)
  153. {
  154. smallColors[i] = GOSCopyColor(data+i);
  155. }
  156. MemoryStreamIO_Write(stream, &smallColors);
  157. #endif
  158. }
  159. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  160. //
  161. void
  162. MLR_I_C_PMesh::TestInstance() const
  163. {
  164. Verify(IsDerivedFrom(DefaultData));
  165. }
  166. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  167. //
  168. void
  169. MLR_I_C_PMesh::SetColorData(
  170. #if COLOR_AS_DWORD
  171. const DWORD *data,
  172. #else
  173. const RGBAColor *data,
  174. #endif
  175. int dataSize
  176. )
  177. {
  178. Check_Object(this);
  179. Check_Pointer(data);
  180. Verify(coords.GetLength() == 0 || dataSize == coords.GetLength());
  181. Verify(texCoords.GetLength() == 0 || dataSize == texCoords.GetLength());
  182. colors.AssignData(data, dataSize);
  183. }
  184. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  185. //
  186. void
  187. MLR_I_C_PMesh::GetColorData(
  188. #if COLOR_AS_DWORD
  189. DWORD **data,
  190. #else
  191. RGBAColor **data,
  192. #endif
  193. int *dataSize
  194. )
  195. {
  196. Check_Object(this);
  197. *data = colors.GetData();
  198. *dataSize = colors.GetLength();
  199. }
  200. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  201. //
  202. void
  203. MLR_I_C_PMesh::PaintMe(
  204. #if COLOR_AS_DWORD
  205. const DWORD *paintMe
  206. #else
  207. const RGBAColor *paintMe
  208. #endif
  209. )
  210. {
  211. Check_Object(this);
  212. // original color is lost !!!;
  213. int k, len = colors.GetLength();
  214. #if COLOR_AS_DWORD
  215. DWORD argb = GOSCopyColor(paintMe);
  216. for(k=0;k<len;k++)
  217. {
  218. colors[k] = argb;
  219. }
  220. #else
  221. for(k=0;k<len;k++)
  222. {
  223. colors[k] = *paintMe;
  224. }
  225. #endif
  226. }
  227. #undef I_SAY_YES_TO_DUAL_TEXTURES
  228. #define I_SAY_YES_TO_COLOR
  229. #undef I_SAY_YES_TO_LIGHTING
  230. #define CLASSNAME MLR_I_C_PMesh
  231. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  232. #define SET_MLR_PMESH_CLIP() MLR_I_C_PMesh_Clip->Set()
  233. #define CLEAR_MLR_PMESH_CLIP() MLR_I_C_PMesh_Clip->Clear()
  234. #else
  235. #define SET_MLR_PMESH_CLIP()
  236. #define CLEAR_MLR_PMESH_CLIP()
  237. #endif
  238. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  239. // This include contains follwing functions:
  240. // void MLR_I_C_PMesh::TransformNoClip(Matrix4D*, GOSVertexPool*);
  241. // int MLR_I_C_PMesh::Clip(MLRClippingState, GOSVertexPool*);
  242. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  243. #include <MLR\MLRPrimitiveClipping.hpp>
  244. #undef I_SAY_YES_TO_COLOR
  245. #undef CLASSNAME
  246. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  247. //
  248. MLR_I_C_PMesh*
  249. MidLevelRenderer::CreateIndexedCube_Color_NoLit(
  250. Scalar half,
  251. RGBAColor *eightColors,
  252. MLRState *state
  253. )
  254. {
  255. gos_PushCurrentHeap(Heap);
  256. MLR_I_C_PMesh *ret = new MLR_I_C_PMesh;
  257. Register_Object(ret);
  258. Point3D *coords = new Point3D [8];
  259. Register_Pointer(coords);
  260. coords[0] = Point3D( half, -half, half);
  261. coords[1] = Point3D(-half, -half, half);
  262. coords[2] = Point3D( half, -half, -half);
  263. coords[3] = Point3D(-half, -half, -half);
  264. coords[4] = Point3D(-half, half, half);
  265. coords[5] = Point3D( half, half, half);
  266. coords[6] = Point3D( half, half, -half);
  267. coords[7] = Point3D(-half, half, -half);
  268. unsigned char *lengths = new unsigned char [6];
  269. Register_Pointer(lengths);
  270. int i;
  271. for(i=0;i<6;i++)
  272. {
  273. lengths[i] = 4;
  274. }
  275. ret->SetSubprimitiveLengths(lengths, 6);
  276. Unregister_Pointer(lengths);
  277. delete [] lengths;
  278. ret->SetCoordData(coords, 8);
  279. Unregister_Pointer(coords);
  280. delete [] coords;
  281. unsigned short *index = new unsigned short [6*4];
  282. Register_Pointer(index);
  283. index[0] = 0;
  284. index[1] = 2;
  285. index[2] = 6;
  286. index[3] = 5;
  287. index[4] = 0;
  288. index[5] = 5;
  289. index[6] = 4;
  290. index[7] = 1;
  291. index[8] = 5;
  292. index[9] = 6;
  293. index[10] = 7;
  294. index[11] = 4;
  295. index[12] = 2;
  296. index[13] = 3;
  297. index[14] = 7;
  298. index[15] = 6;
  299. index[16] = 1;
  300. index[17] = 4;
  301. index[18] = 7;
  302. index[19] = 3;
  303. index[20] = 0;
  304. index[21] = 1;
  305. index[22] = 3;
  306. index[23] = 2;
  307. ret->SetIndexData(index, 6*4);
  308. Unregister_Pointer(index);
  309. delete [] index;
  310. ret->FindFacePlanes();
  311. if(eightColors!=NULL)
  312. {
  313. #if COLOR_AS_DWORD
  314. DWORD *dwColor = new DWORD [8];
  315. Register_Pointer(dwColor);
  316. for(i=0;i<8;i++)
  317. {
  318. dwColor[i] = GOSCopyColor(eightColors+i);
  319. }
  320. ret->SetColorData(dwColor, 8);
  321. Unregister_Pointer(dwColor);
  322. delete [] dwColor;
  323. #else
  324. ret->SetColorData(eightColors, 8);
  325. #endif
  326. }
  327. Vector2DScalar *texCoords = new Vector2DScalar[8];
  328. Register_Pointer(texCoords);
  329. texCoords[0] = Vector2DScalar(0.0f, 0.0f);
  330. texCoords[1] = Vector2DScalar(0.0f, 0.0f);
  331. texCoords[2] = Vector2DScalar(0.0f, 0.0f);
  332. texCoords[3] = Vector2DScalar(0.0f, 0.0f);
  333. texCoords[4] = Vector2DScalar(0.0f, 0.0f);
  334. texCoords[5] = Vector2DScalar(0.0f, 0.0f);
  335. texCoords[6] = Vector2DScalar(0.0f, 0.0f);
  336. texCoords[7] = Vector2DScalar(0.0f, 0.0f);
  337. if(state != NULL)
  338. {
  339. ret->SetReferenceState(*state);
  340. if(state->GetTextureHandle() > 0)
  341. {
  342. texCoords[0] = Vector2DScalar(0.0f, 0.0f);
  343. texCoords[1] = Vector2DScalar(1.0f, 0.0f);
  344. texCoords[2] = Vector2DScalar(0.25f, 0.25f);
  345. texCoords[3] = Vector2DScalar(0.75f, 0.25f);
  346. texCoords[4] = Vector2DScalar(1.0f, 1.0f);
  347. texCoords[5] = Vector2DScalar(0.0f, 1.0f);
  348. texCoords[6] = Vector2DScalar(0.25f, 0.75f);
  349. texCoords[7] = Vector2DScalar(0.75f, 0.75f);
  350. }
  351. }
  352. ret->SetTexCoordData(texCoords, 8);
  353. Unregister_Pointer(texCoords);
  354. delete [] texCoords;
  355. gos_PopCurrentHeap();
  356. return ret;
  357. }
  358. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  359. //
  360. MLR_I_PMesh*
  361. MidLevelRenderer::CreateIndexedViewFrustrum_Color_NoLit(
  362. Scalar near_clip,
  363. Scalar far_clip,
  364. Scalar left_clip,
  365. Scalar right_clip,
  366. Scalar top_clip,
  367. Scalar bottom_clip,
  368. RGBAColor& color,
  369. MLRState *state
  370. )
  371. {
  372. gos_PushCurrentHeap(Heap);
  373. MLR_I_C_PMesh *ret = new MLR_I_C_PMesh();
  374. Register_Object(ret);
  375. Point3D *coords = new Point3D [8];
  376. Register_Pointer(coords);
  377. Scalar right_far, left_far, top_far, bottom_far;
  378. Scalar fac = (near_clip + far_clip) / near_clip;
  379. right_far = right_clip*fac;
  380. left_far = left_clip*fac;
  381. top_far = top_clip*fac;
  382. bottom_far = bottom_clip*fac;
  383. coords[0] = Point3D(left_far, bottom_far, far_clip);
  384. coords[1] = Point3D(right_far, bottom_far, far_clip);
  385. coords[2] = Point3D(left_clip, bottom_clip, near_clip);
  386. coords[3] = Point3D(right_clip, bottom_clip, near_clip);
  387. coords[4] = Point3D(right_far, top_far, far_clip);
  388. coords[5] = Point3D(left_far, top_far, far_clip);
  389. coords[6] = Point3D(left_clip, top_clip, near_clip);
  390. coords[7] = Point3D(right_clip, top_clip, near_clip);
  391. unsigned char *lengths = new unsigned char [6];
  392. Register_Pointer(lengths);
  393. int i;
  394. for(i=0;i<6;i++)
  395. {
  396. lengths[i] = 4;
  397. }
  398. // ret->SetSubprimitiveLengths(lengths, 6);
  399. ret->SetSubprimitiveLengths(lengths, 5);
  400. ret->SetCoordData(coords, 8);
  401. unsigned short *index = new unsigned short [6*4];
  402. Register_Pointer(index);
  403. index[0] = 0;
  404. index[1] = 5;
  405. index[2] = 4;
  406. index[3] = 1;
  407. index[4] = 0;
  408. index[5] = 1;
  409. index[6] = 3;
  410. index[7] = 2;
  411. index[8] = 0;
  412. index[9] = 2;
  413. index[10] = 6;
  414. index[11] = 5;
  415. index[12] = 1;
  416. index[13] = 4;
  417. index[14] = 7;
  418. index[15] = 3;
  419. index[16] = 2;
  420. index[17] = 3;
  421. index[18] = 7;
  422. index[19] = 6;
  423. // index[20] = 5;
  424. // index[21] = 6;
  425. // index[22] = 7;
  426. // index[23] = 4;
  427. // ret->SetIndexData(index, 6*4);
  428. ret->SetIndexData(index, 5*4);
  429. ret->FindFacePlanes();
  430. #if COLOR_AS_DWORD
  431. DWORD *colors = new DWORD [8];
  432. Register_Pointer(colors);
  433. colors[0] = GOSCopyColor(color);
  434. for(i=1;i<8;i++)
  435. {
  436. colors[i] = colors[0];
  437. }
  438. #else
  439. RGBAColor *colors = new RGBAColor[8];
  440. Register_Pointer(colors);
  441. for(i=0;i<8;i++)
  442. {
  443. colors[i] = color;
  444. }
  445. for(i=0;i<4;i++)
  446. {
  447. colors[i].red *= 0.8f;;
  448. colors[i].green *= 0.8f;;
  449. colors[i].blue *= 0.8f;;
  450. colors[i+4].red *= 1.2f;;
  451. colors[i+4].green *= 1.2f;;
  452. colors[i+4].blue *= 1.2f;;
  453. }
  454. #endif
  455. ret->SetColorData(colors, 8);
  456. Vector2DScalar *texCoords = new Vector2DScalar[8];
  457. Register_Pointer(texCoords);
  458. texCoords[0] = Vector2DScalar(0.0f, 0.0f);
  459. texCoords[1] = Vector2DScalar(0.0f, 0.0f);
  460. texCoords[2] = Vector2DScalar(0.0f, 0.0f);
  461. texCoords[3] = Vector2DScalar(0.0f, 0.0f);
  462. texCoords[4] = Vector2DScalar(0.0f, 0.0f);
  463. texCoords[5] = Vector2DScalar(0.0f, 0.0f);
  464. texCoords[6] = Vector2DScalar(0.0f, 0.0f);
  465. texCoords[7] = Vector2DScalar(0.0f, 0.0f);
  466. ret->SetTexCoordData(texCoords, 8);
  467. if(state != NULL)
  468. {
  469. ret->SetReferenceState(*state);
  470. }
  471. Unregister_Pointer(texCoords);
  472. delete [] texCoords;
  473. Unregister_Pointer(colors);
  474. delete [] colors;
  475. Unregister_Pointer(index);
  476. delete [] index;
  477. Unregister_Pointer(lengths);
  478. delete [] lengths;
  479. Unregister_Pointer(coords);
  480. delete [] coords;
  481. gos_PopCurrentHeap();
  482. return ret;
  483. }
  484. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  485. //
  486. MLRShape*
  487. MidLevelRenderer::CreateIndexedIcosahedron_Color_NoLit(
  488. IcoInfo& icoInfo,
  489. MLRState *state
  490. )
  491. {
  492. gos_PushCurrentHeap(Heap);
  493. MLRShape *ret = new MLRShape(20);
  494. Register_Object(ret);
  495. int i, j, k;
  496. long nrTri = (long) ceil (icoInfo.all * pow (4.0f, icoInfo.depth));
  497. Point3D v[3];
  498. if(3*nrTri >= Limits::Max_Number_Vertices_Per_Mesh)
  499. {
  500. nrTri = Limits::Max_Number_Vertices_Per_Mesh/3;
  501. }
  502. unsigned char *lengths = new unsigned char [nrTri];
  503. Register_Pointer(lengths);
  504. for(i=0;i<nrTri;i++)
  505. {
  506. lengths[i] = 3;
  507. }
  508. Point3D *coords = new Point3D [nrTri*3];
  509. Register_Pointer(coords);
  510. Point3D *collapsedCoords = NULL;
  511. if(icoInfo.indexed==true)
  512. {
  513. collapsedCoords = new Point3D [nrTri*3];
  514. Register_Pointer(collapsedCoords);
  515. }
  516. unsigned short *index = new unsigned short [nrTri*3];
  517. Register_Pointer(index);
  518. Vector2DScalar *texCoords = new Vector2DScalar[nrTri*3];
  519. Register_Pointer(texCoords);
  520. RGBAColor *colors = new RGBAColor[nrTri*3];
  521. Register_Pointer(colors);
  522. int uniquePoints = 0;
  523. for (k=0;k<20;k++)
  524. {
  525. MidLevelRenderer::triDrawn = 0;
  526. MLR_I_C_PMesh *mesh = new MLR_I_C_PMesh();
  527. Register_Object(mesh);
  528. // setup vertex position information
  529. for (j=0;j<3;j++)
  530. {
  531. v[j].x = vdata[tindices[k][j]][0];
  532. v[j].y = vdata[tindices[k][j]][1];
  533. v[j].z = vdata[tindices[k][j]][2];
  534. }
  535. subdivide (coords, v[0], v[1], v[2], icoInfo.depth, nrTri, icoInfo.radius);
  536. mesh->SetSubprimitiveLengths(lengths, nrTri);
  537. if(icoInfo.indexed==true)
  538. {
  539. uniquePoints = 1;
  540. collapsedCoords[0] = coords[0];
  541. index[0] = 0;
  542. for(i=1;i<nrTri*3;i++)
  543. {
  544. for(j=0;j<uniquePoints;j++)
  545. {
  546. if(coords[i] == collapsedCoords[j])
  547. {
  548. break;
  549. }
  550. }
  551. if(j==uniquePoints)
  552. {
  553. collapsedCoords[uniquePoints++] = coords[i];
  554. }
  555. index[i] = static_cast<unsigned short>(j);
  556. }
  557. mesh->SetCoordData(collapsedCoords, uniquePoints);
  558. }
  559. else
  560. {
  561. uniquePoints = nrTri*3;
  562. for(i=0;i<nrTri*3;i++)
  563. {
  564. index[i] = static_cast<unsigned short>(i);
  565. }
  566. mesh->SetCoordData(coords, nrTri*3);
  567. }
  568. mesh->SetIndexData(index, nrTri*3);
  569. mesh->FindFacePlanes();
  570. if(state == NULL)
  571. {
  572. for(i=0;i<uniquePoints;i++)
  573. {
  574. texCoords[i] = Vector2DScalar(0.0f, 0.0f);
  575. }
  576. }
  577. else
  578. {
  579. mesh->SetReferenceState(*state);
  580. if(state->GetTextureHandle() > 0)
  581. {
  582. if(icoInfo.indexed==true)
  583. {
  584. for(i=0;i<uniquePoints;i++)
  585. {
  586. texCoords[i] =
  587. Vector2DScalar(
  588. (1.0f + collapsedCoords[i].x)/2.0f,
  589. (1.0f + collapsedCoords[i].y)/2.0f
  590. );
  591. }
  592. }
  593. else
  594. {
  595. for(i=0;i<nrTri;i++)
  596. {
  597. texCoords[3*i] =
  598. Vector2DScalar(
  599. (1.0f + coords[3*i].x)/2.0f,
  600. (1.0f + coords[3*i].y)/2.0f
  601. );
  602. texCoords[3*i+1] =
  603. Vector2DScalar(
  604. (1.0f + coords[3*i+1].x)/2.0f,
  605. (1.0f + coords[3*i+1].y)/2.0f
  606. );
  607. texCoords[3*i+2] =
  608. Vector2DScalar(
  609. (1.0f + coords[3*i+2].x)/2.0f,
  610. (1.0f + coords[3*i+2].y)/2.0f
  611. );
  612. }
  613. }
  614. }
  615. else
  616. {
  617. for(i=0;i<uniquePoints;i++)
  618. {
  619. texCoords[i] = Vector2DScalar(0.0f, 0.0f);
  620. }
  621. }
  622. }
  623. mesh->SetTexCoordData(texCoords, uniquePoints);
  624. if(icoInfo.indexed==true)
  625. {
  626. for(i=0;i<uniquePoints;i++)
  627. {
  628. colors[i] =
  629. RGBAColor(
  630. (1.0f + collapsedCoords[i].x)/2.0f,
  631. (1.0f + collapsedCoords[i].y)/2.0f,
  632. (1.0f + collapsedCoords[i].z)/2.0f,
  633. 1.0f
  634. );
  635. }
  636. }
  637. else
  638. {
  639. for(i=0;i<uniquePoints;i++)
  640. {
  641. colors[i] =
  642. RGBAColor(
  643. (1.0f + coords[i].x)/2.0f,
  644. (1.0f + coords[i].y)/2.0f,
  645. (1.0f + coords[i].z)/2.0f,
  646. 1.0f
  647. );
  648. }
  649. }
  650. mesh->SetColorData(colors, uniquePoints);
  651. ret->Add(mesh);
  652. mesh->DetachReference();
  653. }
  654. Unregister_Pointer(colors);
  655. delete [] colors;
  656. Unregister_Pointer(texCoords);
  657. delete [] texCoords;
  658. Unregister_Pointer(index);
  659. delete [] index;
  660. if(icoInfo.indexed==true)
  661. {
  662. Unregister_Pointer(collapsedCoords);
  663. delete [] collapsedCoords;
  664. }
  665. Unregister_Pointer(coords);
  666. delete [] coords;
  667. Unregister_Pointer(lengths);
  668. delete [] lengths;
  669. gos_PopCurrentHeap();
  670. return ret;
  671. }