My Project
SceneState.h
1 #pragma once
2 
3 #include "util/unordered_array.hpp"
4 #include "SortedFaceGroups.h"
5 #include "util/ParaMemPool.h"
6 #include "IRefObject.h"
7 #include "ParaXModel/ParticleSystemRef.h"
8 #include "BillBoardViewInfo.h"
9 #include "BaseObject.h"
10 
11 using namespace std;
12 
13 namespace ParaEngine
14 {
15  class CBaseObject;
16  class IBatchedElementDraw;
17  class CParaWorldAsset;
18  class CContainerObject;
19  class CSpriteObject;
20  class CBipedObject;
21  struct GUIState;
22  class ParticleSystem;
23  class CEffectFile;
24  class CSortedFaceGroups;
25  class IScene;
26  class CLightObject;
27 
28  struct SceneState;
29 
30  enum RENDER_GROUP{
31  RENDER_SKY_BOX = 1,
32  RENDER_GLOBAL_TERRAIN = 0x1 << 1,
33  RENDER_MESH_FRONT_TO_BACK = 0x1 << 2,
34  RENDER_MESH_BACK_TO_FRONT = 0x1 << 3,
35  RENDER_MESH_TRANSPARENT = 0x1 << 4,
36  RENDER_CHARACTERS = 0x1 << 5,
37  RENDER_PLAYER = 0x1 << 6,
38  RENDER_SPRITES = 0x1 << 7,
39  RENDER_MISSILES = 0x1 << 8,
40  RENDER_PARTICLES = 0x1 << 9,
41  RENDER_BOUNDINGBOX = 0x1 << 10,
42  RENDER_GEN_SM_TERRAIN = 0x1 << 11, // generate shadow map for global terrain object.
43  RENDER_TRANSLUCENT_FACE_GROUPS = 0x1 << 12,
44  RENDER_PORTAL_SYSTEM = 0x1 << 13,
45  RENDER_SELECTION = 0x1 << 14,
46  RENDER_SHADOWMAP = 0x1 << 15,
47  RENDER_TRANSPARENT_CHARACTERS = 0x1 << 16,
48  RENDER_OWNER_DRAW = 0x1 << 17,
49  RENDER_POST_RENDER_LIST = 0x1 << 18,
50  };
51 
52 #define CHECK_SELECTION(x) ((dwSelection&(x))>0)
53 
56  PIPELINE_ORDER_PRE_SCENE = 0,
57  PIPELINE_3D_SCENE = 1,
58  // TODO: add more here.
59  PIPELINE_UI = 50,
60  PIPELINE_POST_UI_3D_SCENE = 51,
61  PIPELINE_COLOR_PICKING = 100,
62  };
63 
68  {
69  public:
70  float m_fObjectToCameraDistance;
71  // this will lead to crash if used cross frame.
72  CBaseObject::WeakPtr_type m_pRenderObject;
75  public:
77  PostRenderObjectWeakPtr(CBaseObject* pRenderObject, float fObjectToCameraDistance, int nOccluded = 0)
78  :m_pRenderObject(pRenderObject), m_fObjectToCameraDistance(fObjectToCameraDistance), m_nOccluded(nOccluded) {};
79 
81  bool IsOccluded() const { return m_nOccluded > 1; }
82  };
83 
88  {
89  public:
90  float m_fObjectToCameraDistance;
91  // this will lead to crash if used cross frame.
92  CBaseObject* m_pRenderObject;
95  public:
97  //PostRenderObject(CBaseObject* pRenderObject);
99  PostRenderObject(CBaseObject* pRenderObject, float fObjectToCameraDistance, int nOccluded = 0);
100 
101  //PostRenderObject& operator = (const PostRenderObject& _right);
102 
104  bool IsOccluded() const { return m_nOccluded > 1; }
105  };
106 
111  {
112  public:
113  // the alpha value: 0-1.0f
114  float m_fAlpha;
117  ALPHA_INCREASE_ANIM,
120  };
123  public:
124  AlphaPostRenderObject(CBaseObject* pRenderObject, float fObjectToCameraDistance, int nOccluded = 0, float fAlpha = 1.f, ALPHA_ANIM nAlphaAnimMethod = ALPHA_NO_ANIM);
125  };
126 
131  template<class Type>
133  {
134  bool operator()(
135  const Type& _Left,
136  const Type& _Right
137  ) const
138  {
139  int nLeftTech = _Left.m_pRenderObject->GetPrimaryTechniqueHandle();
140  int nRightTech = _Right.m_pRenderObject->GetPrimaryTechniqueHandle();
141 
142 #ifdef DONOT_SORT_BY_ASSET
143  return ( (nLeftTech < nRightTech) ||
144  ((nLeftTech == nRightTech) && ((_Left.m_fObjectToCameraDistance) < (_Right.m_fObjectToCameraDistance))) );
145 #else
146  AssetEntity* LeftAsset = _Left.m_pRenderObject->GetPrimaryAsset();
147  AssetEntity* RightAsset = _Right.m_pRenderObject->GetPrimaryAsset();
148  return ((nLeftTech < nRightTech) ||
149  ((nLeftTech == nRightTech) && (LeftAsset < RightAsset)) ||
150  ((nLeftTech == nRightTech) && (LeftAsset == RightAsset) && ((_Left.m_fObjectToCameraDistance) < (_Right.m_fObjectToCameraDistance))));
151 #endif
152  };
153  };
154 
155  template<class Type>
157  {
158  inline bool operator()(
159  const Type& _Left,
160  const Type& _Right
161  ) const
162  {
163  return (_Left.m_fObjectToCameraDistance) < (_Right.m_fObjectToCameraDistance);
164  };
165  };
166 
168  template<class Type>
170  {
171  inline bool operator()(
172  const Type& _Left,
173  const Type& _Right
174  ) const
175  {
176  int nLeftRenderImportance = _Left.m_pRenderObject->GetRenderImportance();
177  int nRightRenderImportance = _Right.m_pRenderObject->GetRenderImportance();
178  return (nLeftRenderImportance > nRightRenderImportance) ||
179  ((nLeftRenderImportance == nRightRenderImportance) && ((_Left.m_fObjectToCameraDistance) < (_Right.m_fObjectToCameraDistance)));
180  };
181  };
182 
184  template<class Type>
186  {
187  inline bool operator()(
188  const Type& _Left,
189  const Type& _Right
190  ) const
191  {
192  float leftOrder = _Left.m_pRenderObject->GetRenderOrder();
193  float rightOrder = _Right.m_pRenderObject->GetRenderOrder();
194  return (leftOrder < rightOrder) ||
195  ((leftOrder == rightOrder) && ((_Left.m_pRenderObject->GetPrimaryTechniqueHandle()) < (_Right.m_pRenderObject->GetPrimaryTechniqueHandle())));
196  };
197  };
198 
199  template<class Type>
201  {
202  bool operator()(
203  const Type& _Left,
204  const Type& _Right
205  ) const
206  {
207  int nLeftTech = _Left.m_pRenderObject->GetPrimaryTechniqueHandle();
208  int nRightTech = _Right.m_pRenderObject->GetPrimaryTechniqueHandle();
209 
210 #ifdef DONOT_SORT_BY_ASSET
211  return ( (nLeftTech < nRightTech) ||
212  ((nLeftTech == nRightTech) && ((_Left.m_fObjectToCameraDistance) > (_Right.m_fObjectToCameraDistance))) );
213 #else
214  AssetEntity* LeftAsset = _Left.m_pRenderObject->GetPrimaryAsset();
215  AssetEntity* RightAsset = _Right.m_pRenderObject->GetPrimaryAsset();
216  return ((nLeftTech < nRightTech) ||
217  ((nLeftTech == nRightTech) && (LeftAsset < RightAsset)) ||
218  ((nLeftTech == nRightTech) && (LeftAsset == RightAsset) && ((_Left.m_fObjectToCameraDistance) > (_Right.m_fObjectToCameraDistance))));
219 #endif
220  };
221  };
222 
223  template<class Type>
225  {
226  inline bool operator()(
227  const Type& _Left,
228  const Type& _Right
229  ) const
230  {
231  return (_Left.m_fObjectToCameraDistance) > (_Right.m_fObjectToCameraDistance);
232  };
233  };
234 
236  template<class Type>
238  {
239  bool operator()(
240  const Type& _Left,
241  const Type& _Right
242  ) const
243  {
244  int nLeftTech = _Left->GetPrimaryTechniqueHandle();
245  int nRightTech = _Right->GetPrimaryTechniqueHandle();
246  return ((nLeftTech < nRightTech) ||
247  ((nLeftTech == nRightTech) && (_Left->GetPrimaryAsset()) < (_Right->GetPrimaryAsset())));
248  };
249  };
250 
253  {
254  RenderState_Standard,
255  RenderState_3D,
256  RenderState_Shadowmap,
257  RenderState_Headon,
258  RenderState_Overlay_ZPass,
259  RenderState_Overlay,
260  RenderState_Overlay_Picking,
261  RenderState_Debug,
262  RenderState_GUI,
263  };
264 
267  {
268  public:
270  CPushRenderState(SceneState* pState);
271  ~CPushRenderState();
272  private:
273  SceneState* m_pState;
274  SceneStateRenderState m_lastState;
275  };
276 
284  struct SceneState : public IAttributeFields
285  {
286  public:
287  // constructor
288  SceneState(IScene * pScene = NULL);
289 
290  ATTRIBUTE_DEFINE_CLASS(SceneState);
292  virtual int InstallFields(CAttributeClass* pClass, bool bOverride);
293 
294  ATTRIBUTE_METHOD1(SceneState, GetCurrentRenderPipeline_s, int*) { *p1 = cls->GetCurrentRenderPipeline(); return S_OK; }
295  ATTRIBUTE_METHOD1(SceneState, SetCurrentRenderPipeline_s, int) { cls->SetCurrentRenderPipeline(p1); return S_OK; }
296 
297  ATTRIBUTE_METHOD1(SceneState, GetRenderState_s, int*) { *p1 = (int)cls->GetRenderState(); return S_OK; }
298  ATTRIBUTE_METHOD1(SceneState, SetRenderState_s, int) { cls->SetRenderState((SceneStateRenderState)p1); return S_OK; }
299 
300  ATTRIBUTE_METHOD1(SceneState, GetCameraToCurObjectDistance_s, float*) { *p1 = cls->GetCameraToCurObjectDistance(); return S_OK; }
301  ATTRIBUTE_METHOD1(SceneState, SetCameraToCurObjectDistance_s, float) { cls->SetCameraToCurObjectDistance(p1); return S_OK; }
302 
303  ATTRIBUTE_METHOD1(SceneState, GetGlobalTime_s, int*) { *p1 = cls->GetGlobalTime(); return S_OK; }
304  ATTRIBUTE_METHOD1(SceneState, SetGlobalTime_s, int) { cls->SetGlobalTime(p1); return S_OK; }
305 
306  public:
312  CBaseObject* GetCurrentSceneObject();
314  void SetCurrentSceneObject(CBaseObject* pObj);
315 
317  void SetCameraToCurObjectDistance(float fDist);
318 
320  float GetCameraToCurObjectDistance();
321 
323  bool IsLODEnabled();
324 
326  void SetShadowMapCameraToCurObjectDistance(float fDist);
327 
329  float GetShadowMapCameraToCurObjectDistance();;
330 
331  SceneStateRenderState GetRenderState() const;
332  void SetRenderState(SceneStateRenderState val);
333 
335  void SetShadowPass(bool bShadowPass);
337  bool IsShadowPass();
338 
340  void SetBatchedElementDrawer(IBatchedElementDraw* pDebugDraw);
341 
343  IBatchedElementDraw* GetBatchedElementDrawer();
344 
346  int GetGlobalTime();
347  void SetGlobalTime(int nGlobalTime);
348 
350  inline bool IsIgnoreTransparent() { return m_bIgnoreTransparent; }
351  void SetIgnoreTransparent(bool bIgnore);
352 
354  ParaMaterial& GetGlobalMaterial();
355 
357  ParaMaterial& GetLocalMaterial();
358 
360  ParaMaterial& GetCurrentMaterial();
361 
363  void EnableLocalMaterial(bool bEnable = true);
364 
366  bool HasLocalMaterial();
367 
369  Vector3& GetCurrentLightStrength();
370 
371  RenderDevicePtr GetRenderDevice(){ return m_pd3dDevice; };
372 
373  BillBoardViewInfo& BillBoardInfo() { return m_BillBoardInfo; }
374 
376  bool IsDeferredShading() const;
377 
379  void SetDeferredShading(bool val);
380 
382  CBaseCamera* GetCamera();
383 
385  int GetCurrentRenderPipeline() const;
387  void SetCurrentRenderPipeline(int val);
388 
389  void AddToDeferredLightPool(CLightObject * lightObject);
390 
391  bool IsGlobalLODEnabled() const;
392  void EnableGlobalLOD(bool val);
393 
394  public:
395  IScene* GetScene(){ return m_pScene; }
396 
397  RenderDevicePtr m_pd3dDevice;
398  // scene associated with the state
404 
407 
409  double dTimeDelta;
412  ParaMaterial m_matLocalMaterial;
413  bool m_bUseLocalMaterial : 1;
415  bool bIsBatchRender : 1;
416  bool m_bCameraMoved : 1;
417  bool m_bGlobalLOD : 1;
418 
424  //int m_nFrameNumber;
425 
426  float fAlphaFactor;
427  DWORD m_dwD3D_CULLMODE;
431 
434 
435  // -- camera and view
436  Matrix4 mxView;
441 
442  FLOAT fViewCullingRadius;
443  CBaseObject* pTouchedObject;
445  CEffectFile* m_pCurrentEffect;
449 
450  enum TOUCH_METHOD{ LButton, RButton, None }touchMethod; // how the above object is being touched
451 
454  {
455  /* stages during frame move*/
456  stage_framemove = 0,
457  stage_UserInputHandling = 1,
458  stage_EnvironmentSim = 2,
459  stage_AISimulator = 3,
460  stage_RootSceneAnimate = 4,
461  stage_RootGUIAnimate = 5,
462 
463  /* stages during rendering*/
464  stage_SetCamera = 101,
465  stage_AdvanceScene = 102,
466  stage_AdvanceGUI = 103
467  }CurrentStage;
468 
471  public:
472  typedef std::vector<PostRenderObject> List_PostRenderObject_Type;
473  typedef std::vector<CSpriteObject*> List_CSpriteObjectPtr_Type;
475  typedef std::vector<AlphaPostRenderObject> List_AlphaPostRenderObject_Type;
476  typedef std::vector<PostRenderObjectWeakPtr> List_HeadOnRenderObject_Type;
477  typedef std::vector<CLightObject*> List_LightObject_Type;
478 
479 
481  class List_PostRenderObject_TrackRef_Type : public IRefObject, public List_PostRenderObject_Type
482  {
483  public:
485  void push_back(PostRenderObject& item);
486 
488  void clear();
489 
496  virtual void OnRefDeleted(IRefObject* rm);
497  };
498 
499  // -- for batched rendering optimization such as sprites objects
512 #ifdef USE_DIRECTX_RENDERER
513  SceneState::List_CSpriteObjectPtr_Type listPRSprite;
514 #endif
517 
519  SceneState::List_PostRenderObject_Type listPRSolidObject;
520  SceneState::List_PostRenderObject_Type listShadowCasters;
521  SceneState::List_PostRenderObject_Type listShadowReceivers;
523  SceneState::List_AlphaPostRenderObject_Type listPRSmallObject;
524  SceneState::List_HeadOnRenderObject_Type listHeadonDisplayObject;
525 
529  SceneState::List_AlphaPostRenderObject_Type listPRTransparentObject;
530  SceneState::List_AlphaPostRenderObject_Type listPRMissiles;
531  SceneState::List_ParticleSystemPtr_Type listParticleSystems;
532 
533  SceneState::List_PostRenderObject_Type listZones;
534  SceneState::List_PostRenderObject_Type listPortals;
535  SceneState::List_PostRenderObject_Type listBoundingBox;
536  SceneState::List_PostRenderObject_Type listSelections;
537  SceneState::List_LightObject_Type listDeferredLightObjects;
538  // like the render target
539  unordered_array<WeakPtr> listOwnerDrawObjs;
540 
541  bool m_bSkipOcean;
542  bool m_bSkipTerrain;
543  bool m_bSkipSky;
544  // whether the outer world is visible. this is usually true, unless camera is inside a zone.
545  // if this is false, ocean terrain sky are escaped.
546  bool m_bOuterWorldZoneVisible;
549 
551  vector<int> m_MaxRenderCount;
552 
554  vector<int> m_RenderCount;
555 
557  void SetMaxRenderCount(int nRenderImportance, int nCount);
558 
560  bool CheckAddRenderCount(int nRenderImportance);
561 
563  inline int GetRenderFrameCount() { return m_nRenderCount; }
564 
565  template <class T>
566  typename T::value_type* FindObject(T& renderlist, CBaseObject* pObject)
567  {
568  typename T::iterator itCur, itEnd = renderlist.end();
569  for (itCur = renderlist.begin(); itCur != itEnd; ++itCur)
570  {
571  if (itCur->m_pRenderObject == pObject)
572  {
573  return &(*itCur);
574  }
575  }
576  return NULL;
577  }
578 
581 
583  void CleanupSceneState();
585  void Cleanup();
586 
590  void AddParticleSystem(ParticleSystem* ps);
591 
593  CSortedFaceGroups* GetFaceGroups();
594 
595  HRESULT InitDeviceObjects(); // device independent
596  HRESULT RestoreDeviceObjects(); // device dependent
597  HRESULT InvalidateDeviceObjects();
598  HRESULT DeleteDeviceObjects();
599 
600  private:
602  static CBaseObject* m_pCurrentSceneObject;
603 
605  IBatchedElementDraw* m_pBatchedDrawer;
606 
608  float m_fCameraToObjectDistance;
609 
612  float m_fShadowMapCameraToObjectDistance;
613 
615  bool m_bIgnoreTransparent;
616 
618  bool m_bIsShadowPass;
619 
621  int m_nGlobalTime;
622 
624  SceneStateRenderState m_renderState;
625  };
626 }
bool IsOccluded() const
return true if occluded
Definition: SceneState.h:81
this is an independent local light scene object.
Definition: LightObject.h:13
an attribute class is a collection of attribute fields.
Definition: AttributeClass.h:10
It&#39;s used as parameter to Draw method of each scene object.
Definition: SceneState.h:284
ProcessingStage
At what stage is the scene currently being processed, ascending order.
Definition: SceneState.h:453
PostRenderObjectWeakPtr(CBaseObject *pRenderObject, float fObjectToCameraDistance, int nOccluded=0)
create a new post render object with the Object surface to camera distance.
Definition: SceneState.h:77
Vector3 vEye
projection transform of the current camera
Definition: SceneState.h:438
Definition: combase.h:159
Basic scene interface: implemented by the CSceneObject and CMiniScenegraph class. ...
Definition: IScene.h:12
different physics engine has different winding order.
Definition: EventBinding.h:32
int m_nOccluded
whether this object is occluded. default to 0, which means undetermined.
Definition: SceneState.h:74
increase the alpha value, if it is 1.0f, it will be removed from the list.
Definition: SceneState.h:118
SceneStateRenderState
which render state the current render pipeline is in.
Definition: SceneState.h:252
int m_nOccluded
whether this object is occluded. default to 0, which means undetermined.
Definition: SceneState.h:94
CSortedFaceGroups m_SortedFaceGroups
face groups.
Definition: SceneState.h:580
BillBoardViewInfo m_BillBoardInfo
camera look at position
Definition: SceneState.h:440
bool m_bEnableTranslucentFaceSorting
translucent face sorting is enabled.
Definition: SceneState.h:420
int GetRenderFrameCount()
get the current frame render number
Definition: SceneState.h:563
SceneState::List_PostRenderObject_TrackRef_Type listPostRenderingObjects
listPostRenderingObjects: this is the general post rendering list.
Definition: SceneState.h:511
a very thin wrapper to DirectX device with a portable version of openGL implementation.
Definition: RenderDeviceDirectX.h:10
CParaWorldAsset * pAssetManager
pointer to the asset manager
Definition: SceneState.h:403
push a render state
Definition: SceneState.h:266
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
ALPHA_ANIM
alpha animation
Definition: SceneState.h:116
Definition: BaseCamera.h:70
Matrix4 mxProj
view transform of the current camera
Definition: SceneState.h:437
first sort from large to small by render importance and if render importance are same from near camer...
Definition: SceneState.h:185
decrease the alpha value, if it is 0.0f, it will be removed from the list.
Definition: SceneState.h:119
double dTimeDelta
time advance delta
Definition: SceneState.h:409
first sort from large to small by render importance and if render importance are same from near camer...
Definition: SceneState.h:169
SceneState::List_PostRenderObject_TrackRef_Type listPRTransparentBiped
list of Post Rendering object: biped
Definition: SceneState.h:516
IParaDebugDraw draw interface draws line based primitives for debugging purposes. ...
Definition: IBatchedElementDraw.h:12
vector< int > m_MaxRenderCount
m_MaxRenderCount[RenderImportance] is the max number of objects to be rendered of a given render impo...
Definition: SceneState.h:551
Vector3 m_curLightStrength
x is the sun light strength, y is local light strength.
Definition: SceneState.h:448
The particle class keeps a list of all of its instances.A particle system instance is always associat...
Definition: particle.h:99
RENDER_PIPELINE_ORDER
general render pipeline order.
Definition: SceneState.h:55
SceneState::List_AlphaPostRenderObject_Type listPRTransparentObject
transparent meshes (object) are usually rendered from back to front(far to near). ...
Definition: SceneState.h:529
bool IsIgnoreTransparent()
ignore all globally transparent and particle object
Definition: SceneState.h:350
DWORD m_nCurRenderGroup
bitwise fields of RENDER_GROUP
Definition: SceneState.h:470
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
It&#39;s used as parameter to Render method of each GUI object.
Definition: GUIState.h:16
Definition: RenderCore.h:50
for post rendering Object.
Definition: SceneState.h:87
for post rendering object with alpha.
Definition: SceneState.h:110
Vector3 vLookAt
camera position
Definition: SceneState.h:439
bool IsOccluded() const
return true if occluded
Definition: SceneState.h:104
Definition: effect_file.h:323
SceneState::List_PostRenderObject_TrackRef_Type listPRBiped
list of general post rendering object
Definition: SceneState.h:515
A common interface for all classes implementing IAttributeFields By implementing this class&#39;s virtual...
Definition: IAttributeFields.h:59
Definition: SceneState.h:200
ParaMaterial m_matGlobalMaterial
default material
Definition: SceneState.h:411
SceneState::List_PostRenderObject_Type listPRSolidObject
list of Post Rendering object: biped with alpha blending
Definition: SceneState.h:519
int m_nCurrentRenderPipeline
one of the enum RENDER_PIPELINE_ORDER
Definition: SceneState.h:406
vector< int > m_RenderCount
m_RenderCount[RenderImportance] is the max number of objects that has already been rendered of a give...
Definition: SceneState.h:554
IScene * m_pScene
rendering device pointer
Definition: SceneState.h:399
for post rendering Object.
Definition: SceneState.h:67
current billboard info is here.
Definition: BillBoardViewInfo.h:8
float fAlphaFactor
the frame number that this object is last accessed.
Definition: SceneState.h:426
Defines the base class of all scene elements:CBaseObject for Parallel World Engine.
Definition: BaseObject.h:230
the asset manager class in ParaEngine.
Definition: ParaWorldAsset.h:44
ALPHA_ANIM m_nAlphaAnimMethod
alpha animation method.
Definition: SceneState.h:122
int m_nOccluded
the default value of Occluded property when new objects are added to it.
Definition: SceneState.h:433
Same as IRefObject except that it tracks references of the object.
Definition: SceneState.h:481
Definition: SceneState.h:156
SceneState::List_AlphaPostRenderObject_Type listPRSmallObject
smaller objects are rendered after bigger objects are drawn for better occlusion.They are rendered fr...
Definition: SceneState.h:523
This class provides sorting and rendering of translucent face groups.
Definition: SortedFaceGroups.h:102
compare two post rendering object by its object to camera distance Type must be pointer to PostRender...
Definition: SceneState.h:132
int m_nRenderCount
how many times the scene has been rendered since its creation.
Definition: SceneState.h:422
GUIState * pGUIState
the GUI rendering state
Definition: SceneState.h:401
compare two object&#39;s primary asset
Definition: SceneState.h:237
Base class for managed asset entity in ParaEngine.
Definition: AssetEntity.h:25
bool m_bDeferredShading
whether we are currently using deferred shading
Definition: SceneState.h:548
anything that makes references to other objects.
Definition: IRefObject.h:65