MLRPrimitiveBase.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #pragma once
  5. #define MLR_MLRPRIMITIVEBASE_HPP
  6. #if !defined(MLR_MLR_HPP)
  7. #include <MLR\MLR.hpp>
  8. #endif
  9. #if !defined(MLR_GOSVERTEXPOOL_HPP)
  10. #include <MLR\GOSVertexPool.hpp>
  11. #endif
  12. namespace MidLevelRenderer {
  13. struct ClipPolygon2
  14. {
  15. void Init(int);
  16. void Destroy();
  17. Stuff::DynamicArrayOf<Stuff::Vector4D> coords; // [Max_Number_Vertices_Per_Polygon]
  18. #if COLOR_AS_DWORD
  19. Stuff::DynamicArrayOf<DWORD> colors; //[Max_Number_Vertices_Per_Polygon];
  20. #else
  21. Stuff::DynamicArrayOf<Stuff::RGBAColor> colors; //[Max_Number_Vertices_Per_Polygon];
  22. #endif
  23. Stuff::DynamicArrayOf<Vector2DScalar> texCoords; //[2*Max_Number_Vertices_Per_Polygon];
  24. Stuff::DynamicArrayOf<MLRClippingState> clipPerVertex; //[Max_Number_Vertices_Per_Polygon];
  25. };
  26. class MLRPrimitiveBase__ClassData;
  27. //##########################################################################
  28. //#################### MLRPrimitiveBase ##############################
  29. //##########################################################################
  30. // this is the abstract base class for all geometry. it has contains geometry
  31. // with one and only one renderer state !!!
  32. typedef Stuff::Vector2DOf<Stuff::Scalar> Vector2DScalar;
  33. class _declspec(novtable) MLRPrimitiveBase:
  34. public Stuff::RegisteredClass
  35. {
  36. friend class MLRShape;
  37. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. // Initialization
  39. //
  40. public:
  41. static void
  42. InitializeClass();
  43. static void
  44. TerminateClass();
  45. typedef MLRPrimitiveBase__ClassData ClassData;
  46. static ClassData
  47. *DefaultData;
  48. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  49. // Constructors/Destructors
  50. //
  51. protected:
  52. MLRPrimitiveBase(
  53. ClassData *class_data,
  54. Stuff::MemoryStream *stream,
  55. int version
  56. );
  57. ~MLRPrimitiveBase();
  58. public:
  59. MLRPrimitiveBase(ClassData *class_data);
  60. typedef MLRPrimitiveBase*
  61. (*Factory)(
  62. Stuff::MemoryStream *stream,
  63. int version
  64. );
  65. static MLRPrimitiveBase*
  66. Make(
  67. Stuff::MemoryStream *stream,
  68. int version
  69. );
  70. virtual void
  71. Save(Stuff::MemoryStream *stream);
  72. // Subprimitves are units in which this geometry is split off
  73. // ie. nr of polygons in a polygon mesh or number of tripstrips
  74. // in a tristriped mesh, every of this subprimitves has another
  75. // number which is type specific
  76. // ie. number of vertices in a polygon or number of vertices in
  77. // a tristrip
  78. // the data for the coord/color/texcoord/normal or index
  79. // ARE IN THIS ORDER
  80. virtual int
  81. GetNumPrimitives()
  82. { Check_Object(this); return lengths.GetLength(); }
  83. virtual void
  84. SetSubprimitiveLengths(
  85. unsigned char *length_array,
  86. int subprimitive_count
  87. ) = 0;
  88. // returns the number of subprimitives
  89. void
  90. GetSubprimitiveLengths(unsigned char **length_array, int*);
  91. int
  92. GetSubprimitiveLength(int i) const;
  93. // ==============================================================
  94. virtual void SetReferenceState(const MLRState& _state, int=0)
  95. { Check_Object(this); referenceState = _state; };
  96. virtual const MLRState&
  97. GetReferenceState(int=0) const
  98. { Check_Object(this); return referenceState; };
  99. virtual const MLRState&
  100. GetCurrentState(int=0) const
  101. { Check_Object(this); return state; };
  102. virtual void
  103. CombineStates (const MLRState& master)
  104. { Check_Object(this); state.Combine(master, referenceState); };
  105. int
  106. GetNumVertices()
  107. { Check_Object(this); return coords.GetLength(); }
  108. virtual void
  109. SetCoordData(
  110. const Stuff::Point3D *array,
  111. int point_count
  112. );
  113. virtual void
  114. GetCoordData(
  115. Stuff::Point3D **array,
  116. int *point_count
  117. );
  118. virtual void
  119. SetTexCoordData(
  120. const Vector2DScalar *array,
  121. int point_count
  122. );
  123. virtual void
  124. GetTexCoordData(
  125. Vector2DScalar **array,
  126. int *point_count
  127. );
  128. // is to call befor clipping, parameter: camera point
  129. virtual int FindBackFace(const Stuff::Point3D&) = 0;
  130. virtual void Lighting(MLRLight* const*, int nrLights) = 0;
  131. static void InitializeDraw();
  132. virtual void InitializeDrawPrimitive(unsigned char, int=0);
  133. int GetVisible ()
  134. { Check_Object(this); return visible; }
  135. virtual GOSVertex*
  136. GetGOSVertices(int=0)
  137. { Check_Object(this); return gos_vertices; }
  138. int
  139. GetNumGOSVertices()
  140. { Check_Object(this); return numGOSVertices; }
  141. virtual GOSVertex2UV*
  142. GetGOSVertices2UV(int=0)
  143. { Check_Object(this); return NULL; }
  144. int
  145. GetSortDataMode()
  146. { Check_Object(this); return drawMode; }
  147. virtual bool
  148. CastRay(
  149. Stuff::Line3D *line,
  150. Stuff::Normal3D *normal
  151. );
  152. virtual void
  153. PaintMe(const Stuff::RGBAColor *paintMe) = 0;
  154. virtual int
  155. TransformAndClip(Stuff::Matrix4D*, MLRClippingState, GOSVertexPool*,bool=false) = 0;
  156. virtual void
  157. TransformNoClip(Stuff::Matrix4D*, GOSVertexPool*,bool=false) = 0;
  158. virtual int
  159. GetNumPasses()
  160. { Check_Object(this); return passes; }
  161. virtual void
  162. HurtMe(const Stuff::LinearMatrix4D&, Stuff::Scalar radius)
  163. { Check_Object(this); }
  164. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  165. // This functions using the static buffers
  166. //
  167. void
  168. SetClipCoord(Stuff::Point3D &point, int index)
  169. {
  170. Check_Object(this); Verify(clipExtraCoords->GetLength() > index);
  171. (*clipExtraCoords)[index].x = point.x;
  172. (*clipExtraCoords)[index].y = point.y;
  173. (*clipExtraCoords)[index].z = point.z;
  174. }
  175. void
  176. FlashClipCoords(int num)
  177. {
  178. Check_Object(this); Verify(clipExtraCoords->GetLength() > num);
  179. coords.SetLength(num);
  180. for(int i=0;i<num;i++)
  181. {
  182. coords[i].x = (*clipExtraCoords)[i].x;
  183. coords[i].y = (*clipExtraCoords)[i].y;
  184. coords[i].z = (*clipExtraCoords)[i].z;
  185. }
  186. }
  187. void
  188. SetClipTexCoord(Vector2DScalar &uvs, int index)
  189. {
  190. Check_Object(this); Verify(clipExtraTexCoords->GetLength() > index);
  191. Verify( MLRState::GetHasMaxUVs() ? (uvs[0]>=-100.0 && uvs[0]<=100.0) : 1);
  192. Verify( MLRState::GetHasMaxUVs() ? (uvs[1]>=-100.0 && uvs[1]<=100.0) : 1);
  193. (*clipExtraTexCoords)[index] = uvs;
  194. }
  195. void
  196. FlashClipTexCoords(int num)
  197. {
  198. Check_Object(this); Verify(clipExtraTexCoords->GetLength() > num);
  199. texCoords.SetLength(num);
  200. Mem_Copy(texCoords.GetData(), clipExtraTexCoords->GetData(), sizeof(Vector2DScalar)*num, sizeof(Vector2DScalar)*num);
  201. }
  202. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  203. // Reference counting
  204. //
  205. public:
  206. void
  207. AttachReference()
  208. {Check_Object(this); ++referenceCount;}
  209. void
  210. DetachReference()
  211. {
  212. Check_Object(this); Verify(referenceCount > 0);
  213. if ((--referenceCount) == 0)
  214. {
  215. Unregister_Object(this);
  216. delete this;
  217. }
  218. }
  219. int
  220. GetReferenceCount()
  221. {return referenceCount;}
  222. protected:
  223. int
  224. referenceCount;
  225. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  226. // Testing
  227. //
  228. public:
  229. void
  230. TestInstance() const;
  231. virtual int
  232. GetSize()
  233. {
  234. Check_Object(this);
  235. int ret = 0;
  236. ret += coords.GetSize();
  237. ret += texCoords.GetSize();
  238. ret += lengths.GetSize();
  239. return ret;
  240. }
  241. void
  242. GetExtend(Stuff::ExtentBox *box);
  243. protected:
  244. virtual void
  245. Transform(Stuff::Matrix4D*);
  246. static ClipPolygon2 *clipBuffer;
  247. unsigned char visible; // primitive visibilty per frame
  248. unsigned char passes;
  249. // int numPrimitives; // Number of primitives, e.g. - num quads
  250. // Replaced by GetNumPrimitives
  251. // int numVertices; // number of verts for stats and vert arrays
  252. // Replaced by GetNumVertices
  253. Stuff::DynamicArrayOf<Stuff::Point3D> coords; // Base address of coordinate list
  254. Stuff::DynamicArrayOf<Vector2DScalar> texCoords; // Base address of texture coordinate list
  255. static Stuff::DynamicArrayOf<Stuff::Vector4D> *transformedCoords;
  256. Stuff::DynamicArrayOf<unsigned char> lengths; // List of strip lengths
  257. #if COLOR_AS_DWORD // clipExtraColors for the future generations !!!
  258. static Stuff::DynamicArrayOf<DWORD> *clipExtraColors; // , Max_Number_Vertices_Per_Mesh
  259. #else
  260. static Stuff::DynamicArrayOf<Stuff::RGBAColor> *clipExtraColors; // , Max_Number_Vertices_Per_Mesh
  261. #endif
  262. static Stuff::DynamicArrayOf<MLRClippingState> *clipPerVertex; // , Max_Number_Vertices_Per_Mesh
  263. static Stuff::DynamicArrayOf<Stuff::Vector4D> *clipExtraCoords; // , Max_Number_Vertices_Per_Mesh
  264. static Stuff::DynamicArrayOf<Vector2DScalar> *clipExtraTexCoords; // , Max_Number_Vertices_Per_Mesh
  265. static Stuff::DynamicArrayOf<unsigned short> *clipExtraLength; // , Max_Number_Primitives_Per_Frame
  266. MLRState state, referenceState;
  267. int drawMode;
  268. GOSVertex *gos_vertices;
  269. unsigned short numGOSVertices;
  270. };
  271. struct IcoInfo {
  272. int type;
  273. int depth;
  274. bool indexed;
  275. Stuff::Scalar radius;
  276. Stuff::Scalar all;
  277. bool onOff;
  278. const char *GetTypeName();
  279. };
  280. MLRShape*
  281. CreateIndexedIcosahedron(
  282. IcoInfo&,
  283. Stuff::DynamicArrayOf<MLRState>*
  284. );
  285. //##########################################################################
  286. //################### MLRPrimitiveBase__ClassData ####################
  287. //##########################################################################
  288. class MLRPrimitiveBase__ClassData:
  289. public Stuff::RegisteredClass::ClassData
  290. {
  291. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  292. //
  293. public:
  294. MLRPrimitiveBase__ClassData(
  295. Stuff::RegisteredClass::ClassID class_id,
  296. const char* class_name,
  297. Stuff::RegisteredClass::ClassData *parent_class,
  298. MLRPrimitiveBase::Factory primitive_factory
  299. ):
  300. RegisteredClass__ClassData(class_id, class_name, parent_class),
  301. primitiveFactory(primitive_factory)
  302. {}
  303. MLRPrimitiveBase::Factory
  304. primitiveFactory;
  305. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  306. //
  307. public:
  308. void
  309. TestInstance();
  310. };
  311. struct ClipData2
  312. {
  313. Stuff::Vector4D *coords;
  314. #if COLOR_AS_DWORD
  315. DWORD *colors;
  316. #else
  317. Stuff::RGBAColor *colors;
  318. #endif
  319. Vector2DScalar *texCoords;
  320. MLRClippingState *clipPerVertex;
  321. unsigned short length;
  322. };
  323. #if 0 // still defined in "MLRPrimitive.hpp"
  324. inline Stuff::Scalar
  325. GetBC(int nr, const Stuff::Vector4D& v4d)
  326. {
  327. switch(nr)
  328. {
  329. case 0:
  330. return (v4d.w - v4d.y);
  331. case 1:
  332. return v4d.y;
  333. case 2:
  334. return (v4d.w - v4d.x);
  335. case 3:
  336. return v4d.x;
  337. case 4:
  338. return v4d.z;
  339. case 5:
  340. return (v4d.w - v4d.z);
  341. }
  342. return 0.0f;
  343. }
  344. inline void
  345. GetDoubleBC
  346. (
  347. int nr,
  348. Stuff::Scalar& result1,
  349. Stuff::Scalar& result2,
  350. const Stuff::Vector4D& v4d1,
  351. const Stuff::Vector4D& v4d2
  352. )
  353. {
  354. switch(nr)
  355. {
  356. case 0:
  357. result1 = (v4d1.w - v4d1.y);
  358. result2 = (v4d2.w - v4d2.y);
  359. break;
  360. case 1:
  361. result1 = v4d1.y;
  362. result2 = v4d2.y;
  363. break;
  364. case 2:
  365. result1 = (v4d1.w - v4d1.x);
  366. result2 = (v4d2.w - v4d2.x);
  367. break;
  368. case 3:
  369. result1 = v4d1.x;
  370. result2 = v4d2.x;
  371. break;
  372. case 4:
  373. result1 = v4d1.z;
  374. result2 = v4d2.z;
  375. break;
  376. case 5:
  377. result1 = (v4d1.w - v4d1.z);
  378. result2 = (v4d2.w - v4d2.z);
  379. break;
  380. }
  381. }
  382. inline Stuff::Scalar
  383. GetLerpFactor
  384. (
  385. int nr,
  386. const Stuff::Vector4D& v4d1,
  387. const Stuff::Vector4D& v4d2
  388. )
  389. {
  390. Stuff::Scalar result1, result2;
  391. switch(nr)
  392. {
  393. case 0:
  394. result1 = (v4d1.w - v4d1.y);
  395. result2 = (v4d2.w - v4d2.y);
  396. break;
  397. case 1:
  398. result1 = v4d1.y;
  399. result2 = v4d2.y;
  400. break;
  401. case 2:
  402. result1 = (v4d1.w - v4d1.x);
  403. result2 = (v4d2.w - v4d2.x);
  404. break;
  405. case 3:
  406. result1 = v4d1.x;
  407. result2 = v4d2.x;
  408. break;
  409. case 4:
  410. result1 = v4d1.z;
  411. result2 = v4d2.z;
  412. break;
  413. case 5:
  414. result1 = (v4d1.w - v4d1.z);
  415. result2 = (v4d2.w - v4d2.z);
  416. break;
  417. default:
  418. result1 = 0.0f;
  419. result2 = 0.0f;
  420. Abort_Program("Invalid plane number used !");
  421. break;
  422. }
  423. Verify(!Stuff::Close_Enough(result1, result2));
  424. return result1 / (result1 - result2);
  425. }
  426. #endif
  427. inline Stuff::Scalar
  428. GetBC(int nr, const Stuff::Vector4D& v4d)
  429. {
  430. switch(nr)
  431. {
  432. case 0:
  433. return (v4d.w - v4d.y);
  434. case 1:
  435. return v4d.y;
  436. case 2:
  437. return (v4d.w - v4d.x);
  438. case 3:
  439. return v4d.x;
  440. case 4:
  441. return v4d.z;
  442. case 5:
  443. return (v4d.w - v4d.z);
  444. }
  445. return 0.0f;
  446. }
  447. inline void
  448. GetDoubleBC
  449. (
  450. int nr,
  451. Stuff::Scalar& result1,
  452. Stuff::Scalar& result2,
  453. const Stuff::Vector4D& v4d1,
  454. const Stuff::Vector4D& v4d2
  455. )
  456. {
  457. switch(nr)
  458. {
  459. case 0:
  460. result1 = (v4d1.w - v4d1.y);
  461. result2 = (v4d2.w - v4d2.y);
  462. break;
  463. case 1:
  464. result1 = v4d1.y;
  465. result2 = v4d2.y;
  466. break;
  467. case 2:
  468. result1 = (v4d1.w - v4d1.x);
  469. result2 = (v4d2.w - v4d2.x);
  470. break;
  471. case 3:
  472. result1 = v4d1.x;
  473. result2 = v4d2.x;
  474. break;
  475. case 4:
  476. result1 = v4d1.z;
  477. result2 = v4d2.z;
  478. break;
  479. case 5:
  480. result1 = (v4d1.w - v4d1.z);
  481. result2 = (v4d2.w - v4d2.z);
  482. break;
  483. }
  484. }
  485. inline Stuff::Scalar
  486. GetLerpFactor
  487. (
  488. int nr,
  489. const Stuff::Vector4D& v4d1,
  490. const Stuff::Vector4D& v4d2
  491. )
  492. {
  493. Stuff::Scalar result1, result2;
  494. switch(nr)
  495. {
  496. case 0:
  497. result1 = (v4d1.w - v4d1.y);
  498. result2 = (v4d2.w - v4d2.y);
  499. break;
  500. case 1:
  501. result1 = v4d1.y;
  502. result2 = v4d2.y;
  503. break;
  504. case 2:
  505. result1 = (v4d1.w - v4d1.x);
  506. result2 = (v4d2.w - v4d2.x);
  507. break;
  508. case 3:
  509. result1 = v4d1.x;
  510. result2 = v4d2.x;
  511. break;
  512. case 4:
  513. result1 = v4d1.z;
  514. result2 = v4d2.z;
  515. break;
  516. case 5:
  517. result1 = (v4d1.w - v4d1.z);
  518. result2 = (v4d2.w - v4d2.z);
  519. break;
  520. default:
  521. result1 = 0.0f;
  522. result2 = 0.0f;
  523. STOP(("Invalid plane number used !"));
  524. break;
  525. }
  526. if(result1 == 0.0f)
  527. {
  528. return 0.0f;
  529. }
  530. Verify(!Stuff::Close_Enough(result1, result2, Stuff::SMALL*0.1f));
  531. return result1 / (result1 - result2);
  532. }
  533. }