CrystalSpace

Public API Reference

csplugincommon/particlesys/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 
00027 #include "csextern.h"
00028 #include "csgeom/box.h"
00029 #include "cstool/objmodel.h"
00030 #include "csgeom/vector3.h"
00031 #include "csgfx/shadervarcontext.h"
00032 #include "cstool/rendermeshholder.h"
00033 #include "csutil/cscolor.h"
00034 #include "csutil/flags.h"
00035 #include "csutil/floatrand.h"
00036 #include "csutil/refarr.h"
00037 #include "iengine/lightmgr.h"
00038 #include "iengine/material.h"
00039 #include "imesh/object.h"
00040 #include "imesh/particle.h"
00041 #include "imesh/partsys.h"
00042 #include "imesh/sprite2d.h"
00043 #include "ivideo/graph3d.h"
00044 
00048 struct iMeshObjectFactory;
00049 struct iMaterialWrapper;
00050 struct iMovable;
00051 struct iRenderView;
00052 struct iObjectRegistry;
00053 struct iEngine;
00054 struct iMeshWrapper;
00055 
00056 #include "csutil/win32/msvc_deprecated_warn_off.h"
00057 
00063 class CS_CRYSTALSPACE_EXPORT csParticleSystem :
00064   public scfImplementationExt2<csParticleSystem,
00065     csObjectModel, iMeshObject, iParticleState>
00066 {
00067 protected:
00068   iObjectRegistry* object_reg;
00069   iMeshObjectFactory* factory;
00070   iMeshWrapper* logparent;
00071   iEngine* engine;
00072   csRef<iLightManager> light_mgr;
00074   float radius;
00076   csRefArray<iMeshObject> partmeshes;
00077   csRefArray<iSprite2DState> sprite2ds;
00078   csRefArray<iParticle> particles;
00080   bool self_destruct;
00081   csTicks time_to_live; // msec
00083   csColor color;
00085   csRef<iMaterialWrapper> mat;
00087   uint MixMode;
00089   bool change_color; csColor colorpersecond;
00091   bool change_size; float scalepersecond;
00093   bool change_alpha; float alphapersecond; float alpha_now;
00095   bool change_rotation; float anglepersecond;
00101   csBox3 bbox;
00102   iMeshObjectDrawCallback* vis_cb;
00103 
00105   csRef<iMeshObjectFactory> spr_factory;
00107   csTicks prev_time;
00108   float current_lod;
00109   uint32 current_features;
00110 
00111   csFlags flags;
00112 
00113   // Number of particles.
00114   size_t number;
00116   csVector3 *part_pos;
00117 
00118   csRenderMeshHolder rmHolder;
00119 
00120   size_t VertexCount;
00121   size_t TriangleCount;
00122   struct PerFrameData
00123   {
00124     csRef<csRenderBufferHolder> bufferHolder;
00125     csRef<iRenderBuffer> vertex_buffer;
00126     csRef<iRenderBuffer> texel_buffer;
00127     csRef<iRenderBuffer> color_buffer;
00128   };
00129   csFrameDataHolder<PerFrameData> perFrameHolder;
00130   csRef<iRenderBuffer> index_buffer;
00131 
00132   csRef<iGraphics3D> g3d;
00133 
00135   void SetupBuffers (size_t part_sides);
00136   size_t part_sides;    // Number of vertices per particle (for buffers).
00137 
00138   bool initialized;
00140   virtual void SetupObject ();
00141 
00142   // Call if object needs changing.
00143   void ChangeObject ()
00144   {
00145     initialized = false;
00146     ShapeChanged ();
00147   }
00148 
00149 private:
00150   csRandomFloatGen randgen;
00151 protected:
00153   csVector3 GetRandomDirection();
00155   csVector3 GetRandomDirection(csVector3 const& magnitude,
00156     csVector3 const& offset);
00158   csVector3 GetRandomPosition(csBox3 const& box);
00159 
00160 public:
00165   csParticleSystem (iObjectRegistry* object_reg, iMeshObjectFactory* factory);
00166 
00170   virtual ~csParticleSystem ();
00171 
00173   virtual void SetParticleCount (size_t num)
00174   {
00175     number = num;
00176     ChangeObject ();
00177   }
00179   size_t GetParticleCount () const { return number; }
00180 
00181   void UpdateLighting (const csArray<iLightSectorInfluence*>& lights,
00182       iMovable* movable);
00183 
00185   inline size_t GetNumParticles () const { return particles.Length();}
00187   inline iParticle* GetParticle (size_t idx) const
00188   { return particles[idx]; }
00190   void RemoveParticles ();
00191 
00193   inline void AppendParticle (iMeshObject* mesh,
00194         iParticle *part, iSprite2DState* spr2d)
00195   {
00196     partmeshes.Push (mesh);
00197     sprite2ds.Push (spr2d);
00198     particles.Push (part);
00199   }
00200 
00205   void AppendRectSprite (float width, float height, iMaterialWrapper* mat,
00206     bool lighted);
00207 
00212   void AppendRegularSprite (int n, float radius, iMaterialWrapper* mat,
00213     bool lighted);
00214 
00216   inline void SetSelfDestruct (csTicks t)
00217   { self_destruct=true; time_to_live = t; };
00219   inline void UnSetSelfDestruct () { self_destruct=false; }
00221   inline bool GetSelfDestruct () const { return self_destruct; }
00223   inline csTicks GetTimeToLive () const { return time_to_live; }
00224 
00226   inline void SetChangeColor(const csColor& col)
00227   {change_color = true; colorpersecond = col;}
00229   inline void UnsetChangeColor() {change_color=false;}
00231   inline bool GetChangeColor (csColor& col) const
00232   { if(!change_color) return false; col = colorpersecond; return true; }
00233 
00235   inline void SetChangeSize(float factor)
00236   {change_size = true; scalepersecond = factor;}
00238   inline void UnsetChangeSize() {change_size=false;}
00240   inline bool GetChangeSize (float& factor) const
00241   { if(!change_size) return false; factor = scalepersecond; return true; }
00242 
00244   inline void SetAlpha(float alpha)
00245   {alpha_now = alpha; MixMode = CS_FX_SETALPHA (alpha); SetupMixMode (); }
00247   inline float GetAlpha() const {return alpha_now;}
00249   inline void SetChangeAlpha(float factor)
00250   {change_alpha = true; alphapersecond = factor;}
00252   inline void UnsetChangeAlpha() {change_alpha=false;}
00254   inline bool GetChangeAlpha (float& factor) const
00255   { if(!change_alpha) return false; factor = alphapersecond; return true; }
00256 
00258   inline void SetChangeRotation(float angle)
00259   {change_rotation = true; anglepersecond = angle;}
00261   inline void UnsetChangeRotation() {change_rotation=false;}
00263   inline bool GetChangeRotation (float& angle) const
00264   { if(!change_rotation) return false; angle = anglepersecond; return true; }
00265 
00267   inline const csBox3& GetBoundingBox() const {return bbox;}
00268 
00270   virtual void SetupColor ();
00272   virtual void AddColor (const csColor& col);
00274   virtual void ScaleBy(float factor);
00276   virtual void SetupMixMode ();
00278   virtual void Rotate(float angle);
00279 
00286   virtual void Update (csTicks elapsed_time);
00287 
00288   virtual void GetObjectBoundingBox (csBox3& bbox)
00289   {
00290     SetupObject ();
00291     bbox = csParticleSystem::bbox;
00292   }
00293   virtual const csBox3& GetObjectBoundingBox ()
00294   {
00295     SetupObject ();
00296     return bbox;
00297   }
00298   virtual void SetObjectBoundingBox (const csBox3& bbox)
00299   {
00300     csParticleSystem::bbox = bbox;
00301     ShapeChanged ();
00302   }
00303   virtual void GetRadius (float& rad, csVector3& cent)
00304   {
00305     SetupObject ();
00306     rad = radius;
00307     cent = bbox.GetCenter();
00308   }
00309 
00310   //----------------------- iMeshObject implementation ------------------------
00311   virtual iMeshObjectFactory* GetFactory () const { return factory; }
00312   virtual csFlags& GetFlags () { return flags; }
00313   virtual csPtr<iMeshObject> Clone () { return 0; }
00314   virtual bool PreGetRenderMeshes (iRenderView* rview, iMovable* movable,
00315         uint32 frustum_mask);
00316   virtual csRenderMesh** GetRenderMeshes (int& n, iRenderView* rview,
00317     iMovable* movable, uint32 frustum_mask);
00318   virtual void SetVisibleCallback (iMeshObjectDrawCallback* cb)
00319   {
00320     if (cb) cb->IncRef ();
00321     if (vis_cb) vis_cb->DecRef ();
00322     vis_cb = cb;
00323   }
00324   virtual iMeshObjectDrawCallback* GetVisibleCallback () const
00325   {
00326     return vis_cb;
00327   }
00328   virtual void NextFrame (csTicks current_time, const csVector3& /*pos*/, 
00329     uint /*currentFrame*/)
00330   {
00331     csTicks elaps = 0;
00332     if (prev_time != 0) elaps = current_time-prev_time;
00333     if (elaps > 200) elaps = 200;
00334     prev_time = current_time;
00335     Update (elaps);
00336   }
00337   virtual int HitBeamBBox (const csVector3&, const csVector3&,
00338         csVector3&, float*)
00339   { return -1; }
00340   virtual bool HitBeamOutline (const csVector3&, const csVector3&,
00341         csVector3&, float*)
00342   { return false; }
00343   virtual bool HitBeamObject (const csVector3& start, const csVector3& end,
00344     csVector3& isect, float* pr, int* polygon_idx = 0, 
00345     iMaterialWrapper** material = 0) 
00346   { 
00347     (void)start; (void)end; (void)isect; (void)pr; (void)polygon_idx;
00348     (void)material;
00349     return false; 
00350   }
00351   virtual void SetMeshWrapper (iMeshWrapper* lp) { logparent = lp; }
00352   virtual iMeshWrapper* GetMeshWrapper () const { return logparent; }
00353 
00354   //------------------------- iObjectModel implementation ----------------
00355   virtual iObjectModel* GetObjectModel () { return this; }
00356   virtual bool SetColor (const csColor& col)
00357   {
00358     color = col;
00359     SetupColor ();
00360     return true;
00361   }
00362   virtual bool GetColor (csColor& col) const
00363   {
00364     col = color;
00365     return true;
00366   }
00367   virtual bool SetMaterialWrapper (iMaterialWrapper* mat)
00368   {
00369     initialized = false;
00370     csParticleSystem::mat = mat;
00371     return true;
00372   }
00373   virtual iMaterialWrapper* GetMaterialWrapper () const { return mat; }
00374   virtual iTerraFormer* GetTerraFormerColldet () { return 0; }
00375   //------------------------- iParticleState implementation ----------------
00376   virtual void SetMixMode (uint mode)
00377   {
00378     MixMode = mode;
00379     SetupMixMode ();
00380   }
00381   virtual uint GetMixMode () const { return MixMode; }
00382   virtual void InvalidateMaterialHandles () { }
00387   virtual void PositionChild (iMeshObject* /*child*/,csTicks /*current_time*/) { }
00388 };
00389 
00394 class CS_CRYSTALSPACE_EXPORT csNewtonianParticleSystem :
00395   public scfImplementationExt0<csNewtonianParticleSystem, csParticleSystem>
00396 {
00397 protected:
00399   csVector3 *part_speed;
00401   csVector3 *part_accel;
00402 
00403 public:
00405   csNewtonianParticleSystem (iObjectRegistry* object_reg,
00406         iMeshObjectFactory* factory);
00407   virtual ~csNewtonianParticleSystem ();
00408 
00409   void SetCount (int max);
00410 
00412   virtual void Update (csTicks elapsed_time);
00413 
00415   csVector3& GetSpeed (int idx) const { return part_speed[idx]; }
00417   void SetSpeed (int idx, const csVector3& spd)
00418   { part_speed[idx] = spd; }
00419 
00421   csVector3& GetAccel (int idx) const { return part_accel[idx]; }
00423   void SetAccel (int idx, const csVector3& acl)
00424   { part_accel[idx] = acl; }
00425 };
00426 
00427 #include "csutil/win32/msvc_deprecated_warn_on.h"
00428 
00431 #endif // __CS_CSPLUGINCOMMON_PARTICLESYS_PARTGEN_H__

Generated for Crystal Space by doxygen 1.4.7