Planeshift
pseffectobj.h
Go to the documentation of this file.
1 /*
2  * Author: Andrew Robberts
3  *
4  * Copyright (C) 2003 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #ifndef PS_EFFECT_OBJ_HEADER
21 #define PS_EFFECT_OBJ_HEADER
22 
23 #include <csgfx/shadervar.h>
24 #include <csutil/csstring.h>
25 #include <csutil/array.h>
26 #include <csutil/bitarray.h>
27 #include <csutil/parray.h>
28 #include <csutil/refcount.h>
29 #include <csutil/leakguard.h>
30 #include <iutil/virtclk.h>
31 #include <imesh/object.h>
32 #include <ivideo/rendermesh.h>
33 #include <csgeom/matrix3.h>
34 #include <csgeom/vector3.h>
35 
36 
37 struct iDocumentNode;
38 struct iEngine;
39 struct iView;
40 struct iMeshFactoryWrapper;
41 struct iSector;
42 struct iCollection;
43 struct iLoaderContext;
44 
45 class psEffect2DRenderer;
46 class psEffectAnchor;
47 
56 {
57 public:
60  psEffectObjKeyFrame(iDocumentNode* node, const psEffectObjKeyFrame* prevKeyFrame);
62 
68  bool SetParamScalings(const float* scale);
69 
70 
72  csTicks time;
73 
75  {
76  IT_NONE = 0,
80 
82  };
83 
85  {
86  KA_SCALE = 1,
93 
95  };
96 
98  {
103 
105  };
106 
110 
112  csBitArray specAction;
113 };
114 
119 {
120 private:
121  csPDelArray<psEffectObjKeyFrame> keyFrames;
122 
123 public:
126 
132  size_t GetSize() const
133  {
134  return keyFrames.GetSize();
135  }
136 
143  psEffectObjKeyFrame* Get(size_t idx) const
144  {
145  return keyFrames[idx];
146  }
147 
154  psEffectObjKeyFrame* operator [](size_t idx) const
155  {
156  return keyFrames[idx];
157  }
158 
164  void Push(psEffectObjKeyFrame* keyFrame)
165  {
166  keyFrames.Push(keyFrame);
167  }
168 
174  void DeleteIndex(size_t idx)
175  {
176  keyFrames.DeleteIndex(idx);
177  }
178 
182  void DeleteAll()
183  {
184  keyFrames.DeleteAll();
185  }
186 
190  csPtr<psEffectObjKeyFrameGroup> Clone() const; //ticket 6051
191 
198  bool SetFrameParamScalings(const float* scale);
199 
200 };
201 
208 {
209 public:
210  psEffectObj(iView* parentView, psEffect2DRenderer* renderer2d);
211  virtual ~psEffectObj();
212 
220  virtual bool Load(iDocumentNode* node, iLoaderContext* ldr_context);
221 
228  virtual bool Render(const csVector3 &up);
229 
233  virtual bool SetScaling(float scale, float aspect);
234 
241  virtual bool SetFrameParamScalings(const float* scale);
242 
249  virtual bool Update(csTicks elapsed);
250 
256  virtual void CloneBase(psEffectObj* newObj) const;
257 
263  virtual psEffectObj* Clone() const;
264 
271  virtual bool AttachToAnchor(psEffectAnchor* newAnchor);
272 
278  virtual void Show(bool value);
279 
285  int GetKillTime() const
286  {
287  return killTime;
288  }
289 
295  void SetKillTime(int newKillTime)
296  {
297  killTime = newKillTime;
298  }
299 
305  void SetRotBase(const csMatrix3 &newRotBase)
306  {
307  if(dir == DT_TO_TARGET)
308  matBase = newRotBase;
309  }
310 
316  void SetPosition(const csMatrix3 &newPosTransf)
317  {
318  if(dir == DT_ORIGIN)
319  matBase = newPosTransf;
320  }
321 
327  void SetTarget(const csMatrix3 &newTargetTransf)
328  {
329  if(dir == DT_TARGET)
330  matBase = newTargetTransf;
331  }
332 
338  int GetDirection() const
339  {
340  return dir;
341  }
342 
348  void SetAnchorName(const csString &anchor)
349  {
350  anchorName = anchor;
351  }
352 
358  const csString &GetAnchorName() const
359  {
360  return anchorName;
361  }
362 
368  CS::Graphics::RenderPriority GetRenderPriority() const
369  {
370  return priority;
371  }
372 
378  csZBufMode GetZBufMode() const
379  {
380  return zFunc;
381  }
382 
388  unsigned int GetMixMode() const
389  {
390  return mixmode;
391  }
392 
398  float GetBirth() const
399  {
400  return birth;
401  }
402 
408  float GetAnimLength() const
409  {
410  return animLength;
411  }
412 
413  void SetAnimationScaling(float s);
414 
420  csString GetName() const
421  {
422  return name;
423  }
424 
430  size_t GetKeyFrameCount() const
431  {
432  return keyFrames->GetSize();
433  }
434 
441  psEffectObjKeyFrame* GetKeyFrame(size_t idx) const
442  {
443  return keyFrames->Get(idx);
444  }
445 
446  enum DIR_TYPE
447  {
448  DT_NONE = 0,
454 
455  DT_COUNT
456  };
457 
458 protected:
459 
461  {
462  SCALING_NONE = 0,
463  SCALING_BIRTH = 1,
464  SCALING_DEATH = 2,
465  SCALING_FRAMES = 4,
466  SCALING_LOOP = 8
467  };
468 
475  size_t FindKeyFrameByTime(csTicks time) const;
476 
485  bool FindNextKeyFrameWithAction(size_t startFrame, size_t action, size_t &index) const;
486 
490  void FillInLerps();
491 
498  csMatrix3 BuildRotMatrix(const csVector3 &up) const;
499 
500  csRef<iShaderVarStringSet> stringSet;
501 
502  csString name;
503  csString materialName;
504 
505  int killTime;
506  csTicks life;
507  csTicks animLength;
508  float animScaling;
510 
511  // the effect anchor that this obj is attached to
512  csString anchorName;
513  csRef<iMeshWrapper> anchorMesh;
515 
516  csVector3 target;
517 
518  csVector3 objUp;
519  csMatrix3 matBase;
520  csMatrix3 matUp;
521 
522  csRef<iMeshFactoryWrapper> meshFact;
523  csRef<iMeshWrapper> mesh;
524 
525  csTicks birth;
526  bool isAlive;
527  float baseScale;
528 
530 
531  csZBufMode zFunc;
532  CS::Graphics::RenderPriority priority;
533  unsigned int mixmode;
534 
535  // direction
536  int dir;
537 
538  // used for the update loop
539  size_t currKeyFrame;
540  size_t nextKeyFrame;
541 
542  //csArray<psEffectObjKeyFrame> keyFrames;
543  csRef<psEffectObjKeyFrameGroup> keyFrames;
544 
545  // CS references
546  csRef<iEngine> engine;
547  csRef<iView> view;
548  csRef<iStringSet> globalStringSet;
549 
551  csRef<iCollection> effectsCollection;
552 
553  float scale;
554  float aspect;
555 
556  inline float lerp(float f1, float f2, float factor)
557  {
558  if(factor == 0.f)
559  {
560  return f1;
561  }
562  else
563  {
564  return f1 + (f2-f1)*factor;
565  }
566  }
567 
568  inline csVector3 lerpVec(const csVector3 &v1, const csVector3 &v2, float factor)
569  {
570  if(factor == 0.f)
571  {
572  return v1;
573  }
574  else
575  {
576  return v1 + (v2-v1)*factor;
577  }
578  }
579 
580  inline float lerpFactor(csTicks t1, csTicks t2, csTicks t)
581  {
582  if(t2 == t1)
583  {
584  return 0.f;
585  }
586  else
587  {
588  if(autoScale & SCALING_FRAMES)
589  {
590  t /= animScaling;
591  }
592  else if(autoScale & SCALING_LOOP)
593  {
594  t %= animLength;
595  }
596 
597  return ((float)(t-t1)/(float)(t2-t1));
598  }
599  }
600 };
601 
602 #define LERP_KEY(action,factor) \
603  lerp(keyFrames->Get(currKeyFrame)->actions[psEffectObjKeyFrame::action], \
604  keyFrames->Get(nextKeyFrame)->actions[psEffectObjKeyFrame::action], \
605  factor)
606 
607 #define LERP_VEC_KEY(action,factor) \
608  lerpVec(keyFrames->Get(currKeyFrame)->vecActions[psEffectObjKeyFrame::action - psEffectObjKeyFrame::KA_COUNT], \
609  keyFrames->Get(nextKeyFrame)->vecActions[psEffectObjKeyFrame::action - psEffectObjKeyFrame::KA_COUNT], \
610  factor)
611 
612 #define LERP_FACTOR \
613  lerpFactor(keyFrames->Get(currKeyFrame)->time, \
614  keyFrames->Get(nextKeyFrame)->time, \
615  life)
616 
619 #endif
size_t GetKeyFrameCount() const
Returns the number of keyframes in this obj.
Definition: pseffectobj.h:430
size_t GetSize() const
Returns the number of keyframes in the group.
Definition: pseffectobj.h:132
unsigned int mixmode
Definition: pseffectobj.h:533
csString name
Definition: pseffectobj.h:502
csRef< iShaderVarStringSet > stringSet
Definition: pseffectobj.h:500
psEffectObjKeyFrame * GetKeyFrame(size_t idx) const
Returns the keyframe at the given index.
Definition: pseffectobj.h:441
float aspect
Definition: pseffectobj.h:554
csBitArray specAction
keep track of which actions were specified for which
Definition: pseffectobj.h:112
The manager of all 2D effect elements.
void DeleteIndex(size_t idx)
Deletes the keyframe at the given index.
Definition: pseffectobj.h:174
csString anchorName
Definition: pseffectobj.h:512
csVector3 vecActions[KA_VEC_COUNT-KA_COUNT]
Definition: pseffectobj.h:108
csVector3 lerpVec(const csVector3 &v1, const csVector3 &v2, float factor)
Definition: pseffectobj.h:568
csRef< psEffectObjKeyFrameGroup > keyFrames
Definition: pseffectobj.h:543
psEffectObjKeyFrame * Get(size_t idx) const
Returns the keyframe at the given index.
Definition: pseffectobj.h:143
int useScale[KA_VEC_COUNT]
If set upon load the actions are scaled after cloning. 0 no scaling, 1=scale1, 2=scale2, 3=scale3.
Definition: pseffectobj.h:109
CS::Graphics::RenderPriority GetRenderPriority() const
Get the priority for rendering the mesh.
Definition: pseffectobj.h:368
int GetDirection() const
Get the direction.
Definition: pseffectobj.h:338
int GetKillTime() const
Gets the time left that the effect obj has to live.
Definition: pseffectobj.h:285
csTicks life
Definition: pseffectobj.h:506
size_t nextKeyFrame
Definition: pseffectobj.h:540
Effect objects KeyFrame group.
Definition: pseffectobj.h:118
size_t currKeyFrame
Definition: pseffectobj.h:539
csRef< iMeshWrapper > mesh
Definition: pseffectobj.h:523
float baseScale
Definition: pseffectobj.h:527
void SetRotBase(const csMatrix3 &newRotBase)
Sets the base rotation matrix of the effect obj.
Definition: pseffectobj.h:305
csTicks animLength
Definition: pseffectobj.h:507
Stores data for a specific effect object keyframe.
Definition: pseffectobj.h:55
float actions[KA_COUNT]
Definition: pseffectobj.h:107
csZBufMode zFunc
Definition: pseffectobj.h:531
csMatrix3 matBase
Definition: pseffectobj.h:519
csTicks time
this is the time of the keyframe animation (in milliseconds)
Definition: pseffectobj.h:72
csRef< iView > view
Definition: pseffectobj.h:547
csTicks birth
Definition: pseffectobj.h:525
csRef< iMeshFactoryWrapper > meshFact
Definition: pseffectobj.h:522
void Push(psEffectObjKeyFrame *keyFrame)
Pushes a keyframe onto the group.
Definition: pseffectobj.h:164
unsigned int GetMixMode() const
Get the mixmode for rendering the mesh.
Definition: pseffectobj.h:388
An effect is not much more than a collection of effect objects.
Definition: pseffectobj.h:207
void SetKillTime(int newKillTime)
Sets the new time left that the effect obj has to live.
Definition: pseffectobj.h:295
csVector3 target
Definition: pseffectobj.h:516
float GetAnimLength() const
Accessor function to get the animation length of this effect obj.
Definition: pseffectobj.h:408
csString materialName
Definition: pseffectobj.h:503
void SetTarget(const csMatrix3 &newTargetTransf)
Sets the target rotation of the effect obj.
Definition: pseffectobj.h:327
csRef< iStringSet > globalStringSet
Definition: pseffectobj.h:548
csString GetName() const
Gets the name of this effect obj.
Definition: pseffectobj.h:420
csVector3 objUp
Definition: pseffectobj.h:518
const csString & GetAnchorName() const
Gets the name of the anchor that this effect obj is attached to.
Definition: pseffectobj.h:358
void SetAnchorName(const csString &anchor)
Sets the name of the anchor that this effect obj is attached to.
Definition: pseffectobj.h:348
psEffectAnchor * anchor
Definition: pseffectobj.h:514
void DeleteAll()
Deletes all of the keyframes in this group.
Definition: pseffectobj.h:182
void SetPosition(const csMatrix3 &newPosTransf)
Sets the position rotation of the effect obj.
Definition: pseffectobj.h:316
csMatrix3 matUp
Definition: pseffectobj.h:520
bool SetParamScalings(const float *scale)
Adjust each parameter that have the use_scale property set with this scale.
float lerpFactor(csTicks t1, csTicks t2, csTicks t)
Definition: pseffectobj.h:580
CS::Graphics::RenderPriority priority
Definition: pseffectobj.h:532
csRef< iEngine > engine
Definition: pseffectobj.h:546
float GetBirth() const
Get the birth.
Definition: pseffectobj.h:398
float animScaling
Definition: pseffectobj.h:508
csZBufMode GetZBufMode() const
Get the zbuf mode for rendering the mesh.
Definition: pseffectobj.h:378
csRef< iCollection > effectsCollection
region to store the CS objects
Definition: pseffectobj.h:551
float lerp(float f1, float f2, float factor)
Definition: pseffectobj.h:556
csRef< iMeshWrapper > anchorMesh
Definition: pseffectobj.h:513
Effect anchors provide a base location / anchor point for all effect objs.
psEffect2DRenderer * renderer2d
Definition: pseffectobj.h:529