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   csRef<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.GetSize ();}
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     vis_cb = cb;
00321   }
00322   virtual iMeshObjectDrawCallback* GetVisibleCallback () const
00323   {
00324     return vis_cb;
00325   }
00326   virtual void NextFrame (csTicks current_time, const csVector3& /*pos*/, 
00327     uint /*currentFrame*/)
00328   {
00329     csTicks elaps = 0;
00330     if (prev_time != 0) elaps = current_time-prev_time;
00331     if (elaps > 200) elaps = 200;
00332     prev_time = current_time;
00333     Update (elaps);
00334   }
00335   virtual int HitBeamBBox (const csVector3&, const csVector3&,
00336         csVector3&, float*)
00337   { return -1; }
00338   virtual bool HitBeamOutline (const csVector3&, const csVector3&,
00339         csVector3&, float*)
00340   { return false; }
00341   virtual bool HitBeamObject (const csVector3& start, const csVector3& end,
00342     csVector3& isect, float* pr, int* polygon_idx = 0, 
00343     iMaterialWrapper** material = 0) 
00344   { 
00345     (void)start; (void)end; (void)isect; (void)pr; (void)polygon_idx;
00346     (void)material;
00347     return false; 
00348   }
00349   virtual void SetMeshWrapper (iMeshWrapper* lp) { logparent = lp; }
00350   virtual iMeshWrapper* GetMeshWrapper () const { return logparent; }
00351 
00352   //------------------------- iObjectModel implementation ----------------
00353   virtual iObjectModel* GetObjectModel () { return this; }
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 iTerraFormer* GetTerraFormerColldet () { return 0; }
00373   virtual iTerrainSystem* GetTerrainColldet () { return 0; }
00374   //------------------------- iParticleState implementation ----------------
00375   virtual void SetMixMode (uint mode)
00376   {
00377     MixMode = mode;
00378     SetupMixMode ();
00379   }
00380   virtual uint GetMixMode () const { return MixMode; }
00381   virtual void InvalidateMaterialHandles () { }
00386   virtual void PositionChild (iMeshObject* /*child*/,csTicks /*current_time*/) { }
00387 
00392   virtual void BuildDecal(const csVector3* pos, float decalRadius,
00393         iDecalBuilder* decalBuilder)
00394   {
00395   }
00396 };
00397 
00402 class CS_CRYSTALSPACE_EXPORT csNewtonianParticleSystem :
00403   public scfImplementationExt0<csNewtonianParticleSystem, csParticleSystem>
00404 {
00405 protected:
00407   csVector3 *part_speed;
00409   csVector3 *part_accel;
00410 
00411 public:
00413   csNewtonianParticleSystem (iObjectRegistry* object_reg,
00414         iMeshObjectFactory* factory);
00415   virtual ~csNewtonianParticleSystem ();
00416 
00417   void SetCount (int max);
00418 
00420   virtual void Update (csTicks elapsed_time);
00421 
00423   csVector3& GetSpeed (int idx) const { return part_speed[idx]; }
00425   void SetSpeed (int idx, const csVector3& spd)
00426   { part_speed[idx] = spd; }
00427 
00429   csVector3& GetAccel (int idx) const { return part_accel[idx]; }
00431   void SetAccel (int idx, const csVector3& acl)
00432   { part_accel[idx] = acl; }
00433 };
00434 
00435 #include "csutil/win32/msvc_deprecated_warn_on.h"
00436 
00439 #endif // __CS_CSPLUGINCOMMON_PARTICLESYS_PARTGEN_H__

Generated for Crystal Space 1.2.1 by doxygen 1.5.3