MLR_I_MT_PMesh.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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_MT_PMesh_Clip;
  7. #endif
  8. //#############################################################################
  9. //###### MLRIndexedPolyMesh with no color no lighting two texture layer ######
  10. //#############################################################################
  11. MLR_I_MT_PMesh::ClassData*
  12. MLR_I_MT_PMesh::DefaultData = NULL;
  13. DynamicArrayOf<DynamicArrayOf<Vector2DScalar> >
  14. *MLR_I_MT_PMesh::clipExtraMultiTexCoords;
  15. DynamicArrayOf<DynamicArrayOf<Vector2DScalar> >
  16. *MLR_I_MT_PMesh::extraMultiTexCoords;
  17. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. //
  19. void
  20. MLR_I_MT_PMesh::InitializeClass()
  21. {
  22. Verify(!DefaultData);
  23. Verify(gos_GetCurrentHeap() == StaticHeap);
  24. DefaultData =
  25. new ClassData(
  26. MLR_I_MT_PMeshClassID,
  27. "MidLevelRenderer::MLR_I_MT_PMesh",
  28. MLR_I_PMesh::DefaultData,
  29. (MLRPrimitiveBase::Factory)&Make
  30. );
  31. Register_Object(DefaultData);
  32. clipExtraMultiTexCoords = new DynamicArrayOf<DynamicArrayOf<Vector2DScalar> >;
  33. Register_Object(clipExtraMultiTexCoords);
  34. clipExtraMultiTexCoords->SetLength(Limits::Max_Number_Of_Multitextures);
  35. extraMultiTexCoords = new DynamicArrayOf<DynamicArrayOf<Vector2DScalar> >;
  36. Register_Object(extraMultiTexCoords);
  37. extraMultiTexCoords->SetLength(Limits::Max_Number_Of_Multitextures);
  38. for(int i=0;i<clipExtraMultiTexCoords->GetLength();i++)
  39. {
  40. (*clipExtraMultiTexCoords)[i].SetLength(Limits::Max_Number_Vertices_Per_Mesh);
  41. (*extraMultiTexCoords)[i].SetLength(Limits::Max_Number_Vertices_Per_Mesh);
  42. }
  43. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  44. MLR_I_MT_PMesh_Clip = new BitTrace("MLR_I_MT_PMesh_Clip");
  45. Register_Object(MLR_I_MT_PMesh_Clip);
  46. #endif
  47. }
  48. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  49. //
  50. void
  51. MLR_I_MT_PMesh::TerminateClass()
  52. {
  53. for(int i=0;i<clipExtraMultiTexCoords->GetLength();i++)
  54. {
  55. (*clipExtraMultiTexCoords)[i].SetLength(0);
  56. (*extraMultiTexCoords)[i].SetLength(0);
  57. }
  58. Unregister_Object(clipExtraMultiTexCoords);
  59. delete clipExtraMultiTexCoords;
  60. Unregister_Object(extraMultiTexCoords);
  61. delete extraMultiTexCoords;
  62. Unregister_Object(DefaultData);
  63. delete DefaultData;
  64. DefaultData = NULL;
  65. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  66. Unregister_Object(MLR_I_MT_PMesh_Clip);
  67. delete MLR_I_MT_PMesh_Clip;
  68. #endif
  69. }
  70. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. //
  72. MLR_I_MT_PMesh::MLR_I_MT_PMesh(
  73. ClassData *class_data,
  74. MemoryStream *stream,
  75. int version
  76. ):
  77. MLR_I_PMesh(class_data, stream, version)
  78. {
  79. Check_Pointer(this);
  80. Check_Pointer(stream);
  81. Verify(gos_GetCurrentHeap() == Heap);
  82. multiTexCoords.SetLength(Limits::Max_Number_Of_Multitextures);
  83. multiTexCoordsPointers.SetLength(Limits::Max_Number_Of_Multitextures);
  84. multiState.SetLength(Limits::Max_Number_Of_Multitextures);
  85. multiReferenceState.SetLength(Limits::Max_Number_Of_Multitextures);
  86. multiTexCoords[0] = &texCoords;
  87. multiTexCoordsPointers[0] = texCoords.GetData();
  88. multiReferenceState[0] = referenceState;
  89. *stream >> passes;
  90. for(int i=1;i<passes;i++)
  91. {
  92. multiReferenceState[i].Save(stream);
  93. multiTexCoords[i] = new DynamicArrayOf<Vector2DScalar>;
  94. Register_Object(multiTexCoords[i]);
  95. MemoryStreamIO_Read(stream, multiTexCoords[i]);
  96. multiTexCoordsPointers[i] = multiTexCoords[i]->GetData();
  97. }
  98. currentNrOfPasses = passes;
  99. }
  100. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  101. //
  102. MLR_I_MT_PMesh::MLR_I_MT_PMesh(ClassData *class_data):
  103. MLR_I_PMesh(class_data)
  104. {
  105. Check_Pointer(this);
  106. Verify(gos_GetCurrentHeap() == Heap);
  107. multiTexCoords.SetLength(Limits::Max_Number_Of_Multitextures);
  108. multiTexCoordsPointers.SetLength(Limits::Max_Number_Of_Multitextures);
  109. multiTexCoords[0] = &texCoords;
  110. multiTexCoordsPointers[0] = texCoords.GetData();
  111. multiState.SetLength(Limits::Max_Number_Of_Multitextures);
  112. multiReferenceState.SetLength(Limits::Max_Number_Of_Multitextures);
  113. passes = 1;
  114. currentNrOfPasses = passes;
  115. }
  116. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  117. //
  118. void
  119. MLR_I_MT_PMesh::Copy(MLR_I_PMesh *pMesh)
  120. {
  121. Check_Pointer(this);
  122. Verify(gos_GetCurrentHeap() == Heap);
  123. int num;
  124. Point3D *points;
  125. referenceState = pMesh->GetReferenceState();
  126. multiReferenceState[0] = referenceState;
  127. pMesh->GetCoordData(&points, &num);
  128. SetCoordData(points, num);
  129. Vector2DScalar *tex;
  130. pMesh->GetTexCoordData(&tex, &num);
  131. SetTexCoordData(tex, num);
  132. multiTexCoords[0] = &texCoords;
  133. multiTexCoordsPointers[0] = texCoords.GetData();
  134. unsigned char *length_array;
  135. pMesh->GetSubprimitiveLengths(&length_array, &num);
  136. SetSubprimitiveLengths(length_array, num);
  137. drawMode = pMesh->GetSortDataMode();
  138. unsigned short *index_array;
  139. pMesh->GetIndexData(&index_array, &num);
  140. SetIndexData(index_array, num);
  141. facePlanes.SetLength(GetNumPrimitives());
  142. testList.SetLength(GetNumPrimitives());
  143. FindFacePlanes();
  144. passes = 1;
  145. currentNrOfPasses = passes;
  146. }
  147. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  148. //
  149. MLR_I_MT_PMesh::~MLR_I_MT_PMesh()
  150. {
  151. Check_Object(this);
  152. for(int i=1;i<passes;i++)
  153. {
  154. Unregister_Object(multiTexCoords[i]);
  155. delete multiTexCoords[i];
  156. }
  157. }
  158. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  159. //
  160. MLR_I_MT_PMesh*
  161. MLR_I_MT_PMesh::Make(
  162. MemoryStream *stream,
  163. int version
  164. )
  165. {
  166. Check_Object(stream);
  167. gos_PushCurrentHeap(Heap);
  168. MLR_I_MT_PMesh *mesh = new MLR_I_MT_PMesh(DefaultData, stream, version);
  169. gos_PopCurrentHeap();
  170. return mesh;
  171. }
  172. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  173. //
  174. void
  175. MLR_I_MT_PMesh::Save(MemoryStream *stream)
  176. {
  177. Check_Object(this);
  178. Check_Object(stream);
  179. MLR_I_PMesh::Save(stream);
  180. *stream << passes;
  181. for(int i=1;i<passes;i++)
  182. {
  183. multiReferenceState[i].Save(stream);
  184. MemoryStreamIO_Write(stream, multiTexCoords[i]);
  185. }
  186. }
  187. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  188. //
  189. void
  190. MLR_I_MT_PMesh::TestInstance() const
  191. {
  192. Verify(IsDerivedFrom(DefaultData));
  193. }
  194. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  195. //
  196. void
  197. MLR_I_MT_PMesh::InitializeDrawPrimitive(unsigned char vis, int parameter)
  198. {
  199. MLR_I_PMesh::InitializeDrawPrimitive(vis, parameter);
  200. currentNrOfPasses = passes;
  201. }
  202. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  203. //
  204. void
  205. MLR_I_MT_PMesh::SetTexCoordData(
  206. const Vector2DScalar *data,
  207. int dataSize,
  208. int pass
  209. )
  210. {
  211. Check_Object(this);
  212. Check_Pointer(data);
  213. Verify(pass>=0 && pass<Limits::Max_Number_Of_Multitextures);
  214. Verify(coords.GetLength() == 0 || dataSize == coords.GetLength());
  215. if(pass == 0)
  216. {
  217. texCoords.AssignData((Vector2DScalar *)data, dataSize);
  218. multiTexCoordsPointers[0] = texCoords.GetData();
  219. }
  220. else
  221. {
  222. if(pass == passes)
  223. {
  224. Verify(dataSize == multiTexCoords[pass-1]->GetLength());
  225. gos_PushCurrentHeap(Heap);
  226. multiTexCoords[pass] = new DynamicArrayOf<Vector2DScalar>;
  227. Register_Object(multiTexCoords[pass]);
  228. multiTexCoords[pass]->AssignData((Vector2DScalar *)data, dataSize);
  229. gos_PopCurrentHeap();
  230. multiTexCoordsPointers[pass] = multiTexCoords[pass]->GetData();
  231. passes++;
  232. currentNrOfPasses++;
  233. }
  234. else
  235. {
  236. STOP(("Add texture layers only one by one !!!"));
  237. }
  238. }
  239. }
  240. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  241. //
  242. void
  243. MLR_I_MT_PMesh::GetTexCoordData(
  244. const Vector2DScalar **data,
  245. int *dataSize,
  246. int pass
  247. )
  248. {
  249. Check_Object(this);
  250. Check_Pointer(data);
  251. Verify(pass>=0 && pass<passes);
  252. *data = multiTexCoords[pass]->GetData();
  253. *dataSize = multiTexCoords[pass]->GetLength();
  254. }
  255. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  256. //
  257. void
  258. MLR_I_MT_PMesh::SetTexCoordDataPointer(const Vector2DScalar *data)
  259. {
  260. Check_Pointer(data);
  261. Verify(currentNrOfPasses>=passes);
  262. multiTexCoordsPointers[currentNrOfPasses++] = data;
  263. }
  264. #define I_SAY_YES_TO_MULTI_TEXTURES
  265. #undef I_SAY_YES_TO_DUAL_TEXTURES
  266. #undef I_SAY_YES_TO_COLOR
  267. #undef I_SAY_YES_TO_LIGHTING
  268. #define CLASSNAME MLR_I_MT_PMesh
  269. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  270. #define SET_MLR_PMESH_CLIP() MLR_I_MT_PMesh_Clip->Set()
  271. #define CLEAR_MLR_PMESH_CLIP() MLR_I_MT_PMesh_Clip->Clear()
  272. #else
  273. #define SET_MLR_PMESH_CLIP()
  274. #define CLEAR_MLR_PMESH_CLIP()
  275. #endif
  276. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  277. // This include contains follwing functions:
  278. // void MLR_I_MT_PMesh::TransformNoClip(Matrix4D*, GOSVertexPool*);
  279. // int MLR_I_MT_PMesh::Clip(MLRClippingState, GOSVertexPool*);
  280. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  281. #include <MLR\MLRPrimitiveClipping.hpp>
  282. #undef I_SAY_YES_TO_MULTI_TEXTURES
  283. #undef CLASSNAME
  284. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  285. //
  286. MLRShape*
  287. MidLevelRenderer::CreateIndexedIcosahedron_NoColor_NoLit_MultiTexture(
  288. IcoInfo& icoInfo,
  289. DynamicArrayOf<MLRState> *states
  290. )
  291. {
  292. gos_PushCurrentHeap(Heap);
  293. MLRShape *ret = new MLRShape(20);
  294. Register_Object(ret);
  295. int i, j, k;
  296. int nrOfPasses = states->GetLength();
  297. long nrTri = (long) ceil (icoInfo.all * pow (4.0f, icoInfo.depth));
  298. Point3D v[3];
  299. if(3*nrTri >= Limits::Max_Number_Vertices_Per_Mesh)
  300. {
  301. nrTri = Limits::Max_Number_Vertices_Per_Mesh/3;
  302. }
  303. unsigned char *lengths = new unsigned char [nrTri];
  304. Register_Pointer(lengths);
  305. for(i=0;i<nrTri;i++)
  306. {
  307. lengths[i] = 3;
  308. }
  309. Point3D *coords = new Point3D [nrTri*3];
  310. Register_Pointer(coords);
  311. Point3D *collapsedCoords = NULL;
  312. if(icoInfo.indexed==true)
  313. {
  314. collapsedCoords = new Point3D [nrTri*3];
  315. Register_Pointer(collapsedCoords);
  316. }
  317. DynamicArrayOf<Vector2DScalar *> texCoords(nrOfPasses);
  318. for(i=0;i<nrOfPasses;i++)
  319. {
  320. texCoords[i] = new Vector2DScalar[nrTri*3];
  321. Register_Pointer(texCoords[i]);
  322. }
  323. unsigned short *index = new unsigned short [nrTri*3];
  324. Register_Pointer(index);
  325. int uniquePoints = 0;
  326. for (k=0;k<20;k++)
  327. {
  328. triDrawn = 0;
  329. MLR_I_MT_PMesh *mesh = new MLR_I_MT_PMesh();
  330. Register_Object(mesh);
  331. // setup vertex position information
  332. for (j=0;j<3;j++)
  333. {
  334. v[j].x = vdata[tindices[k][j]][0];
  335. v[j].y = vdata[tindices[k][j]][1];
  336. v[j].z = vdata[tindices[k][j]][2];
  337. }
  338. subdivide (coords, v[0], v[1], v[2], icoInfo.depth, nrTri, icoInfo.radius);
  339. mesh->SetSubprimitiveLengths(lengths, nrTri);
  340. if(icoInfo.indexed==true)
  341. {
  342. uniquePoints = 1;
  343. collapsedCoords[0] = coords[0];
  344. index[0] = 0;
  345. for(i=1;i<nrTri*3;i++)
  346. {
  347. for(j=0;j<uniquePoints;j++)
  348. {
  349. if(coords[i] == collapsedCoords[j])
  350. {
  351. break;
  352. }
  353. }
  354. if(j==uniquePoints)
  355. {
  356. collapsedCoords[uniquePoints++] = coords[i];
  357. }
  358. index[i] = static_cast<unsigned short>(j);
  359. }
  360. mesh->SetCoordData(collapsedCoords, uniquePoints);
  361. }
  362. else
  363. {
  364. uniquePoints = nrTri*3;
  365. for(i=0;i<nrTri*3;i++)
  366. {
  367. index[i] = static_cast<unsigned short>(i);
  368. }
  369. mesh->SetCoordData(coords, nrTri*3);
  370. }
  371. mesh->SetIndexData(index, nrTri*3);
  372. mesh->FindFacePlanes();
  373. for(j=0;j<nrOfPasses;j++)
  374. {
  375. if(icoInfo.indexed==true)
  376. {
  377. for(i=0;i<uniquePoints;i++)
  378. {
  379. texCoords[j][i] =
  380. Vector2DScalar(
  381. (1.0f + collapsedCoords[i].x)/2.0f,
  382. (1.0f + collapsedCoords[i].y)/2.0f
  383. );
  384. }
  385. }
  386. else
  387. {
  388. for(i=0;i<nrTri;i++)
  389. {
  390. texCoords[j][3*i] =
  391. Vector2DScalar(
  392. (1.0f + coords[3*i].x)/2.0f,
  393. (1.0f + coords[3*i].y)/2.0f
  394. );
  395. texCoords[j][3*i+1] =
  396. Vector2DScalar(
  397. (1.0f + coords[3*i+1].x)/2.0f,
  398. (1.0f + coords[3*i+1].y)/2.0f
  399. );
  400. texCoords[j][3*i+2] =
  401. Vector2DScalar(
  402. (1.0f + coords[3*i+2].x)/2.0f,
  403. (1.0f + coords[3*i+2].y)/2.0f
  404. );
  405. }
  406. }
  407. mesh->SetTexCoordData(texCoords[j], uniquePoints, j);
  408. }
  409. for(i=0;i<nrOfPasses;i++)
  410. {
  411. mesh->SetReferenceState((*states)[i], i);
  412. }
  413. ret->Add(mesh);
  414. mesh->DetachReference();
  415. }
  416. for(i=0;i<nrOfPasses;i++)
  417. {
  418. Unregister_Pointer(texCoords[i]);
  419. delete [] texCoords[i];
  420. }
  421. Unregister_Pointer(index);
  422. delete [] index;
  423. if(icoInfo.indexed==true)
  424. {
  425. Unregister_Pointer(collapsedCoords);
  426. delete [] collapsedCoords;
  427. }
  428. Unregister_Pointer(coords);
  429. delete [] coords;
  430. Unregister_Pointer(lengths);
  431. delete [] lengths;
  432. gos_PopCurrentHeap();
  433. return ret;
  434. }