CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

partgen.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2000-2001 by Jorrit Tyberghein
00003     Copyright (C) 2000 by W.C.A. Wijngaards
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_CSPLUGINCOMMON_PARTICLESYS_PARTGEN_H__
00021 #define __CS_CSPLUGINCOMMON_PARTICLESYS_PARTGEN_H__
00022 
00026 #include "csextern.h"
00027 #include "csgeom/box.h"
00028 #include "csgeom/objmodel.h"
00029 #include "csgeom/vector3.h"
00030 #include "csgfx/shadervarcontext.h"
00031 #include "cstool/rendermeshholder.h"
00032 #include "csutil/cscolor.h"
00033 #include "csutil/flags.h"
00034 #include "csutil/floatrand.h"
00035 #include "csutil/refarr.h"
00036 #include "iengine/lightmgr.h"
00037 #include "iengine/material.h"
00038 #include "imesh/object.h"
00039 #include "imesh/particle.h"
00040 #include "imesh/partsys.h"
00041 #include "imesh/sprite2d.h"
00042 #include "ivideo/graph3d.h"
00043 
00047 struct iMeshObjectFactory;
00048 struct iMaterialWrapper;
00049 struct iMovable;
00050 struct iRenderView;
00051 struct iObjectRegistry;
00052 struct iEngine;
00053 struct iMeshWrapper;
00054 
00060 class CS_CRYSTALSPACE_EXPORT csParticleSystem : public iMeshObject
00061 {
00062 protected:
00063   iObjectRegistry* object_reg;
00064   iMeshObjectFactory* factory;
00065   iBase* logparent;
00066   iEngine* engine;
00067   csRef<iLightManager> light_mgr;
00069   csVector3 radius;
00071   csRefArray<iSprite2DState> sprite2ds;
00072   csRefArray<iParticle> particles;
00074   bool self_destruct;
00075   csTicks time_to_live; // msec
00077   csColor color;
00079   csRef<iMaterialWrapper> mat;
00081   uint MixMode;
00083   bool change_color; csColor colorpersecond;
00085   bool change_size; float scalepersecond;
00087   bool change_alpha; float alphapersecond; float alpha_now;
00089   bool change_rotation; float anglepersecond;
00095   csBox3 bbox;
00096   iMeshObjectDrawCallback* vis_cb;
00097 
00099   csRef<iMeshObjectFactory> spr_factory;
00101   csTicks prev_time;
00102   float current_lod;
00103   uint32 current_features;
00104 
00105   csFlags flags;
00106 
00107   // Number of particles.
00108   size_t number;
00110   csVector3 *part_pos;
00111 
00112   csRenderMeshHolder rmHolder;
00113 
00114   size_t VertexCount;
00115   size_t TriangleCount;
00116   struct PerFrameData
00117   {
00118     csRef<csRenderBufferHolder> bufferHolder;
00119     csRef<iRenderBuffer> vertex_buffer;
00120     csRef<iRenderBuffer> texel_buffer;
00121     csRef<iRenderBuffer> color_buffer;
00122   };
00123   csFrameDataHolder<PerFrameData> perFrameHolder;
00124   csRef<iRenderBuffer> index_buffer;
00125 
00126   csRef<iGraphics3D> g3d;
00127 
00129   void SetupBuffers (size_t part_sides);
00130   size_t part_sides;    // Number of vertices per particle (for buffers).
00131 
00132   bool initialized;
00134   virtual void SetupObject ();
00135 
00136   // Call if object needs changing.
00137   void ChangeObject ()
00138   {
00139     initialized = false;
00140     scfiObjectModel.ShapeChanged ();
00141   }
00142 
00143 private:
00144   csRandomFloatGen randgen;
00145 protected:
00147   csVector3 GetRandomDirection();
00149   csVector3 GetRandomDirection(csVector3 const& magnitude,
00150     csVector3 const& offset);
00152   csVector3 GetRandomPosition(csBox3 const& box);
00153 
00154 public:
00159   csParticleSystem (iObjectRegistry* object_reg, iMeshObjectFactory* factory);
00160 
00164   virtual ~csParticleSystem ();
00165 
00167   virtual void SetParticleCount (size_t num)
00168   {
00169     number = num;
00170     ChangeObject ();
00171   }
00173   size_t GetParticleCount () const { return number; }
00174 
00175   void UpdateLighting (const csArray<iLight*>& lights, iMovable* movable);
00176 
00178   inline size_t GetNumParticles () const { return particles.Length();}
00180   inline iParticle* GetParticle (size_t idx) const
00181   { return particles[idx]; }
00183   void RemoveParticles ();
00184 
00186   inline void AppendParticle (iParticle *part, iSprite2DState* spr2d)
00187   {
00188     sprite2ds.Push (spr2d);
00189     particles.Push (part);
00190   }
00191 
00196   void AppendRectSprite (float width, float height, iMaterialWrapper* mat,
00197     bool lighted);
00198 
00203   void AppendRegularSprite (int n, float radius, iMaterialWrapper* mat,
00204     bool lighted);
00205 
00207   inline void SetSelfDestruct (csTicks t)
00208   { self_destruct=true; time_to_live = t; };
00210   inline void UnSetSelfDestruct () { self_destruct=false; }
00212   inline bool GetSelfDestruct () const { return self_destruct; }
00214   inline csTicks GetTimeToLive () const { return time_to_live; }
00215 
00217   inline void SetChangeColor(const csColor& col)
00218   {change_color = true; colorpersecond = col;}
00220   inline void UnsetChangeColor() {change_color=false;}
00222   inline bool GetChangeColor (csColor& col) const
00223   { if(!change_color) return false; col = colorpersecond; return true; }
00224 
00226   inline void SetChangeSize(float factor)
00227   {change_size = true; scalepersecond = factor;}
00229   inline void UnsetChangeSize() {change_size=false;}
00231   inline bool GetChangeSize (float& factor) const
00232   { if(!change_size) return false; factor = scalepersecond; return true; }
00233 
00235   inline void SetAlpha(float alpha)
00236   {alpha_now = alpha; MixMode = CS_FX_SETALPHA (alpha); SetupMixMode (); }
00238   inline float GetAlpha() const {return alpha_now;}
00240   inline void SetChangeAlpha(float factor)
00241   {change_alpha = true; alphapersecond = factor;}
00243   inline void UnsetChangeAlpha() {change_alpha=false;}
00245   inline bool GetChangeAlpha (float& factor) const
00246   { if(!change_alpha) return false; factor = alphapersecond; return true; }
00247 
00249   inline void SetChangeRotation(float angle)
00250   {change_rotation = true; anglepersecond = angle;}
00252   inline void UnsetChangeRotation() {change_rotation=false;}
00254   inline bool GetChangeRotation (float& angle) const
00255   { if(!change_rotation) return false; angle = anglepersecond; return true; }
00256 
00258   inline const csBox3& GetBoundingBox() const {return bbox;}
00259 
00261   virtual void SetupColor ();
00263   virtual void AddColor (const csColor& col);
00265   virtual void ScaleBy(float factor);
00267   virtual void SetupMixMode ();
00269   virtual void Rotate(float angle);
00270 
00277   virtual void Update (csTicks elapsed_time);
00278 
00279   void GetObjectBoundingBox (csBox3& bbox)
00280   {
00281     SetupObject ();
00282     bbox = csParticleSystem::bbox;
00283   }
00284   void SetObjectBoundingBox (const csBox3& bbox)
00285   {
00286     csParticleSystem::bbox = bbox;
00287     scfiObjectModel.ShapeChanged ();
00288   }
00289   void GetRadius (csVector3& rad, csVector3& cent)
00290   {
00291     SetupObject ();
00292     rad = radius;
00293     cent = bbox.GetCenter();
00294   }
00295 
00296   //----------------------- iMeshObject implementation ------------------------
00297   SCF_DECLARE_IBASE;
00298 
00299   virtual iMeshObjectFactory* GetFactory () const { return factory; }
00300   virtual csFlags& GetFlags () { return flags; }
00301   virtual csPtr<iMeshObject> Clone () { return 0; }
00302   virtual bool PreGetRenderMeshes (iRenderView* rview, iMovable* movable,
00303         uint32 frustum_mask);
00304   virtual csRenderMesh** GetRenderMeshes (int& n, iRenderView* rview,
00305     iMovable* movable, uint32 frustum_mask);
00306   virtual void SetVisibleCallback (iMeshObjectDrawCallback* cb)
00307   {
00308     if (cb) cb->IncRef ();
00309     if (vis_cb) vis_cb->DecRef ();
00310     vis_cb = cb;
00311   }
00312   virtual iMeshObjectDrawCallback* GetVisibleCallback () const
00313   {
00314     return vis_cb;
00315   }
00316   virtual void NextFrame (csTicks current_time, const csVector3& /*pos*/)
00317   {
00318     csTicks elaps = 0;
00319     if (prev_time != 0) elaps = current_time-prev_time;
00320     prev_time = current_time;
00321     Update (elaps);
00322   }
00323   virtual int HitBeamBBox (const csVector3&, const csVector3&,
00324         csVector3&, float*)
00325   { return -1; }
00326   virtual bool HitBeamOutline (const csVector3&, const csVector3&,
00327         csVector3&, float*)
00328   { return false; }
00329   virtual bool HitBeamObject (const csVector3&, const csVector3&,
00330         csVector3&, float*, int* = 0) { return false; }
00331   virtual void SetLogicalParent (iBase* lp) { logparent = lp; }
00332   virtual iBase* GetLogicalParent () const { return logparent; }
00333 
00334   //------------------------- iObjectModel implementation ----------------
00335   class ObjectModel : public csObjectModel
00336   {
00337     SCF_DECLARE_EMBEDDED_IBASE (csParticleSystem);
00338     virtual void GetObjectBoundingBox (csBox3& bbox)
00339     {
00340       scfParent->GetObjectBoundingBox (bbox);
00341     }
00342     virtual void SetObjectBoundingBox (const csBox3& bbox)
00343     {
00344       scfParent->SetObjectBoundingBox (bbox);
00345     }
00346     virtual void GetRadius (csVector3& rad, csVector3& cent)
00347     {
00348       scfParent->GetRadius (rad, cent);
00349     }
00350   } scfiObjectModel;
00351   friend class ObjectModel;
00352 
00353   virtual iObjectModel* GetObjectModel () { return &scfiObjectModel; }
00354   virtual bool SetColor (const csColor& col)
00355   {
00356     color = col;
00357     SetupColor ();
00358     return true;
00359   }
00360   virtual bool GetColor (csColor& col) const
00361   {
00362     col = color;
00363     return true;
00364   }
00365   virtual bool SetMaterialWrapper (iMaterialWrapper* mat)
00366   {
00367     initialized = false;
00368     csParticleSystem::mat = mat;
00369     return true;
00370   }
00371   virtual iMaterialWrapper* GetMaterialWrapper () const { return mat; }
00372   virtual void InvalidateMaterialHandles () { }
00377   virtual void PositionChild (iMeshObject* /*child*/,csTicks /*current_time*/) { }
00378 
00379   //------------------------- iParticleState implementation ----------------
00380   class ParticleState : public iParticleState
00381   {
00382     SCF_DECLARE_EMBEDDED_IBASE (csParticleSystem);
00383     virtual void SetMaterialWrapper (iMaterialWrapper* material)
00384     {
00385       scfParent->initialized = false;
00386       scfParent->mat = material;
00387     }
00388     virtual iMaterialWrapper* GetMaterialWrapper () const
00389     {
00390       return scfParent->mat;
00391     }
00392     virtual void SetMixMode (uint mode)
00393     {
00394       scfParent->MixMode = mode;
00395       scfParent->SetupMixMode ();
00396     }
00397     virtual uint GetMixMode () const { return scfParent->MixMode; }
00398     virtual void SetColor (const csColor& color)
00399     {
00400       scfParent->color = color;
00401       scfParent->SetupColor ();
00402     }
00403     virtual const csColor& GetColor () const
00404     {
00405       return scfParent->color;
00406     }
00407     virtual void SetAlpha(float alpha) {scfParent->SetAlpha(alpha);}
00408     virtual float GetAlpha() const {return scfParent->GetAlpha ();}
00409     virtual void SetChangeColor (const csColor& color)
00410     {
00411       scfParent->SetChangeColor (color);
00412     }
00413     virtual void UnsetChangeColor ()
00414     {
00415       scfParent->UnsetChangeColor ();
00416     }
00417     virtual bool GetChangeColor (csColor& col) const
00418     {
00419       return scfParent->GetChangeColor(col); }
00420     virtual void SetChangeSize (float factor)
00421     {
00422       scfParent->SetChangeSize (factor);
00423     }
00424     virtual void UnsetChangeSize ()
00425     {
00426       scfParent->UnsetChangeSize ();
00427     }
00428     virtual bool GetChangeSize (float& factor) const
00429     {
00430       return scfParent->GetChangeSize(factor);
00431     }
00432     virtual void SetChangeRotation (float angle)
00433     {
00434       scfParent->SetChangeRotation (angle);
00435     }
00436     virtual void UnsetChangeRotation ()
00437     {
00438       scfParent->UnsetChangeRotation ();
00439     }
00440     virtual bool GetChangeRotation (float& angle) const
00441     {
00442       return scfParent->GetChangeRotation(angle);
00443     }
00444     virtual void SetChangeAlpha (float factor)
00445     {
00446       scfParent->SetChangeAlpha (factor);
00447     }
00448     virtual void UnsetChangeAlpha ()
00449     {
00450       scfParent->UnsetChangeAlpha ();
00451     }
00452     virtual bool GetChangeAlpha (float& factor) const
00453     {
00454       return scfParent->GetChangeAlpha(factor);
00455     }
00456     virtual void SetSelfDestruct (csTicks t)
00457     {
00458       scfParent->SetSelfDestruct (t);
00459     }
00460     virtual void UnSetSelfDestruct ()
00461     {
00462       scfParent->UnSetSelfDestruct ();
00463     }
00464   } scfiParticleState;
00465   friend class ParticleState;
00466 };
00467 
00472 class CS_CRYSTALSPACE_EXPORT csNewtonianParticleSystem : public csParticleSystem
00473 {
00474 protected:
00476   csVector3 *part_speed;
00478   csVector3 *part_accel;
00479 
00480 public:
00482   csNewtonianParticleSystem (iObjectRegistry* object_reg,
00483         iMeshObjectFactory* factory);
00484   virtual ~csNewtonianParticleSystem ();
00485 
00486   void SetCount (int max);
00487 
00489   virtual void Update (csTicks elapsed_time);
00490 
00492   csVector3& GetSpeed (int idx) const { return part_speed[idx]; }
00494   void SetSpeed (int idx, const csVector3& spd)
00495   { part_speed[idx] = spd; }
00496 
00498   csVector3& GetAccel (int idx) const { return part_accel[idx]; }
00500   void SetAccel (int idx, const csVector3& acl)
00501   { part_accel[idx] = acl; }
00502 };
00503 
00506 #endif // __CS_CSPLUGINCOMMON_PARTICLESYS_PARTGEN_H__

Generated for Crystal Space by doxygen 1.4.4