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 "csgeom/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 00061 class CS_CRYSTALSPACE_EXPORT csParticleSystem : 00062 public scfImplementationExt2<csParticleSystem, 00063 csObjectModel, iMeshObject, iParticleState> 00064 { 00065 protected: 00066 iObjectRegistry* object_reg; 00067 iMeshObjectFactory* factory; 00068 iMeshWrapper* logparent; 00069 iEngine* engine; 00070 csRef<iLightManager> light_mgr; 00072 csVector3 radius; 00074 csRefArray<iSprite2DState> sprite2ds; 00075 csRefArray<iParticle> particles; 00077 bool self_destruct; 00078 csTicks time_to_live; // msec 00080 csColor color; 00082 csRef<iMaterialWrapper> mat; 00084 uint MixMode; 00086 bool change_color; csColor colorpersecond; 00088 bool change_size; float scalepersecond; 00090 bool change_alpha; float alphapersecond; float alpha_now; 00092 bool change_rotation; float anglepersecond; 00098 csBox3 bbox; 00099 iMeshObjectDrawCallback* vis_cb; 00100 00102 csRef<iMeshObjectFactory> spr_factory; 00104 csTicks prev_time; 00105 float current_lod; 00106 uint32 current_features; 00107 00108 csFlags flags; 00109 00110 // Number of particles. 00111 size_t number; 00113 csVector3 *part_pos; 00114 00115 csRenderMeshHolder rmHolder; 00116 00117 size_t VertexCount; 00118 size_t TriangleCount; 00119 struct PerFrameData 00120 { 00121 csRef<csRenderBufferHolder> bufferHolder; 00122 csRef<iRenderBuffer> vertex_buffer; 00123 csRef<iRenderBuffer> texel_buffer; 00124 csRef<iRenderBuffer> color_buffer; 00125 }; 00126 csFrameDataHolder<PerFrameData> perFrameHolder; 00127 csRef<iRenderBuffer> index_buffer; 00128 00129 csRef<iGraphics3D> g3d; 00130 00132 void SetupBuffers (size_t part_sides); 00133 size_t part_sides; // Number of vertices per particle (for buffers). 00134 00135 bool initialized; 00137 virtual void SetupObject (); 00138 00139 // Call if object needs changing. 00140 void ChangeObject () 00141 { 00142 initialized = false; 00143 ShapeChanged (); 00144 } 00145 00146 private: 00147 csRandomFloatGen randgen; 00148 protected: 00150 csVector3 GetRandomDirection(); 00152 csVector3 GetRandomDirection(csVector3 const& magnitude, 00153 csVector3 const& offset); 00155 csVector3 GetRandomPosition(csBox3 const& box); 00156 00157 public: 00162 csParticleSystem (iObjectRegistry* object_reg, iMeshObjectFactory* factory); 00163 00167 virtual ~csParticleSystem (); 00168 00170 virtual void SetParticleCount (size_t num) 00171 { 00172 number = num; 00173 ChangeObject (); 00174 } 00176 size_t GetParticleCount () const { return number; } 00177 00178 void UpdateLighting (const csArray<iLightSectorInfluence*>& lights, 00179 iMovable* movable); 00180 00182 inline size_t GetNumParticles () const { return particles.Length();} 00184 inline iParticle* GetParticle (size_t idx) const 00185 { return particles[idx]; } 00187 void RemoveParticles (); 00188 00190 inline void AppendParticle (iParticle *part, iSprite2DState* spr2d) 00191 { 00192 sprite2ds.Push (spr2d); 00193 particles.Push (part); 00194 } 00195 00200 void AppendRectSprite (float width, float height, iMaterialWrapper* mat, 00201 bool lighted); 00202 00207 void AppendRegularSprite (int n, float radius, iMaterialWrapper* mat, 00208 bool lighted); 00209 00211 inline void SetSelfDestruct (csTicks t) 00212 { self_destruct=true; time_to_live = t; }; 00214 inline void UnSetSelfDestruct () { self_destruct=false; } 00216 inline bool GetSelfDestruct () const { return self_destruct; } 00218 inline csTicks GetTimeToLive () const { return time_to_live; } 00219 00221 inline void SetChangeColor(const csColor& col) 00222 {change_color = true; colorpersecond = col;} 00224 inline void UnsetChangeColor() {change_color=false;} 00226 inline bool GetChangeColor (csColor& col) const 00227 { if(!change_color) return false; col = colorpersecond; return true; } 00228 00230 inline void SetChangeSize(float factor) 00231 {change_size = true; scalepersecond = factor;} 00233 inline void UnsetChangeSize() {change_size=false;} 00235 inline bool GetChangeSize (float& factor) const 00236 { if(!change_size) return false; factor = scalepersecond; return true; } 00237 00239 inline void SetAlpha(float alpha) 00240 {alpha_now = alpha; MixMode = CS_FX_SETALPHA (alpha); SetupMixMode (); } 00242 inline float GetAlpha() const {return alpha_now;} 00244 inline void SetChangeAlpha(float factor) 00245 {change_alpha = true; alphapersecond = factor;} 00247 inline void UnsetChangeAlpha() {change_alpha=false;} 00249 inline bool GetChangeAlpha (float& factor) const 00250 { if(!change_alpha) return false; factor = alphapersecond; return true; } 00251 00253 inline void SetChangeRotation(float angle) 00254 {change_rotation = true; anglepersecond = angle;} 00256 inline void UnsetChangeRotation() {change_rotation=false;} 00258 inline bool GetChangeRotation (float& angle) const 00259 { if(!change_rotation) return false; angle = anglepersecond; return true; } 00260 00262 inline const csBox3& GetBoundingBox() const {return bbox;} 00263 00265 virtual void SetupColor (); 00267 virtual void AddColor (const csColor& col); 00269 virtual void ScaleBy(float factor); 00271 virtual void SetupMixMode (); 00273 virtual void Rotate(float angle); 00274 00281 virtual void Update (csTicks elapsed_time); 00282 00283 void GetObjectBoundingBox (csBox3& bbox) 00284 { 00285 SetupObject (); 00286 bbox = csParticleSystem::bbox; 00287 } 00288 void SetObjectBoundingBox (const csBox3& bbox) 00289 { 00290 csParticleSystem::bbox = bbox; 00291 ShapeChanged (); 00292 } 00293 void GetRadius (csVector3& rad, csVector3& cent) 00294 { 00295 SetupObject (); 00296 rad = radius; 00297 cent = bbox.GetCenter(); 00298 } 00299 00300 //----------------------- iMeshObject implementation ------------------------ 00301 virtual iMeshObjectFactory* GetFactory () const { return factory; } 00302 virtual csFlags& GetFlags () { return flags; } 00303 virtual csPtr<iMeshObject> Clone () { return 0; } 00304 virtual bool PreGetRenderMeshes (iRenderView* rview, iMovable* movable, 00305 uint32 frustum_mask); 00306 virtual csRenderMesh** GetRenderMeshes (int& n, iRenderView* rview, 00307 iMovable* movable, uint32 frustum_mask); 00308 virtual void SetVisibleCallback (iMeshObjectDrawCallback* cb) 00309 { 00310 if (cb) cb->IncRef (); 00311 if (vis_cb) vis_cb->DecRef (); 00312 vis_cb = cb; 00313 } 00314 virtual iMeshObjectDrawCallback* GetVisibleCallback () const 00315 { 00316 return vis_cb; 00317 } 00318 virtual void NextFrame (csTicks current_time, const csVector3& /*pos*/) 00319 { 00320 csTicks elaps = 0; 00321 if (prev_time != 0) elaps = current_time-prev_time; 00322 if (elaps > 200) elaps = 200; 00323 prev_time = current_time; 00324 Update (elaps); 00325 } 00326 virtual int HitBeamBBox (const csVector3&, const csVector3&, 00327 csVector3&, float*) 00328 { return -1; } 00329 virtual bool HitBeamOutline (const csVector3&, const csVector3&, 00330 csVector3&, float*) 00331 { return false; } 00332 virtual bool HitBeamObject (const csVector3&, const csVector3&, 00333 csVector3&, float*, int* = 0, iMaterialWrapper** = 0) { return false; } 00334 virtual void SetMeshWrapper (iMeshWrapper* lp) { logparent = lp; } 00335 virtual iMeshWrapper* GetMeshWrapper () const { return logparent; } 00336 00337 //------------------------- iObjectModel implementation ---------------- 00338 virtual iObjectModel* GetObjectModel () { return this; } 00339 virtual bool SetColor (const csColor& col) 00340 { 00341 color = col; 00342 SetupColor (); 00343 return true; 00344 } 00345 virtual bool GetColor (csColor& col) const 00346 { 00347 col = color; 00348 return true; 00349 } 00350 virtual bool SetMaterialWrapper (iMaterialWrapper* mat) 00351 { 00352 initialized = false; 00353 csParticleSystem::mat = mat; 00354 return true; 00355 } 00356 virtual iMaterialWrapper* GetMaterialWrapper () const { return mat; } 00357 //------------------------- iParticleState implementation ---------------- 00358 virtual void SetMixMode (uint mode) 00359 { 00360 MixMode = mode; 00361 SetupMixMode (); 00362 } 00363 virtual uint GetMixMode () const { return MixMode; } 00364 virtual void InvalidateMaterialHandles () { } 00369 virtual void PositionChild (iMeshObject* /*child*/,csTicks /*current_time*/) { } 00370 }; 00371 00376 class CS_CRYSTALSPACE_EXPORT csNewtonianParticleSystem : 00377 public scfImplementationExt0<csNewtonianParticleSystem, csParticleSystem> 00378 { 00379 protected: 00381 csVector3 *part_speed; 00383 csVector3 *part_accel; 00384 00385 public: 00387 csNewtonianParticleSystem (iObjectRegistry* object_reg, 00388 iMeshObjectFactory* factory); 00389 virtual ~csNewtonianParticleSystem (); 00390 00391 void SetCount (int max); 00392 00394 virtual void Update (csTicks elapsed_time); 00395 00397 csVector3& GetSpeed (int idx) const { return part_speed[idx]; } 00399 void SetSpeed (int idx, const csVector3& spd) 00400 { part_speed[idx] = spd; } 00401 00403 csVector3& GetAccel (int idx) const { return part_accel[idx]; } 00405 void SetAccel (int idx, const csVector3& acl) 00406 { part_accel[idx] = acl; } 00407 }; 00408 00411 #endif // __CS_CSPLUGINCOMMON_PARTICLESYS_PARTGEN_H__
Generated for Crystal Space by doxygen 1.4.6