MLRPrimitiveBase.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "MLRHeaders.hpp"
  5. int clipTrick[6][2] =
  6. {
  7. { 1, 1},
  8. { 1, 0},
  9. { 0, 1},
  10. { 0, 0},
  11. { 2, 0},
  12. { 2, 1}
  13. };
  14. //#############################################################################
  15. //######################### ClipPolygon2 ############################
  16. //#############################################################################
  17. void ClipPolygon2::Init(int passes)
  18. {
  19. Verify(gos_GetCurrentHeap() == StaticHeap);
  20. coords.SetLength(Limits::Max_Number_Vertices_Per_Polygon);
  21. colors.SetLength(Limits::Max_Number_Vertices_Per_Polygon);
  22. texCoords.SetLength(passes*Limits::Max_Number_Vertices_Per_Polygon);
  23. clipPerVertex.SetLength(Limits::Max_Number_Vertices_Per_Polygon);
  24. }
  25. void ClipPolygon2::Destroy()
  26. {
  27. coords.SetLength(0);
  28. colors.SetLength(0);
  29. texCoords.SetLength(0);
  30. clipPerVertex.SetLength(0);
  31. }
  32. //#############################################################################
  33. //######################### MLRPrimitiveBase ############################
  34. //#############################################################################
  35. MLRPrimitiveBase::ClassData*
  36. MLRPrimitiveBase::DefaultData = NULL;
  37. DynamicArrayOf<Vector4D>
  38. *MLRPrimitiveBase::transformedCoords;
  39. DynamicArrayOf<MLRClippingState>
  40. *MLRPrimitiveBase::clipPerVertex;
  41. DynamicArrayOf<Vector4D>
  42. *MLRPrimitiveBase::clipExtraCoords;
  43. DynamicArrayOf<Vector2DScalar>
  44. *MLRPrimitiveBase::clipExtraTexCoords;
  45. #if COLOR_AS_DWORD
  46. DynamicArrayOf<DWORD>
  47. #else
  48. DynamicArrayOf<RGBAColor>
  49. #endif
  50. *MLR_I_C_PMesh::clipExtraColors;
  51. DynamicArrayOf<unsigned short>
  52. *MLRPrimitiveBase::clipExtraLength;
  53. ClipPolygon2
  54. *MLRPrimitiveBase::clipBuffer;
  55. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. //
  57. void
  58. MLRPrimitiveBase::InitializeClass()
  59. {
  60. Verify(!DefaultData);
  61. Verify(gos_GetCurrentHeap() == StaticHeap);
  62. DefaultData =
  63. new ClassData(
  64. MLRPrimitiveBaseClassID,
  65. "MidLevelRenderer::MLRPrimitiveBase",
  66. RegisteredClass::DefaultData,
  67. NULL
  68. );
  69. Register_Object(DefaultData);
  70. transformedCoords = new DynamicArrayOf<Vector4D> (Limits::Max_Number_Vertices_Per_Mesh);
  71. Register_Object(transformedCoords);
  72. clipPerVertex = new DynamicArrayOf<MLRClippingState> (Limits::Max_Number_Vertices_Per_Mesh);
  73. Register_Object(clipPerVertex);
  74. clipExtraCoords = new DynamicArrayOf<Vector4D> (Limits::Max_Number_Vertices_Per_Mesh);
  75. Register_Object(clipExtraCoords);
  76. clipExtraTexCoords = new DynamicArrayOf<Vector2DScalar> (Limits::Max_Number_Vertices_Per_Mesh);
  77. Register_Object(clipExtraTexCoords);
  78. clipExtraColors = new DynamicArrayOf<
  79. #if COLOR_AS_DWORD
  80. DWORD
  81. #else
  82. RGBAColor
  83. #endif
  84. > (Limits::Max_Number_Primitives_Per_Frame);
  85. Register_Object(clipExtraColors);
  86. clipExtraLength = new DynamicArrayOf<unsigned short> (Limits::Max_Number_Primitives_Per_Frame);
  87. Register_Object(clipExtraLength);
  88. clipBuffer = new ClipPolygon2 [2];
  89. Register_Pointer(clipBuffer);
  90. clipBuffer[0].Init(Limits::Max_Number_Of_Multitextures);
  91. clipBuffer[1].Init(Limits::Max_Number_Of_Multitextures);
  92. }
  93. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  94. //
  95. void
  96. MLRPrimitiveBase::TerminateClass()
  97. {
  98. clipBuffer[1].Destroy();
  99. clipBuffer[0].Destroy();
  100. Unregister_Pointer(clipBuffer);
  101. delete [] clipBuffer;
  102. Unregister_Object(transformedCoords);
  103. delete transformedCoords;
  104. Unregister_Object(clipPerVertex);
  105. delete clipPerVertex;
  106. Unregister_Object(clipExtraCoords);
  107. delete clipExtraCoords;
  108. Unregister_Object(clipExtraTexCoords);
  109. delete clipExtraTexCoords;
  110. Unregister_Object(clipExtraColors);
  111. delete clipExtraColors;
  112. Unregister_Object(clipExtraLength);
  113. delete clipExtraLength;
  114. Unregister_Object(DefaultData);
  115. delete DefaultData;
  116. DefaultData = NULL;
  117. }
  118. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  119. //
  120. MLRPrimitiveBase::MLRPrimitiveBase(
  121. ClassData *class_data,
  122. MemoryStream *stream,
  123. int version
  124. ):
  125. RegisteredClass(class_data)
  126. {
  127. Check_Pointer(this);
  128. Check_Object(stream);
  129. Verify(gos_GetCurrentHeap() == Heap);
  130. switch(version)
  131. {
  132. case 1:
  133. case 2:
  134. {
  135. STOP(("This class got created only after version 2 !"));
  136. }
  137. break;
  138. default:
  139. {
  140. MemoryStreamIO_Read(stream, &coords);
  141. MemoryStreamIO_Read(stream, &texCoords);
  142. MemoryStreamIO_Read(stream, &lengths);
  143. *stream >> drawMode;
  144. referenceState.Load(stream, version);
  145. }
  146. break;
  147. }
  148. passes = 1;
  149. referenceCount = 1;
  150. }
  151. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  152. //
  153. void
  154. MLRPrimitiveBase::Save(MemoryStream *stream)
  155. {
  156. Check_Object(this);
  157. Check_Object(stream);
  158. *stream << GetClassID();
  159. MemoryStreamIO_Write(stream, &coords);
  160. MemoryStreamIO_Write(stream, &texCoords);
  161. MemoryStreamIO_Write(stream, &lengths);
  162. *stream << static_cast<int>(drawMode);
  163. referenceState.Save(stream);
  164. }
  165. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166. //
  167. MLRPrimitiveBase::MLRPrimitiveBase(ClassData *class_data):
  168. RegisteredClass(class_data),
  169. lengths(0), texCoords(0), coords(0)
  170. {
  171. Verify(gos_GetCurrentHeap() == Heap);
  172. referenceState = 0;
  173. state = 0;
  174. passes = 1;
  175. referenceCount = 1;
  176. }
  177. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  178. //
  179. MLRPrimitiveBase::~MLRPrimitiveBase()
  180. {
  181. Verify(referenceCount==0);
  182. }
  183. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  184. //
  185. void
  186. MLRPrimitiveBase::TestInstance() const
  187. {
  188. Verify(IsDerivedFrom(DefaultData));
  189. }
  190. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  191. //
  192. void
  193. MLRPrimitiveBase::InitializeDraw()
  194. {
  195. }
  196. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  197. //
  198. void
  199. MLRPrimitiveBase::InitializeDrawPrimitive(unsigned char vis, int)
  200. {
  201. gos_vertices = NULL;
  202. numGOSVertices = -1;
  203. visible = vis;
  204. }
  205. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  206. //
  207. void
  208. MLRPrimitiveBase::SetSubprimitiveLengths(unsigned char *data, int l)
  209. {
  210. Check_Object(this);
  211. lengths.AssignData(data, l);
  212. }
  213. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  214. //
  215. void
  216. MLRPrimitiveBase::GetSubprimitiveLengths(unsigned char **data, int *len)
  217. {
  218. Check_Object(this);
  219. *data = lengths.GetData();
  220. *len = lengths.GetLength();
  221. }
  222. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  223. //
  224. int
  225. MLRPrimitiveBase::GetSubprimitiveLength (int i) const
  226. {
  227. Check_Object(this);
  228. return (lengths.GetLength() > 0 ? abs(lengths[i]) : 1);
  229. }
  230. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  231. //
  232. void
  233. MLRPrimitiveBase::SetCoordData(
  234. const Point3D *data,
  235. int dataSize
  236. )
  237. {
  238. Check_Object(this);
  239. Check_Pointer(data);
  240. Verify(texCoords.GetLength() == 0 || dataSize == texCoords.GetLength());
  241. #if defined (MAX_NUMBER_VERTICES)
  242. Verify(dataSize <= MAX_NUMBER_VERTICES);
  243. #endif
  244. coords.AssignData(data, dataSize);
  245. }
  246. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  247. //
  248. void
  249. MLRPrimitiveBase::GetCoordData(
  250. Point3D **data,
  251. int *dataSize
  252. )
  253. {
  254. Check_Object(this);
  255. *data = coords.GetData();
  256. *dataSize = coords.GetLength();
  257. }
  258. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  259. //
  260. void
  261. MLRPrimitiveBase::SetTexCoordData(
  262. const Vector2DScalar *data,
  263. int dataSize
  264. )
  265. {
  266. Check_Object(this);
  267. Check_Pointer(data);
  268. Verify(coords.GetLength() == 0 || dataSize == coords.GetLength());
  269. texCoords.AssignData((Vector2DScalar *)data, dataSize);
  270. }
  271. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  272. //
  273. void
  274. MLRPrimitiveBase::GetTexCoordData(
  275. Vector2DScalar **data,
  276. int *dataSize
  277. )
  278. {
  279. Check_Object(this);
  280. *data = texCoords.GetData();
  281. *dataSize = texCoords.GetLength();
  282. }
  283. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  284. //
  285. void
  286. MLRPrimitiveBase::Transform(Matrix4D *mat)
  287. {
  288. Check_Object(this);
  289. int i, len = coords.GetLength();
  290. for(i=0;i<len;i++)
  291. {
  292. (*transformedCoords)[i].Multiply(coords[i], *mat);
  293. }
  294. #ifdef LAB_ONLY
  295. Set_Statistic(TransformedVertices, TransformedVertices+len);
  296. #endif
  297. }
  298. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  299. //
  300. void
  301. MLRPrimitiveBase::GetExtend(Stuff::ExtentBox *box)
  302. {
  303. Check_Object(this);
  304. Check_Object(box);
  305. if(coords.GetLength()==0)
  306. {
  307. return;
  308. }
  309. box->minX = box->maxX = coords[0].x;
  310. box->minY = box->maxY = coords[0].y;
  311. box->minZ = box->maxZ = coords[0].z;
  312. for(int i=0;i<coords.GetLength();i++)
  313. {
  314. if(coords[i].x < box->minX)
  315. {
  316. box->minX = coords[i].x;
  317. }
  318. if(coords[i].y < box->minY)
  319. {
  320. box->minY = coords[i].y;
  321. }
  322. if(coords[i].z < box->minZ)
  323. {
  324. box->minZ = coords[i].z;
  325. }
  326. if(coords[i].x > box->maxX)
  327. {
  328. box->maxX = coords[i].x;
  329. }
  330. if(coords[i].y > box->maxY)
  331. {
  332. box->maxY = coords[i].y;
  333. }
  334. if(coords[i].z > box->maxZ)
  335. {
  336. box->maxZ = coords[i].z;
  337. }
  338. }
  339. }
  340. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  341. //
  342. bool
  343. MLRPrimitiveBase::CastRay(
  344. Line3D *line,
  345. Normal3D *normal
  346. )
  347. {
  348. Check_Object(this);
  349. Check_Object(line);
  350. Check_Pointer(normal);
  351. STOP(("Not implemented"));
  352. return false;
  353. }
  354. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  355. //
  356. MLRShape*
  357. MidLevelRenderer::CreateIndexedIcosahedron(IcoInfo& icoInfo, Stuff::DynamicArrayOf<MLRState> *states)
  358. {
  359. switch(icoInfo.type)
  360. {
  361. case MLR_I_PMeshClassID:
  362. return CreateIndexedIcosahedron_NoColor_NoLit(icoInfo, &(*states)[0]);
  363. break;
  364. case MLR_I_C_PMeshClassID:
  365. return CreateIndexedIcosahedron_Color_NoLit(icoInfo, &(*states)[0]);
  366. break;
  367. case MLR_I_L_PMeshClassID:
  368. return CreateIndexedIcosahedron_Color_Lit(icoInfo, &(*states)[0]);
  369. break;
  370. case MLR_I_DT_PMeshClassID:
  371. return CreateIndexedIcosahedron_NoColor_NoLit_2Tex(icoInfo, &(*states)[0], &(*states)[1]);
  372. break;
  373. case MLR_I_C_DT_PMeshClassID:
  374. return CreateIndexedIcosahedron_Color_NoLit_2Tex(icoInfo, &(*states)[0], &(*states)[1]);
  375. break;
  376. case MLR_I_L_DT_PMeshClassID:
  377. return CreateIndexedIcosahedron_Color_Lit_2Tex(icoInfo, &(*states)[0], &(*states)[1]);
  378. break;
  379. case MLR_I_MT_PMeshClassID:
  380. return CreateIndexedIcosahedron_NoColor_NoLit_MultiTexture(icoInfo, states);
  381. break;
  382. case MLR_I_DeT_PMeshClassID:
  383. return CreateIndexedIcosahedron_NoColor_NoLit_DetTex(icoInfo, &(*states)[0], &(*states)[1]);
  384. break;
  385. case MLR_I_C_DeT_PMeshClassID:
  386. return CreateIndexedIcosahedron_Color_NoLit_DetTex(icoInfo, &(*states)[0], &(*states)[1]);
  387. break;
  388. case MLR_I_L_DeT_PMeshClassID:
  389. return CreateIndexedIcosahedron_Color_Lit_DetTex(icoInfo, &(*states)[0], &(*states)[1]);
  390. break;
  391. case MLR_I_TMeshClassID:
  392. return CreateIndexedTriIcosahedron_NoColor_NoLit(icoInfo, &(*states)[0]);
  393. break;
  394. case MLR_I_C_TMeshClassID:
  395. return CreateIndexedTriIcosahedron_Color_NoLit(icoInfo, &(*states)[0]);
  396. break;
  397. case MLR_I_L_TMeshClassID:
  398. return CreateIndexedTriIcosahedron_Color_Lit(icoInfo, &(*states)[0]);
  399. break;
  400. case MLR_I_DeT_TMeshClassID:
  401. return CreateIndexedTriIcosahedron_NoColor_NoLit_DetTex(icoInfo, &(*states)[0], &(*states)[1]);
  402. break;
  403. case MLR_I_C_DeT_TMeshClassID:
  404. return CreateIndexedTriIcosahedron_Color_NoLit_DetTex(icoInfo, &(*states)[0], &(*states)[1]);
  405. break;
  406. case MLR_I_L_DeT_TMeshClassID:
  407. return CreateIndexedTriIcosahedron_Color_Lit_DetTex(icoInfo, &(*states)[0], &(*states)[1]);
  408. break;
  409. case MLR_I_DT_TMeshClassID:
  410. return CreateIndexedTriIcosahedron_NoColor_NoLit_2Tex(icoInfo, &(*states)[0], &(*states)[1]);
  411. break;
  412. case MLR_I_C_DT_TMeshClassID:
  413. return CreateIndexedTriIcosahedron_Color_NoLit_2Tex(icoInfo, &(*states)[0], &(*states)[1]);
  414. break;
  415. case MLR_I_L_DT_TMeshClassID:
  416. return CreateIndexedTriIcosahedron_Color_Lit_2Tex(icoInfo, &(*states)[0], &(*states)[1]);
  417. break;
  418. case MLR_TerrainClassID:
  419. return CreateIndexedTriIcosahedron_TerrainTest(icoInfo, &(*states)[0], &(*states)[1]);
  420. break;
  421. }
  422. return NULL;
  423. }
  424. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  425. //
  426. const char *
  427. MidLevelRenderer::IcoInfo::GetTypeName()
  428. {
  429. switch(type)
  430. {
  431. case MLR_I_PMeshClassID:
  432. return "not colored, unlit mesh";
  433. break;
  434. case MLR_I_C_PMeshClassID:
  435. return "colored, unlit mesh";
  436. break;
  437. case MLR_I_L_PMeshClassID:
  438. return "colored, lit mesh";
  439. break;
  440. case MLR_I_DT_PMeshClassID:
  441. return "not colored, unlit mesh w/ 2 Tex";
  442. break;
  443. case MLR_I_C_DT_PMeshClassID:
  444. return "colored, unlit mesh w/ 2 Tex";
  445. break;
  446. case MLR_I_L_DT_PMeshClassID:
  447. return "colored, lit mesh w/ 2 Tex";
  448. break;
  449. case MLR_I_MT_PMeshClassID:
  450. return "not colored, unlit mesh w/ 4 Tex";
  451. break;
  452. case MLR_I_DeT_PMeshClassID:
  453. return "not colored, unlit mesh w/ DetTex";
  454. break;
  455. case MLR_I_C_DeT_PMeshClassID:
  456. return "colored, unlit mesh w/ DetTex";
  457. break;
  458. case MLR_I_L_DeT_PMeshClassID:
  459. return "colored, lit mesh w/ DetTex";
  460. break;
  461. case MLR_I_TMeshClassID:
  462. return "not colored, unlit tri. mesh";
  463. break;
  464. case MLR_I_C_TMeshClassID:
  465. return "colored, unlit tri. mesh";
  466. break;
  467. case MLR_I_L_TMeshClassID:
  468. return "colored, lit tri. mesh";
  469. break;
  470. case MLR_I_DeT_TMeshClassID:
  471. return "not colored, unlit tri. mesh w/ DetTex";
  472. break;
  473. case MLR_I_C_DeT_TMeshClassID:
  474. return "colored, unlit tri. mesh w/ DetTex";
  475. break;
  476. case MLR_I_L_DeT_TMeshClassID:
  477. return "colored, lit tri. mesh w/ DetTex";
  478. break;
  479. case MLR_I_DT_TMeshClassID:
  480. return "not colored, unlit tri. mesh w/ 2 Tex";
  481. break;
  482. case MLR_I_C_DT_TMeshClassID:
  483. return "colored, unlit tri. mesh w/ 2 Tex";
  484. break;
  485. case MLR_I_L_DT_TMeshClassID:
  486. return "colored, lit tri. mesh w/ 2 Tex";
  487. break;
  488. case MLR_TerrainClassID:
  489. return "not colored, unlit terrain w/ DetTex";
  490. break;
  491. }
  492. return "mesh";
  493. }
  494. //#############################################################################
  495. //##################### MLRPrimitiveBase__ClassData #####################
  496. //#############################################################################
  497. void
  498. MLRPrimitiveBase__ClassData::TestInstance()
  499. {
  500. Verify(IsDerivedFrom(MLRPrimitiveBase::DefaultData));
  501. }