PageRenderTime 83ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/CS/migrated/branches/R0_17/include/csengine/cssprite.h

#
C++ Header | 891 lines | 325 code | 151 blank | 415 comment | 4 complexity | dcf551069138ec22798426be7314673b 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 CSSPRITE_H
  16. #define CSSPRITE_H
  17. #include "csutil/cscolor.h"
  18. #include "csgeom/math3d.h"
  19. #include "csgeom/math2d.h"
  20. #include "csgeom/poly2d.h"
  21. #include "csgeom/poly3d.h"
  22. #include "csgeom/box.h"
  23. #include "csobject/nobjvec.h"
  24. #include "csengine/polyint.h"
  25. #include "csengine/bspbbox.h"
  26. #include "csengine/rview.h"
  27. #include "csengine/material.h"
  28. #include "csengine/tranman.h"
  29. #include "csengine/triangle.h"
  30. #include "csengine/movable.h"
  31. #include "igraph3d.h"
  32. #include "iparticl.h"
  33. #include "ipolmesh.h"
  34. class Dumper;
  35. class csMaterialList;
  36. class csMaterialWrapper;
  37. class csLightHitsSprite;
  38. class csSkeleton;
  39. class csSkeletonState;
  40. class csSprite3D;
  41. class csBspContainer;
  42. struct iTextureHandle;
  43. /**
  44. * A frame for 3D sprite animation.
  45. */
  46. class csFrame : public csBase
  47. {
  48. private:
  49. int animation_index;
  50. int texturing_index;
  51. char* name;
  52. /// If true then normals are already calculated for this frame.
  53. bool normals_calculated;
  54. /// Bounding box in object space for this frame.
  55. csBox3 box;
  56. public:
  57. ///
  58. csFrame (int anm_idx, int tex_idx);
  59. ///
  60. virtual ~csFrame ();
  61. ///
  62. int GetAnmIndex () { return animation_index; }
  63. ///
  64. int GetTexIndex () { return texturing_index; }
  65. ///
  66. void SetTexIndex (int tex_idx) { texturing_index = tex_idx; }
  67. /// Return true if normals are already calculated.
  68. bool NormalsCalculated () { return normals_calculated; }
  69. /// Set normals calculated to value.
  70. void SetNormalsCalculated (bool n) { normals_calculated = n; }
  71. ///
  72. void SetName (char * n);
  73. ///
  74. char* GetName () { return name; }
  75. /**
  76. * Compute the object space bounding box for this frame.
  77. * This has to be called after setting up the frame and before
  78. * using it.
  79. */
  80. void SetBoundingBox (csBox3& b) { box = b; }
  81. /**
  82. * Get the bounding box in object space.
  83. */
  84. void GetBoundingBox (csBox3& b) { b = box; }
  85. };
  86. /**
  87. * A Action frameset for a 3D sprite animation.
  88. */
  89. class csSpriteAction : public csBase
  90. {
  91. public:
  92. /// Initialize a action object
  93. csSpriteAction ();
  94. /// Destroy this action object
  95. virtual ~csSpriteAction ();
  96. /// Add a frame to this action
  97. void AddFrame (csFrame * frame, int delay);
  98. /// Set action name
  99. void SetName (char *n);
  100. /// Get action name
  101. char * GetName ()
  102. { return name; }
  103. /// Get total number of frames in this action
  104. int GetNumFrames ()
  105. { return frames.Length (); }
  106. /// Query the frame number f
  107. csFrame* GetFrame (int f)
  108. { return (f < frames.Length ()) ? (csFrame *)frames [f] : (csFrame*)NULL; }
  109. /// Get delay for frame number f
  110. int GetFrameDelay (int f)
  111. { return (int)delays [f]; }
  112. private:
  113. char *name;
  114. csVector frames;
  115. csVector delays;
  116. };
  117. /**
  118. * A 3D sprite based on a triangle mesh with a single texture.
  119. * Animation is done with frames.
  120. * This class represents a template from which a csSprite3D
  121. * class can be made.
  122. */
  123. class csSpriteTemplate : public csObject
  124. {
  125. friend class Dumper;
  126. private:
  127. friend class csSprite3D;
  128. /// Material handle as returned by iTextureManager.
  129. csMaterialWrapper* cstxt;
  130. /// An optional skeleton.
  131. csSkeleton* skeleton;
  132. /**
  133. * The order in which to introduce levels in order to get to a higher LOD.
  134. * The index of this array is the vertex number which is introduced.
  135. * The vertices of this template were reordered (by GenerateLOD()) so that
  136. * the first vertices are used in low-detail. The contents of this array
  137. * is the vertex number to emerge from.
  138. */
  139. int* emerge_from;
  140. /// The frames
  141. csNamedObjVector frames;
  142. /// The actions (a vector of csSpriteAction objects)
  143. csNamedObjVector actions;
  144. /// Enable tweening.
  145. bool do_tweening;
  146. /// The base mesh is also the texture alignment mesh.
  147. csTriangleMesh* texel_mesh;
  148. /// The array of texels
  149. DECLARE_TYPED_VECTOR (csTexelsVector,csPoly2D) texels;
  150. /// The vertices
  151. DECLARE_TYPED_VECTOR (csVerticesVector,csPoly3D) vertices;
  152. /// The normals
  153. DECLARE_TYPED_VECTOR (csNormalsVector,csPoly3D) normals;
  154. /**
  155. * Connectivity information for this sprite template.
  156. * Also contains temporary vertex position information
  157. * for one sprite (@@@this should be avoided!!!!)
  158. */
  159. csTriangleVertices* tri_verts;
  160. public:
  161. /// Create the sprite template
  162. csSpriteTemplate ();
  163. /// Destroy the template
  164. virtual ~csSpriteTemplate ();
  165. /**
  166. * Create a new sprite for this template.
  167. * The 'default' action will be made default. If there is
  168. * no default action the first action will be made default.
  169. * The sprite will also be initialized (csSprite3D::InitSprite()).
  170. */
  171. csSprite3D* NewSprite (csObject* parent);
  172. /// Set the skeleton for this sprite template.
  173. void SetSkeleton (csSkeleton* sk);
  174. /// Get the skeleton for this sprite template.
  175. csSkeleton* GetSkeleton () { return skeleton; }
  176. /// Get the 'emerge_from' array from which you can construct triangles.
  177. int* GetEmergeFrom () { return emerge_from; }
  178. /// Enable or disable tweening frames (default false).
  179. void EnableTweening (bool en) { do_tweening = en; }
  180. /// Is tweening enabled?
  181. bool IsTweeningEnabled () { return do_tweening; }
  182. /**
  183. * Generate the collapse order.
  184. * This function will also reorder all the vertices in the template.
  185. * So be careful!
  186. */
  187. void GenerateLOD ();
  188. /**
  189. * Compute the object space bounding box for all frames in this
  190. * template. This has to be called after setting up the template and before
  191. * using it.
  192. */
  193. void ComputeBoundingBox ();
  194. ///
  195. csTriangleMesh* GetTexelMesh () {return texel_mesh;}
  196. /// Add some vertices, normals, and texels
  197. void AddVertices (int num);
  198. /// Add a vertex, normal, and texel
  199. void AddVertex () { AddVertices (1); }
  200. /// Query the number of texels.
  201. int GetNumTexels () { return texels.Get(0)->GetNumVertices (); }
  202. /// Get a texel.
  203. csVector2& GetTexel (int frame, int vertex)
  204. { return (*texels.Get(frame)) [vertex]; }
  205. /// Get array of texels.
  206. csVector2* GetTexels (int frame)
  207. { return (*texels.Get(frame)).GetVertices (); }
  208. /// Query the number of vertices.
  209. int GetNumVertices () { return vertices.Get (0)->GetNumVertices (); }
  210. /// Get a vertex.
  211. csVector3& GetVertex (int frame, int vertex)
  212. { return (*vertices.Get(frame)) [vertex]; }
  213. /// Get vertex array.
  214. csVector3* GetVertices (int frame)
  215. { return (*vertices.Get(frame)).GetVertices (); }
  216. /// Query the number of normals.
  217. int GetNumNormals () { return normals.Get (0)->GetNumVertices (); }
  218. /// Get a normal.
  219. csVector3& GetNormal (int frame, int vertex)
  220. { return (*normals.Get(frame)) [vertex]; }
  221. /// Get normal array.
  222. csVector3* GetNormals (int frame)
  223. { return (*normals.Get(frame)).GetVertices (); }
  224. /**
  225. * Add a triangle to the normal, texel, and vertex meshes
  226. * a, b and c are indices to texel vertices
  227. */
  228. void AddTriangle (int a, int b, int c);
  229. /// returns the texel indices for triangle 'x'
  230. csTriangle GetTriangle (int x) { return texel_mesh->GetTriangle(x); }
  231. /// returns the triangles of the texel_mesh
  232. csTriangle* GetTriangles () { return texel_mesh->GetTriangles(); }
  233. /// returns the number of triangles in the sprite
  234. int GetNumTriangles () { return texel_mesh->GetNumTriangles(); }
  235. /// Create and add a new frame to the sprite.
  236. csFrame* AddFrame ();
  237. /// find a named frame into the sprite.
  238. csFrame* FindFrame (char * name);
  239. /// Query the number of frames
  240. int GetNumFrames () { return frames.Length (); }
  241. /// Query the frame number f
  242. csFrame* GetFrame (int f)
  243. { return (f < frames.Length ()) ? (csFrame *)frames [f] : (csFrame*)NULL; }
  244. /// Create and add a new action frameset to the sprite.
  245. csSpriteAction* AddAction ();
  246. /// find a named action into the sprite.
  247. csSpriteAction* FindAction (const char * name);
  248. /// Get the first action.
  249. csSpriteAction* GetFirstAction ()
  250. { return (csSpriteAction *)actions [0]; }
  251. /// Get number of actions in sprite
  252. int GetNumActions ()
  253. { return actions.Length (); }
  254. /// Get action number No
  255. csSpriteAction* GetAction (int No)
  256. { return (csSpriteAction *)actions [No]; }
  257. /// Get the material
  258. csMaterialWrapper* GetMaterial () const { return cstxt; }
  259. /// Get the material handle.
  260. iMaterialHandle* GetMaterialHandle () const { return cstxt->GetMaterialHandle (); }
  261. /// Set the material used for this sprite
  262. void SetMaterial (csMaterialWrapper *material);
  263. /**
  264. * Compute all normals in a frame.
  265. */
  266. void ComputeNormals (csFrame* frame, csVector3* object_verts);
  267. CSOBJTYPE;
  268. };
  269. /// A callback function for csSprite3D::Draw().
  270. typedef void (csSpriteCallback) (csSprite3D* spr, csRenderView* rview);
  271. /// A callback function for csSprite3D::Draw().
  272. typedef void (csSpriteCallback2) (csSprite3D* spr, csRenderView* rview, csObject *callbackData);
  273. /**
  274. * The base class for all types of sprites.
  275. */
  276. class csSprite : public csObject, public iBase
  277. {
  278. friend class Dumper;
  279. friend class csMovable;
  280. protected:
  281. /// Points to Actor class which "owns" this sprite.
  282. csObject* myOwner;
  283. /**
  284. * Points to the parent container object of this sprite.
  285. * This is usually csWorld or csParticleSystem.
  286. */
  287. csObject* parent;
  288. /**
  289. * Camera space bounding box is cached here.
  290. * GetCameraBoundingBox() will check the current cookie from the
  291. * transformation manager to see if it needs to recalculate this.
  292. */
  293. csBox3 camera_bbox;
  294. /// Current cookie for camera_bbox.
  295. csTranCookie camera_cookie;
  296. /// Mixmode for the triangles/polygons of the sprite.
  297. UInt MixMode;
  298. /**
  299. * List of light-hits-sprites for this sprite.
  300. */
  301. csLightHitsSprite* dynamiclights;
  302. /// Defered lighting. If > 0 then we have defered lighting.
  303. int defered_num_lights;
  304. /// Flags to use for defered lighting.
  305. int defered_lighting_flags;
  306. /// The callback which is called just before drawing.
  307. csSpriteCallback* draw_callback;
  308. /// This callback is only called if the sprite is actually drawn.
  309. csSpriteCallback2* draw_callback2;
  310. /**
  311. * Flag which is set to true when the sprite is visible.
  312. * This is used by the c-buffer/bsp routines. The sprite itself
  313. * will not use this flag in any way at all. It is simply intended
  314. * for external visibility culling routines.
  315. */
  316. bool is_visible;
  317. /**
  318. * Pointer to the object to place in the polygon tree.
  319. */
  320. csPolyTreeObject* ptree_obj;
  321. /**
  322. * Update this sprite in the polygon trees.
  323. */
  324. virtual void UpdateInPolygonTrees () = 0;
  325. /// Update defered lighting.
  326. void UpdateDeferedLighting (const csVector3& pos);
  327. protected:
  328. /**
  329. * Position in the world.
  330. */
  331. csMovable movable;
  332. /// Move this sprite to the specified sector. Can be called multiple times.
  333. virtual void MoveToSector (csSector* s);
  334. /// Remove this sprite from all sectors it is in (but not from the world).
  335. virtual void RemoveFromSectors ();
  336. /**
  337. * Update transformations after the sprite has moved
  338. * (through updating the movable instance).
  339. * This MUST be done after you change the movable otherwise
  340. * some of the internal data structures will not be updated
  341. * correctly. This function is called by movable.UpdateMove();
  342. */
  343. virtual void UpdateMove ();
  344. public:
  345. /// Constructor.
  346. csSprite (csObject* theParent);
  347. /// Destructor.
  348. virtual ~csSprite ();
  349. /// Set owner (actor) for this sprite.
  350. void SetMyOwner (csObject* newOwner) { myOwner = newOwner; }
  351. /// Get owner (actor) for this sprite.
  352. csObject* GetMyOwner () { return myOwner; }
  353. /// Set parent container for this sprite.
  354. void SetParentContainer (csObject* newParent) { parent = newParent; }
  355. /// Get parent container for this sprite.
  356. csObject* GetParentContainer () { return parent; }
  357. /// Get the pointer to the object to place in the polygon tree.
  358. csPolyTreeObject* GetPolyTreeObject ()
  359. {
  360. return ptree_obj;
  361. }
  362. /**
  363. * Do some initialization needed for visibility testing.
  364. * i.e. clear camera transformation.
  365. */
  366. virtual void VisTestReset () { }
  367. /// Mark this sprite as visible.
  368. void MarkVisible () { is_visible = true; }
  369. /// Mark this sprite as invisible.
  370. void MarkInvisible () { is_visible = false; }
  371. /// Return if this sprite is visible.
  372. bool IsVisible () { return is_visible; }
  373. /**
  374. * Update lighting as soon as the sprite becomes visible.
  375. * This will call world->GetNearestLights with the supplied
  376. * parameters.
  377. */
  378. virtual void DeferUpdateLighting (int flags, int num_lights);
  379. /// Sets the mode that is used, when drawing that sprite.
  380. virtual void SetMixmode (UInt m) { MixMode = m; }
  381. /// Gets the mode that is used, when drawing that sprite.
  382. virtual UInt GetMixmode () { return MixMode; }
  383. /**
  384. * Set a callback which is called just before the sprite is drawn.
  385. * This is useful to do some expensive computations which only need
  386. * to be done on a visible sprite.
  387. */
  388. void SetDrawCallback (csSpriteCallback* callback) { draw_callback = callback; }
  389. /**
  390. * Set a callback which is called only if the sprite is actually drawn.
  391. */
  392. void SetDrawCallback2 (csSpriteCallback2* callback) { draw_callback2 = callback; }
  393. /**
  394. * Get the draw callback. If there are multiple draw callbacks you can
  395. * use this function to chain.
  396. */
  397. csSpriteCallback* GetDrawCallback () { return draw_callback; }
  398. /**
  399. * Get the draw callback. If there are multiple draw callbacks you can
  400. * use this function to chain.
  401. */
  402. csSpriteCallback2* GetDrawCallback2 () { return draw_callback2; }
  403. /**
  404. * Unlink a light-hits-sprite from the list.
  405. * Warning! This function does not test if it
  406. * is really on the list!
  407. */
  408. void UnlinkDynamicLight (csLightHitsSprite* lp);
  409. /**
  410. * Add a light-hits-sprite to the list.
  411. */
  412. void AddDynamicLight (csLightHitsSprite *lp);
  413. /**
  414. * Get the list of dynamic lights that hit this sprite.
  415. */
  416. csLightHitsSprite* GetDynamicLights () { return dynamiclights; }
  417. /**
  418. * Light sprite according to the given array of lights (i.e.
  419. * fill the vertex color array).
  420. * No shadow calculation will be done. This is assumed to have
  421. * been done earlier. This is a primitive lighting process
  422. * based on the lights which hit one point of the sprite (usually
  423. * the center). More elaborate lighting systems are possible
  424. * but this will do for now.
  425. */
  426. virtual void UpdateLighting (csLight** lights, int num_lights) = 0;
  427. /**
  428. * Draw this sprite given a camera transformation.
  429. * If needed the skeleton state will first be updated.
  430. * Optionally update lighting if needed (DeferUpdateLighting()).
  431. */
  432. virtual void Draw (csRenderView& rview) = 0;
  433. /**
  434. * Get the movable instance for this sprite.
  435. * It is very important to call GetMovable().UpdateMove()
  436. * after doing any kind of modification to this movable
  437. * to make sure that internal data structures are
  438. * correctly updated.
  439. */
  440. csMovable& GetMovable () { return movable; }
  441. /// Set the color of this sprite.
  442. virtual void SetColor (const csColor&) = 0;
  443. /// Add color to the color of the sprite.
  444. virtual void AddColor (const csColor& col) = 0;
  445. /// Scale sprite by this factor.
  446. virtual void ScaleBy (float factor) = 0;
  447. /// Rotate sprite in some manner (as defined by subclass), in radians.
  448. virtual void Rotate (float angle) = 0;
  449. //------------------------- iParticle implementation -----------------------//
  450. struct Particle : public iParticle
  451. {
  452. DECLARE_EMBEDDED_IBASE (csSprite);
  453. virtual void MoveToSector(csSector*);
  454. virtual void SetPosition(const csVector3&);
  455. virtual void MovePosition(const csVector3&);
  456. virtual void SetColor(const csColor&);
  457. virtual void AddColor(const csColor&);
  458. virtual void ScaleBy(float factor);
  459. virtual void SetMixmode(UInt mode);
  460. virtual void Rotate(float angle);
  461. virtual void Draw(csRenderView&);
  462. virtual void UpdateLighting(csLight**, int num_lights);
  463. virtual void DeferUpdateLighting(int flags, int num_lights);
  464. } scfiParticle;
  465. DECLARE_IBASE;
  466. CSOBJTYPE;
  467. };
  468. /**
  469. * A 3D sprite based on a triangle mesh with a single texture.
  470. * Animation is done with frames (a frame may be controlled by
  471. * a skeleton).
  472. */
  473. class csSprite3D : public csSprite
  474. {
  475. friend class Dumper;
  476. private:
  477. /// Set the size of internally used tables
  478. static void UpdateWorkTables (int max_size);
  479. public:
  480. /**
  481. * Configuration value for global LOD. 0 is lowest detail, 1 is maximum.
  482. * If negative then the base mesh is used and no LOD reduction/computation
  483. * is done.
  484. */
  485. static float cfg_lod_detail;
  486. /**
  487. * Quality setting for sprite lighting. If true this uses
  488. * high quality lighting which is more accurate on the vertices.
  489. * Otherwise an approximation is used. This is a lot faster though.
  490. */
  491. static bool do_quality_lighting;
  492. private:
  493. /**
  494. * A mesh which contains a number of triangles as generated
  495. * by the LOD algorithm. This is static since it will likely
  496. * change every frame anyway. We hold it static also since
  497. * we don't want to allocate it again every time.
  498. */
  499. static csTriangleMesh mesh;
  500. /**
  501. * Array of colors for the vertices. If not set then this
  502. * sprite does not have colored vertices.
  503. */
  504. csColor* vertex_colors;
  505. /// The template.
  506. csSpriteTemplate* tpl;
  507. /// The material handle as returned by iTextureManager.
  508. csMaterialWrapper* cstxt;
  509. /// The current frame number.
  510. int cur_frame;
  511. /// The current action.
  512. csSpriteAction* cur_action;
  513. /// The last frame time action.
  514. cs_time last_time;
  515. /// Animation tweening ratio: next frame / this frame.
  516. float tween_ratio;
  517. /// Enable tweening.
  518. bool do_tweening;
  519. ///
  520. bool force_otherskin;
  521. /// Skeleton state (optional).
  522. csSkeletonState* skeleton_state;
  523. /// Bounding box for polygon trees.
  524. csPolyTreeBBox bbox;
  525. private:
  526. /**
  527. * High quality version of UpdateLighting() which recalculates
  528. * the distance between the light and every vertex.
  529. */
  530. void UpdateLightingHQ (csLight** lights, int num_lights, csVector3* object_vertices);
  531. /**
  532. * Low quality version of UpdateLighting() which only
  533. * calculates the distance once (with the center of the sprite).
  534. */
  535. void UpdateLightingLQ (csLight** lights, int num_lights, csVector3* object_vertices);
  536. protected:
  537. /**
  538. * Update this sprite in the polygon trees.
  539. */
  540. virtual void UpdateInPolygonTrees ();
  541. public:
  542. ///
  543. csSprite3D (csObject* theParent);
  544. ///
  545. virtual ~csSprite3D ();
  546. ///
  547. void SetTemplate (csSpriteTemplate* tmpl);
  548. ///
  549. csSpriteTemplate* GetTemplate () { return tpl; }
  550. /// Get the skeleton state for this sprite.
  551. csSkeletonState* GetSkeletonState () { return skeleton_state; }
  552. /// force a new material skin other than default
  553. void SetMaterial (csMaterialWrapper *material);
  554. /// Enable or disable tweening frames (default false).
  555. void EnableTweening (bool en) { do_tweening = en; }
  556. /// Is tweening enabled?
  557. bool IsTweeningEnabled () { return do_tweening; }
  558. /// Scale the sprite by scaling the diagonal of the transform
  559. virtual void ScaleBy (float factor);
  560. /**
  561. * Rotate the sprite in some way, angle in radians.
  562. * currently first z-rotates angle then x-rotates angle.
  563. */
  564. virtual void Rotate (float angle);
  565. /// Set color for all vertices
  566. virtual void SetColor (const csColor& col);
  567. /// Add color to all vertices
  568. virtual void AddColor (const csColor& col);
  569. /**
  570. * Set a color for a vertex.
  571. * As soon as you use this function this sprite will be rendered
  572. * using gouraud shading. Calling this function for the first time
  573. * will initialize all colors to black.
  574. */
  575. void SetVertexColor (int i, const csColor& col);
  576. /**
  577. * Add a color for a vertex.
  578. * As soon as you use this function this sprite will be rendered
  579. * using gouraud shading. Calling this function for the first time
  580. * will initialize all colors to black.
  581. */
  582. void AddVertexColor (int i, const csColor& col);
  583. /**
  584. * Reset the color list. If you call this function then the
  585. * sprite will no longer use gouraud shading.
  586. */
  587. void ResetVertexColors ();
  588. /**
  589. * Clamp all vertice colors to 2.0. This is called inside
  590. * csSprite3D::UpdateLighting() so that 3D renderer doesn't have
  591. * to deal with brightness lighter than 2.0
  592. */
  593. void FixVertexColors ();
  594. /**
  595. * Light sprite according to the given array of lights (i.e.
  596. * fill the vertex color array).
  597. */
  598. virtual void UpdateLighting (csLight** lights, int num_lights);
  599. ///
  600. void UnsetTexture ()
  601. { force_otherskin = false; }
  602. /**
  603. * Fill the static mesh with the current sprite
  604. * for a given LOD level.
  605. */
  606. void GenerateSpriteLOD (int num_vts);
  607. /**
  608. * Do some initialization needed for visibility testing.
  609. * i.e. clear camera transformation.
  610. */
  611. virtual void VisTestReset ()
  612. {
  613. bbox.ClearTransform ();
  614. }
  615. /**
  616. * Get a bounding box in object space.
  617. */
  618. void GetObjectBoundingBox (csBox3& box);
  619. /**
  620. * Get a radius in object space.
  621. */
  622. csVector3 GetRadius ();
  623. /**
  624. * Get a 3D bounding box in camera space. This function is smart.
  625. * It will only recompute this information if needed. So if you call
  626. * this function several times in the same frame it will not recompute
  627. * the bounding box.
  628. */
  629. void GetCameraBoundingBox (const csCamera& camtrans, csBox3& boundingBox);
  630. /**
  631. * Get the coordinates of the sprite in screen coordinates.
  632. * Fills in the boundingBox with the X and Y locations
  633. * of the sprite. Returns the max Z location of the sprite,
  634. * or -1 if the sprite is not on-screen.
  635. * If the sprite is not on-screen, the X and Y values are
  636. * not valid.
  637. */
  638. float GetScreenBoundingBox (const csCamera& camtrans, csBox2& boundingBox);
  639. /**
  640. * Draw this sprite given a camera transformation.
  641. * If needed the skeleton state will first be updated.
  642. * Optionally update lighting if needed (DeferUpdateLighting()).
  643. */
  644. virtual void Draw (csRenderView& rview);
  645. /**
  646. * Go to the next frame depending on the current time in milliseconds.
  647. */
  648. bool NextFrame (cs_time current_time, bool onestep = false, bool stoptoend = false);
  649. /**
  650. * Go to a specified frame.
  651. */
  652. void SetFrame (int f)
  653. {
  654. if (cur_action && f < cur_action->GetNumFrames ()) cur_frame = f;
  655. UpdateInPolygonTrees ();
  656. }
  657. /**
  658. * Get the current frame number.
  659. */
  660. int GetCurFrame () { return cur_frame; }
  661. /**
  662. * Get the current frame number.
  663. */
  664. csSpriteAction* GetCurAction () { return cur_action; }
  665. /**
  666. * Get the number of frames.
  667. */
  668. int GetNumFrames () { return cur_action->GetNumFrames (); }
  669. /**
  670. * Select an action.
  671. */
  672. bool SetAction (const char * name)
  673. {
  674. csSpriteAction *act;
  675. if ((act = tpl->FindAction (name)) != NULL)
  676. {
  677. SetFrame (0);
  678. cur_action = act;
  679. return true;
  680. }
  681. return false;
  682. }
  683. /**
  684. * Initialize a sprite. This function is called automatically
  685. * from within 'load'. However you should call it directly
  686. * if you created the sprite on the fly (without 'load').
  687. */
  688. void InitSprite ();
  689. /**
  690. * Get an array of object vertices which is valid for the given frame.
  691. * This function correcty acounts for sprites which use skeletons. In
  692. * that case it will use the current transformation state of the skeleton
  693. * to compute object space vertices.<br>
  694. * Warning! The returned array should be used immediatelly or copied. It
  695. * points to a private static array in the sprite class and can be reused
  696. * if other calls to the sprite happen.
  697. */
  698. csVector3* GetObjectVerts (csFrame* fr);
  699. CSOBJTYPE;
  700. //-------------------- iPolygonMesh interface implementation ------------------
  701. DECLARE_IBASE_EXT (csSprite);
  702. struct PolyMesh : public iPolygonMesh
  703. {
  704. DECLARE_EMBEDDED_IBASE (csSprite3D);
  705. /// Get the number of vertices for this mesh.
  706. virtual int GetNumVertices ()
  707. {
  708. csSpriteTemplate* tmpl = scfParent->GetTemplate ();
  709. return tmpl->GetNumVertices ();
  710. }
  711. /// Get the pointer to the array of vertices.
  712. virtual csVector3* GetVertices ()
  713. {
  714. csSpriteTemplate* tmpl = scfParent->GetTemplate ();
  715. return tmpl->GetVertices (0);
  716. }
  717. /// Get the number of polygons for this mesh.
  718. virtual int GetNumPolygons ()
  719. {
  720. csSpriteTemplate* tmpl = scfParent->GetTemplate ();
  721. return tmpl->GetNumTriangles ();
  722. }
  723. /// Get the pointer to the array of polygons.
  724. virtual csMeshedPolygon* GetPolygons ();
  725. PolyMesh ()
  726. {
  727. polygons = NULL;
  728. }
  729. virtual ~PolyMesh ()
  730. {
  731. delete polygons;
  732. }
  733. csMeshedPolygon* polygons;
  734. } scfiPolygonMesh;
  735. friend struct PolyMesh;
  736. };
  737. #endif /*CSSPRITE_H*/