tgl.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. //-------------------------------------------------------------------------------
  2. //
  3. // Tiny Geometry Layer
  4. //
  5. // For MechCommander 2 -- Copyright (c) 1999 Microsoft
  6. //
  7. //---------------------------------------------------------------------------//
  8. // Copyright (C) Microsoft Corporation. All rights reserved. //
  9. //===========================================================================//
  10. #ifndef TGL_H
  11. #define TGL_H
  12. //-------------------------------------------------------------------------------
  13. // Include Files
  14. #ifndef HEAP_H
  15. #include "heap.h"
  16. #endif
  17. #ifndef FILE_H
  18. #include "file.h"
  19. #endif
  20. #include <stuff\stuff.hpp>
  21. #include <gameos.hpp>
  22. //-------------------------------------------------------------------------------
  23. // Structs used by layer.
  24. //
  25. typedef DWORD* DWORDPtr;
  26. //-------------------------------------------------------------------------------
  27. // TG_TypeVertex
  28. //This Structure stores information for each vertex of the shape which DOES NOT CHANGE by instance
  29. typedef struct _TG_TypeVertex
  30. {
  31. //Only changes at load time.
  32. Stuff::Point3D position; //Position of vertex relative to base position of shape.
  33. Stuff::Vector3D normal; //Vertex Normal
  34. DWORD aRGBLight; //Vertex Light and Alpha
  35. } TG_TypeVertex;
  36. typedef TG_TypeVertex* TG_TypeVertexPtr;
  37. //-------------------------------------------------------------------------------
  38. // TG_Vertex
  39. // This Structure stores information for each vertex of the shape which is instance specific
  40. // Its used to store UNCHANGING or rarely changing Light/Fog data so I don't have to calc every frame!
  41. typedef struct _TG_Vertex
  42. {
  43. BYTE fog;
  44. BYTE redSpec;
  45. BYTE greenSpec;
  46. BYTE blueSpec;
  47. } TG_Vertex;
  48. typedef TG_Vertex* TG_VertexPtr;
  49. //-------------------------------------------------------------------------------
  50. // TG_ShadowVertex
  51. //This Structure stores information for each vertex of the shadow for this shape.
  52. // Nothing changes frame to frame here anymore unless light source is moving which is now RARE!
  53. typedef struct _TG_ShadowVertex
  54. {
  55. //Changes every frame if local light. Rarely for the sun. Moves slowly!
  56. bool bDataIsNotValid; //Indicates that the struct contains unitialized data
  57. Stuff::Point3D position; //Position of vertex relative to base position of shape.
  58. } TG_ShadowVertex;
  59. typedef TG_ShadowVertex* TG_ShadowVertexPtr;
  60. //-----------------------------------------------------------------------------------
  61. // TG_ShadowVertexTemp
  62. // Stores VOLATILE information for the shadow vertices. These can come from a pool!
  63. typedef struct _TG_ShadowVertexTemp
  64. {
  65. DWORD fRGBFog; //Vertex Fog and Specular Color. Needed if shadow is fogged.
  66. //Every frame for local light. Once in a blue moon for infinite light.
  67. //Changes each call to Transform.
  68. Stuff::Vector4D transformedPosition; //ScreenCoords with Z depth. Valid after Transform called.
  69. } TG_ShadowVertexTemp;
  70. typedef TG_ShadowVertexTemp* TG_ShadowVertexTempPtr;
  71. //-------------------------------------------------------------------------------
  72. // TG_UVData
  73. typedef struct _TG_UVData
  74. {
  75. float u0; //UV Data for each vertex of the triangle
  76. float v0; //Allows same vertex to have different UVs
  77. float u1;
  78. float v1;
  79. float u2;
  80. float v2;
  81. } TG_UVData;
  82. typedef TG_UVData* TG_UVDataPtr;
  83. //-------------------------------------------------------------------------------
  84. // TG_TypeTriangle
  85. // This structure stores the information needed to draw the triangle which does NOT change per instance
  86. typedef struct _TG_TypeTriangle
  87. {
  88. DWORD Vertices[3]; //Indices into Vertex List.
  89. DWORD localTextureHandle; //Index into texture List.
  90. DWORD renderStateFlags; //Flags about render for this face.
  91. //Bit 0 -- backFacing
  92. TG_UVData uvdata; //Texture UVs for this face.
  93. Stuff::Vector3D faceNormal; //Normal Vector to face
  94. } TG_TypeTriangle;
  95. // Texture handle can change from frame to frame. Texture Animation!
  96. typedef TG_TypeTriangle* TG_TypeTrianglePtr;
  97. //-------------------------------------------------------------------------------
  98. // TG_Triangle
  99. // This structure stores the information needed to draw the triangle per instance.
  100. typedef struct _TG_Triangle
  101. {
  102. DWORD aRGBLight[3]; //RGB Light for this triangle's vertices.
  103. DWORD fRGBLight[3]; //RGB Fog for this triangle's vertices.
  104. } TG_Triangle;
  105. // Texture handle can change from frame to frame. Texture Animation!
  106. typedef TG_Triangle* TG_TrianglePtr;
  107. //-------------------------------------------------------------------------------
  108. // TG_ShadowTriangle
  109. // This structure stores the information needed to draw the shadow triangle.
  110. typedef struct _TG_ShadowTriangle
  111. {
  112. DWORD Vertices[3]; //Indices into Shadow Vertex List.
  113. } TG_ShadowTriangle;
  114. // Texture handle can change from frame to frame. Texture Animation!
  115. typedef TG_ShadowTriangle* TG_ShadowTrianglePtr;
  116. //-------------------------------------------------------------------------------
  117. // TG_LightTypes
  118. #define TG_LIGHT_NONE 0xffffffff
  119. #define TG_LIGHT_AMBIENT 0
  120. #define TG_LIGHT_INFINITE 1
  121. #define TG_LIGHT_INFINITEWITHFALLOFF 2
  122. #define TG_LIGHT_POINT 3
  123. #define TG_LIGHT_SPOT 4
  124. #define TG_LIGHT_TERRAIN 5
  125. #define MAX_LIGHTS_IN_WORLD 256
  126. #define TG_NODE_ID 25
  127. #define MAX_SCAN_LENGTH 40
  128. #define MAX_NODES 256
  129. #define MAX_SHADOWS 1
  130. //-------------------------------------------------------------------------------
  131. // TG_Light
  132. // This structure stores the information necessary to light the shape.
  133. typedef struct _TG_Light
  134. {
  135. DWORD lightType; //Ambient, directional, etc.
  136. bool active; //Should this light be considered on?
  137. protected:
  138. DWORD aRGB; //Color
  139. DWORD OEMaRGB;
  140. public:
  141. float intensity; //How Bright
  142. float closeDistance; //Distance out light is constant
  143. float farDistance; //Distance at which light is off
  144. float oneOverDistance; //Used for falloff calc
  145. Stuff::Point3D direction; //Direction in world of light source. This is the light spot center for POINT and SPOT
  146. Stuff::LinearMatrix4D lightToWorld; //Transformation Matrix
  147. Stuff::Vector3D position; //Explicit position to aid terrain code with point sources.
  148. Stuff::Point3D spotDir; //Direction of the actual Spotlight to help with shadows, etc.
  149. float maxSpotLength; //Maximum length spotlight can be from target.
  150. void init (DWORD lType)
  151. {
  152. gosASSERT((lType != TG_LIGHT_NONE) && (lType >= TG_LIGHT_AMBIENT) && (lType <= TG_LIGHT_TERRAIN));
  153. lightType = lType;
  154. aRGB = OEMaRGB = 0xffffffff;
  155. intensity = 1.0f;
  156. direction.x = direction.y = direction.z = 0.0f;
  157. closeDistance = 0.0f;
  158. farDistance = 0.0f;
  159. oneOverDistance = 0.0f;
  160. position.x = position.y = position.z = -999999.0f;
  161. spotDir.x = spotDir.y = spotDir.z = 0.0f;
  162. maxSpotLength = 0.0f;
  163. active = false;
  164. }
  165. void SetaRGB (DWORD newaRGB)
  166. {
  167. OEMaRGB = aRGB = newaRGB;
  168. }
  169. DWORD GetaRGB (void)
  170. {
  171. return aRGB;
  172. }
  173. void SetLightToWorld (Stuff::LinearMatrix4D *l2w)
  174. {
  175. lightToWorld = *l2w;
  176. }
  177. void SetPosition (Stuff::Vector3D *pos)
  178. {
  179. position = *pos;
  180. }
  181. void SetFalloffDistances (float inner, float outer)
  182. {
  183. closeDistance = inner;
  184. farDistance = outer;
  185. oneOverDistance = 1.0f/(outer - inner);
  186. }
  187. void SetIntensity (float intensity)
  188. {
  189. if (intensity > 1.0f)
  190. intensity = 1.0f;
  191. //Scale aRGB by intensity
  192. DWORD r,g,b;
  193. r = intensity * ((OEMaRGB >> 16) & 0x000000ff);
  194. g = intensity * ((OEMaRGB >> 8) & 0x000000ff);
  195. b = intensity * ((OEMaRGB) & 0x000000ff);
  196. aRGB = (0xff << 24) + (r << 16) + (g << 8) + (b);
  197. }
  198. bool GetFalloff(float length, float &falloff)
  199. {
  200. if (length <= closeDistance)
  201. {
  202. falloff = 1.0f;
  203. return true;
  204. }
  205. if (length >= farDistance)
  206. {
  207. return false;
  208. }
  209. falloff = (farDistance - length) * oneOverDistance;
  210. return true;
  211. }
  212. } TG_Light;
  213. typedef TG_Light *TG_LightPtr;
  214. //-------------------------------------------------------------------------------
  215. // TG_Texture
  216. typedef struct _TG_Texture
  217. {
  218. char textureName[256];
  219. DWORD mcTextureNodeIndex;
  220. DWORD gosTextureHandle;
  221. bool textureAlpha;
  222. } TG_Texture;
  223. typedef TG_Texture* TG_TexturePtr;
  224. //-------------------------------------------------------------------------------
  225. // TG_TinyTexture
  226. typedef struct _TG_TinyTexture
  227. {
  228. DWORD mcTextureNodeIndex;
  229. DWORD gosTextureHandle;
  230. bool textureAlpha;
  231. } TG_TinyTexture;
  232. typedef TG_TinyTexture* TG_TinyTexturePtr;
  233. class TG_Shape;
  234. //-------------------------------------------------------------------------------
  235. // TG_Animation
  236. typedef struct _TG_Animation
  237. {
  238. char nodeId[TG_NODE_ID]; //Node ID
  239. DWORD shapeId; //DON'T SCAN EVERY FRAME. WOW IS IT SLOW!!!!! Set this first time through and its simple.
  240. DWORD numFrames; //Number of Frames of animation.
  241. float frameRate; //Number of Frames Per Second.
  242. float tickRate; //Number of Ticks Per Second.
  243. Stuff::UnitQuaternion *quat; //Stores animation offset in Quaternion rotation.
  244. Stuff::Point3D *pos; //Stores Positional offsets if present. OTHERWISE NULL!!!!!!!!
  245. void SaveBinaryCopy (File *binFile);
  246. void LoadBinaryCopy (File *binFile);
  247. } TG_Animation;
  248. typedef TG_Animation *TG_AnimationPtr;
  249. //-------------------------------------------------------------------------------
  250. // TG_ShapeRec
  251. typedef struct _TG_ShapeRec
  252. {
  253. TG_Shape* node; //Pointer to the actual TG_Shape for this piece
  254. Stuff::LinearMatrix4D localShapeToWorld; //Matrix to transform this TG_Shape.
  255. Stuff::LinearMatrix4D shapeToWorld; //Matrix to transform this TG_Shape.
  256. Stuff::LinearMatrix4D worldToShape; //Inverse of above Matrix.
  257. long calcedThisFrame; //Turn number this matrix is current for.
  258. bool processMe; //Flag indicating if I should transform/draw this. Used for arms off.
  259. TG_AnimationPtr currentAnimation; //Animation data being applied to this shape. OK if NULL
  260. _TG_ShapeRec *parentNode; //Parent Node. OK if NULL but only for ROOT node!
  261. Stuff::UnitQuaternion baseRotation; //Always ZERO unless set by appearance controlling this shape.
  262. _TG_ShapeRec &operator=(const _TG_ShapeRec &rec)
  263. {
  264. node = rec.node;
  265. calcedThisFrame = -1;
  266. processMe = true;
  267. currentAnimation = NULL;
  268. parentNode = rec.parentNode;
  269. baseRotation = rec.baseRotation;
  270. return *this;
  271. }
  272. } TG_ShapeRec;
  273. typedef TG_ShapeRec *TG_ShapeRecPtr;
  274. #define TYPE_NODE 0
  275. #define SHAPE_NODE 1
  276. class TG_TypeNode
  277. {
  278. //---------------
  279. // Data Members
  280. protected:
  281. Stuff::Point3D nodeCenter;
  282. Stuff::Point3D relativeNodeCenter;
  283. char nodeId[TG_NODE_ID];
  284. char parentId[TG_NODE_ID];
  285. //---------------------
  286. // Member Functions
  287. protected:
  288. public:
  289. void *operator new (size_t mySize);
  290. void operator delete (void *us);
  291. virtual void init (void)
  292. {
  293. nodeId[0] = parentId[0] = '\0';
  294. relativeNodeCenter.x = relativeNodeCenter.y = relativeNodeCenter.z = 0.0f;
  295. nodeCenter.x = nodeCenter.y = nodeCenter.z = 0.0f;
  296. }
  297. virtual void destroy (void);
  298. char *getNodeId (void)
  299. {
  300. return nodeId;
  301. }
  302. char *getParentId (void)
  303. {
  304. return parentId;
  305. }
  306. Stuff::Point3D GetNodeCenter (void)
  307. {
  308. return nodeCenter;
  309. }
  310. Stuff::Point3D GetRelativeNodeCenter (void)
  311. {
  312. return relativeNodeCenter;
  313. }
  314. void MoveNodeCenterRelative (Stuff::Point3D parent)
  315. {
  316. relativeNodeCenter = nodeCenter;
  317. relativeNodeCenter -= parent;
  318. }
  319. virtual void movePosRelativeCenterNode (void)
  320. {
  321. }
  322. virtual long GetNumTypeVertices (void)
  323. {
  324. return 0;
  325. }
  326. virtual long GetNodeType (void)
  327. {
  328. return TYPE_NODE;
  329. }
  330. //Function return 0 is OK. -1 if file is not ASE Format or missing data.
  331. //This function simply parses the ASE buffers handed to it. This allows
  332. //users to load the ase file themselves and manage their own memory for it.
  333. //It allocates memory for internal Lists. These are straight mallocs at present.
  334. //
  335. // NOTE: Only takes the first GEOMOBJECT from the ASE file. Multi-object
  336. // Files will require user intervention to parse!!
  337. virtual long ParseASEFile (BYTE *aseBuffer, char *filename)
  338. {
  339. return 0;
  340. }
  341. //Function return 0 is OK. -1 if file is not ASE Format or missing data.
  342. //This function simply parses the ASE buffers handed to it. This allows
  343. //users to load the ase file themselves and manage their own memory for it.
  344. //It allocates memory for internal Lists. These are straight mallocs at present.
  345. //
  346. // NOTE: Only takes the first HELPEROBJECT from the ASE file. Multi-object
  347. // Files will require user intervention to parse!!
  348. virtual long MakeFromHelper (BYTE *aseBuffer, char *filename);
  349. //Function returns 0 if OK. -1 if file not found or file not ASE Format.
  350. //This function loads the ASE file into the TG_Triangle and TG_Vertex lists.
  351. //It allocates memory. These are straight mallocs at present.
  352. //
  353. // NOTE: Only takes the first GEOMOBJECT from the ASE file. Multi-object
  354. // Files will require user intervention to parse!!
  355. virtual long LoadTGShapeFromASE (char *fileName)
  356. {
  357. return 0;
  358. }
  359. //Function returns 0 if OK. -1 if textureNum is out of range of numTextures.
  360. //This function takes the gosTextureHandle passed in and assigns it to the
  361. //textureNum entry of the listOfTextures;
  362. virtual long SetTextureHandle (DWORD textureNum, DWORD gosTextureHandle)
  363. {
  364. return 0;
  365. }
  366. //Function returns 0 if OK. -1 if textureNum is out of range of numTextures.
  367. //This function takes the gosTextureHandle passed in and assigns it to the
  368. //textureNum entry of the listOfTextures;
  369. virtual long SetTextureAlpha (DWORD textureNum, bool alphaFlag)
  370. {
  371. return 0;
  372. }
  373. //Need this so that Multi-Shapes can let each shape know texture info.
  374. virtual void CreateListOfTextures (TG_TexturePtr list, DWORD numTxms)
  375. {
  376. }
  377. //--------------------------------------------------------------
  378. // Creates an instance of this shape for the game to muck with.
  379. virtual TG_Shape *CreateFrom (void);
  380. virtual void SetAlphaTest (bool flag)
  381. {
  382. }
  383. virtual void SetFilter (bool flag)
  384. {
  385. }
  386. virtual void SetLightRGBs (DWORD hPink, DWORD hGreen, DWORD hYellow)
  387. {
  388. }
  389. virtual void LoadBinaryCopy (File &binFile);
  390. virtual void SaveBinaryCopy (File &binFile);
  391. };
  392. typedef TG_TypeNode* TG_TypeNodePtr;
  393. class TG_MultiShape;
  394. class TG_TypeMultiShape;
  395. class TG_Shape;
  396. //-------------------------------------------------------------------------------
  397. // The meat and Potatoes part.
  398. // TG_Shape
  399. class TG_TypeShape : public TG_TypeNode
  400. {
  401. //-------------------------------------------------------
  402. // This class runs the shape. Knows how to load/import
  403. // an ASE file. Can dig information out of the file for
  404. // User use (i.e. Texture Names). Transforms, lights, clips
  405. // and renders the shape.
  406. friend TG_TypeMultiShape;
  407. friend TG_MultiShape;
  408. friend TG_Shape;
  409. //-------------
  410. //Data Members
  411. protected:
  412. DWORD numTypeVertices; //Number of vertices in Shape
  413. DWORD numTypeTriangles; //NUmber of triangles in Shape
  414. DWORD numTextures; //Number of textures in Shape
  415. TG_TypeVertexPtr listOfTypeVertices; //Memory holding all vertex data
  416. TG_TypeTrianglePtr listOfTypeTriangles; //Memory holding all triangle data
  417. TG_TinyTexturePtr listOfTextures; //List of texture Structures for this shape.
  418. DWORD hotPinkRGB; //Stores the value for this shape to replace hot Pink With
  419. DWORD hotYellowRGB; //Stores the value for this shape to replace hot Yellow With
  420. DWORD hotGreenRGB; //Stores the value for this shape to replace hot Green With
  421. bool alphaTestOn; //Decides if we should draw alphaTest On or not!
  422. bool filterOn; //Decides if we should filter the shape or not!
  423. //-----------------
  424. //Member Functions
  425. protected:
  426. public:
  427. virtual void init (void)
  428. {
  429. numTypeVertices = numTypeTriangles = numTextures = 0;
  430. listOfTypeVertices = NULL;
  431. listOfTypeTriangles = NULL;
  432. listOfTextures = NULL;
  433. alphaTestOn = false;
  434. filterOn = true;
  435. relativeNodeCenter.x = relativeNodeCenter.y = relativeNodeCenter.z = 0.0f;
  436. nodeCenter.x = nodeCenter.y = nodeCenter.z = 0.0f;
  437. hotPinkRGB = 0x00cbf0ff;
  438. hotYellowRGB = 0x00FEfF91;
  439. hotGreenRGB = 0x000081b6;
  440. }
  441. TG_TypeShape (void)
  442. {
  443. init();
  444. }
  445. virtual void destroy (void);
  446. ~TG_TypeShape (void)
  447. {
  448. destroy();
  449. }
  450. virtual long GetNumTypeVertices (void)
  451. {
  452. return numTypeVertices;
  453. }
  454. //Function return 0 is OK. -1 if file is not ASE Format or missing data.
  455. //This function simply parses the ASE buffers handed to it. This allows
  456. //users to load the ase file themselves and manage their own memory for it.
  457. //It allocates memory for internal Lists. These are straight mallocs at present.
  458. //
  459. // NOTE: Only takes the first GEOMOBJECT from the ASE file. Multi-object
  460. // Files will require user intervention to parse!!
  461. virtual long ParseASEFile (BYTE *aseBuffer, char *filename); //filename for error reporting ONLY
  462. //Function return 0 is OK. -1 if file is not ASE Format or missing data.
  463. //This function simply parses the ASE buffers handed to it. This allows
  464. //users to load the ase file themselves and manage their own memory for it.
  465. //It allocates memory for internal Lists. These are straight mallocs at present.
  466. //
  467. // NOTE: Only takes the first HELPEROBJECT from the ASE file. Multi-object
  468. // Files will require user intervention to parse!!
  469. virtual long MakeFromHelper (BYTE *aseBuffer, char *filename);
  470. //Function returns 0 if OK. -1 if file not found or file not ASE Format.
  471. //This function loads the ASE file into the TG_Triangle and TG_Vertex lists.
  472. //It allocates memory. These are straight mallocs at present.
  473. //
  474. // NOTE: Only takes the first GEOMOBJECT from the ASE file. Multi-object
  475. // Files will require user intervention to parse!!
  476. virtual long LoadTGShapeFromASE (char *fileName);
  477. //Function returns 0 if OK. -1 if textureNum is out of range of numTextures.
  478. //This function takes the gosTextureHandle passed in and assigns it to the
  479. //textureNum entry of the listOfTextures;
  480. virtual long SetTextureHandle (DWORD textureNum, DWORD gosTextureHandle);
  481. //Function returns 0 if OK. -1 if textureNum is out of range of numTextures.
  482. //This function takes the gosTextureHandle passed in and assigns it to the
  483. //textureNum entry of the listOfTextures;
  484. virtual long SetTextureAlpha (DWORD textureNum, bool alphaFlag);
  485. //Need this so that Multi-Shapes can let each shape know texture info.
  486. virtual void CreateListOfTextures (TG_TexturePtr list, DWORD numTxms);
  487. virtual void movePosRelativeCenterNode (void);
  488. //--------------------------------------------------------------
  489. // Creates an instance of this shape for the game to muck with.
  490. virtual TG_Shape *CreateFrom (void);
  491. virtual void SetAlphaTest (bool flag)
  492. {
  493. alphaTestOn = flag;
  494. }
  495. virtual void SetFilter (bool flag)
  496. {
  497. filterOn = flag;
  498. }
  499. virtual void SetLightRGBs (DWORD hPink, DWORD hGreen, DWORD hYellow)
  500. {
  501. hotPinkRGB = hPink;
  502. hotGreenRGB = hGreen;
  503. hotYellowRGB = hYellow;
  504. }
  505. virtual long GetNodeType (void)
  506. {
  507. return SHAPE_NODE;
  508. }
  509. virtual void LoadBinaryCopy (File &binFile);
  510. virtual void SaveBinaryCopy (File &binFile);
  511. };
  512. typedef TG_TypeShape* TG_TypeShapePtr;
  513. //-------------------------------------------------------------------------------------
  514. // The OTHER meat and Potatoes part. This is the actual instance the game draws with.
  515. // TG_Shape
  516. class TG_Shape
  517. {
  518. //-------------------------------------------------------
  519. // This class runs the shape instance.
  520. // Transforms, lights, clips and renders the shape.
  521. friend TG_MultiShape;
  522. friend TG_TypeShape;
  523. friend TG_TypeNode;
  524. friend TG_TypeMultiShape;
  525. //-------------
  526. //Data Members
  527. protected:
  528. TG_TypeNodePtr myType; //Pointer to the instance of the shape.
  529. DWORD numVertices; //Number of vertices in Shape
  530. DWORD numTriangles; //NUmber of triangles in Shape
  531. DWORD numVisibleFaces; //Number of non-backfaced non-clipped faces.
  532. DWORD numVisibleShadows; //Number of visible Shadow Faces.
  533. TG_Vertex * listOfColors; //Memory holding all unchanged or rarely changed color data.
  534. gos_VERTEX * listOfVertices; //Memory holding all vertex data
  535. TG_TrianglePtr listOfTriangles; //Memory holding all triangle data
  536. DWORDPtr listOfVisibleFaces; //Memory holding indices into listOfTriangles
  537. //Draw in this order. First entry with value 0xffffffff
  538. //Means all done, no more to draw.
  539. TG_ShadowVertexPtr listOfShadowVertices; //Stores shadow vertex information for the shape.
  540. //We just use existing listOfTriangles to draw!
  541. TG_ShadowVertexTempPtr listOfShadowTVertices; //Stores just the Volatile data. Comes from a pool!!
  542. DWORDPtr listOfVisibleShadows; //Memory holding indices into listOfTriangles
  543. //Draw in this order. First entry with value 0xffffffff
  544. //Means all done, no more to draw.
  545. bool shadowsVisible[MAX_SHADOWS];//Is this shadow worth drawing?
  546. DWORD aRGBHighlight; //Color to add to vertices to make building stand out.
  547. DWORD fogRGB; //Color to make fog.
  548. float shapeScalar;
  549. bool lightsOut;
  550. bool noShadow;
  551. bool recalcShadows;
  552. bool isWindow;
  553. bool isSpotlight;
  554. DWORD lastTurnTransformed;
  555. public:
  556. //Matrices used to transform the shapes.
  557. static Stuff::LinearMatrix4D *cameraOrigin;
  558. static Stuff::Matrix4D *cameraToClip;
  559. static Stuff::Matrix4D worldToClip;
  560. static Stuff::LinearMatrix4D worldToCamera;
  561. static TG_LightPtr *listOfLights; //List passed in a transform time
  562. static DWORD numLights;
  563. static float viewMulX;
  564. static float viewAddX;
  565. static float viewMulY;
  566. static float viewAddY;
  567. static DWORD fogColor;
  568. static float fogFull;
  569. static float fogStart;
  570. static Stuff::LinearMatrix4D lightToShape[MAX_LIGHTS_IN_WORLD];
  571. static Stuff::Vector3D lightDir[MAX_LIGHTS_IN_WORLD];
  572. static Stuff::Vector3D spotDir[MAX_LIGHTS_IN_WORLD];
  573. static Stuff::Vector3D rootLightDir[MAX_LIGHTS_IN_WORLD];
  574. static UserHeapPtr tglHeap; //Stores all TGL data so we don't need to go through the FREE merry go round of GOS!
  575. static DWORD lighteningLevel;
  576. //-----------------
  577. //Member Functions
  578. protected:
  579. public:
  580. void * operator new (size_t mySize);
  581. void operator delete (void * us);
  582. void init (void)
  583. {
  584. numVertices = numTriangles = numVisibleFaces = 0;
  585. listOfVertices = NULL;
  586. listOfTriangles = NULL;
  587. listOfLights = NULL;
  588. listOfVisibleFaces = NULL;
  589. listOfShadowVertices = NULL;
  590. listOfVisibleShadows = NULL;
  591. aRGBHighlight = 0x00000000;
  592. shapeScalar = 0.0f;
  593. lightsOut = false;
  594. noShadow = false;
  595. recalcShadows = true;
  596. isWindow = isSpotlight = false;
  597. for (long i=0;i<MAX_SHADOWS;i++)
  598. shadowsVisible[i] = false;
  599. }
  600. TG_Shape (void)
  601. {
  602. init();
  603. }
  604. void destroy (void);
  605. ~TG_Shape (void)
  606. {
  607. destroy();
  608. }
  609. //This function sets up the camera Matrices for this TG_Shape to transform
  610. //itself with. These matrices are static and only need to be set once per
  611. //render pass if the camera does not change for that pass.
  612. static void SetCameraMatrices (Stuff::LinearMatrix4D *camOrigin, Stuff::Matrix4D *camToClip);
  613. static void SetViewport (float mulX, float mulY, float addX, float addY);
  614. static void SetFog (DWORD fRGB, float fStart, float fFull);
  615. //This function sets the list of lights used by the TransformShape function
  616. //to light the shape.
  617. //Function returns 0 if lightList entries are all OK. -1 otherwise.
  618. //
  619. long SetLightList (TG_LightPtr *lightList, DWORD nLights);
  620. //This function sets the fog values for the shape. Straight fog right now.
  621. void SetFogRGB (DWORD fRGB);
  622. //This function does exactly what TranformShape does EXCEPT that the shapeToClip,
  623. //Lighting and backface matrices have been calculated in the step above this one.
  624. //This saves enormous processor cycles when matrices are the same and transforms
  625. //Can just be run from the same matrices without recalcing them!
  626. //Function returns -1 if all vertex screen positions are off screen.
  627. //Function returns 0 if any one vertex screen position is off screen.
  628. //Function returns 1 is all vertex screen positions are on screen.
  629. // NOTE: THIS IS NOT A RIGOROUS CLIP!!!!!!!!!
  630. long MultiTransformShape (Stuff::Matrix4D *shapeToClip, Stuff::Point3D *backFacePoint, TG_ShapeRecPtr parentNode, bool isHudElement, BYTE alphaValue, bool isClamped);
  631. //This function creates the list of shadows and transforms them in preparation to drawing.
  632. //
  633. void MultiTransformShadows (Stuff::Point3D *pos, Stuff::LinearMatrix4D *s2w, float rotation);
  634. //This function takes the current listOfVisibleFaces and draws them using
  635. //gos_DrawTriangle. Does clipping, too!
  636. void Render (float forceZ = -1.0f, bool isHudElement = false, BYTE alphaValue = 0xff, bool isClamped = false);
  637. //This function takes the current listOfShadowTriangles and draws them using
  638. //gos_DrawTriangle. Does clipping, too!
  639. long RenderShadows (long startFace);
  640. void SetARGBHighLight (DWORD argb)
  641. {
  642. aRGBHighlight = argb;
  643. }
  644. void SetLightsOut (bool lightFlag)
  645. {
  646. lightsOut = lightFlag;
  647. }
  648. char * getNodeName (void)
  649. {
  650. return myType->getNodeId();
  651. }
  652. Stuff::Point3D GetRelativeNodeCenter (void)
  653. {
  654. return myType->GetRelativeNodeCenter();
  655. }
  656. bool PerPolySelect (float mouseX, float mouseY);
  657. void SetRecalcShadows (bool flag)
  658. {
  659. recalcShadows = flag;
  660. }
  661. virtual void ScaleShape (float scaleFactor)
  662. {
  663. shapeScalar = scaleFactor;
  664. }
  665. };
  666. typedef TG_Shape* TG_ShapePtr;
  667. //Pools are defined here.
  668. class TG_VertexPool
  669. {
  670. protected:
  671. TG_Vertex *tgVertexPool;
  672. TG_Vertex *nextVertex;
  673. DWORD totalVertices;
  674. DWORD numVertices;
  675. public:
  676. TG_VertexPool (void)
  677. {
  678. tgVertexPool = nextVertex = NULL;
  679. totalVertices = numVertices = 0;
  680. }
  681. ~TG_VertexPool (void)
  682. {
  683. destroy();
  684. }
  685. void destroy (void)
  686. {
  687. TG_Shape::tglHeap->Free(tgVertexPool);
  688. tgVertexPool = nextVertex = NULL;
  689. totalVertices = numVertices = 0;
  690. }
  691. void init (DWORD maxVertices)
  692. {
  693. tgVertexPool = (TG_VertexPtr)TG_Shape::tglHeap->Malloc(sizeof(TG_Vertex) * maxVertices);
  694. gosASSERT(tgVertexPool != NULL);
  695. nextVertex = tgVertexPool;
  696. totalVertices = maxVertices;
  697. numVertices = 0;
  698. }
  699. void reset (void)
  700. {
  701. nextVertex = tgVertexPool;
  702. numVertices = 0;
  703. }
  704. TG_VertexPtr getColorsFromPool (DWORD numRequested)
  705. {
  706. TG_VertexPtr result = NULL;
  707. numVertices += numRequested;
  708. if (numVertices < totalVertices)
  709. {
  710. result = nextVertex;
  711. nextVertex += numRequested;
  712. }
  713. return result;
  714. }
  715. };
  716. class TG_GOSVertexPool
  717. {
  718. protected:
  719. gos_VERTEX *gVertexPool;
  720. gos_VERTEX *nextVertex;
  721. DWORD totalVertices;
  722. DWORD numVertices;
  723. public:
  724. TG_GOSVertexPool (void)
  725. {
  726. gVertexPool = nextVertex = NULL;
  727. totalVertices = numVertices = 0;
  728. }
  729. ~TG_GOSVertexPool (void)
  730. {
  731. destroy();
  732. }
  733. void destroy (void)
  734. {
  735. TG_Shape::tglHeap->Free(gVertexPool);
  736. gVertexPool = nextVertex = NULL;
  737. totalVertices = numVertices = 0;
  738. }
  739. void init (DWORD maxVertices)
  740. {
  741. gVertexPool = (gos_VERTEX *)TG_Shape::tglHeap->Malloc(sizeof(gos_VERTEX) * maxVertices);
  742. gosASSERT(gVertexPool != NULL);
  743. nextVertex = gVertexPool;
  744. totalVertices = maxVertices;
  745. numVertices = 0;
  746. }
  747. void reset (void)
  748. {
  749. nextVertex = gVertexPool;
  750. numVertices = 0;
  751. }
  752. gos_VERTEX * getVerticesFromPool (DWORD numRequested)
  753. {
  754. gos_VERTEX* result = NULL;
  755. numVertices += numRequested;
  756. if (numVertices < totalVertices)
  757. {
  758. result = nextVertex;
  759. nextVertex += numRequested;
  760. }
  761. return result;
  762. }
  763. };
  764. class TG_TrianglePool
  765. {
  766. protected:
  767. TG_Triangle *trianglePool;
  768. TG_Triangle *nextTriangle;
  769. DWORD totalTriangles;
  770. DWORD numTriangles;
  771. public:
  772. TG_TrianglePool (void)
  773. {
  774. trianglePool = nextTriangle = NULL;
  775. totalTriangles = numTriangles = 0;
  776. }
  777. ~TG_TrianglePool (void)
  778. {
  779. destroy();
  780. }
  781. void destroy (void)
  782. {
  783. TG_Shape::tglHeap->Free(trianglePool);
  784. trianglePool = nextTriangle = NULL;
  785. totalTriangles = numTriangles = 0;
  786. }
  787. void init (DWORD maxTriangles)
  788. {
  789. trianglePool = (TG_Triangle *)TG_Shape::tglHeap->Malloc(sizeof(TG_Triangle) * maxTriangles);
  790. gosASSERT(trianglePool != NULL);
  791. nextTriangle = trianglePool;
  792. totalTriangles = maxTriangles;
  793. numTriangles = 0;
  794. }
  795. void reset (void)
  796. {
  797. nextTriangle = trianglePool;
  798. numTriangles = 0;
  799. }
  800. TG_Triangle * getTrianglesFromPool (DWORD numRequested)
  801. {
  802. TG_Triangle* result = NULL;
  803. numTriangles += numRequested;
  804. if (numTriangles < totalTriangles)
  805. {
  806. result = nextTriangle;
  807. nextTriangle += numRequested;
  808. }
  809. return result;
  810. }
  811. };
  812. class TG_ShadowPool
  813. {
  814. protected:
  815. TG_ShadowVertexTemp *tVertexPool;
  816. TG_ShadowVertexTemp *nextVertex;
  817. DWORD totalVertices;
  818. DWORD numVertices;
  819. public:
  820. TG_ShadowPool (void)
  821. {
  822. tVertexPool = nextVertex = NULL;
  823. totalVertices = numVertices = 0;
  824. }
  825. ~TG_ShadowPool (void)
  826. {
  827. destroy();
  828. }
  829. void destroy (void)
  830. {
  831. TG_Shape::tglHeap->Free(tVertexPool);
  832. tVertexPool = nextVertex = NULL;
  833. totalVertices = numVertices = 0;
  834. }
  835. void init (DWORD maxVertices)
  836. {
  837. tVertexPool = (TG_ShadowVertexTempPtr)TG_Shape::tglHeap->Malloc(sizeof(TG_ShadowVertexTemp) * maxVertices);
  838. gosASSERT(tVertexPool != NULL);
  839. nextVertex = tVertexPool;
  840. totalVertices = maxVertices;
  841. numVertices = 0;
  842. }
  843. void reset (void)
  844. {
  845. nextVertex = tVertexPool;
  846. numVertices = 0;
  847. }
  848. TG_ShadowVertexTempPtr getShadowsFromPool (DWORD numRequested)
  849. {
  850. TG_ShadowVertexTempPtr result = NULL;
  851. numVertices += numRequested;
  852. if (numVertices < totalVertices)
  853. {
  854. result = nextVertex;
  855. nextVertex += numRequested;
  856. }
  857. return result;
  858. }
  859. };
  860. class TG_DWORDPool
  861. {
  862. protected:
  863. DWORD *triPool;
  864. DWORD *nextTri;
  865. DWORD totalTriangles;
  866. DWORD numTriangles;
  867. public:
  868. TG_DWORDPool (void)
  869. {
  870. triPool = nextTri = NULL;
  871. totalTriangles = numTriangles = 0;
  872. }
  873. ~TG_DWORDPool (void)
  874. {
  875. destroy();
  876. }
  877. void destroy (void)
  878. {
  879. TG_Shape::tglHeap->Free(triPool);
  880. triPool = nextTri = NULL;
  881. totalTriangles = numTriangles = 0;
  882. }
  883. void init (DWORD maxTriangles)
  884. {
  885. triPool = (DWORD *)TG_Shape::tglHeap->Malloc(sizeof(DWORD) * maxTriangles);
  886. gosASSERT(triPool != NULL);
  887. nextTri = triPool;
  888. totalTriangles = maxTriangles;
  889. numTriangles = 0;
  890. }
  891. void reset (void)
  892. {
  893. nextTri = triPool;
  894. numTriangles = 0;
  895. }
  896. DWORD * getFacesFromPool (DWORD numRequested)
  897. {
  898. DWORD* result = NULL;
  899. numTriangles += numRequested;
  900. if (numTriangles < totalTriangles)
  901. {
  902. result = nextTri;
  903. nextTri += numRequested;
  904. }
  905. return result;
  906. }
  907. };
  908. //-------------------------------------------------------------------------------
  909. extern TG_VertexPool *colorPool;
  910. extern TG_GOSVertexPool *vertexPool;
  911. extern TG_DWORDPool *facePool;
  912. extern TG_ShadowPool *shadowPool;
  913. extern TG_TrianglePool *trianglePool;
  914. //-------------------------------------------------------------------------------
  915. // ASE File Parse String Macros
  916. #define ASE_HEADER "*3DSMAX_ASCIIEXPORT\t200"
  917. #define ASE_OBJECT "*GEOMOBJECT {"
  918. #define ASE_HELP_OBJECT "*HELPEROBJECT {"
  919. #define ASE_MESH "*MESH {"
  920. #define ASE_NUM_VERTEX "*MESH_NUMVERTEX"
  921. #define ASE_NUM_FACE "*MESH_NUMFACES"
  922. #define ASE_VERTEX_LIST "*MESH_VERTEX_LIST {"
  923. #define ASE_FACE_LIST "*MESH_FACE_LIST {"
  924. #define ASE_NUM_TVERTEX "*MESH_NUMTVERTEX"
  925. #define ASE_TVERTLIST "*MESH_TVERTLIST {"
  926. #define ASE_NUM_TVFACES "*MESH_NUMTVFACES"
  927. #define ASE_TFACELIST "*MESH_TFACELIST {"
  928. #define ASE_NUM_CVERTEX "*MESH_NUMCVERTEX"
  929. #define ASE_CVERTLIST "*MESH_CVERTLIST {"
  930. #define ASE_NUM_CVFACES "*MESH_NUMCVFACES"
  931. #define ASE_CFACELIST "*MESH_CFACELIST {"
  932. #define ASE_MESH_NORMALS "*MESH_NORMALS {"
  933. #define ASE_NODE_NAME "*NODE_NAME"
  934. #define ASE_NODE_PARENT "*NODE_PARENT"
  935. #define ASE_NODE_POS "*TM_POS"
  936. #define ASE_ANIMATION "*TM_ANIMATION {"
  937. #define ASE_ANIM_ROT_HEADER "*CONTROL_ROT_TRACK {"
  938. #define ASE_ANIM_ROT_SAMPLE "*CONTROL_ROT_SAMPLE"
  939. #define ASE_ANIM_POS_HEADER "*CONTROL_POS_TRACK {"
  940. #define ASE_ANIM_POS_SAMPLE "*CONTROL_POS_SAMPLE"
  941. #define ASE_ANIM_FIRST_FRAME "*SCENE_FIRSTFRAME"
  942. #define ASE_ANIM_LAST_FRAME "*SCENE_LASTFRAME"
  943. #define ASE_ANIM_FRAME_SPEED "*SCENE_FRAMESPEED"
  944. #define ASE_ANIM_TICKS_FRAME "*SCENE_TICKSPERFRAME"
  945. #define ASE_FACE_NORMAL_ID "*MESH_FACENORMAL"
  946. #define ASE_VERTEX_NORMAL_ID "*MESH_VERTEXNORMAL"
  947. #define ASE_MESH_CFACE_ID "*MESH_CFACE"
  948. #define ASE_MESH_VERTCOL_ID "*MESH_VERTCOL"
  949. #define ASE_MESH_TFACE_ID "*MESH_TFACE"
  950. #define ASE_MESH_TVERT_ID "*MESH_TVERT"
  951. #define ASE_MESH_FACE_ID "*MESH_FACE"
  952. #define ASE_MESH_VERTEX_ID "*MESH_VERTEX"
  953. #define ASE_MATERIAL_COUNT "*MATERIAL_COUNT"
  954. #define ASE_SUBMATERIAL_COUNT "*NUMSUBMTLS"
  955. #define ASE_MATERIAL_ID "*MATERIAL"
  956. #define ASE_MATERIAL_BITMAP_ID "*BITMAP "
  957. #define ASE_MATERIAL_CLASS "*MATERIAL_CLASS"
  958. #define ASE_MATERIAL_TWOSIDED "*MATERIAL_TWOSIDED"
  959. #define ASE_FACE_MATERIAL_ID "*MESH_MTLID"
  960. //-------------------------------------------------------------------------------
  961. #endif