MLR_I_L_DeT_TMesh.cpp 12 KB

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