MLR_I_L_DT_TMesh.cpp 12 KB

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