MLR_I_L_DeT_PMesh.cpp 11 KB

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