PageRenderTime 51ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/CS/migrated/branches/R0_18/plugins/mesh/object/spr3d/spr3d.h

#
C++ Header | 1265 lines | 653 code | 158 blank | 454 comment | 14 complexity | d4043ac8e288b7ff9a3d1303d94f690a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. Copyright (C) 1998,2000 by Jorrit Tyberghein
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this library; if not, write to the Free
  13. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef __CS_SPR3D_H__
  16. #define __CS_SPR3D_H__
  17. #include "csutil/cscolor.h"
  18. #include "csutil/csvector.h"
  19. #include "csutil/rng.h"
  20. #include "csgeom/math3d.h"
  21. #include "csgeom/math2d.h"
  22. #include "csgeom/poly2d.h"
  23. #include "csgeom/poly3d.h"
  24. #include "csgeom/box.h"
  25. #include "plugins/mesh/object/spr3d/sprtri.h"
  26. #include "plugins/mesh/object/spr3d/skel3d.h"
  27. #include "igraph3d.h"
  28. #include "ipolmesh.h"
  29. #include "imspr3d.h"
  30. #include "imater.h"
  31. #include "imeshobj.h"
  32. #include "itranman.h"
  33. #include "iconfig.h"
  34. struct iSystem;
  35. /**
  36. * A frame for 3D sprite animation.
  37. */
  38. class csSpriteFrame : public iSpriteFrame
  39. {
  40. private:
  41. int animation_index;
  42. int texturing_index;
  43. char* name;
  44. /// If true then normals are already calculated for this frame.
  45. bool normals_calculated;
  46. /// Bounding box in object space for this frame.
  47. csBox3 box;
  48. public:
  49. ///
  50. csSpriteFrame (int anm_idx, int tex_idx);
  51. ///
  52. virtual ~csSpriteFrame ();
  53. ///
  54. void SetTexIndex (int tex_idx) { texturing_index = tex_idx; }
  55. /// Return true if normals are already calculated.
  56. bool NormalsCalculated () { return normals_calculated; }
  57. /// Set normals calculated to value.
  58. void SetNormalsCalculated (bool n) { normals_calculated = n; }
  59. /// Set the name.
  60. virtual void SetName (char const*);
  61. /// Get the name.
  62. virtual char const* GetName () const { return name; }
  63. ///
  64. virtual int GetAnmIndex () const { return animation_index; }
  65. ///
  66. virtual int GetTexIndex () const { return texturing_index; }
  67. /**
  68. * Compute the object space bounding box for this frame.
  69. * This has to be called after setting up the frame and before
  70. * using it.
  71. */
  72. void SetBoundingBox (csBox3& b) { box = b; }
  73. /**
  74. * Get the bounding box in object space.
  75. */
  76. void GetBoundingBox (csBox3& b) { b = box; }
  77. DECLARE_IBASE;
  78. };
  79. /**
  80. * An action frameset for a 3D sprite animation.
  81. */
  82. class csSpriteAction2 : public iSpriteAction
  83. {
  84. public:
  85. /// Initialize a action object
  86. csSpriteAction2 ();
  87. /// Destroy this action object
  88. virtual ~csSpriteAction2 ();
  89. /// Add a frame to this action @@@ OBSOLETE WHEN MOVED TO MESH SYSTEM
  90. void AddCsFrame (csSpriteFrame* frame, int delay);
  91. /// Add a frame to this action
  92. virtual void AddFrame (iSpriteFrame* frame, int delay);
  93. /// Set action name
  94. virtual void SetName (char const*);
  95. /// Get action name
  96. virtual char const* GetName () const
  97. { return name; }
  98. /// Get total number of frames in this action
  99. virtual int GetNumFrames () { return frames.Length (); }
  100. /// Query the frame number f. @@@ OBSOLETE WHEN MOVED TO MESH SYSTEM
  101. csSpriteFrame* GetCsFrame (int f)
  102. { return (f < frames.Length ()) ? (csSpriteFrame *)frames [f] : (csSpriteFrame*)NULL; }
  103. /// Returns the looping frame after frame number f. @@@ OBSOLETE WHEN MOVED TO MESH SYSTEM
  104. csSpriteFrame* GetCsNextFrame (int f)
  105. { f++; return f<frames.Length() ? (csSpriteFrame*)frames[f]:(csSpriteFrame*)frames[0]; }
  106. /// Query the frame number f.
  107. virtual iSpriteFrame* GetFrame (int f)
  108. { return (iSpriteFrame*)((f < frames.Length ()) ? (csSpriteFrame *)frames [f] : (csSpriteFrame*)NULL); }
  109. /// Returns the looping frame after frame number f.
  110. virtual iSpriteFrame* GetNextFrame (int f)
  111. { f++; return (iSpriteFrame*)(f<frames.Length() ? (csSpriteFrame*)frames[f]:(csSpriteFrame*)frames[0]); }
  112. /// Get delay for frame number f
  113. virtual int GetFrameDelay (int f)
  114. { return (int)delays [f]; }
  115. DECLARE_IBASE;
  116. private:
  117. char *name;
  118. csVector frames;
  119. csVector delays;
  120. };
  121. /**
  122. * A vector for frames which knows how to clean them up.
  123. */
  124. class csSpriteFrameVector : public csVector
  125. {
  126. public:
  127. /// Delete all inserted objects before deleting the object itself.
  128. virtual ~csSpriteFrameVector ();
  129. /// Free a item as a frame.
  130. virtual bool FreeItem (csSome Item);
  131. };
  132. /**
  133. * A vector for actions which knows how to clean them up.
  134. */
  135. class csSpriteActionVector : public csVector
  136. {
  137. public:
  138. /// Delete all inserted objects before deleting the object itself.
  139. virtual ~csSpriteActionVector ();
  140. /// Free a item as an action.
  141. virtual bool FreeItem (csSome Item);
  142. };
  143. class csSprite3DMeshObject;
  144. /**
  145. * A 3D sprite based on a triangle mesh with a single texture.
  146. * Animation is done with frames.
  147. * This class represents a template from which a csSprite3D
  148. * class can be made.
  149. */
  150. class csSprite3DMeshObjectFactory : public iMeshObjectFactory
  151. {
  152. private:
  153. friend class csSprite3DMeshObject;
  154. /// Material handle as returned by iTextureManager.
  155. iMaterialWrapper* cstxt;
  156. /// An optional skeleton.
  157. csSkel* skeleton;
  158. /**
  159. * The order in which to introduce levels in order to get to a higher LOD.
  160. * The index of this array is the vertex number which is introduced.
  161. * The vertices of this template were reordered (by GenerateLOD()) so that
  162. * the first vertices are used in low-detail. The contents of this array
  163. * is the vertex number to emerge from.
  164. */
  165. int* emerge_from;
  166. /// The frames
  167. csSpriteFrameVector frames;
  168. /// The actions (a vector of csSpriteAction2 objects)
  169. csSpriteActionVector actions;
  170. /// Enable tweening.
  171. bool do_tweening;
  172. /// The lighting_quality for this template. See macros CS_SPR_LIGHTING_*
  173. int lighting_quality;
  174. /**
  175. * The lighting_quality_config for this template.
  176. * See macros CS_SPR_LIGHT_*
  177. * This is used to set new sprites lighting_quality_config to this one.
  178. */
  179. int lighting_quality_config;
  180. /*
  181. * Configuration value for template LOD. 0 is lowest detail, 1 is maximum.
  182. * If negative then the base mesh is used and no LOD reduction/computation
  183. * is done.
  184. */
  185. float lod_level;
  186. /**
  187. * The lod_level_config for this template.
  188. * See macros CS_SPR_LOD_*
  189. * This is used to set new sprites lod_level_config to this one.
  190. */
  191. int lod_level_config;
  192. /// The base mesh is also the texture alignment mesh.
  193. csTriangleMesh2* texel_mesh;
  194. /// The array of texels
  195. DECLARE_TYPED_VECTOR (csTexelsVector,csPoly2D) texels;
  196. /// The vertices
  197. DECLARE_TYPED_VECTOR (csVerticesVector,csPoly3D) vertices;
  198. /// The normals
  199. DECLARE_TYPED_VECTOR (csNormalsVector,csPoly3D) normals;
  200. /**
  201. * Connectivity information for this sprite template.
  202. * Also contains temporary vertex position information
  203. * for one sprite (@@@this should be avoided!!!!)
  204. */
  205. csTriangleVertices2* tri_verts;
  206. /// If true then this factory has been initialized.
  207. bool initialized;
  208. public:
  209. iSystem* System;
  210. public:
  211. /// Create the sprite template
  212. csSprite3DMeshObjectFactory ();
  213. /// Destroy the template
  214. virtual ~csSprite3DMeshObjectFactory ();
  215. /// Set the skeleton for this sprite template.
  216. void SetSkeleton (csSkel* sk);
  217. /// Get the skeleton for this sprite template.
  218. csSkel* GetSkeleton () { return skeleton; }
  219. /// Get the 'emerge_from' array from which you can construct triangles.
  220. int* GetEmergeFrom () { return emerge_from; }
  221. /// Enable or disable tweening frames (default false).
  222. void EnableTweening (bool en) { do_tweening = en; }
  223. /// Is tweening enabled?
  224. bool IsTweeningEnabled () { return do_tweening; }
  225. /// Returns the lighting quality for this template.
  226. int GetLightingQuality() { return lighting_quality; }
  227. /// Sets the lighting quality for this template. See CS_SPR_LIGHTING_* defs.
  228. void SetLightingQuality(int quality) {lighting_quality = quality; }
  229. /**
  230. * Sets which lighting config variable that all new sprites created
  231. * from this template will use.
  232. * The options are:
  233. * <ul>
  234. * <li>CS_SPR_LIGHT_GLOBAL (default)
  235. * <li>CS_SPR_LIGHT_TEMPLATE
  236. * <li>CS_SPR_LIGHT_LOCAL
  237. * </ul>
  238. */
  239. void SetLightingQualityConfig (int config_flag)
  240. { lighting_quality_config = config_flag; };
  241. /**
  242. * Returns what this template is using for determining the lighting quality.
  243. */
  244. int GetLightingQualityConfig ()
  245. { return lighting_quality_config; };
  246. /// Returns the lod_level for this template.
  247. float GetLodLevel() { return lod_level; }
  248. /// Sets the lod level for this template. See CS_SPR_LOD_* defs.
  249. void SetLodLevel(float level) {lod_level = level; }
  250. /**
  251. * Sets which lod config variable that all new sprites created
  252. * from this template will use.
  253. * The options are:
  254. * <ul>
  255. * <li>CS_SPR_LOD_GLOBAL (default)
  256. * <li>CS_SPR_LOD_TEMPLATE
  257. * <li>CS_SPR_LOD_LOCAL
  258. * </ul>
  259. */
  260. void SetLodLevelConfig (int config_flag)
  261. { lod_level_config = config_flag; };
  262. /**
  263. * Returns what this template is using for determining the lighting quality.
  264. */
  265. int GetLodLevelConfig ()
  266. { return lod_level_config; };
  267. /**
  268. * Generate the collapse order.
  269. * This function will also reorder all the vertices in the template.
  270. * So be careful!
  271. */
  272. void GenerateLOD ();
  273. /**
  274. * Compute the object space bounding box for all frames in this
  275. * template. This has to be called after setting up the template and before
  276. * using it.
  277. */
  278. void ComputeBoundingBox ();
  279. ///
  280. csTriangleMesh2* GetTexelMesh () {return texel_mesh;}
  281. /// Add some vertices, normals, and texels
  282. void AddVertices (int num);
  283. /// Add a vertex, normal, and texel
  284. void AddVertex () { AddVertices (1); }
  285. /// Query the number of texels.
  286. int GetNumTexels () { return texels.Get(0)->GetNumVertices (); }
  287. /// Get a texel.
  288. csVector2& GetTexel (int frame, int vertex)
  289. { return (*texels.Get(frame)) [vertex]; }
  290. /// Get array of texels.
  291. csVector2* GetTexels (int frame)
  292. { return (*texels.Get(frame)).GetVertices (); }
  293. /// Query the number of vertices.
  294. int GetNumVertices () { return vertices.Get (0)->GetNumVertices (); }
  295. /// Get a vertex.
  296. csVector3& GetVertex (int frame, int vertex)
  297. { return (*vertices.Get(frame)) [vertex]; }
  298. /// Get vertex array.
  299. csVector3* GetVertices (int frame)
  300. { return (*vertices.Get(frame)).GetVertices (); }
  301. /// Query the number of normals.
  302. int GetNumNormals () { return normals.Get (0)->GetNumVertices (); }
  303. /// Get a normal.
  304. csVector3& GetNormal (int frame, int vertex)
  305. { return (*normals.Get(frame)) [vertex]; }
  306. /// Get normal array.
  307. csVector3* GetNormals (int frame)
  308. { return (*normals.Get(frame)).GetVertices (); }
  309. /**
  310. * Add a triangle to the normal, texel, and vertex meshes
  311. * a, b and c are indices to texel vertices
  312. */
  313. void AddTriangle (int a, int b, int c);
  314. /// returns the texel indices for triangle 'x'
  315. csTriangle GetTriangle (int x) { return texel_mesh->GetTriangle(x); }
  316. /// returns the triangles of the texel_mesh
  317. csTriangle* GetTriangles () { return texel_mesh->GetTriangles(); }
  318. /// returns the number of triangles in the sprite
  319. int GetNumTriangles () { return texel_mesh->GetNumTriangles(); }
  320. /// Create and add a new frame to the sprite.
  321. csSpriteFrame* AddFrame ();
  322. /// find a named frame into the sprite.
  323. csSpriteFrame* FindFrame (const char * name);
  324. /// Query the number of frames
  325. int GetNumFrames () { return frames.Length (); }
  326. /// Query the frame number f
  327. csSpriteFrame* GetFrame (int f)
  328. { return (f < frames.Length ()) ? (csSpriteFrame *)frames [f] : (csSpriteFrame*)NULL; }
  329. /// Create and add a new action frameset to the sprite.
  330. csSpriteAction2* AddAction ();
  331. /// find a named action into the sprite.
  332. csSpriteAction2* FindAction (const char * name);
  333. /// Get the first action.
  334. csSpriteAction2* GetFirstAction ()
  335. { return (csSpriteAction2 *)actions [0]; }
  336. /// Get number of actions in sprite
  337. int GetNumActions ()
  338. { return actions.Length (); }
  339. /// Get action number No
  340. csSpriteAction2* GetAction (int No)
  341. { return (csSpriteAction2 *)actions [No]; }
  342. /// Get the material
  343. iMaterialWrapper* GetMaterial () const
  344. { return cstxt; }
  345. /// Get the material handle.
  346. iMaterialHandle* GetMaterialHandle () const
  347. { return cstxt->GetMaterialHandle (); }
  348. /// Set the material used for this sprite
  349. void SetMaterial (iMaterialWrapper *material);
  350. /**
  351. * Compute all normals in a frame.
  352. */
  353. void ComputeNormals (csSpriteFrame* frame);
  354. /**
  355. * Smooth out the gouraud shading by merging the precalculated
  356. * vertex normals along seams in frame 'frame' based on which
  357. * vertices are very close in frame 'base'
  358. */
  359. void MergeNormals (int base, int frame);
  360. /**
  361. * Smooth out the gouraud shading by merging the precalculated
  362. * vertex normals along seams in all frames based on which
  363. * vertices are very close in frame 'base'
  364. */
  365. void MergeNormals (int base);
  366. /**
  367. * Smooth out the gouraud shading by merging the precalculated
  368. * vertex normals along seams in all frames based on which
  369. * vertices are very close in each frame
  370. */
  371. void MergeNormals ();
  372. //------------------------ iMeshObjectFactory implementation --------------
  373. DECLARE_IBASE;
  374. virtual iMeshObject* NewInstance ();
  375. virtual void HardTransform (const csReversibleTransform& t);
  376. virtual bool SupportsHardTransform () { return true; }
  377. //--------------------- iSprite3DFactoryState implementation -------------//
  378. struct Sprite3DFactoryState : public iSprite3DFactoryState
  379. {
  380. DECLARE_EMBEDDED_IBASE (csSprite3DMeshObjectFactory);
  381. virtual void SetMaterialWrapper (iMaterialWrapper* material)
  382. {
  383. scfParent->SetMaterial (material);
  384. }
  385. virtual iMaterialWrapper* GetMaterialWrapper ()
  386. {
  387. return scfParent->GetMaterial ();
  388. }
  389. virtual void AddVertices (int num)
  390. {
  391. scfParent->AddVertices (num);
  392. }
  393. virtual int GetNumTexels ()
  394. {
  395. return scfParent->GetNumTexels ();
  396. }
  397. virtual csVector2& GetTexel (int frame, int vertex)
  398. {
  399. return scfParent->GetTexel (frame, vertex);
  400. }
  401. virtual csVector2* GetTexels (int frame)
  402. {
  403. return scfParent->GetTexels (frame);
  404. }
  405. virtual int GetNumVertices ()
  406. {
  407. return scfParent->GetNumVertices ();
  408. }
  409. virtual csVector3& GetVertex (int frame, int vertex)
  410. {
  411. return scfParent->GetVertex (frame, vertex);
  412. }
  413. virtual csVector3* GetVertices (int frame)
  414. {
  415. return scfParent->GetVertices (frame);
  416. }
  417. virtual int GetNumNormals ()
  418. {
  419. return scfParent->GetNumNormals ();
  420. }
  421. virtual csVector3& GetNormal (int frame, int vertex)
  422. {
  423. return scfParent->GetNormal (frame, vertex);
  424. }
  425. virtual csVector3* GetNormals (int frame)
  426. {
  427. return scfParent->GetNormals (frame);
  428. }
  429. virtual void AddTriangle (int a, int b, int c)
  430. {
  431. scfParent->AddTriangle (a, b, c);
  432. }
  433. virtual csTriangle GetTriangle (int x)
  434. {
  435. return scfParent->GetTriangle (x);
  436. }
  437. virtual csTriangle* GetTriangles ()
  438. {
  439. return scfParent->GetTriangles ();
  440. }
  441. virtual int GetNumTriangles ()
  442. {
  443. return scfParent->GetNumTriangles ();
  444. }
  445. virtual iSpriteFrame* AddFrame ()
  446. {
  447. iSpriteFrame* ifr = QUERY_INTERFACE_SAFE (scfParent->AddFrame (), iSpriteFrame);
  448. if (ifr) ifr->DecRef ();
  449. return ifr;
  450. }
  451. virtual iSpriteFrame* FindFrame (const char* name)
  452. {
  453. iSpriteFrame* ifr = QUERY_INTERFACE_SAFE (scfParent->FindFrame (name), iSpriteFrame);
  454. if (ifr) ifr->DecRef ();
  455. return ifr;
  456. }
  457. virtual int GetNumFrames ()
  458. {
  459. return scfParent->GetNumFrames ();
  460. }
  461. virtual iSpriteFrame* GetFrame (int f)
  462. {
  463. iSpriteFrame* ifr = QUERY_INTERFACE_SAFE (scfParent->GetFrame (f), iSpriteFrame);
  464. if (ifr) ifr->DecRef ();
  465. return ifr;
  466. }
  467. virtual iSpriteAction* AddAction ()
  468. {
  469. iSpriteAction* ia = QUERY_INTERFACE_SAFE (scfParent->AddAction (), iSpriteAction);
  470. if (ia) ia->DecRef ();
  471. return ia;
  472. }
  473. virtual iSpriteAction* FindAction (const char* name)
  474. {
  475. iSpriteAction* ia = QUERY_INTERFACE_SAFE (scfParent->FindAction (name), iSpriteAction);
  476. if (ia) ia->DecRef ();
  477. return ia;
  478. }
  479. virtual iSpriteAction* GetFirstAction ()
  480. {
  481. iSpriteAction* ia = QUERY_INTERFACE_SAFE (scfParent->GetFirstAction (), iSpriteAction);
  482. if (ia) ia->DecRef ();
  483. return ia;
  484. }
  485. virtual int GetNumActions ()
  486. {
  487. return scfParent->GetNumActions ();
  488. }
  489. virtual iSpriteAction* GetAction (int No)
  490. {
  491. iSpriteAction* ia = QUERY_INTERFACE_SAFE (scfParent->GetAction (No), iSpriteAction);
  492. if (ia) ia->DecRef ();
  493. return ia;
  494. }
  495. virtual void EnableSkeletalAnimation ();
  496. virtual iSkeleton* GetSkeleton ();
  497. virtual void EnableTweening (bool en)
  498. {
  499. scfParent->EnableTweening (en);
  500. }
  501. virtual bool IsTweeningEnabled ()
  502. {
  503. return scfParent->IsTweeningEnabled ();
  504. }
  505. virtual void SetLightingQuality (int qual)
  506. {
  507. scfParent->SetLightingQuality (qual);
  508. }
  509. virtual int GetLightingQuality ()
  510. {
  511. return scfParent->GetLightingQuality ();
  512. }
  513. virtual void SetLightingQualityConfig (int qual)
  514. {
  515. scfParent->SetLightingQualityConfig (qual);
  516. }
  517. virtual int GetLightingQualityConfig ()
  518. {
  519. return scfParent->GetLightingQualityConfig ();
  520. }
  521. virtual float GetLodLevel ()
  522. {
  523. return scfParent->GetLodLevel ();
  524. }
  525. virtual void SetLodLevel (float level)
  526. {
  527. scfParent->SetLodLevel (level);
  528. }
  529. virtual void SetLodLevelConfig (int config_flag)
  530. {
  531. scfParent->SetLodLevelConfig (config_flag);
  532. }
  533. virtual int GetLodLevelConfig ()
  534. {
  535. return scfParent->GetLodLevelConfig ();
  536. }
  537. virtual void MergeNormals (int base, int frame)
  538. {
  539. scfParent->MergeNormals (base, frame);
  540. }
  541. virtual void MergeNormals (int base)
  542. {
  543. scfParent->MergeNormals (base);
  544. }
  545. virtual void MergeNormals ()
  546. {
  547. scfParent->MergeNormals ();
  548. }
  549. } scfiSprite3DFactoryState;
  550. };
  551. /**
  552. * A 3D sprite based on a triangle mesh with a single texture.
  553. * Animation is done with frames (a frame may be controlled by
  554. * a skeleton).
  555. */
  556. class csSprite3DMeshObject : public iMeshObject
  557. {
  558. private:
  559. /// Set the size of internally used tables
  560. static void UpdateWorkTables (int max_size);
  561. public:
  562. /**
  563. * Configuration value for global LOD. 0 is lowest detail, 1 is maximum.
  564. * If negative then the base mesh is used and no LOD reduction/computation
  565. * is done.
  566. */
  567. static float global_lod_level;
  568. private:
  569. /**
  570. * Used to determine where to look for the lod detail level.
  571. * The possible values are:
  572. * <ul>
  573. * <li>CS_SPR_LOD_GLOBAL (default)
  574. * <li>CS_SPR_LOD_TEMPLATE
  575. * <li>CS_SPR_LOD_LOCAL
  576. * </ul>
  577. */
  578. int lod_level_config;
  579. /**
  580. * Configuration value for an individuals LOD. 0 is lowest detail,
  581. * 1 is maximum. If negative then the base mesh is used and no LOD
  582. * reduction/computation is done.
  583. */
  584. float local_lod_level;
  585. /**
  586. * Quality setting for sprite lighting.
  587. * See the CS_SPR_LIGHTING_* macros defined in this header file for the
  588. * different types of lighting. This is the local setting. It overrides the
  589. * template, and global lighting settings.
  590. */
  591. int local_lighting_quality;
  592. /**
  593. * Used to determine where to look for the quality setting of the lighting.
  594. * The possible values are:
  595. * <ul>
  596. * <li>CS_SPR_LIGHT_GLOBAL (default)
  597. * <li>CS_SPR_LIGHT_TEMPLATE
  598. * <li>CS_SPR_LIGHT_LOCAL
  599. * </ul>
  600. */
  601. int lighting_quality_config;
  602. public:
  603. /**
  604. * Quality setting for sprite lighting.
  605. * See the CS_SPR_LIGHTING_* macros defined in this header file for the
  606. * different types of lighting. This is the global setting that is used for
  607. * all csSprite3Ds(unless the template or the individual sprite overrides
  608. * it).
  609. */
  610. static int global_lighting_quality;
  611. /**
  612. * Returns the lighting quality level used by this sprite.
  613. * See SPT_LIGHTING_* macros defined in this header for the different types
  614. * of lighting.
  615. */
  616. int GetLightingQuality ()
  617. {
  618. switch (lighting_quality_config)
  619. {
  620. case CS_SPR_LIGHT_GLOBAL: return global_lighting_quality; break;
  621. case CS_SPR_LIGHT_TEMPLATE: return factory->GetLightingQuality(); break;
  622. case CS_SPR_LIGHT_LOCAL: return local_lighting_quality; break;
  623. default:
  624. {
  625. lighting_quality_config = factory->GetLightingQualityConfig();
  626. return factory->GetLightingQuality();
  627. }
  628. }
  629. }
  630. /**
  631. * Sets the local lighting quality for this sprite. NOTE: you must use
  632. * SetLightingQualityConfig (CS_SPR_LIGHT_LOCAL) for the sprite to use this.
  633. */
  634. void SetLocalLightingQuality(int lighting_quality)
  635. { local_lighting_quality = lighting_quality; }
  636. /**
  637. * Sets the global lighting quality for all csSprite3Ds.
  638. * NOTE: You must use SetLightingQualityConfig(CS_SPR_LIGHT_GLOBAL) for the
  639. * sprite to use this.
  640. */
  641. void SetGlobalLightingQuality (int lighting_quality)
  642. { global_lighting_quality = lighting_quality; }
  643. /**
  644. * Sets which lighting config variable this sprite will use.
  645. * The options are:
  646. * <ul>
  647. * <li>CS_SPR_LIGHT_GLOBAL (default)
  648. * <li>CS_SPR_LIGHT_TEMPLATE
  649. * <li>CS_SPR_LIGHT_LOCAL
  650. * </ul>
  651. */
  652. void SetLightingQualityConfig (int config_flag)
  653. { lighting_quality_config = config_flag; }
  654. /**
  655. * Returns what this sprite is using for determining the lighting quality.
  656. */
  657. int GetLightingQualityConfig ()
  658. { return lighting_quality_config; }
  659. /**
  660. * Returns the lod level used by this sprite.
  661. */
  662. float GetLodLevel ()
  663. {
  664. switch (lod_level_config)
  665. {
  666. case CS_SPR_LOD_GLOBAL: return global_lod_level; break;
  667. case CS_SPR_LOD_TEMPLATE: return factory->GetLodLevel(); break;
  668. case CS_SPR_LOD_LOCAL: return local_lod_level; break;
  669. default:
  670. {
  671. lod_level_config = factory->GetLodLevelConfig();
  672. return factory->GetLodLevel();
  673. }
  674. }
  675. }
  676. /**
  677. * Sets the local lod level for this sprite. NOTE: you must use
  678. * SetLodLevelConfig (CS_SPR_LOD_LOCAL) for the sprite to use this.
  679. */
  680. void SetLocalLodLevel (float lod_level)
  681. { local_lod_level = lod_level; }
  682. /**
  683. * Sets the global lod level for all csSprite3Ds. NOTE: you must use
  684. * SetLodLevelConfig(CS_SPR_LOD_GLOBAL) for the sprite to use this.
  685. */
  686. void SetGlobalLodLevel (float lod_level)
  687. { global_lod_level = lod_level; }
  688. /**
  689. * Sets which lighting config variable this sprite will use.
  690. * The options are:
  691. * <ul>
  692. * <li>CS_SPR_LOD_GLOBAL (default)
  693. * <li>CS_SPR_LOD_TEMPLATE
  694. * <li>CS_SPR_LOD_LOCAL
  695. * </ul>
  696. */
  697. void SetLodLevelConfig (int config_flag)
  698. { lod_level_config = config_flag; }
  699. /**
  700. * Returns what this sprite is using for determining the lighting quality.
  701. */
  702. int GetLodLevelConfig ()
  703. { return lod_level_config; }
  704. /**
  705. * GetNumVertsToLight returns the number of vertices to light based on LOD.
  706. */
  707. int GetNumVertsToLight ();
  708. private:
  709. /**
  710. * num_verts_for_lod represents the number of lights that are used by lod.
  711. * If -1 means that it is not used.
  712. */
  713. int num_verts_for_lod;
  714. /**
  715. * A mesh which contains a number of triangles as generated
  716. * by the LOD algorithm. This is static since it will likely
  717. * change every frame anyway. We hold it static also since
  718. * we don't want to allocate it again every time.
  719. */
  720. static csTriangleMesh2 mesh;
  721. /// Mixmode for the triangles/polygons of the sprite.
  722. UInt MixMode;
  723. /**
  724. * Array of colors for the vertices. If not set then this
  725. * sprite does not have colored vertices.
  726. */
  727. csColor* vertex_colors;
  728. /// The parent.
  729. csSprite3DMeshObjectFactory* factory;
  730. /// The material handle as returned by iTextureManager.
  731. iMaterialWrapper* cstxt;
  732. /// The current frame number.
  733. int cur_frame;
  734. /// The current action.
  735. csSpriteAction2* cur_action;
  736. /// The last frame time action.
  737. cs_time last_time;
  738. /// Animation tweening ratio: next frame / this frame.
  739. float tween_ratio;
  740. /// Enable tweening.
  741. bool do_tweening;
  742. ///
  743. bool force_otherskin;
  744. /// Skeleton state (optional).
  745. csSkelState* skeleton_state;
  746. csMeshCallback* vis_cb;
  747. void* vis_cbData;
  748. /**
  749. * Camera space bounding box is cached here.
  750. * GetCameraBoundingBox() will check the current cookie from the
  751. * transformation manager to see if it needs to recalculate this.
  752. */
  753. csBox3 camera_bbox;
  754. /// Current cookie for camera_bbox.
  755. csTranCookie camera_cookie;
  756. // Remembered info between DrawTest and Draw.
  757. G3DTriangleMesh g3dmesh;
  758. bool initialized;
  759. /// Setup this object.
  760. void SetupObject ();
  761. private:
  762. /**
  763. * High quality version of UpdateLighting() which recalculates
  764. * the distance between the light and every vertex.
  765. * This version can use tweening of the normals and vertices
  766. */
  767. void UpdateLightingHQ (iLight** lights, int num_lights, iMovable* movable);
  768. /**
  769. * Low quality version of UpdateLighting() which only
  770. * calculates the distance once (from the center of the sprite.)
  771. * This method can use tweening of the normals.
  772. */
  773. void UpdateLightingLQ (iLight** lights, int num_lights, iMovable* movable);
  774. /**
  775. * Low quality Fast version of UpdateLighting() which only
  776. * calculates the distance once (from the center of the sprite.)
  777. * This version can NOT use any tweening.
  778. */
  779. void UpdateLightingFast (iLight** lights, int num_lights, iMovable* movable);
  780. /**
  781. * A fairly fast :P totally inaccurate(usually) lighting method.
  782. * Intended for use for things like powerups.
  783. */
  784. void UpdateLightingRandom ();
  785. /// random number generator used for random lighting.
  786. csRandomGen *rand_num;
  787. public:
  788. /// Constructor.
  789. csSprite3DMeshObject ();
  790. /// Destructor.
  791. virtual ~csSprite3DMeshObject ();
  792. /// Set the factory.
  793. void SetFactory (csSprite3DMeshObjectFactory* factory);
  794. /// Get the factory.
  795. csSprite3DMeshObjectFactory* GetFactory3D () { return factory; }
  796. /// Get the skeleton state for this sprite.
  797. csSkelState* GetSkeletonState () { return skeleton_state; }
  798. /// Force a new material skin other than default
  799. void SetMaterial (iMaterialWrapper *material);
  800. /// Get the material for this sprite.
  801. iMaterialWrapper* GetMaterial () { return cstxt; }
  802. /// Sets the mode that is used, when drawing that sprite.
  803. void SetMixmode (UInt m) { MixMode = m; }
  804. /// Gets the mode that is used, when drawing that sprite.
  805. UInt GetMixmode () { return MixMode; }
  806. /// Enable or disable tweening frames (default false).
  807. void EnableTweening (bool en) { do_tweening = en; }
  808. /// Is tweening enabled?
  809. bool IsTweeningEnabled () { return do_tweening; }
  810. /// Set color for all vertices
  811. void SetColor (const csColor& col);
  812. /// Add color to all vertices
  813. void AddColor (const csColor& col);
  814. /**
  815. * Set a color for a vertex.
  816. * As soon as you use this function this sprite will be rendered
  817. * using gouraud shading. Calling this function for the first time
  818. * will initialize all colors to black.
  819. */
  820. void SetVertexColor (int i, const csColor& col);
  821. /**
  822. * Add a color for a vertex.
  823. * As soon as you use this function this sprite will be rendered
  824. * using gouraud shading. Calling this function for the first time
  825. * will initialize all colors to black.
  826. */
  827. void AddVertexColor (int i, const csColor& col);
  828. /**
  829. * Reset the color list. If you call this function then the
  830. * sprite will no longer use gouraud shading.
  831. */
  832. void ResetVertexColors ();
  833. /**
  834. * Clamp all vertice colors to 2.0. This is called inside
  835. * csSprite3D::UpdateLighting() so that 3D renderer doesn't have
  836. * to deal with brightness lighter than 2.0
  837. */
  838. void FixVertexColors ();
  839. /// Unset the texture.
  840. void UnsetTexture ()
  841. { force_otherskin = false; }
  842. /**
  843. * Fill the static mesh with the current sprite
  844. * for a given LOD level.
  845. */
  846. void GenerateSpriteLOD (int num_vts);
  847. /**
  848. * Go to the next frame depending on the current time in milliseconds.
  849. */
  850. bool OldNextFrame (cs_time current_time, bool onestep = false,
  851. bool stoptoend = false);
  852. /**
  853. * Go to a specified frame.
  854. */
  855. void SetFrame (int f)
  856. {
  857. if (cur_action && f < cur_action->GetNumFrames ()) cur_frame = f;
  858. //@@@!!! WHAT DO WE DO HERE!!! UpdateInPolygonTrees ();
  859. }
  860. /**
  861. * Get the current frame number.
  862. */
  863. int GetCurFrame () { return cur_frame; }
  864. /**
  865. * Get the current frame number.
  866. */
  867. csSpriteAction2* GetCurAction () { return cur_action; }
  868. /**
  869. * Get the number of frames.
  870. */
  871. int GetNumFrames () { return cur_action->GetNumFrames (); }
  872. /**
  873. * Select an action.
  874. */
  875. bool SetAction (const char * name)
  876. {
  877. csSpriteAction2 *act;
  878. if ((act = factory->FindAction (name)) != NULL)
  879. {
  880. cur_action = act;
  881. SetFrame (0);
  882. return true;
  883. }
  884. return false;
  885. }
  886. /**
  887. * Initialize a sprite. This function is called automatically
  888. * from within 'load'. However you should call it directly
  889. * if you created the sprite on the fly (without 'load').
  890. */
  891. void InitSprite ();
  892. /**
  893. * Get an array of object vertices which is valid for the given frame.
  894. * This function correcty acounts for sprites which use skeletons. In
  895. * that case it will use the current transformation state of the skeleton
  896. * to compute object space vertices.<br>
  897. * Warning! The returned array should be used immediatelly or copied. It
  898. * points to a private static array in the sprite class and can be reused
  899. * if other calls to the sprite happen.
  900. */
  901. csVector3* GetObjectVerts (csSpriteFrame* fr);
  902. /// Get the bounding box in transformed space.
  903. void GetTransformedBoundingBox (iTransformationManager* tranman,
  904. const csReversibleTransform& trans, csBox3& cbox);
  905. /**
  906. * Get the coordinates of the sprite in screen coordinates.
  907. * Fills in the boundingBox with the X and Y locations of the cube.
  908. * Returns the max Z location of the sprite, or -1 if not
  909. * on-screen. If the sprite is not on-screen, the X and Y values are not
  910. * valid.
  911. */
  912. float GetScreenBoundingBox (iTransformationManager* tranman, float fov,
  913. float sx, float sy,
  914. const csReversibleTransform& trans, csBox2& sbox, csBox3& cbox);
  915. ///------------------------ iMeshObject implementation ------------------------
  916. DECLARE_IBASE;
  917. virtual iMeshObjectFactory* GetFactory ()
  918. {
  919. iMeshObjectFactory* ifact = QUERY_INTERFACE (factory, iMeshObjectFactory);
  920. ifact->DecRef ();
  921. return ifact;
  922. }
  923. virtual bool DrawTest (iRenderView* rview, iMovable* movable);
  924. virtual void UpdateLighting (iLight** lights, int num_lights,
  925. iMovable* movable);
  926. virtual bool Draw (iRenderView* rview, iMovable* movable);
  927. virtual void SetVisibleCallback (csMeshCallback* cb, void* cbData)
  928. {
  929. vis_cb = cb;
  930. vis_cbData = cbData;
  931. }
  932. virtual csMeshCallback* GetVisibleCallback ()
  933. {
  934. return vis_cb;
  935. }
  936. virtual void GetObjectBoundingBox (csBox3& bbox, bool accurate = false);
  937. virtual csVector3 GetRadius ();
  938. virtual void NextFrame (cs_time current_time)
  939. {
  940. OldNextFrame (current_time);
  941. }
  942. virtual bool WantToDie () { return false; }
  943. virtual void HardTransform (const csReversibleTransform&) { }
  944. virtual bool SupportsHardTransform () { return false; }
  945. virtual bool HitBeamObject (const csVector3& start, const csVector3& end,
  946. csVector3& isect, float* pr);
  947. //------------------ iPolygonMesh interface implementation ----------------//
  948. struct PolyMesh : public iPolygonMesh
  949. {
  950. DECLARE_EMBEDDED_IBASE (csSprite3DMeshObject);
  951. /// Get the number of vertices for this mesh.
  952. virtual int GetNumVertices ()
  953. {
  954. csSprite3DMeshObjectFactory* fact = scfParent->GetFactory3D ();
  955. return fact->GetNumVertices ();
  956. }
  957. /// Get the pointer to the array of vertices.
  958. virtual csVector3* GetVertices ()
  959. {
  960. csSprite3DMeshObjectFactory* fact = scfParent->GetFactory3D ();
  961. return fact->GetVertices (0);
  962. }
  963. /// Get the number of polygons for this mesh.
  964. virtual int GetNumPolygons ()
  965. {
  966. csSprite3DMeshObjectFactory* fact = scfParent->GetFactory3D ();
  967. return fact->GetNumTriangles ();
  968. }
  969. /// Get the pointer to the array of polygons.
  970. virtual csMeshedPolygon* GetPolygons ();
  971. PolyMesh ()
  972. {
  973. polygons = NULL;
  974. }
  975. virtual ~PolyMesh ()
  976. {
  977. delete[] polygons;
  978. }
  979. csMeshedPolygon* polygons;
  980. } scfiPolygonMesh;
  981. friend struct PolyMesh;
  982. //--------------------- iSprite3DState implementation -------------//
  983. struct Sprite3DState : public iSprite3DState
  984. {
  985. DECLARE_EMBEDDED_IBASE (csSprite3DMeshObject);
  986. virtual void SetMaterialWrapper (iMaterialWrapper* material)
  987. {
  988. scfParent->SetMaterial (material);
  989. }
  990. virtual iMaterialWrapper* GetMaterialWrapper ()
  991. {
  992. return scfParent->GetMaterial ();
  993. }
  994. virtual void SetMixMode (UInt mode)
  995. {
  996. scfParent->SetMixmode (mode);
  997. }
  998. virtual UInt GetMixMode ()
  999. {
  1000. return scfParent->GetMixmode ();
  1001. }
  1002. virtual iSkeletonState* GetSkeletonState ();
  1003. virtual void SetFrame (int f)
  1004. {
  1005. scfParent->SetFrame (f);
  1006. }
  1007. virtual int GetCurFrame ()
  1008. {
  1009. return scfParent->GetCurFrame ();
  1010. }
  1011. virtual int GetNumFrames ()
  1012. {
  1013. return scfParent->GetNumFrames ();
  1014. }
  1015. virtual bool SetAction (const char * name)
  1016. {
  1017. return scfParent->SetAction (name);
  1018. }
  1019. virtual iSpriteAction* GetCurAction ()
  1020. {
  1021. iSpriteAction* ia = QUERY_INTERFACE_SAFE (scfParent->GetCurAction (), iSpriteAction);
  1022. if (ia) ia->DecRef ();
  1023. return ia;
  1024. }
  1025. virtual void EnableTweening (bool en)
  1026. {
  1027. scfParent->EnableTweening (en);
  1028. }
  1029. virtual bool IsTweeningEnabled ()
  1030. {
  1031. return scfParent->IsTweeningEnabled ();
  1032. }
  1033. virtual void UnsetTexture ()
  1034. {
  1035. scfParent->UnsetTexture ();
  1036. }
  1037. virtual int GetLightingQuality ()
  1038. {
  1039. return scfParent->GetLightingQuality ();
  1040. }
  1041. virtual void SetLocalLightingQuality (int lighting_quality)
  1042. {
  1043. scfParent->SetLocalLightingQuality (lighting_quality);
  1044. }
  1045. virtual void SetLightingQualityConfig (int config_flag)
  1046. {
  1047. scfParent->SetLightingQualityConfig (config_flag);
  1048. }
  1049. virtual int GetLightingQualityConfig ()
  1050. {
  1051. return scfParent->GetLightingQualityConfig ();
  1052. }
  1053. virtual float GetLodLevel ()
  1054. {
  1055. return scfParent->GetLodLevel ();
  1056. }
  1057. virtual void SetLocalLodLevel (float lod_level)
  1058. {
  1059. scfParent->SetLocalLodLevel (lod_level);
  1060. }
  1061. virtual void SetLodLevelConfig (int config_flag)
  1062. {
  1063. scfParent->SetLodLevelConfig (config_flag);
  1064. }
  1065. virtual int GetLodLevelConfig ()
  1066. {
  1067. return scfParent->GetLodLevelConfig ();
  1068. }
  1069. virtual bool IsLodEnabled ()
  1070. {
  1071. return scfParent->GetLodLevel () >= 0;
  1072. }
  1073. } scfiSprite3DState;
  1074. };
  1075. /**
  1076. * Sprite 3D type. This is the plugin you have to use to create instances
  1077. * of csSprite3DMeshObjectFactory.
  1078. */
  1079. class csSprite3DMeshObjectType : public iMeshObjectType
  1080. {
  1081. private:
  1082. iSystem* System;
  1083. public:
  1084. /// Constructor.
  1085. csSprite3DMeshObjectType (iBase*);
  1086. /// Destructor.
  1087. virtual ~csSprite3DMeshObjectType ();
  1088. /// Register plugin with the system driver
  1089. virtual bool Initialize (iSystem *pSystem);
  1090. //------------------------ iMeshObjectType implementation --------------
  1091. DECLARE_IBASE;
  1092. /// New Factory.
  1093. virtual iMeshObjectFactory* NewFactory ();
  1094. ///------------------- iConfig interface implementation -------------------
  1095. struct csSprite3DConfig : public iConfig
  1096. {
  1097. DECLARE_EMBEDDED_IBASE (csSprite3DMeshObjectType);
  1098. virtual bool GetOptionDescription (int idx, csOptionDescription *option);
  1099. virtual bool SetOption (int id, csVariant* value);
  1100. virtual bool GetOption (int id, csVariant* value);
  1101. } scfiConfig;
  1102. friend struct csSprite3DConfig;
  1103. };
  1104. #endif // __CS_SPR3D_H__