PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

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