Planeshift

pseffectobj.h

Go to the documentation of this file.
00001 /*
00002  * Author: Andrew Robberts
00003  *
00004  * Copyright (C) 2003 Atomic Blue ([email protected], http://www.atomicblue.org)
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation (version 2 of the License)
00010  * This program 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
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 
00020 #ifndef PS_EFFECT_OBJ_HEADER
00021 #define PS_EFFECT_OBJ_HEADER
00022 
00023 #include <csgfx/shadervar.h>
00024 #include <csutil/csstring.h>
00025 #include <csutil/array.h>
00026 #include <csutil/bitarray.h>
00027 #include <csutil/parray.h>
00028 #include <csutil/refcount.h>
00029 #include <csutil/leakguard.h>
00030 #include <iutil/virtclk.h>
00031 #include <imesh/object.h>
00032 #include <ivideo/rendermesh.h>
00033 #include <csgeom/matrix3.h>
00034 #include <csgeom/vector3.h>
00035 
00036 
00037 struct iDocumentNode;
00038 struct iEngine;
00039 struct iView;
00040 struct iMeshFactoryWrapper;
00041 struct iSector;
00042 struct iCollection;
00043 struct iLoaderContext;
00044 
00045 class psEffect2DRenderer;
00046 class psEffectAnchor;
00047 
00055 class psEffectObjKeyFrame
00056 {
00057 public:
00058     psEffectObjKeyFrame();
00059     psEffectObjKeyFrame(const psEffectObjKeyFrame* other);
00060     psEffectObjKeyFrame(iDocumentNode* node, const psEffectObjKeyFrame* prevKeyFrame);
00061     ~psEffectObjKeyFrame();
00062 
00068     bool SetParamScalings(const float* scale);
00069 
00070 
00072     csTicks time;
00073 
00074     enum INTERP_TYPE
00075     {
00076         IT_NONE = 0,
00077         IT_FLOOR,
00078         IT_CEILING,
00079         IT_LERP,
00080 
00081         IT_COUNT
00082     };
00083 
00084     enum KEY_ACTION
00085     {
00086         KA_SCALE = 1,
00087         KA_TOPSCALE,
00088         KA_CELL,
00089         KA_ALPHA,
00090         KA_HEIGHT,
00091         KA_PADDING,
00092         KA_ANIMATE,
00093 
00094         KA_COUNT
00095     };
00096 
00097     enum KEY_VEC_ACTION
00098     {
00099         KA_POS = KA_COUNT,
00100         KA_ROT,
00101         KA_SPIN,
00102         KA_COLOUR,
00103 
00104         KA_VEC_COUNT
00105     };
00106 
00107     float     actions[KA_COUNT];
00108     csVector3 vecActions[KA_VEC_COUNT - KA_COUNT];
00109     int       useScale[KA_VEC_COUNT];  
00110 
00112     csBitArray specAction;
00113 };
00114 
00118 class psEffectObjKeyFrameGroup : public csRefCount
00119 {
00120 private:
00121     csPDelArray<psEffectObjKeyFrame> keyFrames;
00122 
00123 public:
00124     psEffectObjKeyFrameGroup();
00125     ~psEffectObjKeyFrameGroup();
00126 
00132     size_t GetSize() const
00133     {
00134         return keyFrames.GetSize();
00135     }
00136 
00143     psEffectObjKeyFrame* Get(size_t idx) const
00144     {
00145         return keyFrames[idx];
00146     }
00147 
00154     psEffectObjKeyFrame* operator [](size_t idx) const
00155     {
00156         return keyFrames[idx];
00157     }
00158 
00164     void Push(psEffectObjKeyFrame* keyFrame)
00165     {
00166         keyFrames.Push(keyFrame);
00167     }
00168 
00174     void DeleteIndex(size_t idx)
00175     {
00176         keyFrames.DeleteIndex(idx);
00177     }
00178 
00182     void DeleteAll()
00183     {
00184         keyFrames.DeleteAll();
00185     }
00186 
00190     csPtr<psEffectObjKeyFrameGroup> Clone() const;    //ticket 6051
00191 
00198     bool SetFrameParamScalings(const float* scale);
00199 
00200 };
00201 
00207 class psEffectObj
00208 {
00209 public:
00210     psEffectObj(iView* parentView, psEffect2DRenderer* renderer2d);
00211     virtual ~psEffectObj();
00212 
00220     virtual bool Load(iDocumentNode* node, iLoaderContext* ldr_context);
00221 
00228     virtual bool Render(const csVector3 &up);
00229 
00233     virtual bool SetScaling(float scale, float aspect);
00234 
00241     virtual bool SetFrameParamScalings(const float* scale);
00242 
00249     virtual bool Update(csTicks elapsed);
00250 
00256     virtual void CloneBase(psEffectObj* newObj) const;
00257 
00263     virtual psEffectObj* Clone() const;
00264 
00271     virtual bool AttachToAnchor(psEffectAnchor* newAnchor);
00272 
00278     virtual void Show(bool value);
00279 
00285     int GetKillTime() const
00286     {
00287         return killTime;
00288     }
00289 
00295     void SetKillTime(int newKillTime)
00296     {
00297         killTime = newKillTime;
00298     }
00299 
00305     void SetRotBase(const csMatrix3 &newRotBase)
00306     {
00307         if(dir == DT_TO_TARGET)
00308             matBase = newRotBase;
00309     }
00310 
00316     void SetPosition(const csMatrix3 &newPosTransf)
00317     {
00318         if(dir == DT_ORIGIN)
00319             matBase = newPosTransf;
00320     }
00321 
00327     void SetTarget(const csMatrix3 &newTargetTransf)
00328     {
00329         if(dir == DT_TARGET)
00330             matBase = newTargetTransf;
00331     }
00332 
00338     int GetDirection() const
00339     {
00340         return dir;
00341     }
00342 
00348     void SetAnchorName(const csString &anchor)
00349     {
00350         anchorName = anchor;
00351     }
00352 
00358     const csString &GetAnchorName() const
00359     {
00360         return anchorName;
00361     }
00362 
00368     CS::Graphics::RenderPriority GetRenderPriority() const
00369     {
00370         return priority;
00371     }
00372 
00378     csZBufMode GetZBufMode() const
00379     {
00380         return zFunc;
00381     }
00382 
00388     unsigned int GetMixMode() const
00389     {
00390         return mixmode;
00391     }
00392 
00398     float GetBirth() const
00399     {
00400         return birth;
00401     }
00402 
00408     float GetAnimLength() const
00409     {
00410         return animLength;
00411     }
00412 
00413     void SetAnimationScaling(float s);
00414 
00420     csString GetName() const
00421     {
00422         return name;
00423     }
00424 
00430     size_t GetKeyFrameCount() const
00431     {
00432         return keyFrames->GetSize();
00433     }
00434 
00441     psEffectObjKeyFrame* GetKeyFrame(size_t idx) const
00442     {
00443         return keyFrames->Get(idx);
00444     }
00445 
00446     enum DIR_TYPE
00447     {
00448         DT_NONE = 0,
00449         DT_ORIGIN,
00450         DT_TARGET,
00451         DT_TO_TARGET,
00452         DT_CAMERA,
00453         DT_BILLBOARD,
00454 
00455         DT_COUNT
00456     };
00457 
00458 protected:
00459 
00460     enum SCALING_TYPE
00461     {
00462         SCALING_NONE   = 0,
00463         SCALING_BIRTH  = 1,
00464         SCALING_DEATH  = 2,
00465         SCALING_FRAMES = 4,
00466         SCALING_LOOP   = 8
00467     };
00468 
00475     size_t FindKeyFrameByTime(csTicks time) const;
00476 
00485     bool FindNextKeyFrameWithAction(size_t startFrame, size_t action, size_t &index) const;
00486 
00490     void FillInLerps();
00491 
00498     csMatrix3 BuildRotMatrix(const csVector3 &up) const;
00499 
00500     csRef<iShaderVarStringSet> stringSet;
00501 
00502     csString name;
00503     csString materialName;
00504 
00505     int killTime;
00506     csTicks life;
00507     csTicks animLength;
00508     float animScaling;
00509     int autoScale;
00510 
00511     // the effect anchor that this obj is attached to
00512     csString anchorName;
00513     csRef<iMeshWrapper> anchorMesh;
00514     psEffectAnchor* anchor;
00515 
00516     csVector3 target;
00517 
00518     csVector3 objUp;
00519     csMatrix3 matBase;
00520     csMatrix3 matUp;
00521 
00522     csRef<iMeshFactoryWrapper> meshFact;
00523     csRef<iMeshWrapper> mesh;
00524 
00525     csTicks birth;
00526     bool isAlive;
00527     float baseScale;
00528 
00529     psEffect2DRenderer* renderer2d;
00530 
00531     csZBufMode zFunc;
00532     CS::Graphics::RenderPriority priority;
00533     unsigned int mixmode;
00534 
00535     // direction
00536     int dir;
00537 
00538     // used for the update loop
00539     size_t currKeyFrame;
00540     size_t nextKeyFrame;
00541 
00542     //csArray<psEffectObjKeyFrame> keyFrames;
00543     csRef<psEffectObjKeyFrameGroup> keyFrames;
00544 
00545     // CS references
00546     csRef<iEngine> engine;
00547     csRef<iView> view;
00548     csRef<iStringSet> globalStringSet;
00549 
00551     csRef<iCollection> effectsCollection;
00552 
00553     float scale;
00554     float aspect;
00555 
00556     inline float lerp(float f1, float f2, float factor)
00557     {
00558         if(factor == 0.f)
00559         {
00560             return f1;
00561         }
00562         else
00563         {
00564             return f1 + (f2-f1)*factor;
00565         }
00566     }
00567 
00568     inline csVector3 lerpVec(const csVector3 &v1, const csVector3 &v2, float factor)
00569     {
00570         if(factor == 0.f)
00571         {
00572             return v1;
00573         }
00574         else
00575         {
00576             return v1 + (v2-v1)*factor;
00577         }
00578     }
00579 
00580     inline float lerpFactor(csTicks t1, csTicks t2, csTicks t)
00581     {
00582         if(t2 == t1)
00583         {
00584             return 0.f;
00585         }
00586         else
00587         {
00588             if(autoScale & SCALING_FRAMES)
00589             {
00590                 t /= animScaling;
00591             }
00592             else if(autoScale & SCALING_LOOP)
00593             {
00594                 t %= animLength;
00595             }
00596 
00597             return ((float)(t-t1)/(float)(t2-t1));
00598         }
00599     }
00600 };
00601 
00602 #define LERP_KEY(action,factor) \
00603     lerp(keyFrames->Get(currKeyFrame)->actions[psEffectObjKeyFrame::action], \
00604          keyFrames->Get(nextKeyFrame)->actions[psEffectObjKeyFrame::action], \
00605          factor)
00606 
00607 #define LERP_VEC_KEY(action,factor) \
00608     lerpVec(keyFrames->Get(currKeyFrame)->vecActions[psEffectObjKeyFrame::action - psEffectObjKeyFrame::KA_COUNT], \
00609             keyFrames->Get(nextKeyFrame)->vecActions[psEffectObjKeyFrame::action - psEffectObjKeyFrame::KA_COUNT], \
00610             factor)
00611 
00612 #define LERP_FACTOR \
00613     lerpFactor(keyFrames->Get(currKeyFrame)->time, \
00614                keyFrames->Get(nextKeyFrame)->time, \
00615                life)
00616 
00619 #endif