terrData.h

Engine/source/terrain/terrData.h

More...

Classes:

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