PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C++ Header | 2079 lines | 1302 code | 232 blank | 545 comment | 16 complexity | c1c24a9b28afc318a248b826afbcc872 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. Copyright (C) 1998-2001 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 "cssys/sysfunc.h"
  18. #include "csutil/cscolor.h"
  19. #include "csutil/array.h"
  20. #include "csutil/parray.h"
  21. #include "csutil/refarr.h"
  22. #include "csutil/randomgen.h"
  23. #include "csgeom/math3d.h"
  24. #include "csgeom/math2d.h"
  25. #include "csgeom/poly2d.h"
  26. #include "csgeom/poly3d.h"
  27. #include "csgeom/box.h"
  28. #include "csgeom/objmodel.h"
  29. #include "csgeom/trimeshlod.h"
  30. #include "plugins/mesh/spr3d/object/skel3d.h"
  31. #include "imesh/sprite3d.h"
  32. #include "imesh/object.h"
  33. #include "iengine/material.h"
  34. #include "iengine/lod.h"
  35. #include "iengine/sharevar.h"
  36. #include "iutil/config.h"
  37. #include "iutil/eventh.h"
  38. #include "iutil/comp.h"
  39. #include "iutil/virtclk.h"
  40. #include "ivideo/graph3d.h"
  41. #include "ivideo/vbufmgr.h"
  42. #include "ivideo/rendermesh.h"
  43. #include "ivideo/rndbuf.h"
  44. #include "cstool/anonrndbuf.h"
  45. #include "ivideo/material.h"
  46. #include "qint.h"
  47. #include "csgfx/shadervarcontext.h"
  48. struct iObjectRegistry;
  49. struct iEngine;
  50. /**
  51. * A listener to listen to the variables.
  52. */
  53. class csSpriteLODListener : public iSharedVariableListener
  54. {
  55. private:
  56. float* variable;
  57. public:
  58. SCF_DECLARE_IBASE;
  59. csSpriteLODListener (float* variable)
  60. {
  61. SCF_CONSTRUCT_IBASE (0);
  62. csSpriteLODListener::variable = variable;
  63. }
  64. virtual void VariableChanged (iSharedVariable* var)
  65. {
  66. *variable = var->Get ();
  67. }
  68. };
  69. /**
  70. * A frame for 3D sprite animation.
  71. */
  72. class csSpriteFrame : public iSpriteFrame
  73. {
  74. private:
  75. int animation_index;
  76. int texturing_index;
  77. char* name;
  78. /// If true then normals are already calculated for this frame.
  79. bool normals_calculated;
  80. /// Bounding box in object space for this frame.
  81. csBox3 box;
  82. /// Radius in object space for this frame.
  83. csVector3 radius;
  84. public:
  85. ///
  86. csSpriteFrame (int anm_idx, int tex_idx);
  87. ///
  88. virtual ~csSpriteFrame ();
  89. ///
  90. void SetTexIndex (int tex_idx) { texturing_index = tex_idx; }
  91. /// Return true if normals are already calculated.
  92. bool NormalsCalculated () const { return normals_calculated; }
  93. /// Set normals calculated to value.
  94. void SetNormalsCalculated (bool n) { normals_calculated = n; }
  95. /// Set the name.
  96. virtual void SetName (char const*);
  97. /// Get the name.
  98. virtual char const* GetName () const { return name; }
  99. ///
  100. virtual int GetAnmIndex () const { return animation_index; }
  101. ///
  102. virtual int GetTexIndex () const { return texturing_index; }
  103. /**
  104. * Compute the object space bounding box for this frame.
  105. * This has to be called after setting up the frame and before
  106. * using it.
  107. */
  108. void SetBoundingBox (const csBox3& b) { box = b; }
  109. /**
  110. * Get the bounding box in object space.
  111. */
  112. void GetBoundingBox (csBox3& b) const { b = box; }
  113. /**
  114. * Set the radius of this frame in object space.
  115. */
  116. void SetRadius (const csVector3& r) { radius = r; }
  117. /**
  118. * Get the radius of this frame in object space.
  119. */
  120. void GetRadius (csVector3& r) const { r = radius; }
  121. SCF_DECLARE_IBASE;
  122. };
  123. /**
  124. * An action frameset for a 3D sprite animation.
  125. */
  126. class csSpriteAction2 : public iSpriteAction
  127. {
  128. public:
  129. /// Initialize a action object
  130. csSpriteAction2 ();
  131. /// Destroy this action object
  132. virtual ~csSpriteAction2 ();
  133. /// Add a frame to this action.
  134. void AddCsFrame (csSpriteFrame* frame, int delay, float displace);
  135. /// Add a frame to this action
  136. virtual void AddFrame (iSpriteFrame* frame, int delay, float displace);
  137. /// Set action name
  138. virtual void SetName (char const*);
  139. /// Get action name
  140. virtual char const* GetName () const
  141. { return name; }
  142. /// Get total number of frames in this action
  143. virtual int GetFrameCount () { return frames.Length (); }
  144. /// Query the frame number f.
  145. csSpriteFrame* GetCsFrame (int f)
  146. {
  147. return (f < frames.Length ())
  148. ? frames [f]
  149. : (csSpriteFrame*)0;
  150. }
  151. /// Returns the looping frame after frame number f.
  152. csSpriteFrame* GetCsNextFrame (int f)
  153. {
  154. if (!reverse_action)
  155. {
  156. f++;
  157. return f<frames.Length() ? frames[f] : frames[0];
  158. }
  159. else
  160. {
  161. f--;
  162. return f>=0
  163. ? frames[f]
  164. : frames[frames.Length()-1];
  165. }
  166. }
  167. /// Query the frame number f.
  168. virtual iSpriteFrame* GetFrame (int f)
  169. {
  170. return (iSpriteFrame*)((f < frames.Length ())
  171. ? frames [f]
  172. : (csSpriteFrame*)0);
  173. }
  174. /// Returns the looping frame after frame number f.
  175. virtual iSpriteFrame* GetNextFrame (int f)
  176. {
  177. if (!reverse_action)
  178. {
  179. f++;
  180. return (iSpriteFrame*)(f<frames.Length()
  181. ? frames[f]
  182. : frames[0]);
  183. }
  184. else
  185. {
  186. f--;
  187. return (iSpriteFrame*)(f>=0
  188. ? frames[f]
  189. : frames[frames.Length()-1]);
  190. }
  191. }
  192. /// Get delay for frame number f
  193. virtual int GetFrameDelay (int f)
  194. { return delays [f]; }
  195. /// Get displacement for frame number f
  196. virtual float GetFrameDisplacement (int f)
  197. { return displacements [f]; }
  198. void SetReverseAction (bool reverse)
  199. { reverse_action = reverse; }
  200. SCF_DECLARE_IBASE;
  201. private:
  202. char *name;
  203. bool reverse_action;
  204. csArray<csSpriteFrame*> frames;
  205. csArray<int> delays;
  206. csArray<float> displacements;
  207. };
  208. /**
  209. * A socket for specifying where sprites can plug into
  210. * other sprites.
  211. */
  212. class csSpriteSocket : public iSpriteSocket
  213. {
  214. private:
  215. char* name;
  216. int triangle_index;
  217. iMeshWrapper *attached_mesh;
  218. public:
  219. /// Default Constructor
  220. csSpriteSocket();
  221. virtual ~csSpriteSocket ();
  222. /// Set the name.
  223. virtual void SetName (char const*);
  224. /// Get the name.
  225. virtual char const* GetName () const { return name; }
  226. /// Set the attached sprite.
  227. virtual void SetMeshWrapper (iMeshWrapper* mesh) {attached_mesh = mesh;}
  228. /// Get the attached sprite.
  229. virtual iMeshWrapper* GetMeshWrapper () const {return attached_mesh;}
  230. /// Set the index of the triangle for the socket.
  231. virtual void SetTriangleIndex (int tri_index) { triangle_index = tri_index; }
  232. /// Get the index of the triangle for the socket.
  233. virtual int GetTriangleIndex () const { return triangle_index; }
  234. SCF_DECLARE_IBASE;
  235. };
  236. class csSprite3DMeshObject;
  237. /**
  238. * A 3D sprite based on a triangle mesh with a single texture.
  239. * Animation is done with frames.
  240. * This class represents a template from which a csSprite3D
  241. * class can be made.
  242. */
  243. class csSprite3DMeshObjectFactory : public iMeshObjectFactory
  244. {
  245. private:
  246. friend class csSprite3DMeshObject;
  247. /// Material handle as returned by iTextureManager.
  248. iMaterialWrapper* cstxt;
  249. iBase* logparent;
  250. /// Cache name for caching sprite specific data.
  251. char* cachename;
  252. /// An optional skeleton.
  253. csSkel* skeleton;
  254. /**
  255. * The order in which to introduce levels in order to get to a higher LOD.
  256. * The index of this array is the vertex number which is introduced.
  257. * The vertices of this template were reordered (by GenerateLOD()) so that
  258. * the first vertices are used in low-detail. The contents of this array
  259. * is the vertex number to emerge from.
  260. */
  261. int* emerge_from;
  262. /// The frames.
  263. csPDelArray<csSpriteFrame> frames;
  264. /// The actions.
  265. csPDelArray<csSpriteAction2> actions;
  266. /// The sockets.
  267. csPDelArray<csSpriteSocket> sockets;
  268. /// Enable tweening.
  269. bool do_tweening;
  270. /// The lighting_quality for this template. See macros CS_SPR_LIGHTING_*
  271. int lighting_quality;
  272. /**
  273. * The lighting_quality_config for this template.
  274. * See macros CS_SPR_LIGHT_*
  275. * This is used to set new sprites lighting_quality_config to this one.
  276. */
  277. int lighting_quality_config;
  278. /**
  279. * Values for the function <code>lod=m*distance+a</code> that is used
  280. * to compute the actual LOD level for this object.
  281. */
  282. float lod_m, lod_a;
  283. /**
  284. * It is also possible to use variables. If these are not 0 then the
  285. * lod values are taken from variables.
  286. */
  287. csRef<iSharedVariable> lod_varm;
  288. csRef<iSharedVariable> lod_vara;
  289. csRef<csSpriteLODListener> lod_varm_listener;
  290. csRef<csSpriteLODListener> lod_vara_listener;
  291. /**
  292. * The lod_level_config for this template.
  293. * See macros CS_SPR_LOD_*
  294. * This is used to set new sprites lod_level_config to this one.
  295. */
  296. int lod_level_config;
  297. /// The base mesh is also the texture alignment mesh.
  298. csTriangleMesh* texel_mesh;
  299. /// The array of texels
  300. csPDelArray<csPoly2D> texels;
  301. /// The vertices
  302. csPDelArray<csPoly3D> vertices;
  303. /// The normals
  304. csPDelArray<csPoly3D> normals;
  305. /**
  306. * Connectivity information for this sprite template.
  307. * Also contains temporary vertex position information
  308. * for one sprite (@@@this should be avoided!!!!)
  309. */
  310. csTriangleVerticesCost* tri_verts;
  311. /// The default mixing mode for new sprites
  312. int MixMode;
  313. /// If true then this factory has been initialized.
  314. bool initialized;
  315. void GenerateCacheName ();
  316. const char* GetCacheName ();
  317. public:
  318. iObjectRegistry* object_reg;
  319. iVirtualClock* vc;
  320. csRef<iGraphics3D> g3d;
  321. #ifdef CS_USE_NEW_RENDERER
  322. csAnonRenderBufferManager *anon_buffers;
  323. #endif
  324. /**
  325. * Reference to the engine (optional because sprites can also be
  326. * used for the isometric engine).
  327. */
  328. iEngine* engine;
  329. public:
  330. /// Create the sprite template.
  331. csSprite3DMeshObjectFactory (iBase *pParent);
  332. /// Destroy the template.
  333. virtual ~csSprite3DMeshObjectFactory ();
  334. void Report (int severity, const char* msg, ...);
  335. /// Set the skeleton for this sprite template.
  336. void SetSkeleton (csSkel* sk);
  337. /// Get the skeleton for this sprite template.
  338. csSkel* GetSkeleton () const { return skeleton; }
  339. /// Get the 'emerge_from' array from which you can construct triangles.
  340. int* GetEmergeFrom () const { return emerge_from; }
  341. /// Enable or disable tweening frames (default false).
  342. void EnableTweening (bool en) { do_tweening = en; }
  343. /// Is tweening enabled?
  344. bool IsTweeningEnabled () const { return do_tweening; }
  345. /// Returns the lighting quality for this template.
  346. int GetLightingQuality() const { return lighting_quality; }
  347. /// Sets the lighting quality for this template. See CS_SPR_LIGHTING_* defs.
  348. void SetLightingQuality(int quality) {lighting_quality = quality; }
  349. /**
  350. * Sets which lighting config variable that all new sprites created
  351. * from this template will use.
  352. * The options are:
  353. * <ul>
  354. * <li>CS_SPR_LIGHT_GLOBAL (default)
  355. * <li>CS_SPR_LIGHT_TEMPLATE
  356. * <li>CS_SPR_LIGHT_LOCAL
  357. * </ul>
  358. */
  359. void SetLightingQualityConfig (int config_flag)
  360. { lighting_quality_config = config_flag; };
  361. /**
  362. * Returns what this template is using for determining the lighting quality.
  363. */
  364. int GetLightingQualityConfig () const
  365. { return lighting_quality_config; };
  366. /**
  367. * Sets which lod config variable that all new sprites created
  368. * from this template will use.
  369. * The options are:
  370. * <ul>
  371. * <li>CS_SPR_LOD_GLOBAL (default)
  372. * <li>CS_SPR_LOD_TEMPLATE
  373. * <li>CS_SPR_LOD_LOCAL
  374. * </ul>
  375. */
  376. void SetLodLevelConfig (int config_flag)
  377. { lod_level_config = config_flag; };
  378. /**
  379. * Returns what this template is using for determining the lighting quality.
  380. */
  381. int GetLodLevelConfig () const
  382. { return lod_level_config; };
  383. /**
  384. * Generate the collapse order.
  385. * This function will also reorder all the vertices in the template.
  386. * So be careful!
  387. */
  388. void GenerateLOD ();
  389. /**
  390. * Compute the object space bounding box and radius for all frames in this
  391. * template. This has to be called after setting up the template and before
  392. * using it.
  393. */
  394. void ComputeBoundingBox ();
  395. ///
  396. csTriangleMesh* GetTexelMesh () const {return texel_mesh;}
  397. /// Add some vertices, normals, and texels
  398. void AddVertices (int num);
  399. /// Add a vertex, normal, and texel
  400. void AddVertex () { AddVertices (1); }
  401. /// Query the number of vertices.
  402. int GetVertexCount () const { return vertices.Get (0)->GetVertexCount (); }
  403. /// Get a texel.
  404. csVector2& GetTexel (int frame, int vertex) const
  405. { return (*texels.Get(frame)) [vertex]; }
  406. /// Get array of texels.
  407. csVector2* GetTexels (int frame) const
  408. { return (*texels.Get(frame)).GetVertices (); }
  409. /// Set texel array. The array is copied.
  410. void SetTexels(csVector2 const* tex, int frame)
  411. { (*texels.Get(frame)).SetVertices(tex, GetVertexCount ()); }
  412. /// Get a vertex.
  413. csVector3& GetVertex (int frame, int vertex) const
  414. { return (*vertices.Get(frame)) [vertex]; }
  415. /// Get vertex array.
  416. csVector3* GetVertices (int frame) const
  417. { return (*vertices.Get(frame)).GetVertices (); }
  418. /// Set vertex array. The array is copied.
  419. void SetVertices(csVector3 const* verts, int frame)
  420. { (*vertices.Get(frame)).SetVertices(verts, GetVertexCount ()); }
  421. /// Get a normal.
  422. csVector3& GetNormal (int frame, int vertex) const
  423. { return (*normals.Get(frame)) [vertex]; }
  424. /// Get normal array.
  425. csVector3* GetNormals (int frame) const
  426. { return (*normals.Get(frame)).GetVertices (); }
  427. /// Set normal array. The array is copied.
  428. void SetNormals(csVector3 const* norms, int frame)
  429. { (*normals.Get(frame)).SetVertices(norms, GetVertexCount ()); }
  430. /**
  431. * Add a triangle to the normal, texel, and vertex meshes
  432. * a, b and c are indices to texel vertices
  433. */
  434. void AddTriangle (int a, int b, int c);
  435. /// returns the texel indices for triangle 'x'
  436. csTriangle GetTriangle (int x) const { return texel_mesh->GetTriangle (x); }
  437. /// returns the triangles of the texel_mesh
  438. csTriangle* GetTriangles () const { return texel_mesh->GetTriangles (); }
  439. /// returns the number of triangles in the sprite
  440. int GetTriangleCount () const { return texel_mesh->GetTriangleCount (); }
  441. /// Size triangle buffer size
  442. void SetTriangleCount (int count) { texel_mesh->SetSize (count); }
  443. /// Set a bank of triangles. The bank is copied.
  444. void SetTriangles (csTriangle const* trig, int count)
  445. { texel_mesh->SetTriangles(trig, count); }
  446. /// Create and add a new frame to the sprite.
  447. csSpriteFrame* AddFrame ();
  448. /// find a named frame into the sprite.
  449. csSpriteFrame* FindFrame (const char * name);
  450. /// Query the number of frames
  451. int GetFrameCount () const { return frames.Length (); }
  452. /// Query the frame number f
  453. csSpriteFrame* GetFrame (int f) const
  454. {
  455. return (f < frames.Length ())
  456. ? (csSpriteFrame *)frames [f]
  457. : (csSpriteFrame*)0;
  458. }
  459. /// Create and add a new action frameset to the sprite.
  460. csSpriteAction2* AddAction ();
  461. /// find a named action into the sprite.
  462. csSpriteAction2* FindAction (const char * name) const;
  463. /// Get the first action.
  464. csSpriteAction2* GetFirstAction () const
  465. { return (csSpriteAction2 *)actions [0]; }
  466. /// Get number of actions in sprite
  467. int GetActionCount () const
  468. { return actions.Length (); }
  469. /// Get action number No
  470. csSpriteAction2* GetAction (int No) const
  471. { return (csSpriteAction2 *)actions [No]; }
  472. /// Create and add a new socket to the sprite.
  473. csSpriteSocket* AddSocket ();
  474. /// find a named socket into the sprite.
  475. csSpriteSocket* FindSocket (const char * name) const;
  476. /// find a socked based on the sprite attached to it
  477. csSpriteSocket* FindSocket (iMeshWrapper *mesh) const;
  478. /// Query the number of sockets
  479. int GetSocketCount () const { return sockets.Length (); }
  480. /// Query the socket number f
  481. csSpriteSocket* GetSocket (int f) const
  482. {
  483. return (f < sockets.Length ())
  484. ? (csSpriteSocket *)sockets [f]
  485. : (csSpriteSocket*)0;
  486. }
  487. /// Get the material
  488. iMaterialWrapper* GetMaterial () const
  489. { return cstxt; }
  490. /// Get the material handle.
  491. iMaterialHandle* GetMaterialHandle () const
  492. { return cstxt->GetMaterialHandle (); }
  493. /// Set the material used for this sprite
  494. void SetMaterial (iMaterialWrapper *material);
  495. /**
  496. * Compute all normals in a frame.
  497. */
  498. void ComputeNormals (csSpriteFrame* frame);
  499. /**
  500. * Smooth out the gouraud shading by merging the precalculated
  501. * vertex normals along seams in frame 'frame' based on which
  502. * vertices are very close in frame 'base'
  503. */
  504. void MergeNormals (int base, int frame);
  505. /**
  506. * Smooth out the gouraud shading by merging the precalculated
  507. * vertex normals along seams in all frames based on which
  508. * vertices are very close in frame 'base'
  509. */
  510. void MergeNormals (int base);
  511. /**
  512. * Smooth out the gouraud shading by merging the precalculated
  513. * vertex normals along seams in all frames based on which
  514. * vertices are very close in each frame
  515. */
  516. void MergeNormals ();
  517. void SetMixMode (uint mode)
  518. { MixMode = mode; }
  519. uint GetMixMode () const
  520. { return MixMode; }
  521. /// For LOD.
  522. int GetLODPolygonCount (float lod) const;
  523. /// Default LOD level for this factory.
  524. void GetLod (float& m, float& a) const
  525. {
  526. m = lod_m;
  527. a = lod_a;
  528. }
  529. void ClearLODListeners ();
  530. void SetupLODListeners (iSharedVariable* varm, iSharedVariable* vara);
  531. void GetObjectBoundingBox (csBox3& bbox, int type = CS_BBOX_NORMAL);
  532. void GetRadius (csVector3& rad, csVector3 &cent);
  533. //------------------------ iMeshObjectFactory implementation --------------
  534. SCF_DECLARE_IBASE;
  535. virtual csPtr<iMeshObject> NewInstance ();
  536. virtual void HardTransform (const csReversibleTransform& t);
  537. virtual bool SupportsHardTransform () const { return true; }
  538. virtual void SetLogicalParent (iBase* lp) { logparent = lp; }
  539. virtual iBase* GetLogicalParent () const { return logparent; }
  540. //------------------ iPolygonMesh interface implementation ----------------//
  541. struct PolyMesh : public iPolygonMesh
  542. {
  543. private:
  544. csSprite3DMeshObjectFactory* factory;
  545. csFlags flags;
  546. public:
  547. SCF_DECLARE_IBASE;
  548. void SetFactory (csSprite3DMeshObjectFactory* Factory)
  549. {
  550. factory = Factory;
  551. }
  552. virtual int GetVertexCount ()
  553. {
  554. return factory->GetVertexCount ();
  555. }
  556. virtual csVector3* GetVertices ()
  557. {
  558. return factory->GetVertices (0);
  559. }
  560. virtual int GetPolygonCount ()
  561. {
  562. return factory->GetTriangleCount ();
  563. }
  564. virtual csMeshedPolygon* GetPolygons ();
  565. virtual int GetTriangleCount ()
  566. {
  567. return factory->GetTriangleCount ();
  568. }
  569. virtual csTriangle* GetTriangles ()
  570. {
  571. return factory->GetTriangles ();
  572. }
  573. virtual void Lock () { } //PM@@@
  574. virtual void Unlock () { }
  575. virtual csFlags& GetFlags () { return flags; }
  576. virtual uint32 GetChangeNumber() const { return 0; }
  577. PolyMesh () : polygons (0)
  578. {
  579. SCF_CONSTRUCT_IBASE (0);
  580. flags.Set (CS_POLYMESH_TRIANGLEMESH);
  581. }
  582. virtual ~PolyMesh () { Cleanup (); }
  583. void Cleanup () { delete[] polygons; polygons = 0; }
  584. csMeshedPolygon* polygons;
  585. } scfiPolygonMesh;
  586. friend struct PolyMesh;
  587. //------------------------- iObjectModel implementation ----------------
  588. class ObjectModel : public csObjectModel
  589. {
  590. SCF_DECLARE_EMBEDDED_IBASE (csSprite3DMeshObjectFactory);
  591. virtual void GetObjectBoundingBox (csBox3& bbox, int type = CS_BBOX_NORMAL)
  592. {
  593. scfParent->GetObjectBoundingBox (bbox, type);
  594. }
  595. virtual void GetRadius (csVector3& rad, csVector3& cent)
  596. {
  597. scfParent->GetRadius (rad, cent);
  598. }
  599. } scfiObjectModel;
  600. friend class ObjectModel;
  601. virtual iObjectModel* GetObjectModel () { return &scfiObjectModel; }
  602. //--------------------- iSprite3DFactoryState implementation -------------//
  603. struct Sprite3DFactoryState : public iSprite3DFactoryState
  604. {
  605. SCF_DECLARE_EMBEDDED_IBASE (csSprite3DMeshObjectFactory);
  606. virtual void SetMaterialWrapper (iMaterialWrapper* material)
  607. {
  608. scfParent->SetMaterial (material);
  609. }
  610. virtual iMaterialWrapper* GetMaterialWrapper () const
  611. {
  612. return scfParent->GetMaterial ();
  613. }
  614. virtual void AddVertices (int num)
  615. {
  616. scfParent->AddVertices (num);
  617. }
  618. virtual int GetVertexCount () const
  619. {
  620. return scfParent->GetVertexCount ();
  621. }
  622. virtual const csVector3& GetVertex (int frame, int vertex) const
  623. {
  624. return scfParent->GetVertex (frame, vertex);
  625. }
  626. virtual void SetVertex (int frame, int vertex, const csVector3 &val)
  627. {
  628. scfParent->GetVertex (frame, vertex) = val;
  629. }
  630. virtual csVector3* GetVertices (int frame) const
  631. {
  632. return scfParent->GetVertices (frame);
  633. }
  634. virtual void SetVertices(csVector3 const* verts, int frame)
  635. {
  636. scfParent->SetVertices(verts, frame);
  637. }
  638. virtual const csVector2& GetTexel (int frame, int vertex) const
  639. {
  640. return scfParent->GetTexel (frame, vertex);
  641. }
  642. virtual void SetTexel (int frame, int vertex, const csVector2 &val)
  643. {
  644. scfParent->GetTexel (frame, vertex) = val;
  645. }
  646. virtual csVector2* GetTexels (int frame) const
  647. {
  648. return scfParent->GetTexels (frame);
  649. }
  650. virtual void SetTexels(csVector2 const* tex, int frame)
  651. {
  652. scfParent->SetTexels(tex, frame);
  653. }
  654. virtual const csVector3& GetNormal (int frame, int vertex) const
  655. {
  656. return scfParent->GetNormal (frame, vertex);
  657. }
  658. virtual void SetNormal (int frame, int vertex, const csVector3 &val)
  659. {
  660. scfParent->GetNormal (frame, vertex) = val;
  661. }
  662. virtual csVector3* GetNormals (int frame) const
  663. {
  664. return scfParent->GetNormals (frame);
  665. }
  666. virtual void SetNormals(csVector3 const* norms, int frame)
  667. {
  668. scfParent->SetNormals(norms, frame);
  669. }
  670. virtual void AddTriangle (int a, int b, int c)
  671. {
  672. scfParent->AddTriangle (a, b, c);
  673. }
  674. virtual csTriangle GetTriangle (int x) const
  675. {
  676. return scfParent->GetTriangle (x);
  677. }
  678. virtual csTriangle* GetTriangles () const
  679. {
  680. return scfParent->GetTriangles ();
  681. }
  682. virtual int GetTriangleCount () const
  683. {
  684. return scfParent->GetTriangleCount ();
  685. }
  686. virtual void SetTriangleCount( int count )
  687. {
  688. scfParent->SetTriangleCount(count);
  689. }
  690. virtual void SetTriangles( csTriangle const* trig, int count)
  691. {
  692. scfParent->SetTriangles(trig, count);
  693. }
  694. virtual iSpriteFrame* AddFrame ()
  695. {
  696. csRef<iSpriteFrame> ifr (
  697. SCF_QUERY_INTERFACE_SAFE (scfParent->AddFrame (),
  698. iSpriteFrame));
  699. return ifr; // DecRef is ok here.
  700. }
  701. virtual iSpriteFrame* FindFrame (const char* name) const
  702. {
  703. csRef<iSpriteFrame> ifr (SCF_QUERY_INTERFACE_SAFE (
  704. scfParent->FindFrame (name), iSpriteFrame));
  705. return ifr; // DecRef is ok here.
  706. }
  707. virtual int GetFrameCount () const
  708. {
  709. return scfParent->GetFrameCount ();
  710. }
  711. virtual iSpriteFrame* GetFrame (int f) const
  712. {
  713. csRef<iSpriteFrame> ifr (
  714. SCF_QUERY_INTERFACE_SAFE (scfParent->GetFrame (f),
  715. iSpriteFrame));
  716. return ifr; // DecRef is ok here.
  717. }
  718. virtual iSpriteAction* AddAction ()
  719. {
  720. csRef<iSpriteAction> ia (
  721. SCF_QUERY_INTERFACE_SAFE (scfParent->AddAction (),
  722. iSpriteAction));
  723. return ia; // DecRef is ok here.
  724. }
  725. virtual iSpriteAction* FindAction (const char* name) const
  726. {
  727. csRef<iSpriteAction> ia (SCF_QUERY_INTERFACE_SAFE (
  728. scfParent->FindAction (name), iSpriteAction));
  729. return ia; // DecRef is ok here.
  730. }
  731. virtual iSpriteAction* GetFirstAction () const
  732. {
  733. csRef<iSpriteAction> ia (SCF_QUERY_INTERFACE_SAFE (
  734. scfParent->GetFirstAction (), iSpriteAction));
  735. return ia; // DecRef is ok here.
  736. }
  737. virtual int GetActionCount () const
  738. {
  739. return scfParent->GetActionCount ();
  740. }
  741. virtual iSpriteAction* GetAction (int No) const
  742. {
  743. csRef<iSpriteAction> ia (
  744. SCF_QUERY_INTERFACE_SAFE (scfParent->GetAction (No),
  745. iSpriteAction));
  746. return ia; // DecRef is ok here.
  747. }
  748. virtual iSpriteSocket* AddSocket ()
  749. {
  750. csRef<iSpriteSocket> ifr (
  751. SCF_QUERY_INTERFACE_SAFE (scfParent->AddSocket (),
  752. iSpriteSocket));
  753. return ifr; // DecRef is ok here.
  754. }
  755. virtual iSpriteSocket* FindSocket (const char* name) const
  756. {
  757. csRef<iSpriteSocket> ifr (SCF_QUERY_INTERFACE_SAFE (
  758. scfParent->FindSocket (name), iSpriteSocket));
  759. return ifr; // DecRef is ok here.
  760. }
  761. virtual iSpriteSocket* FindSocket (iMeshWrapper* mesh) const
  762. {
  763. csRef<iSpriteSocket> ifr (SCF_QUERY_INTERFACE_SAFE (
  764. scfParent->FindSocket (mesh), iSpriteSocket));
  765. return ifr; // DecRef is ok here.
  766. }
  767. virtual int GetSocketCount () const
  768. {
  769. return scfParent->GetSocketCount ();
  770. }
  771. virtual iSpriteSocket* GetSocket (int f) const
  772. {
  773. csRef<iSpriteSocket> ifr (
  774. SCF_QUERY_INTERFACE_SAFE (scfParent->GetSocket (f),
  775. iSpriteSocket));
  776. return ifr; // DecRef is ok here.
  777. }
  778. virtual void EnableSkeletalAnimation ();
  779. virtual iSkeleton* GetSkeleton () const;
  780. virtual void EnableTweening (bool en)
  781. {
  782. scfParent->EnableTweening (en);
  783. }
  784. virtual bool IsTweeningEnabled () const
  785. {
  786. return scfParent->IsTweeningEnabled ();
  787. }
  788. virtual void SetLightingQuality (int qual)
  789. {
  790. scfParent->SetLightingQuality (qual);
  791. }
  792. virtual int GetLightingQuality () const
  793. {
  794. return scfParent->GetLightingQuality ();
  795. }
  796. virtual void SetLightingQualityConfig (int qual)
  797. {
  798. scfParent->SetLightingQualityConfig (qual);
  799. }
  800. virtual int GetLightingQualityConfig () const
  801. {
  802. return scfParent->GetLightingQualityConfig ();
  803. }
  804. virtual void SetLodLevelConfig (int config_flag)
  805. {
  806. scfParent->SetLodLevelConfig (config_flag);
  807. }
  808. virtual int GetLodLevelConfig () const
  809. {
  810. return scfParent->GetLodLevelConfig ();
  811. }
  812. virtual void MergeNormals (int base, int frame)
  813. {
  814. scfParent->MergeNormals (base, frame);
  815. }
  816. virtual void MergeNormals (int base)
  817. {
  818. scfParent->MergeNormals (base);
  819. }
  820. virtual void MergeNormals ()
  821. {
  822. scfParent->MergeNormals ();
  823. }
  824. virtual void SetMixMode (uint mode)
  825. { scfParent->SetMixMode (mode); }
  826. virtual uint GetMixMode () const
  827. { return scfParent->GetMixMode (); }
  828. } scfiSprite3DFactoryState;
  829. //--------------------- iLODControl implementation -------------//
  830. struct LODControl : public iLODControl
  831. {
  832. SCF_DECLARE_EMBEDDED_IBASE (csSprite3DMeshObjectFactory);
  833. virtual void SetLOD (float m, float a)
  834. {
  835. scfParent->ClearLODListeners ();
  836. scfParent->lod_m = m;
  837. scfParent->lod_a = a;
  838. }
  839. virtual void GetLOD (float& m, float& a) const
  840. {
  841. m = scfParent->lod_m;
  842. a = scfParent->lod_a;
  843. }
  844. virtual void SetLOD (iSharedVariable* varm, iSharedVariable* vara)
  845. {
  846. scfParent->SetupLODListeners (varm, vara);
  847. scfParent->lod_m = varm->Get ();
  848. scfParent->lod_a = vara->Get ();
  849. }
  850. virtual void GetLOD (iSharedVariable*& varm, iSharedVariable*& vara) const
  851. {
  852. varm = scfParent->lod_varm;
  853. vara = scfParent->lod_vara;
  854. }
  855. virtual int GetLODPolygonCount (float lod) const
  856. {
  857. return scfParent->GetLODPolygonCount (lod);
  858. }
  859. } scfiLODControl;
  860. friend struct LODControl;
  861. };
  862. /**
  863. * A 3D sprite based on a triangle mesh with a single texture.
  864. * Animation is done with frames (a frame may be controlled by
  865. * a skeleton).
  866. */
  867. class csSprite3DMeshObject : public iMeshObject
  868. {
  869. private:
  870. /// Set the size of internally used tables
  871. static void UpdateWorkTables (int max_size);
  872. iBase* logparent;
  873. public:
  874. /**
  875. * Configuration values for global LOD (function <code>m*distance+a</code>).
  876. */
  877. static float global_lod_m, global_lod_a;
  878. /**
  879. * It is also possible to use variables. If these are not 0 then the
  880. * lod values are taken from variables.
  881. */
  882. static csRef<iSharedVariable> global_lod_varm;
  883. static csRef<iSharedVariable> global_lod_vara;
  884. private:
  885. /**
  886. * Used to determine where to look for the lod detail level.
  887. * The possible values are:
  888. * <ul>
  889. * <li>CS_SPR_LOD_GLOBAL (default)
  890. * <li>CS_SPR_LOD_TEMPLATE
  891. * <li>CS_SPR_LOD_LOCAL
  892. * </ul>
  893. */
  894. int lod_level_config;
  895. /**
  896. * Configuration values for an individuals LOD
  897. * (function <code>m*distance+a</code>).
  898. */
  899. float local_lod_m, local_lod_a;
  900. /**
  901. * It is also possible to use variables. If these are not 0 then the
  902. * lod values are taken from variables.
  903. */
  904. csRef<iSharedVariable> local_lod_varm;
  905. csRef<iSharedVariable> local_lod_vara;
  906. csRef<csSpriteLODListener> local_lod_varm_listener;
  907. csRef<csSpriteLODListener> local_lod_vara_listener;
  908. /**
  909. * Quality setting for sprite lighting.
  910. * See the CS_SPR_LIGHTING_* macros defined in this header file for the
  911. * different types of lighting. This is the local setting. It overrides the
  912. * template, and global lighting settings.
  913. */
  914. int local_lighting_quality;
  915. /**
  916. * Used to determine where to look for the quality setting of the lighting.
  917. * The possible values are:
  918. * <ul>
  919. * <li>CS_SPR_LIGHT_GLOBAL (default)
  920. * <li>CS_SPR_LIGHT_TEMPLATE
  921. * <li>CS_SPR_LIGHT_LOCAL
  922. * </ul>
  923. */
  924. int lighting_quality_config;
  925. /**
  926. * Setting to manipulate sprite animation speed
  927. * The bigger the value is, the faster the sprite moves.
  928. */
  929. float speedfactor;
  930. /**
  931. * Setting to indicate if the animation should be played in an endless loop.
  932. */
  933. bool loopaction;
  934. /**
  935. * Setting to indicate if the animation should be stopped.
  936. */
  937. bool fullstop;
  938. /**
  939. * Each mesh must have its own individual socket assignments,
  940. * but the vector must be copied down from the factory at create time.
  941. */
  942. csPDelArray<csSpriteSocket> sockets;
  943. public:
  944. /**
  945. * Quality setting for sprite lighting.
  946. * See the CS_SPR_LIGHTING_* macros defined in this header file for the
  947. * different types of lighting. This is the global setting that is used for
  948. * all csSprite3Ds(unless the template or the individual sprite overrides
  949. * it).
  950. */
  951. static int global_lighting_quality;
  952. /**
  953. * Returns the lighting quality level used by this sprite.
  954. * See SPT_LIGHTING_* macros defined in this header for the different types
  955. * of lighting.
  956. */
  957. int GetLightingQuality ()
  958. {
  959. switch (lighting_quality_config)
  960. {
  961. case CS_SPR_LIGHT_GLOBAL: return global_lighting_quality; break;
  962. case CS_SPR_LIGHT_TEMPLATE: return factory->GetLightingQuality(); break;
  963. case CS_SPR_LIGHT_LOCAL: return local_lighting_quality; break;
  964. default:
  965. {
  966. lighting_quality_config = factory->GetLightingQualityConfig();
  967. return factory->GetLightingQuality();
  968. }
  969. }
  970. }
  971. /**
  972. * Sets the local lighting quality for this sprite. NOTE: you must use
  973. * SetLightingQualityConfig (CS_SPR_LIGHT_LOCAL) for the sprite to use this.
  974. */
  975. void SetLocalLightingQuality (int lighting_quality)
  976. { local_lighting_quality = lighting_quality; }
  977. /**
  978. * Sets the global lighting quality for all csSprite3Ds.
  979. * NOTE: You must use SetLightingQualityConfig(CS_SPR_LIGHT_GLOBAL) for the
  980. * sprite to use this.
  981. */
  982. void SetGlobalLightingQuality (int lighting_quality)
  983. { global_lighting_quality = lighting_quality; }
  984. /**
  985. * Sets which lighting config variable this sprite will use.
  986. * The options are:
  987. * <ul>
  988. * <li>CS_SPR_LIGHT_GLOBAL (default)
  989. * <li>CS_SPR_LIGHT_TEMPLATE
  990. * <li>CS_SPR_LIGHT_LOCAL
  991. * </ul>
  992. */
  993. void SetLightingQualityConfig (int config_flag)
  994. { lighting_quality_config = config_flag; }
  995. /**
  996. * Returns what this sprite is using for determining the lighting quality.
  997. */
  998. int GetLightingQualityConfig () const
  999. { return lighting_quality_config; }
  1000. /**
  1001. * Get the lod settings relevant for this sprite.
  1002. */
  1003. void GetRelevantLodSettings (float& m, float& a) const
  1004. {
  1005. switch (lod_level_config)
  1006. {
  1007. case CS_SPR_LOD_GLOBAL:
  1008. m = global_lod_m;
  1009. a = global_lod_a;
  1010. break;
  1011. case CS_SPR_LOD_LOCAL:
  1012. m = local_lod_m;
  1013. a = local_lod_a;
  1014. break;
  1015. //case CS_SPR_LOD_TEMPLATE:
  1016. default:
  1017. factory->GetLod (m, a);
  1018. break;
  1019. }
  1020. }
  1021. /**
  1022. * Returns the lod level used by this sprite.
  1023. */
  1024. float GetLodLevel (float distance) const
  1025. {
  1026. float m, a;
  1027. GetRelevantLodSettings (m, a);
  1028. return m * distance + a;
  1029. }
  1030. /// Return true if LOD is enabled.
  1031. bool IsLodEnabled () const
  1032. {
  1033. float m, a;
  1034. GetRelevantLodSettings (m, a);
  1035. return ABS (m) > SMALL_EPSILON || ABS (a) < (1-SMALL_EPSILON);
  1036. }
  1037. /**
  1038. * Sets which lighting config variable this sprite will use.
  1039. * The options are:
  1040. * <ul>
  1041. * <li>CS_SPR_LOD_GLOBAL (default)
  1042. * <li>CS_SPR_LOD_TEMPLATE
  1043. * <li>CS_SPR_LOD_LOCAL
  1044. * </ul>
  1045. */
  1046. void SetLodLevelConfig (int config_flag)
  1047. { lod_level_config = config_flag; }
  1048. /**
  1049. * Returns what this sprite is using for determining the lighting quality.
  1050. */
  1051. int GetLodLevelConfig () const
  1052. { return lod_level_config; }
  1053. /**
  1054. * GetVertexToLightCount returns the number of vertices to light based on LOD.
  1055. */
  1056. int GetVertexToLightCount ();
  1057. private:
  1058. /**
  1059. * num_verts_for_lod represents the number of lights that are used by lod.
  1060. * If -1 means that it is not used.
  1061. */
  1062. int num_verts_for_lod;
  1063. public:
  1064. /**
  1065. * A mesh which contains a number of triangles as generated
  1066. * by the LOD algorithm. This is static since it will likely
  1067. * change every frame anyway. We hold it static also since
  1068. * we don't want to allocate it again every time.
  1069. */
  1070. CS_DECLARE_STATIC_CLASSVAR (mesh, GetLODMesh, csTriangleMesh)
  1071. private:
  1072. /// Mixmode for the triangles/polygons of the sprite.
  1073. uint MixMode;
  1074. /**
  1075. * Array of colors for the vertices. If not set then this
  1076. * sprite does not have colored vertices.
  1077. */
  1078. csColor* vertex_colors;
  1079. /**
  1080. * Base color that will be added to the sprite colors.
  1081. */
  1082. csColor base_color;
  1083. /// The parent.
  1084. csSprite3DMeshObjectFactory* factory;
  1085. /// The material handle as returned by iTextureManager.
  1086. iMaterialWrapper* cstxt;
  1087. /// The current frame number.
  1088. int cur_frame;
  1089. /// The current action.
  1090. csSpriteAction2* cur_action;
  1091. /// Is action running in reverse? This is either 1 or -1 depending.
  1092. int frame_increment;
  1093. /// The action to restore after running an override action
  1094. csSpriteAction2* last_action;
  1095. /// The loop setting to restore after running an override action
  1096. bool last_loop;
  1097. /// The speed setting to restore after running an override action
  1098. float last_speed;
  1099. /// The reversal setting to restore after an override action
  1100. bool last_reverse;
  1101. /// The last frame time action.
  1102. csTicks last_time;
  1103. /// The last frame position (used for displacement calcs).
  1104. csVector3 last_pos;
  1105. /// The last frame displacement left over from whole frame increment.
  1106. float last_displacement;
  1107. /// Animation tweening ratio: next frame / this frame.
  1108. float tween_ratio;
  1109. /// Enable tweening.
  1110. bool do_tweening;
  1111. /// Enable single-step mode on actions
  1112. bool single_step;
  1113. /// Enable or disable lighting.
  1114. bool do_lighting;
  1115. ///
  1116. bool force_otherskin;
  1117. /// Skeleton state (optional).
  1118. csSkelState* skeleton_state;
  1119. iMeshObjectDrawCallback* vis_cb;
  1120. /**
  1121. * Camera space bounding box is cached here.
  1122. * GetCameraBoundingBox() will check the current camera number
  1123. * to see if it needs to recalculate this.
  1124. */
  1125. csBox3 camera_bbox;
  1126. /// Current camera number.
  1127. long cur_cameranr;
  1128. /// Current movable number.
  1129. long cur_movablenr;
  1130. // Remembered info between DrawTest and Draw.
  1131. #ifndef CS_USE_NEW_RENDERER
  1132. G3DTriangleMesh g3dmesh;
  1133. #else
  1134. csRenderMesh rendermesh;
  1135. csRenderMesh *meshptr;
  1136. #endif // CS_USE_NEW_RENDERER
  1137. bool initialized;
  1138. /**
  1139. * Our vertex buffer.
  1140. * This is only temporarily here. I suppose it is better to
  1141. * origanize this so that the vertex buffer is shared accross several
  1142. * sprites using the same factory. On the other hand there are lots
  1143. * of frames. Do we create a vertex buffer for every frame?
  1144. * @@@
  1145. */
  1146. #ifndef CS_USE_NEW_RENDERER
  1147. iVertexBufferManager* vbufmgr;
  1148. csRef<iVertexBuffer> vbuf;
  1149. /// Vertex buffer for tweening.
  1150. csRef<iVertexBuffer> vbuf_tween;
  1151. /// Data for vertex buffer (initialize by DrawTest, needed by Draw).
  1152. csVector3* vbuf_verts, * vbuf_tween_verts;
  1153. csVector2* vbuf_texels, * vbuf_tween_texels;
  1154. csColor* vbuf_colors, * vbuf_tween_colors;
  1155. int vbuf_num_vertices;
  1156. #else
  1157. csVector3* final_verts;
  1158. csVector2* final_texcoords;
  1159. csColor* final_colors;
  1160. csTriangle* final_triangles;
  1161. csTriangle* shadow_triangles;
  1162. bool shadow_caps;
  1163. bool use_shadow_ind;
  1164. int final_num_vertices;
  1165. int final_num_triangles;
  1166. csRef<iRenderBuffer> vertices;
  1167. csRef<iRenderBuffer> shadow_verts;
  1168. csRef<iRenderBuffer> normals;
  1169. csRef<iRenderBuffer> shadow_norms;
  1170. csRef<iRenderBuffer> texcoords;
  1171. csRef<iRenderBuffer> colors;
  1172. csRef<iRenderBuffer> indices;
  1173. csStringID vertices_name, normals_name,
  1174. texcoords_name, colors_name, indices_name,
  1175. shadow_verts_name, shadow_norms_name;
  1176. csShaderVariableContext dynDomain;
  1177. #endif // CS_USE_NEW_RENDERER
  1178. csReversibleTransform tr_o2c;
  1179. /// Setup this object.
  1180. void SetupObject ();
  1181. #ifndef CS_USE_NEW_RENDERER
  1182. /// interface to receive state of vertexbuffermanager
  1183. struct eiVertexBufferManagerClient : public iVertexBufferManagerClient
  1184. {
  1185. SCF_DECLARE_EMBEDDED_IBASE (csSprite3DMeshObject);
  1186. virtual void ManagerClosing ();
  1187. }scfiVertexBufferManagerClient;
  1188. friend struct eiVertexBufferManagerClient;
  1189. #else
  1190. iRenderBuffer *GetRenderBuffer (csStringID name);
  1191. //------------------------- iStreamSource implementation ----------------
  1192. class RenderBufferSource : public iRenderBufferSource
  1193. {
  1194. SCF_DECLARE_EMBEDDED_IBASE (csSprite3DMeshObject);
  1195. iRenderBuffer *GetRenderBuffer (csStringID name)
  1196. { return scfParent->GetRenderBuffer (name); }
  1197. } scfiRenderBufferSource;
  1198. friend class RenderBufferSource;
  1199. #endif // CS_USE_NEW_RENDERER
  1200. private:
  1201. /**
  1202. * High quality version of UpdateLighting() which recalculates
  1203. * the distance between the light and every vertex.
  1204. * This version can use tweening of the normals and vertices
  1205. */
  1206. void UpdateLightingHQ (iLight** lights, int num_lights, iMovable* movable);
  1207. /**
  1208. * Low quality version of UpdateLighting() which only
  1209. * calculates the distance once (from the center of the sprite.)
  1210. * This method can use tweening of the normals.
  1211. */
  1212. void UpdateLightingLQ (iLight** lights, int num_lights, iMovable* movable);
  1213. /**
  1214. * Low quality Fast version of UpdateLighting() which only
  1215. * calculates the distance once (from the center of the sprite.)
  1216. * This version can NOT use any tweening.
  1217. */
  1218. void UpdateLightingFast (iLight** lights, int num_lights, iMovable* movable);
  1219. /**
  1220. * A fairly fast :P totally inaccurate(usually) lighting method.
  1221. * Intended for use for things like powerups.
  1222. */
  1223. void UpdateLightingRandom ();
  1224. /// random number generator used for random lighting.
  1225. csRandomGen *rand_num;
  1226. public:
  1227. /// Constructor.
  1228. csSprite3DMeshObject ();
  1229. /// Destructor.
  1230. virtual ~csSprite3DMeshObject ();
  1231. /// Set the factory.
  1232. void SetFactory (csSprite3DMeshObjectFactory* factory);
  1233. /// Get the factory.
  1234. csSprite3DMeshObjectFactory* GetFactory3D () const { return factory; }
  1235. /// Get the skeleton state for this sprite.
  1236. csSkelState* GetSkeletonState () const { return skeleton_state; }
  1237. /// Force a new material skin other than default
  1238. void SetMaterial (iMaterialWrapper *material);
  1239. /// Get the material for this sprite.
  1240. iMaterialWrapper* GetMaterial () const { return cstxt; }
  1241. /// Sets the mode that is used, when drawing that sprite.
  1242. void SetMixMode (uint m) { MixMode = m; }
  1243. /// Gets the mode that is used, when drawing that sprite.
  1244. uint GetMixMode () const { return MixMode; }
  1245. /// Enable or disable tweening frames (default false).
  1246. void EnableTweening (bool en) { do_tweening = en; }
  1247. /// Is tweening enabled?
  1248. bool IsTweeningEnabled () const { return do_tweening; }
  1249. /// Set lighting.
  1250. void SetLighting (bool l)
  1251. {
  1252. do_lighting = l;
  1253. ResetVertexColors ();
  1254. }
  1255. /// Is lighting enabled?
  1256. bool IsLighting () const
  1257. {
  1258. return do_lighting;
  1259. }
  1260. float GetTweenRatio(){return tween_ratio;}
  1261. /// Set base color.
  1262. void SetBaseColor (const csColor& col)
  1263. {
  1264. delete[] vertex_colors;
  1265. vertex_colors = 0;
  1266. base_color = col;
  1267. ResetVertexColors ();
  1268. }
  1269. /// Get base color.
  1270. void GetBaseColor (csColor& col) const
  1271. {
  1272. col = base_color;
  1273. }
  1274. #ifdef CS_USE_NEW_RENDERER
  1275. bool AddStream (const char *name, int component_size);
  1276. bool SetStreamComponent (const char *name, int index, int component, float value);
  1277. bool SetStreamComponent (const char *name, int index, int component, int value);
  1278. bool SetStream (const char *name, float *value);
  1279. bool SetStream (const char *name, int *value);
  1280. #endif
  1281. /**
  1282. * Add a color for a vertex.
  1283. * As soon as you use this function this sprite will be rendered
  1284. * using gouraud shading. Calling this function for the first time
  1285. * will initialize all colors to black.
  1286. */
  1287. void AddVertexColor (int i, const csColor& col);
  1288. /**
  1289. * Reset the color list. If you call this function then the
  1290. * sprite will no longer use gouraud shading.
  1291. */
  1292. void ResetVertexColors ();
  1293. /**
  1294. * Clamp all vertice colors to 2.0. This is called inside
  1295. * csSprite3D::UpdateLighting() so that 3D renderer doesn't have
  1296. * to deal with brightness lighter than 2.0
  1297. */
  1298. void FixVertexColors ();
  1299. /// Unset the texture.
  1300. void UnsetTexture ()
  1301. { force_otherskin = false; }
  1302. /**
  1303. * Fill the static mesh with the current sprite
  1304. * for a given LOD level.
  1305. */
  1306. void GenerateSpriteLOD (int num_vts);
  1307. /**
  1308. * Go to the next frame depending on the current time in milliseconds OR
  1309. * depending on the distance displacement from the last render.
  1310. */
  1311. bool OldNextFrame (csTicks current_time, const csVector3& new_pos,
  1312. bool onestep = false, bool stoptoend = false);
  1313. /**
  1314. * Go to a specified frame.
  1315. */
  1316. void SetFrame (int f)
  1317. {
  1318. if (cur_action && f < cur_action->GetFrameCount ()) cur_frame = f;
  1319. //@@@!!! WHAT DO WE DO HERE!!! UpdateInPolygonTrees ();
  1320. }
  1321. /**
  1322. * Get the current frame number.
  1323. */
  1324. int GetCurFrame () const { return cur_frame; }
  1325. /**
  1326. * Get the current frame number.
  1327. */
  1328. csSpriteAction2* GetCurAction () const { return cur_action; }
  1329. /**
  1330. * Get the number of frames.
  1331. */
  1332. int GetFrameCount () const { return cur_action->GetFrameCount (); }
  1333. /**
  1334. * Select an action animation by name.
  1335. */
  1336. bool SetAction (const char * name, bool loop = true, float speed = 1)
  1337. {
  1338. csSpriteAction2 *act;
  1339. if ((act = factory->FindAction (name)) != 0)
  1340. return SetAction (act,loop,speed);
  1341. else
  1342. return false;
  1343. }
  1344. /**
  1345. * Select an action animation by index.
  1346. */
  1347. bool SetAction (int index, bool loop = true, float speed = 1)
  1348. {
  1349. csSpriteAction2 *act;
  1350. if ((act = factory->GetAction(index)) != 0)
  1351. return SetAction (act,loop,speed);
  1352. else
  1353. return false;
  1354. }
  1355. /**
  1356. * Internal function to actually set the action. External users should
  1357. * not need to call this version.
  1358. */
  1359. bool SetAction (csSpriteAction2 *act, bool loop = true, float speed = 1)
  1360. {
  1361. speedfactor = speed;
  1362. // If SetAction is called while an OverrideAction is in progress,
  1363. // this 0 overrides the override, so this action will keep going.
  1364. last_action = 0;
  1365. loopaction = loop;
  1366. fullstop = false;
  1367. single_step = false;
  1368. SetReverseAction(false); // always go forward by default.
  1369. if (act != 0)
  1370. {
  1371. cur_action = act;
  1372. SetFrame (0);
  1373. last_time = factory->vc->GetCurrentTicks ();
  1374. return true;
  1375. }
  1376. return false;
  1377. }
  1378. void SetReverseAction(bool reverse)
  1379. {
  1380. frame_increment = (reverse) ? -1:1;
  1381. if (cur_action)
  1382. cur_action->SetReverseAction(reverse);
  1383. }
  1384. bool GetReverseAction () const
  1385. {
  1386. return (frame_increment < 0);
  1387. }
  1388. void SetSingleStepAction(bool singlestep)
  1389. { single_step = singlestep; }
  1390. bool SetOverrideAction (const char *name,float speed = 1)
  1391. {
  1392. csSpriteAction2* save_last;
  1393. save_last = cur_action;
  1394. last_loop = loopaction;
  1395. last_speed = speedfactor;
  1396. last_reverse = (frame_increment==-1)?true:false;
  1397. bool flag = SetAction (name,false,speed);
  1398. last_action = save_last;
  1399. return flag;
  1400. }
  1401. bool SetOverrideAction (int index,float speed = 1)
  1402. {
  1403. csSpriteAction2* save_last;
  1404. save_last = cur_action;
  1405. last_loop = loopaction;
  1406. last_speed = speedfactor;
  1407. last_reverse = (frame_increment==-1)?true:false;
  1408. bool flag = SetAction (index,false,speed);
  1409. last_action = save_last;
  1410. return flag;
  1411. }
  1412. /**
  1413. * Propogate set action to all children
  1414. */
  1415. virtual bool PropagateAction (const char *name)
  1416. {
  1417. // TODO:: Implement across children
  1418. return SetAction(name);
  1419. }
  1420. /**
  1421. * Gets the center of a sprite socket
  1422. */
  1423. void GetSocketCenter(const char *name, csVector3 & center);
  1424. /**
  1425. * Initialize a sprite. This function is called automatically
  1426. * from within 'load'. However you should call it directly
  1427. * if you created the sprite on the fly (without 'load').
  1428. */
  1429. void InitSprite ();
  1430. /**
  1431. * Get an array of object vertices which is valid for the given frame.
  1432. * This function correcty acounts for sprites which use skeletons. In
  1433. * that case it will use the current transformation state of the skeleton
  1434. * to compute object space vertices.<br>
  1435. * Warning! The returned array should be used immediatelly or copied. It
  1436. * points to a private static array in the sprite class and can be reused
  1437. * if other calls to the sprite happen.
  1438. */
  1439. csVector3* GetObjectVerts (csSpriteFrame* fr);
  1440. /// Get the bounding box in transformed space.
  1441. void GetTransformedBoundingBox (long cameranr, long movablenr,
  1442. const csReversibleTransform& trans, csBox3& cbox);
  1443. /**
  1444. * Get the coordinates of the sprite in screen coordinates.
  1445. * Fills in the boundingBox with the X and Y locations of the cube.
  1446. * Returns the max Z location of the sprite, or -1 if not
  1447. * on-screen. If the sprite is not on-screen, the X and Y values are not
  1448. * valid.
  1449. */
  1450. float GetScreenBoundingBox (long cameranr, long movablenr,
  1451. float fov, float sx, float sy,
  1452. const csReversibleTransform& trans, csBox2& sbox, csBox3& cbox);
  1453. /// For LOD.
  1454. int GetLODPolygonCount (float lod) const;
  1455. void ClearLODListeners ();
  1456. void SetupLODListeners (iSharedVariable* varm, iSharedVariable* vara);
  1457. void GetObjectBoundingBox (csBox3& bbox, int type = CS_BBOX_NORMAL);
  1458. void GetRadius (csVector3& rad, csVector3 &cent);
  1459. /// Create and add a new socket to the sprite.
  1460. csSpriteSocket* AddSocket ();
  1461. /// find a named socket into the sprite.
  1462. csSpriteSocket* FindSocket (const char * name) const;
  1463. /// find a socked based on the sprite attached to it
  1464. csSpriteSocket* FindSocket (iMeshWrapper *mesh) const;
  1465. /// Query the number of sockets
  1466. int GetSocketCount () const { return sockets.Length (); }
  1467. /// Query the socket number f
  1468. csSpriteSocket* GetSocket (int f) const
  1469. {
  1470. return (f < sockets.Length ())
  1471. ? (csSpriteSocket *)sockets [f]
  1472. : (csSpriteSocket*)0;
  1473. }
  1474. ///------------------------ iMeshObject implementation ----------------------
  1475. SCF_DECLARE_IBASE;
  1476. virtual iMeshObjectFactory* GetFactory () const
  1477. {
  1478. csRef<iMeshObjectFactory> ifact (SCF_QUERY_INTERFACE (factory,
  1479. iMeshObjectFactory));
  1480. return ifact; // DecRef is ok here.
  1481. }
  1482. virtual bool DrawTest (iRenderView* rview, iMovable* movable);
  1483. virtual void UpdateLighting (iLight** lights, int num_lights,
  1484. iMovable* movable);
  1485. virtual bool Draw (iRenderView* rview, iMovable* movable, csZBufMode mode);
  1486. virtual csRenderMesh **GetRenderMeshes (int &n);
  1487. virtual void SetVisibleCallback (iMeshObjectDrawCallback* cb)
  1488. {
  1489. if (cb) cb->IncRef ();
  1490. if (vis_cb) vis_cb->DecRef ();
  1491. vis_cb = cb;
  1492. }
  1493. virtual iMeshObjectDrawCallback* GetVisibleCallback () const
  1494. {
  1495. return vis_cb;
  1496. }
  1497. virtual void NextFrame (csTicks current_time,const csVector3& new_pos)
  1498. {
  1499. OldNextFrame (current_time, new_pos, single_step, !loopaction);
  1500. }
  1501. virtual void HardTransform (const csReversibleTransform&) { }
  1502. virtual bool SupportsHardTransform () const { return false; }
  1503. virtual bool HitBeamOutline (const csVector3& start, const csVector3& end,
  1504. csVector3& intersect, float* pr);
  1505. virtual bool HitBeamObject (const csVector3& start, const csVector3& end,
  1506. csVector3& intersect, float* pr, int* polygon_idx = 0);
  1507. virtual void SetLogicalParent (iBase* lp) { logparent = lp; }
  1508. virtual iBase* GetLogicalParent () const { return logparent; }
  1509. //------------------ iPolygonMesh interface implementation ----------------//
  1510. struct PolyMesh : public iPolygonMesh
  1511. {
  1512. private:
  1513. csFlags flags;
  1514. public:
  1515. SCF_DECLARE_EMBEDDED_IBASE (csSprite3DMeshObject);
  1516. virtual int GetVertexCount ()
  1517. {
  1518. csSprite3DMeshObjectFactory* fact = scfParent->GetFactory3D ();
  1519. return fact->GetVertexCount ();
  1520. }
  1521. virtual csVector3* GetVertices ()
  1522. {
  1523. csSprite3DMeshObjectFactory* fact = scfParent->GetFactory3D ();
  1524. return fact->GetVertices (0);
  1525. }
  1526. virtual int GetPolygonCount ()
  1527. {
  1528. csSprite3DMeshObjectFactory* fact = scfParent->GetFactory3D ();
  1529. return fact->GetTriangleCount ();
  1530. }
  1531. virtual csMeshedPolygon* GetPolygons ();
  1532. virtual int GetTriangleCount ()
  1533. {

Large files files are truncated, but you can click here to view the full file