MLR_I_L_DT_PMesh.cpp 12 KB

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