1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef BUMP_WATER_H
4 #define BUMP_WATER_H
5 
6 #include "Rendering/GL/FBO.h"
7 #include "Rendering/GL/myGL.h"
8 #include "IWater.h"
9 
10 #include "System/EventClient.h"
11 #include "System/Misc/RectangleOptimizer.h"
12 
13 
14 namespace Shader {
15 	struct IProgramObject;
16 }
17 
18 class CBumpWater : public IWater, public CEventClient
19 {
20 public:
21 	//! CEventClient interface
WantsEvent(const std::string & eventName)22 	bool WantsEvent(const std::string& eventName) {
23 		return shoreWaves && (eventName == "UnsyncedHeightMapUpdate");
24 	}
GetFullRead()25 	bool GetFullRead() const { return true; }
GetReadAllyTeam()26 	int GetReadAllyTeam() const { return AllAccessTeam; }
27 
28 public:
29 	CBumpWater();
30 	virtual ~CBumpWater();
31 
32 	void Update();
33 	void UpdateWater(CGame* game);
34 	void OcclusionQuery();
35 	void DrawReflection(CGame* game);
36 	void DrawRefraction(CGame* game);
37 	void Draw();
GetID()38 	int GetID() const { return WATER_RENDERER_BUMPMAPPED; }
GetName()39 	const char* GetName() const { return "bumpmapped"; }
40 
41 private:
42 	void SetUniforms(); ///< @see #useUniforms
43 	void SetupUniforms( std::string& definitions );
44 	void GetUniformLocations(const Shader::IProgramObject*);
45 
46 private:
47 	//! coastmap (needed for shorewaves)
48 	struct CoastAtlasRect {
49 		CoastAtlasRect(const SRectangle& rect);
50 		bool isCoastline; ///< if false, then the whole rect is either above water or below water (no coastline -> no need to calc/render distfield)
51 		int ix1, iy1;
52 		int ix2, iy2;
53 		int xsize, ysize;
54 		float x1, y1;
55 		float x2, y2;
56 		float tx1, ty1;
57 		float tx2, ty2;
58 	};
59 
60 	std::vector<CoastAtlasRect> coastmapAtlasRects;
61 	CRectangleOptimizer heightmapUpdates;
62 
63 	void UploadCoastline(const bool forceFull = false);
64 	void UpdateCoastmap();
65 	void UpdateDynWaves(const bool initialize = false);
66 
67 	int atlasX,atlasY;
68 
69 	void UnsyncedHeightMapUpdate(const SRectangle& rect);
70 
71 private:
72 	//! user options
73 	char  reflection;   ///< 0:=off, 1:=don't render the terrain, 2:=render everything+terrain
74 	char  refraction;   ///< 0:=off, 1:=screencopy, 2:=own rendering cycle
75 	int   reflTexSize;
76 	bool  depthCopy;    ///< uses a screen depth copy, which allows a nicer interpolation between deep sea and shallow water
77 	float anisotropy;
78 	char  depthBits;    ///< depthBits for reflection/refraction RBO
79 	bool  blurRefl;
80 	bool  shoreWaves;
81 	bool  endlessOcean; ///< render the water around the whole map
82 	bool  dynWaves;     ///< only usable if bumpmap/normal texture is a TileSet
83 	bool  useUniforms;  ///< use Uniforms instead of \#define'd const. Warning: this is much slower, but has the advantage that you can change the params on runtime.
84 
85 	unsigned char* tileOffsets; ///< used to randomize the wave/bumpmap/normal texture
86 	int  normalTextureX; ///< needed for dynamic waves
87 	int  normalTextureY;
88 
89 	GLuint target; ///< for screen copies (color/depth), can be GL_TEXTURE_RECTANGLE (nvidia) or GL_TEXTURE_2D (others)
90 	int  screenTextureX;
91 	int  screenTextureY;
92 
93 	FBO reflectFBO;
94 	FBO refractFBO;
95 	FBO coastFBO;
96 	FBO dynWavesFBO;
97 
98 	GLuint displayList;
99 
100 	GLuint refractTexture;
101 	GLuint reflectTexture;
102 	GLuint depthTexture;   ///< screen depth copy
103 	GLuint waveRandTexture;
104 	GLuint foamTexture;
105 	GLuint normalTexture;  ///< final used
106 	GLuint normalTexture2; ///< updates normalTexture with dynamic waves turned on
107 	GLuint coastTexture;
108 	GLuint coastUpdateTexture;
109 	std::vector<GLuint> caustTextures;
110 
111 	Shader::IProgramObject* waterShader;
112 	Shader::IProgramObject* blurShader;
113 
114 	GLuint uniforms[20]; ///< see useUniforms
115 
116 	bool wasVisibleLastFrame;
117 	GLuint occlusionQuery;
118 	GLuint occlusionQueryResult;
119 
120 	float3 windVec;
121 	float3 windndir;
122 //	float  windStrength;
123 };
124 
125 #endif // BUMP_WATER_H
126 
127