terrData.h
Engine/source/terrain/terrData.h
Classes:
class
Detailed Description
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 _TERRDATA_H_ 25#define _TERRDATA_H_ 26 27#ifndef _MPOINT3_H_ 28#include "math/mPoint3.h" 29#endif 30#ifndef _SCENEOBJECT_H_ 31#include "scene/sceneObject.h" 32#endif 33#ifndef __RESOURCE_H__ 34#include "core/resource.h" 35#endif 36#ifndef _RENDERPASSMANAGER_H_ 37#include "renderInstance/renderPassManager.h" 38#endif 39#ifndef _TSIGNAL_H_ 40#include "core/util/tSignal.h" 41#endif 42#ifndef _TERRFILE_H_ 43#include "terrain/terrFile.h" 44#endif 45#ifndef _GFXPRIMITIVEBUFFER_H_ 46#include "gfx/gfxPrimitiveBuffer.h" 47#endif 48 49 50 51class GBitmap; 52class TerrainBlock; 53class TerrCell; 54class PhysicsBody; 55class TerrainCellMaterial; 56 57class TerrainBlock : public SceneObject 58{ 59 typedef SceneObject Parent; 60 61 friend class TerrainEditor; 62 friend class TerrainCellMaterial; 63 64protected: 65 66 enum 67 { 68 TransformMask = Parent::NextFreeMask, 69 FileMask = Parent::NextFreeMask << 1, 70 SizeMask = Parent::NextFreeMask << 2, 71 MaterialMask = Parent::NextFreeMask << 3, 72 HeightMapChangeMask = Parent::NextFreeMask << 4, 73 MiscMask = Parent::NextFreeMask << 5, 74 75 NextFreeMask = Parent::NextFreeMask << 6, 76 }; 77 78public: 79 80 enum BaseTexFormat 81 { 82 NONE, DDS, PNG, JPG 83 }; 84 85 static const char* formatToExtension(BaseTexFormat format) 86 { 87 switch (format) 88 { 89 case DDS: 90 return "dds"; 91 case PNG: 92 return "png"; 93 case JPG: 94 return "jpg"; 95 default: 96 return ""; 97 } 98 }; 99 100protected: 101 102 Box3F mBounds; 103 104 /// 105 GBitmap *mLightMap; 106 107 /// The lightmap dimensions in pixels. 108 U32 mLightMapSize; 109 110 /// The lightmap texture. 111 GFXTexHandle mLightMapTex; 112 113 /// The terrain data file. 114 Resource<TerrainFile> mFile; 115 116 /// The TerrainFile CRC sent from the server. 117 U32 mCRC; 118 119 /// 120 FileName mTerrFileName; 121 122 /// The maximum detail distance found in the material list. 123 F32 mMaxDetailDistance; 124 125 /// 126 Vector<GFXTexHandle> mBaseTextures; 127 128 /// 129 GFXTexHandle mLayerTex; 130 131 /// The shader used to generate the base texture map. 132 GFXShaderRef mBaseShader; 133 134 /// 135 GFXStateBlockRef mBaseShaderSB; 136 137 /// 138 GFXShaderConstBufferRef mBaseShaderConsts; 139 140 /// 141 GFXShaderConstHandle *mBaseTexScaleConst; 142 GFXShaderConstHandle *mBaseTexIdConst; 143 GFXShaderConstHandle *mBaseLayerSizeConst; 144 145 /// 146 GFXTextureTargetRef mBaseTarget; 147 148 /// The base texture. 149 GFXTexHandle mBaseTex; 150 151 /// 152 bool mDetailsDirty; 153 154 /// 155 bool mLayerTexDirty; 156 157 /// The desired size for the base texture. 158 U32 mBaseTexSize; 159 160 BaseTexFormat mBaseTexFormat; 161 162 /// 163 TerrCell *mCell; 164 165 /// The shared base material which is used to render 166 /// cells that are outside the detail map range. 167 TerrainCellMaterial *mBaseMaterial; 168 169 /// A dummy material only used for shadow 170 /// material generation. 171 BaseMatInstance *mDefaultMatInst; 172 173 F32 mSquareSize; 174 175 PhysicsBody *mPhysicsRep; 176 177 U32 mScreenError; 178 179 /// The shared primitive buffer used in rendering. 180 GFXPrimitiveBufferHandle mPrimBuffer; 181 182 /// The cells used in the last render pass 183 /// when doing debug rendering. 184 /// @see _renderDebug 185 Vector<TerrCell*> mDebugCells; 186 187 /// Set to enable debug rendering of the terrain. It 188 /// is exposed to the console via $terrain::debugRender. 189 static bool smDebugRender; 190 191 /// Allows the terrain to cast shadows onto itself and other objects. 192 bool mCastShadows; 193 194 /// A global LOD scale used to tweak the default 195 /// terrain screen error value. 196 static F32 smLODScale; 197 198 /// A global detail scale used to tweak the 199 /// material detail distances. 200 static F32 smDetailScale; 201 202 /// True if the zoning needs to be recalculated for the terrain. 203 bool mZoningDirty; 204 205 String _getBaseTexCacheFileName() const; 206 207 void _rebuildQuadtree(); 208 209 void _updatePhysics(); 210 211 void _renderBlock( SceneRenderState *state ); 212 void _renderDebug( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat ); 213 214 /// The callback used to get texture events. 215 /// @see GFXTextureManager::addEventDelegate 216 void _onTextureEvent( GFXTexCallbackCode code ); 217 218 /// Used to release terrain materials when 219 /// the material manager flushes them. 220 /// @see MaterialManager::getFlushSignal 221 void _onFlushMaterials(); 222 223 /// 224 bool _initBaseShader(); 225 226 /// 227 void _updateMaterials(); 228 229 /// 230 void _updateBaseTexture( bool writeToCache ); 231 232 void _updateLayerTexture(); 233 234 void _updateBounds(); 235 236 void _onZoningChanged( SceneZoneSpaceManager *zoneManager ); 237 238 void _updateZoning(); 239 240 // Protected fields 241 static bool _setTerrainFile( void *obj, const char *index, const char *data ); 242 static bool _setSquareSize( void *obj, const char *index, const char *data ); 243 static bool _setBaseTexSize(void *obj, const char *index, const char *data); 244 static bool _setBaseTexFormat(void *obj, const char *index, const char *data); 245 static bool _setLightMapSize( void *obj, const char *index, const char *data ); 246 247public: 248 249 enum 250 { 251 LightmapUpdate = BIT(0), 252 HeightmapUpdate = BIT(1), 253 LayersUpdate = BIT(2), 254 EmptyUpdate = BIT(3) 255 }; 256 257 static Signal<void(U32,TerrainBlock*,const Point2I& ,const Point2I&)> smUpdateSignal; 258 259 /// 260 bool import( const GBitmap &heightMap, 261 F32 heightScale, 262 F32 metersPerPixel, 263 const Vector<U8> &layerMap, 264 const Vector<String> &materials, 265 bool flipYAxis = true ); 266 267#ifdef TORQUE_TOOLS 268 bool exportHeightMap( const UTF8 *filePath, const String &format ) const; 269 bool exportLayerMaps( const UTF8 *filePrefix, const String &format ) const; 270#endif 271 272public: 273 274 TerrainBlock(); 275 virtual ~TerrainBlock(); 276 277 U32 getCRC() const { return(mCRC); } 278 279 Resource<TerrainFile> getFile() const { return mFile; }; 280 281 bool onAdd(); 282 void onRemove(); 283 284 void onEditorEnable(); 285 void onEditorDisable(); 286 287 /// Adds a new material as the top layer or 288 /// inserts it at the specified index. 289 void addMaterial( const String &name, U32 insertAt = -1 ); 290 291 /// Removes the material at the index. 292 void removeMaterial( U32 index ); 293 294 /// Updates the material at the index. 295 void updateMaterial( U32 index, const String &name ); 296 297 /// Deletes all the materials on the terrain. 298 void deleteAllMaterials(); 299 300 //void setMaterialName( U32 index, const String &name ); 301 302 /// Accessors and mutators for TerrainMaterialUndoAction. 303 /// @{ 304 const Vector<TerrainMaterial*>& getMaterials() const { return mFile->mMaterials; } 305 const Vector<U8>& getLayerMap() const { return mFile->mLayerMap; } 306 void setMaterials( const Vector<TerrainMaterial*> &materials ) { mFile->mMaterials = materials; } 307 void setLayerMap( const Vector<U8> &layers ) { mFile->mLayerMap = layers; } 308 /// @} 309 310 TerrainMaterial* getMaterial( U32 index ) const; 311 312 const char* getMaterialName( U32 index ) const; 313 314 U32 getMaterialCount() const; 315 316 //BaseMatInstance* getMaterialInst( U32 x, U32 y ); 317 318 void setHeight( const Point2I &pos, F32 height ); 319 F32 getHeight( const Point2I &pos ); 320 321 // Performs an update to the selected range of the terrain 322 // grid including the collision and rendering structures. 323 void updateGrid( const Point2I &minPt, 324 const Point2I &maxPt, 325 bool updateClient = false ); 326 327 void updateGridMaterials( const Point2I &minPt, const Point2I &maxPt ); 328 329 Point2I getGridPos( const Point3F &worldPos ) const; 330 331 /// This returns true and the terrain z height for 332 /// a 2d position in the terrains object space. 333 /// 334 /// If the terrain at that point is within an empty block 335 /// or the 2d position is outside of the terrain area then 336 /// it returns false. 337 /// 338 bool getHeight( const Point2F &pos, F32 *height ) const; 339 340 void getMinMaxHeight( F32 *minHeight, F32 *maxHeight ) const; 341 342 /// This returns true and the terrain normal for a 343 /// 2d position in the terrains object space. 344 /// 345 /// If the terrain at that point is within an empty block 346 /// or the 2d position is outside of the terrain area then 347 /// it returns false. 348 /// 349 bool getNormal( const Point2F &pos, 350 Point3F *normal, 351 bool normalize = true, 352 bool skipEmpty = true ) const; 353 354 /// This returns true and the smoothed terrain normal 355 // for a 2d position in the terrains object space. 356 /// 357 /// If the terrain at that point is within an empty block 358 /// or the 2d position is outside of the terrain area then 359 /// it returns false. 360 /// 361 bool getSmoothNormal( const Point2F &pos, 362 Point3F *normal, 363 bool normalize = true, 364 bool skipEmpty = true ) const; 365 366 /// This returns true and the terrain normal and z height 367 /// for a 2d position in the terrains object space. 368 /// 369 /// If the terrain at that point is within an empty block 370 /// or the 2d position is outside of the terrain area then 371 /// it returns false. 372 /// 373 bool getNormalAndHeight( const Point2F &pos, 374 Point3F *normal, 375 F32 *height, 376 bool normalize = true ) const; 377 378 /// This returns true and the terrain normal, z height, and 379 /// material name for a 2d position in the terrains object 380 /// space. 381 /// 382 /// If the terrain at that point is within an empty block 383 /// or the 2d position is outside of the terrain area then 384 /// it returns false. 385 /// 386 bool getNormalHeightMaterial( const Point2F &pos, 387 Point3F *normal, 388 F32 *height, 389 StringTableEntry &matName ) const; 390 391 // only the editor currently uses this method - should always be using a ray to collide with 392 bool collideBox( const Point3F &start, const Point3F &end, RayInfo* info ) 393 { 394 return castRay( start, end, info ); 395 } 396 397 /// 398 void setLightMap( GBitmap *newLightMap ); 399 400 /// Fills the lightmap with white. 401 void clearLightMap(); 402 403 /// Retuns the dimensions of the light map. 404 U32 getLightMapSize() const { return mLightMapSize; } 405 406 const GBitmap* getLightMap() const { return mLightMap; } 407 408 GBitmap* getLightMap() { return mLightMap; } 409 410 /// 411 GFXTextureObject* getLightMapTex(); 412 413public: 414 415 bool setFile( const FileName& terrFileName ); 416 417 void setFile(const Resource<TerrainFile>& file); 418 419 bool save(const char* filename); 420 421 F32 getSquareSize() const { return mSquareSize; } 422 423 /// Returns the dimensions of the terrain in world space. 424 F32 getWorldBlockSize() const { return mSquareSize * (F32)mFile->mSize; } 425 426 /// Retuns the dimensions of the terrain in samples. 427 U32 getBlockSize() const { return mFile->mSize; } 428 429 U32 getScreenError() const { return smLODScale * mScreenError; } 430 431 // SceneObject 432 void setTransform( const MatrixF &mat ); 433 void setScale( const VectorF &scale ); 434 435 void prepRenderImage ( SceneRenderState* state ); 436 437 void buildConvex(const Box3F& box,Convex* convex); 438 bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere); 439 bool castRay(const Point3F &start, const Point3F &end, RayInfo* info); 440 bool castRayI(const Point3F &start, const Point3F &end, RayInfo* info, bool emptyCollide); 441 442 bool castRayBlock( const Point3F &pStart, 443 const Point3F &pEnd, 444 const Point2I &blockPos, 445 U32 level, 446 F32 invDeltaX, 447 F32 invDeltaY, 448 F32 startT, 449 F32 endT, 450 RayInfo *info, 451 bool collideEmpty ); 452 453 const FileName& getTerrainFile() const { return mTerrFileName; } 454 455 void postLight(Vector<TerrainBlock*> &terrBlocks) {}; 456 457 458 DECLARE_CONOBJECT(TerrainBlock); 459 static void initPersistFields(); 460 U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream); 461 void unpackUpdate(NetConnection *conn, BitStream *stream); 462 void inspectPostApply(); 463}; 464 465#endif // _TERRDATA_H_ 466
