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/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 const csBox3& GetObjectBoundingBox () 00289 { 00290 SetupObject (); 00291 return bbox; 00292 } 00293 virtual void SetObjectBoundingBox (const csBox3& bbox) 00294 { 00295 csParticleSystem::bbox = bbox; 00296 ShapeChanged (); 00297 } 00298 virtual void GetRadius (float& rad, csVector3& cent) 00299 { 00300 SetupObject (); 00301 rad = radius; 00302 cent = bbox.GetCenter(); 00303 } 00304 00305 //----------------------- iMeshObject implementation ------------------------ 00306 virtual iMeshObjectFactory* GetFactory () const { return factory; } 00307 virtual csFlags& GetFlags () { return flags; } 00308 virtual csPtr<iMeshObject> Clone () { return 0; } 00309 virtual bool PreGetRenderMeshes (iRenderView* rview, iMovable* movable, 00310 uint32 frustum_mask); 00311 virtual CS::Graphics::RenderMesh** GetRenderMeshes (int& n, iRenderView* rview, 00312 iMovable* movable, uint32 frustum_mask); 00313 virtual void SetVisibleCallback (iMeshObjectDrawCallback* cb) 00314 { 00315 vis_cb = cb; 00316 } 00317 virtual iMeshObjectDrawCallback* GetVisibleCallback () const 00318 { 00319 return vis_cb; 00320 } 00321 virtual void NextFrame (csTicks current_time, const csVector3& /*pos*/, 00322 uint /*currentFrame*/) 00323 { 00324 csTicks elaps = 0; 00325 if (prev_time != 0) elaps = current_time-prev_time; 00326 if (elaps > 200) elaps = 200; 00327 prev_time = current_time; 00328 Update (elaps); 00329 } 00330 virtual int HitBeamBBox (const csVector3&, const csVector3&, 00331 csVector3&, float*) 00332 { return -1; } 00333 virtual bool HitBeamOutline (const csVector3&, const csVector3&, 00334 csVector3&, float*) 00335 { return false; } 00336 virtual bool HitBeamObject (const csVector3& start, const csVector3& end, 00337 csVector3& isect, float* pr, int* polygon_idx = 0, 00338 iMaterialWrapper** material = 0) 00339 { 00340 (void)start; (void)end; (void)isect; (void)pr; (void)polygon_idx; 00341 (void)material; 00342 return false; 00343 } 00344 virtual void SetMeshWrapper (iMeshWrapper* lp) { logparent = lp; } 00345 virtual iMeshWrapper* GetMeshWrapper () const { return logparent; } 00346 00347 //------------------------- iObjectModel implementation ---------------- 00348 virtual iObjectModel* GetObjectModel () { return this; } 00349 virtual bool SetColor (const csColor& col) 00350 { 00351 color = col; 00352 SetupColor (); 00353 return true; 00354 } 00355 virtual bool GetColor (csColor& col) const 00356 { 00357 col = color; 00358 return true; 00359 } 00360 virtual bool SetMaterialWrapper (iMaterialWrapper* mat) 00361 { 00362 initialized = false; 00363 csParticleSystem::mat = mat; 00364 return true; 00365 } 00366 virtual iMaterialWrapper* GetMaterialWrapper () const { return mat; } 00367 virtual iTerraFormer* GetTerraFormerColldet () { return 0; } 00368 virtual iTerrainSystem* GetTerrainColldet () { return 0; } 00369 //------------------------- iParticleState implementation ---------------- 00370 virtual void SetMixMode (uint mode) 00371 { 00372 MixMode = mode; 00373 SetupMixMode (); 00374 } 00375 virtual uint GetMixMode () const { return MixMode; } 00376 virtual void InvalidateMaterialHandles () { } 00381 virtual void PositionChild (iMeshObject* /*child*/,csTicks /*current_time*/) { } 00382 00387 virtual void BuildDecal(const csVector3* pos, float decalRadius, 00388 iDecalBuilder* decalBuilder) 00389 { 00390 } 00391 }; 00392 00397 class CS_CRYSTALSPACE_EXPORT csNewtonianParticleSystem : 00398 public scfImplementationExt0<csNewtonianParticleSystem, csParticleSystem> 00399 { 00400 protected: 00402 csVector3 *part_speed; 00404 csVector3 *part_accel; 00405 00406 public: 00408 csNewtonianParticleSystem (iObjectRegistry* object_reg, 00409 iMeshObjectFactory* factory); 00410 virtual ~csNewtonianParticleSystem (); 00411 00412 void SetCount (int max); 00413 00415 virtual void Update (csTicks elapsed_time); 00416 00418 csVector3& GetSpeed (int idx) const { return part_speed[idx]; } 00420 void SetSpeed (int idx, const csVector3& spd) 00421 { part_speed[idx] = spd; } 00422 00424 csVector3& GetAccel (int idx) const { return part_accel[idx]; } 00426 void SetAccel (int idx, const csVector3& acl) 00427 { part_accel[idx] = acl; } 00428 }; 00429 00430 #include "csutil/deprecated_warn_on.h" 00431 00434 #endif // __CS_CSPLUGINCOMMON_PARTICLESYS_PARTGEN_H__
Generated for Crystal Space 1.4.0 by doxygen 1.5.8