terrFile.h

Engine/source/terrain/terrFile.h

More...

Classes:

Public Typedefs

TerrainHeight 

NOTE: The terrain uses 11.5 fixed point which gives us a height range from 0->2048 in 1/32 increments.

Public Functions

Conversion from 11.5 fixed point to floating point.

Conversion from floating point to 11.5 fixed point.

Detailed Description

Public Typedefs

typedef U16 TerrainHeight 

NOTE: The terrain uses 11.5 fixed point which gives us a height range from 0->2048 in 1/32 increments.

Public Functions

fixedToFloat(U16 val)

Conversion from 11.5 fixed point to floating point.

floatToFixed(F32 val)

Conversion from floating point to 11.5 fixed point.

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _TERRFILE_H_
 25#define _TERRFILE_H_
 26
 27#ifndef _TVECTOR_H_
 28#include <core/util/tVector.h>
 29#endif
 30#ifndef _PATH_H_
 31#include <core/util/path.h>
 32#endif
 33#ifndef _MATERIALLIST_H_
 34#include "materials/materialList.h"
 35#endif
 36#ifndef _TERRMATERIAL_H_
 37#include "terrain/terrMaterial.h"
 38#endif
 39
 40class TerrainMaterial;
 41class FileStream;
 42class GBitmap;
 43
 44
 45///
 46struct TerrainSquare
 47{
 48   U16 minHeight;
 49
 50   U16 maxHeight;
 51
 52   U16 heightDeviance;
 53
 54   U16 flags;
 55
 56   enum 
 57   {
 58      Split45  = BIT(0),
 59
 60      Empty    = BIT(1),
 61
 62      HasEmpty = BIT(2),
 63   };
 64};
 65
 66
 67/// NOTE:  The terrain uses 11.5 fixed point which gives
 68/// us a height range from 0->2048 in 1/32 increments.
 69typedef U16 TerrainHeight;
 70
 71
 72/// 
 73class TerrainFile
 74{
 75protected:
 76
 77   friend class TerrainBlock;
 78
 79   /// The materials used to render the terrain.
 80   Vector<TerrainMaterial*> mMaterials;
 81
 82   /// The dimensions of the layer and height maps.
 83   U32 mSize;
 84
 85   /// The layer index at each height map sample.
 86   Vector<U8> mLayerMap;
 87
 88   /// The fixed point height map.
 89   /// @see fixedToFloat
 90   Vector<U16> mHeightMap;
 91
 92   /// The memory pool used by the grid map layers.
 93   Vector<TerrainSquare> mGridMapPool;
 94
 95   ///
 96   U32 mGridLevels;
 97
 98   /// The grid map layers used to accelerate collision
 99   /// queries for the height map data.
100   Vector<TerrainSquare*> mGridMap;
101   
102   /// MaterialList used to map terrain materials to material instances for the
103   /// sake of collision (physics, etc.).
104   MaterialList mMaterialInstMapping;
105
106   /// The file version.
107   U32 mFileVersion;     
108
109   /// The dirty flag.
110   bool mNeedsResaving;  
111
112   /// The full path and name of the TerrainFile
113   Torque::Path mFilePath;
114
115   /// The internal loading function.
116   void _load( FileStream &stream );
117
118   /// The legacy file loading code.
119   void _loadLegacy( FileStream &stream );
120
121   /// Used to populate the materail vector by finding the 
122   /// TerrainMaterial objects by name.
123   void _resolveMaterials( const Vector<String> &materials );
124
125   /// 
126   void _buildGridMap();
127   
128   ///
129   void _initMaterialInstMapping();
130
131public:
132
133   enum Constants
134   {
135      FILE_VERSION = 7
136   };
137
138   TerrainFile();
139
140   virtual ~TerrainFile();
141
142   ///
143   static void create(  String *inOutFilename, 
144                        U32 newSize, 
145                        const Vector<String> &materials );
146
147   ///
148   static TerrainFile* load( const Torque::Path &path );
149
150   bool save( const char *filename );
151
152   ///
153   void import(   const GBitmap &heightMap, 
154                  F32 heightScale,
155                  const Vector<U8> &layerMap, 
156                  const Vector<String> &materials,
157                  bool flipYAxis = true );
158
159   /// Updates the terrain grid for the specified area.
160   void updateGrid( const Point2I &minPt, const Point2I &maxPt );
161
162   /// Performs multiple smoothing steps on the heightmap.
163   void smooth( F32 factor, U32 steps, bool updateCollision );
164
165   void setSize( U32 newResolution, bool clear );
166
167   TerrainSquare* findSquare( U32 level, U32 x, U32 y ) const;
168   
169   BaseMatInstance* getMaterialMapping( U32 index ) const;
170   
171   StringTableEntry getMaterialName( U32 x, U32 y) const;
172
173   void setLayerIndex( U32 x, U32 y, U8 index );
174
175   U8 getLayerIndex( U32 x, U32 y ) const;
176
177   bool isEmptyAt( U32 x, U32 y ) const { return getLayerIndex( x, y ) == U8_MAX; }
178
179   void setHeight( U32 x, U32 y, U16 height );
180
181   const U16* getHeightAddress( U32 x, U32 y ) const;
182
183   U16 getHeight( U32 x, U32 y ) const;
184
185   U16 getMaxHeight() const { return mGridMap[mGridLevels]->maxHeight; }
186
187   /// Returns the constant heightmap vector.
188   const Vector<U16>& getHeightMap() const { return mHeightMap; }
189
190   /// Sets a new heightmap state.
191   void setHeightMap( const Vector<U16> &heightmap, bool updateCollision );
192
193   /// Check if the given point is valid within the (non-tiled) terrain file.
194   bool isPointInTerrain( U32 x, U32 y ) const;
195};
196
197
198inline TerrainSquare* TerrainFile::findSquare( U32 level, U32 x, U32 y ) const
199{
200   x %= mSize;
201   y %= mSize;
202   x >>= level;
203   y >>= level;
204
205   return mGridMap[level] + x + ( y << ( mGridLevels - level ) );
206}
207
208inline void TerrainFile::setHeight( U32 x, U32 y, U16 height )
209{
210   x %= mSize;
211   y %= mSize;
212   mHeightMap[ x + ( y * mSize ) ] = height;
213}
214
215inline const U16* TerrainFile::getHeightAddress( U32 x, U32 y ) const
216{
217   x %= mSize;
218   y %= mSize;
219   return &mHeightMap[ x + ( y * mSize ) ];
220}
221
222inline U16 TerrainFile::getHeight( U32 x, U32 y ) const
223{
224   x %= mSize;
225   y %= mSize;
226   return mHeightMap[ x + ( y * mSize ) ];
227}
228
229inline U8 TerrainFile::getLayerIndex( U32 x, U32 y ) const
230{
231   x %= mSize;
232   y %= mSize;
233   return mLayerMap[ x + ( y * mSize ) ];
234}
235
236inline void TerrainFile::setLayerIndex( U32 x, U32 y, U8 index )
237{
238   x %= mSize;
239   y %= mSize;
240   mLayerMap[ x + ( y * mSize ) ] = index;
241}
242
243inline BaseMatInstance* TerrainFile::getMaterialMapping( U32 index ) const
244{
245   if ( index < mMaterialInstMapping.size() )
246      return mMaterialInstMapping.getMaterialInst( index );
247   else
248      return NULL;
249}
250
251inline StringTableEntry TerrainFile::getMaterialName( U32 x, U32 y) const
252{
253   x %= mSize;
254   y %= mSize;
255   const U8 &index = mLayerMap[ x + ( y * mSize ) ];
256
257   if ( index < mMaterials.size() )
258      return mMaterials[ index ]->getInternalName();
259
260   return StringTable->EmptyString();
261}
262
263
264/// Conversion from 11.5 fixed point to floating point.
265inline F32 fixedToFloat( U16 val )
266{
267   return F32(val) * 0.03125f;
268}
269
270/// Conversion from floating point to 11.5 fixed point.
271inline U16 floatToFixed( F32 val )
272{
273   return U16(val * 32.0 + 0.5f);
274}
275
276inline bool TerrainFile::isPointInTerrain( U32 x, U32 y ) const
277{
278   if ( x < mSize && y < mSize)
279      return true;
280
281   return false;
282}
283
284#endif // _TERRFILE_H_ 
285