My Project
OceanManager.h
1 #pragma once
2 #include "IAttributeFields.h"
3 #include "terrain/TTerrain.h"
4 #include <list>
5 namespace ParaEngine
6 {
7  using namespace std;
8 
9  class TTerrain;
10  struct TextureEntity;
11 
15  struct RippleItem
16  {
17  // max radius in meters
18  float fMaxRadius;
19  // propagation speed
20  float fSpeed;
21  // position in world coordinate system.
22  Vector3 vPos;
23 
24  // current radius, which is automatically updated during frame move
25  float fCurrentRadius;
26  };
27 
29  class RippleList
30  {
31  public:
32  RippleList();
33  ~RippleList();
34 
35 
40  void FrameMove(float fDeltaTime);
41 
45  void ClearAll();
46 
47  public:
48  /* all active ripples */
49  list<RippleItem> m_ripples;
50  };
51 
57  {
58  public:
59  COceanManager(void);
60  ~COceanManager(void);
61  static COceanManager* GetInstance();
62 
63  enum eProcessingStages
64  {
65  k_animateHeight = 0,
66  k_animateNormal,
67  k_heightFFTv,
68  k_heightFFTh,
69  k_normalFFTv,
70  k_normalFFTh,
71  k_uploadBuffer,
72  k_rotateBuffer,
73  k_total_process_stages
74  };
75  enum eWaterMeshes
76  {
77  k_total_water_meshes = 3,
78  k_total_terrain_meshes = 1,
79  };
80 
82  struct sBaseVertex
83  {
84  Vector2 vPos;
85  //DWORD colour;
86  //FLOAT u,v;
87  //static const DWORD FVF;
88  };
89 
91  struct sAnimVertex
92  {
94  float zPos;
97  };
100  {
101  float fHeight;
102  };
103 
109  struct sComplex
110  {
111  float real;
112  float imag;
113 
114  sComplex& operator = (const sComplex& src)
115  {
116  real = src.real;
117  imag = src.imag;
118  return *this;
119  }
120  };
121 
122 
123  public:
125  // implementation of IAttributeFields
126 
128  virtual int GetAttributeClassID(){return ATTRIBUTE_CLASSID_COceanManager;}
130  virtual const char* GetAttributeClassName(){static const char name[] = "COceanManager"; return name;}
132  virtual const char* GetAttributeClassDescription(){static const char desc[] = ""; return desc;}
134  virtual int InstallFields(CAttributeClass* pClass, bool bOverride);
135 
136  ATTRIBUTE_METHOD1(COceanManager, OceanEnabled_s, bool*) {*p1 = cls->OceanEnabled(); return S_OK;}
137  ATTRIBUTE_METHOD1(COceanManager, EnableOcean_s, bool) {cls->EnableOcean(p1); return S_OK;}
138 
139  ATTRIBUTE_METHOD1(COceanManager, GetWaterLevel_s, float*) {*p1 = cls->GetWaterLevel(); return S_OK;}
140  ATTRIBUTE_METHOD1(COceanManager, SetWaterLevel_s, float) {cls->SetWaterLevel(p1); return S_OK;}
141 
142  ATTRIBUTE_METHOD1(COceanManager, IsUnderWater_s, bool*) {*p1 = cls->IsUnderWater(); return S_OK;}
143 
144  ATTRIBUTE_METHOD1(COceanManager, GetOceanColor_s, Vector3*) {*p1 = cls->GetOceanColor().ToVector3(); return S_OK;}
145  ATTRIBUTE_METHOD1(COceanManager, SetGetOceanColor_s, Vector3) {LinearColor c(p1.x, p1.y,p1.z,1); cls->SetGetOceanColor(c); return S_OK;}
146 
147  ATTRIBUTE_METHOD1(COceanManager, GetWindSpeed_s, float*) {*p1 = cls->GetWindSpeed(); return S_OK;}
148  ATTRIBUTE_METHOD1(COceanManager, SetWindSpeed_s, float) {cls->SetWindSpeed(p1); return S_OK;}
149  ATTRIBUTE_METHOD1(COceanManager, GetWindDirection_s, float*) {*p1 = cls->GetWindDirection(); return S_OK;}
150  ATTRIBUTE_METHOD1(COceanManager, SetWindDirection_s, float) {cls->SetWindDirection(p1); return S_OK;}
151 
152  ATTRIBUTE_METHOD1(COceanManager, GetRenderTechnique_s, int*) {*p1 = cls->GetRenderTechnique(); return S_OK;}
153  ATTRIBUTE_METHOD1(COceanManager, SetRenderTechnique_s, int) {cls->SetRenderTechnique(p1); return S_OK;}
154 
155  ATTRIBUTE_METHOD1(COceanManager, IsTerrainReflectionEnabled_s, bool*) {*p1 = cls->IsTerrainReflectionEnabled(); return S_OK;}
156  ATTRIBUTE_METHOD1(COceanManager, EnableTerrainReflection_s, bool) {cls->EnableTerrainReflection(p1); return S_OK;}
157 
158  ATTRIBUTE_METHOD1(COceanManager, IsMeshReflectionEnabled_s, bool*) {*p1 = cls->IsMeshReflectionEnabled(); return S_OK;}
159  ATTRIBUTE_METHOD1(COceanManager, EnableMeshReflection_s, bool) {cls->EnableMeshReflection(p1); return S_OK;}
160 
161  ATTRIBUTE_METHOD1(COceanManager, IsPlayerReflectionEnabled_s, bool*) {*p1 = cls->IsPlayerReflectionEnabled(); return S_OK;}
162  ATTRIBUTE_METHOD1(COceanManager, EnablePlayerReflection_s, bool) {cls->EnablePlayerReflection(p1); return S_OK;}
163 
164  ATTRIBUTE_METHOD1(COceanManager, IsCharacterReflectionEnabled_s, bool*) {*p1 = cls->IsCharacterReflectionEnabled(); return S_OK;}
165  ATTRIBUTE_METHOD1(COceanManager, EnableCharacterReflection_s, bool) {cls->EnableCharacterReflection(p1); return S_OK;}
166 
167  ATTRIBUTE_METHOD1(COceanManager, IsAnimateFFT_s, bool*) {*p1 = cls->IsAnimateFFT(); return S_OK;}
168  ATTRIBUTE_METHOD1(COceanManager, SetAnimateFFT_s, bool) {cls->SetAnimateFFT(p1); return S_OK;}
169 
170  ATTRIBUTE_METHOD(COceanManager, CleanupTerrainCache_s) {cls->CleanupTerrainCache(); return S_OK;}
171 
176  const LinearColor& GetOceanColor(){return m_colorOcean;};
181  void SetGetOceanColor(const LinearColor& color){m_colorOcean= color;};
182 
183 
185  bool OceanEnabled();
187  void EnableOcean(bool Enable);
188 
190  float GetWaterLevel();
193  void SetWaterLevel(float fLevel);
194 
195  enum ReflectionItem
196  {
197  R_SKY_BOX = 0,
198  R_GLOBAL_TERRAIN,
199  R_MESH_FRONT_TO_BACK,
200  R_MESH_BACK_TO_FRONT,
201  R_CHARACTERS,
202  R_CURRENT_PLAYER,
203  R_MESH_TRANSPARENT,
204  R_LAST_ITEM,
205  };
211  void EnableReflectionItem(ReflectionItem item, bool bEnabled);
212  bool IsReflectionItemEnabled(ReflectionItem item);
213 
214  void EnableTerrainReflection(bool bEnabled){EnableReflectionItem(R_GLOBAL_TERRAIN, bEnabled);};
215  bool IsTerrainReflectionEnabled(){return IsReflectionItemEnabled(R_GLOBAL_TERRAIN);};
216 
217  void EnableMeshReflection(bool bEnabled){EnableReflectionItem(R_MESH_FRONT_TO_BACK, bEnabled); EnableReflectionItem(R_MESH_BACK_TO_FRONT, bEnabled);};
218  bool IsMeshReflectionEnabled(){return IsReflectionItemEnabled(R_MESH_FRONT_TO_BACK);};
219 
220  void EnablePlayerReflection(bool bEnabled){EnableReflectionItem(R_CURRENT_PLAYER, bEnabled);};
221  bool IsPlayerReflectionEnabled(){return IsReflectionItemEnabled(R_CURRENT_PLAYER);};
222 
223  void EnableCharacterReflection(bool bEnabled){EnableReflectionItem(R_CHARACTERS, bEnabled);};
224  bool IsCharacterReflectionEnabled(){return IsReflectionItemEnabled(R_CHARACTERS);};
225 
226 
230  bool IsRedrawStillReflection();
231 
233  bool IsUnderWater();
234 
236  float GetWindSpeed();
238  void SetWindSpeed(float fSpeed);
240  float GetWindDirection();
242  void SetWindDirection(float fWindDirection);
243 
250  void SetGlobalWater(bool bEnable, float fWaterLevel = 0.0f);
251 
253  float GetMaxWaterLevel();
254 
255 
260  void AddRipple(const Vector3& vPos);
261  private:
263  bool m_bAnimateFFT;
265  RippleList m_rippleList;
266  int m_nMaxRippleNumber;
267 
270  float m_fGlobalWaterLevel;
272  bool m_bDrawGlobalWater;
274  LinearColor m_colorOcean;
275 
276  // whether to render ocean reflection texture according to occlusion result.
277  bool m_bRenderOceanReflection;
278 
279  int m_grid_size;
280  int m_half_grid_size;
281  int m_log_grid_size;
286  void SetFFTGrid(int nSize);
287  void DeleteGrid();
288 
289  float m_fWindX;
290  float m_fWindY;
291  float m_fWindSpeed;
292  float m_fWaveHeight;
293  float m_fDirectionalDependence;
294  float m_fSuppressSmallWavesFactor;
295 
296  float m_fLargestPossibleWave;
297  float m_fSuppressSmallWaves;
298 
300  float m_ocean_tile_size;
302  float m_ocean_tile_height;
303 
304  /************************************************************************/
305  /* cached terrain tiles for shorelines */
306  /************************************************************************/
307  bool m_bDrawShoreline;
309  map <int, TerrainTileCacheItem> m_pCachedTerrains;
310  int m_nMaxCacheSize;
311 
313  static int GetTileIDFromXY(int X, int Y);
316  static bool GetXYFromTileID(int nTileID, int* X, int* Y);
317 
322  TTerrain* GetTerrainHeightField(int nID);
323 
325  void FrameMoveCachedTerrainTiles();
326 
327  /************************************************************************/
328  /* default values */
329  /************************************************************************/
330  static const float GRAVITY_CONSTANT;
331 
333  static const float m_fDefaultWindDirection;
334  static const float m_fDefaultWindSpeed;
335  static const float m_fDefaultWaveHeight;
336 
343  static const float m_fDefaultDirectionalDependence;
344 
346  static const float m_fDefaultSuppressSmallWavesFactor;
347 
349  float m_fTime;
351  float m_fBumpTime;
352 
354  int m_tickCounter;
355 
357  float m_fInterpolation;
358 
362  Plane m_refractionClipPlaneAboveWater;
363  Plane m_reflectionClipPlaneAboveWater;
364  Plane m_refractionClipPlaneBelowWater;
365  Plane m_reflectionClipPlaneBelowWater;
366 
367  float m_refractionMapOverdraw;
368  float m_reflectionMapOverdraw;
369 
370  int m_reflectionTextureWidth;
371  int m_reflectionTextureHeight;
372  int m_refractionTextureWidth;
373  int m_refractionTextureHeight;
374 #ifdef USE_DIRECTX_RENDERER
375  LPDIRECT3DSURFACE9 m_pDepthStencilSurface;
376  LPDIRECT3DTEXTURE9 m_waveReflectionTexture;
377  LPDIRECT3DSURFACE9 m_waveReflectionSurface;
378  LPDIRECT3DTEXTURE9 m_waveRefractionTexture;
379  LPDIRECT3DSURFACE9 m_waveRefractionSurface;
380  LPDIRECT3DTEXTURE9 m_waveReflectionNearTexture;
381  LPDIRECT3DSURFACE9 m_waveReflectionNearSurface;
382  LPDIRECT3DTEXTURE9 m_waveRefractionNearTexture;
383  LPDIRECT3DSURFACE9 m_waveRefractionNearSurface;
384 #endif
385  // procedure ripples on the surface.
386  //LPDIRECT3DVERTEXBUFFER9 m_lpVBRipples;
387 
388  Matrix4 m_projectionMatrix;
389  Matrix4 m_reflectionProjectionMatrix;
390  Matrix4 m_refractionProjectionMatrix;
391  Matrix4 m_worldViewProjectionInverseTransposeMatrix;
392 
393  bool m_reflectViewMatrix;
394  bool m_underwater;
395  DWORD m_dwTechnique;
396 
398  float m_fLastReflectionTime;
399 
405  void SetMatrices(bool bPostPushMatrices=false, bool bPrePopMatrices=false);
406 
407  private:
408  bool m_ReflectionItems[R_LAST_ITEM];
409 
411  bool m_bDisableFogInReflection;
413  bool m_bIsCreated;
415  asset_ptr<TextureEntity> m_pRippleBumpMappingTexture;
417  asset_ptr<TextureEntity> m_pShorelineTexture;
419  asset_ptr<TextureEntity> m_pRippleTexture;
421  bool m_bUseShorelineTexture;
423  asset_ptr<TextureEntity> m_pWaterColorTexture;
426  asset_ptr<TextureEntity> m_pWaterFadeTexture;
427  /* only used in TECH_OCEAN_SIMPLE */
428  asset_ptr<TextureEntity> m_pSimpleReflectionTexture;
429  /* only used in TECH_OCEAN_SIMPLE */
430  asset_ptr<TextureEntity> m_pSimpleBumpTexture;
431  /* only used in TECH_OCEAN_CLOUD */
432  asset_ptr<TextureEntity> m_pCloudTexture;
433  /* only used in FILE_SUNSETGLOW */
434  asset_ptr<TextureEntity> m_pSunsetTexture;
435 #ifdef USE_DIRECTX_RENDERER
436 
437  LPDIRECT3DVERTEXBUFFER9 m_pBaseBuffer;
439  LPDIRECT3DINDEXBUFFER9 m_pIndexBuffer;
440 
442  LPDIRECT3DVERTEXBUFFER9 m_pAnimBuffer[k_total_water_meshes];
443  LPDIRECT3DVERTEXBUFFER9 m_pTerrainHeightmapBuffer[k_total_terrain_meshes];
444 #endif
445 
446  int m_num_triangle;
447 
448  /*LPDIRECT3DVERTEXBUFFER9 m_quad_vb;
449  LPDIRECT3DINDEXBUFFER9 m_quad_ib;*/
450 
452  int m_activeBuffer;
453 
456  sComplex* m_colH;
457 
460  sComplex* m_colN;
461 
463  sComplex* m_colH0;
464 
466  float* m_colAngularFreq;
467 
469  float getOceanHeight(int x, int y);
470  float getOceanNormalX(int x, int y);
471  float getOceanNormalY(int x, int y);
472 
473  float getKx(int x);
474  float getKy(int y);
475  float getAngularFrequency(float fKLength);
476 
477  int getIndexH0(int x, int y);
478  int getIndex(int x, int y);
479  int getIndexFFT(int x, int y);
480  int getOffset(int x, int y);
481  int getOffsetWrap(int x, int y);
482 
484  void FFT(sComplex* pCmpTable);
486  void setOceanParameters(
487  unsigned long ulSeed,
488  float fWindDirection,
489  float fWindSpeed,
490  float fWaveHeight,
491  float fDirectionalDependence,
492  float fSuppressSmallWavesFactor );
494  float phillipsSpectrum( float fKx, float fKy );
495 #ifdef USE_DIRECTX_RENDERER
496 
497  void fillVertexBuffer(LPDIRECT3DVERTEXBUFFER9 pVertexBuffer);
498 #endif
499 
500  bool CreateStripGridIndexBuffer(
501  int xVerts, // width of grid
502  int yVerts, // height of grid
503  int xStep, // horz vertex count per cell
504  int yStep, // vert vertex count per cell
505  int stride // horz vertex count in vbuffer
506  );
507 
508  /************************************************************************/
509  /* The FFT computational stages */
510  /************************************************************************/
511  void animateHeightTable();
512  void animateNormalTable();
513  void horizontalFFT(sComplex* pCmpTable );
514  void verticalFFT(sComplex* pCmpTable );
515 
516  // rendering techniques
518  void RenderTechnique_Quad();
520  int RenderTechnique_Simple(SceneState* pSceneState, float x0, float y0, float x1, float y1);
522  int RenderTechnique_FFT_HIGH(SceneState* pSceneState, float x0, float y0, float x1, float y1);
524  int RenderTechnique_Cloud(SceneState* pSceneState, float x0, float y0, float x1, float y1);
525 
527  int RenderTechnique_OccusionTest(SceneState* pSceneState, float x0, float y0, float x1, float y1);
529  void RenderRipples(float fTimeDelta);
530 
532  void RenderUnderwaterEffect(SceneState* pSceneState);
533 
535  void CalculateTileToRender(SceneState* pSceneState, float& x0, float& y0, float& x1, float& y1);
536  public:
541  bool create();
542 
544  void CheckLoadOceanTextures(int nTechnique=0);
545 
548  void InitDeviceObjects();
550  void RestoreDeviceObjects();
552  void InvalidateDeviceObjects();
554  void DeleteDeviceObjects();
556  void Cleanup();
557 
560  void update(float fTimeDelta);
561 
565  bool UpdateReflectionTexture(SceneState & sceneState);
566 
568  void ForceUpdateOcean();
569 
571  void Render(SceneState* pSceneState);
573  bool RenderReflectionTexture();
574 
575  enum OCEAN_RENDER_TECHNIQUE{
576  OCEAN_TECH_QUAD=0, // requires just software processing
577  OCEAN_TECH_FFT, // requires just vertex shader 1.1 and no pixel shader
578  OCEAN_TECH_REFLECTION, // requires shader and pixel shader version 3.0
579  OCEAN_TECH_FULL, // requires shader and pixel shader version 3.0
580  OCEAN_TECH_SIMPLE, // requires just vertex and pixel shader 2
581  OCEAN_TECH_CLOUD, // cloud rendering using layered texture.
582  };
587  void SetRenderTechnique(DWORD dwTechnique);
589  DWORD GetRenderTechnique();
590 
593  bool IsPointUnderWater(const Vector3& vPos);
594 
596  bool IsDrawShoreLine();
598  void DrawShoreLine(bool bDraw);
600  void CleanupTerrainCache();
601 
603  bool IsOceanVisible();
604 
606  bool IsAnimateFFT();
608  void SetAnimateFFT(bool bEnable);
609 
610  friend class CSceneObject;
611 
612  private:
613  bool m_useScreenSpaceFog;
614 
615  LinearColor m_CustomUnderWaterColor;
616  public:
617  void EnableScreenSpaceFog(bool value){
618  m_useScreenSpaceFog = value;
619  }
620  bool IsScreenSpaceFog(){return m_useScreenSpaceFog;}
621  };
622 }
virtual const char * GetAttributeClassName()
a static string, describing the attribute class object&#39;s name
Definition: OceanManager.h:130
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
temp height field terrain data used by terrain filters.
Definition: TTerrain.h:16
the base vertex for ocean tile.
Definition: OceanManager.h:82
a ripple
Definition: OceanManager.h:15
different physics engine has different winding order.
Definition: EventBinding.h:32
void SetGetOceanColor(const LinearColor &color)
a color to be multiplied to the sky, giving it a special color hue
Definition: OceanManager.h:181
vertex data for terrain terrain height field.
Definition: OceanManager.h:99
typedef floating-point complex numbers to hold pairs of real and imaginary numbers ...
Definition: OceanManager.h:109
virtual const char * GetAttributeClassDescription()
a static string, describing the attribute class object
Definition: OceanManager.h:132
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
Standard 2-dimensional vector.
Definition: ParaVector2.h:16
vertex data for animation
Definition: OceanManager.h:91
a list of active RippleItem
Definition: OceanManager.h:29
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
Defines a plane in 3D space.
Definition: ParaPlane.h:23
float zPos
the height of the vertex
Definition: OceanManager.h:94
Definition: enum_maker.hpp:46
Vector2 vNormal
the normal of the vertex
Definition: OceanManager.h:96
A common interface for all classes implementing IAttributeFields By implementing this class&#39;s virtual...
Definition: IAttributeFields.h:59
The top level scene management class.
Definition: SceneObject.h:58
virtual int GetAttributeClassID()
attribute class ID should be identical, unless one knows how overriding rules work.
Definition: OceanManager.h:128
A linear, 32-bit/component floating point RGBA color.
Definition: ParaColor.h:12
const LinearColor & GetOceanColor()
a color to be multiplied to the sky, giving it a special color hue
Definition: OceanManager.h:176
FFT based ocean rendering class.
Definition: OceanManager.h:56