MLR_I_L_TMesh.cpp 11 KB

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