MLR_I_DT_PMesh.cpp 13 KB

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