1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ 2 3 #ifndef SM3_TERRAIN_H 4 #define SM3_TERRAIN_H 5 6 #include "TerrainBase.h" 7 #include "Map/SM3/Vector3.h" 8 #include "Map/SM3/Frustum.h" 9 #include "System/Matrix44f.h" 10 11 #include <vector> 12 13 class CSM3Map; 14 class TdfParser; 15 16 namespace terrain { 17 18 // Terrain system internal structures 19 class TQuad; 20 21 struct Heightmap; 22 struct QuadMap; 23 struct IndexTable; 24 25 class QuadRenderData; 26 class TerrainTexture; 27 class RenderDataManager; 28 29 struct ShadowMapParams 30 { 31 float mid[2]; 32 float f_a, f_b; 33 float shadowMatrix[16]; 34 unsigned shadowMap; 35 }; 36 37 class Camera 38 { 39 public: Camera()40 Camera() 41 : yaw(0.0f) 42 , pitch(0.0f) 43 , fov(80.0f) 44 , aspect(1.0f) 45 { 46 } 47 48 void Update(); 49 50 float yaw; 51 float pitch; 52 Vector3 pos; 53 Vector3 front; 54 Vector3 up; 55 Vector3 right; 56 CMatrix44f mat; 57 58 float fov; 59 float aspect; 60 }; 61 62 // for rendering debug text 63 struct IFontRenderer 64 { 65 virtual void printf(int x, int y, float size, const char* fmt, ...) = 0; 66 protected: ~IFontRendererIFontRenderer67 ~IFontRenderer() {} 68 }; 69 70 struct Config 71 { 72 Config(); 73 74 bool cacheTextures; 75 int cacheTextureSize; // size of a single cache texture: 32/64/128/256/512 76 77 bool useBumpMaps; 78 bool terrainNormalMaps; 79 bool useShadowMaps; // do the terrain shaders need shadow map support 80 81 float detailMod; // acceptable range is 0.25f - 4 82 83 // (Only in case of terrainNormalMaps=true) 84 // heightmap level from which detail normal maps are generated, 85 // Normal maps are generated from heightmap[x+node.depth] 86 // 0 disables normal map detail-preservation 87 int normalMapLevel; 88 89 float anisotropicFiltering; // level of anisotropic filtering - default is 0 (no anisotropy) 90 91 bool useStaticShadow; 92 bool forceFallbackTexturing; // only use GL_ARB_texture_env_combine even if shader GL extensions are available 93 int maxLodLevel; // lower max lod usually requires less heavy texturing but more geometry 94 }; 95 96 struct StaticLight 97 { 98 Vector3 color; 99 Vector3 position; 100 bool directional; // if true, position is a direction vector 101 }; 102 103 struct RenderStats 104 { 105 int tris; 106 int passes; 107 int cacheTextureSize; 108 int renderDataSize; 109 }; 110 111 class RenderContext; 112 113 struct LightingInfo 114 { 115 std::vector<StaticLight> staticLights; 116 Vector3 ambient; 117 }; 118 119 class Terrain 120 { 121 public: 122 Terrain(); 123 ~Terrain(); 124 125 // Render contexts 126 RenderContext* AddRenderContext(Camera* cam, bool needsTexturing); 127 void RemoveRenderContext(RenderContext* ctx); 128 void SetActiveContext(RenderContext* ctx); // set active rendering context / camera viewpoint 129 130 void Load(const TdfParser& tdf, LightingInfo* li, ILoadCallback* cb); 131 void LoadHeightMap(const TdfParser&, ILoadCallback*); 132 void ReloadShaders(); 133 void Draw(); 134 void DrawAll(); // draw all terrain nodes, regardless of visibility or lod 135 void Update(); // update lod+visibility, should be called when camera position has changed 136 void DrawSimple(); // no texture/no lighting 137 void DrawOverlayTexture(uint tex); // draw with single texture/no lighting 138 GetQuadTree()139 TQuad* GetQuadTree() { return quadtree; } 140 141 // Allow it to use any part of the current rendering buffer target for caching textures 142 // should be called just before glClear (it doesn't need a cleared framebuffer) 143 // Update() should be called before this, otherwise there could be uncached nodes when doing Draw() 144 void CacheTextures(); 145 // Render a single node flat onto the framebuffer, used for minimap rendering and texture caching 146 void RenderNodeFlat(int x,int y,int depth); 147 148 void DebugEvent(const std::string& event); 149 void DebugPrint(IFontRenderer* fr); 150 151 // World space lighting vector - used for bumpmapping 152 void SetShaderParams(Vector3 dir, Vector3 eyePos); 153 154 void SetShadowMap(uint shadowTex); 155 void SetShadowParams(ShadowMapParams* smp); 156 157 // Heightmap interface, for dynamically changing heightmaps 158 void GetHeightMapSynced(int x, int y, int w, int h, float* dest); 159 void HeightMapUpdatedUnsynced(int x,int y,int w,int h); 160 161 // these return the top-level heightmaps 162 std::vector<float>& GetCornerHeightMapSynced(); 163 std::vector<float>& GetCornerHeightMapUnsynced(); 164 165 Vector3 TerrainSize(); 166 int GetHeightmapWidth() const; 167 int GetHeightmapHeight() const; 168 169 void CalcRenderStats(RenderStats& stats, RenderContext* ctx=0); 170 171 Config config; 172 173 protected: 174 void ClearRenderQuads(); 175 176 void RenderNode(TQuad* q); 177 178 Heightmap* heightmap; // list of heightmaps, starting from highest detail level 179 Heightmap* lowdetailhm; // end of heightmap list, representing lowest detail level 180 181 TQuad* quadtree; 182 std::vector<QuadMap*> qmaps; // list of quadmaps, starting from lowest detail level 183 std::vector<TQuad*> updatequads; // temporary list of quads that are being updated 184 std::vector<TQuad*> culled; 185 std::vector<RenderContext*> contexts; 186 RenderContext* activeRC; 187 RenderContext* curRC; 188 Frustum frustum; 189 IndexTable* indexTable; 190 TerrainTexture* texturing; 191 // settings read from config file 192 //float hmScale; 193 //float hmOffset; 194 uint shadowMap; 195 uint quadTreeDepth; 196 197 RenderDataManager* renderDataManager; 198 199 // Initial quad visibility and LOD estimation, registers to renderquads 200 void QuadVisLod(TQuad* q); 201 // Nabour and lod state calculation 202 void UpdateLodFix(TQuad* q); 203 void ForceQueue(TQuad* q); 204 205 // Heightmap loading using DevIL 206 Heightmap* LoadHeightmapFromImage(const std::string& file, ILoadCallback* cb); 207 // RAW 16 bit heightmap loading 208 Heightmap* LoadHeightmapFromRAW(const std::string& file, ILoadCallback* cb); 209 210 bool IsShadowed(int x, int y); 211 212 inline void CheckNabourLod(TQuad* q, int xOfs, int yOfs); 213 inline void QueueLodFixQuad(TQuad* q); 214 215 // debug variables 216 TQuad* debugQuad; 217 int nodeUpdateCount; // number of node updates since last frame 218 bool logUpdates; 219 220 struct VisNode 221 { 222 TQuad* quad; 223 uint index; 224 }; 225 226 std::vector<VisNode> visNodes; 227 }; 228 229 } 230 231 #endif // SM3_TERRAIN_H 232