terrImport.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "terrain/terrData.h"
  24. #include "gfx/bitmap/gBitmap.h"
  25. #include "sim/netConnection.h"
  26. #include "core/strings/stringUnit.h"
  27. #include "core/resourceManager.h"
  28. #include "gui/worldEditor/terrainEditor.h"
  29. #include "util/noise2d.h"
  30. #include "core/volume.h"
  31. ConsoleStaticMethod( TerrainBlock, createNew, S32, 5, 5,
  32. "TerrainBlock.create( String terrainName, U32 resolution, String materialName, bool genNoise )\n"
  33. "" )
  34. {
  35. const UTF8 *terrainName = argv[1];
  36. U32 resolution = dAtoi( argv[2] );
  37. const UTF8 *materialName = argv[3];
  38. bool genNoise = dAtob( argv[4] );
  39. Vector<String> materials;
  40. materials.push_back( materialName );
  41. TerrainBlock *terrain = new TerrainBlock();
  42. // We create terrains based on level name. If the user wants to rename the terrain names; they have to
  43. // rename it themselves in their file browser. The main reason for this is so we can easily increment for ourselves;
  44. // and because its too easy to rename the terrain object and forget to take care of the terrain filename afterwards.
  45. FileName terrFileName( Con::getVariable("$Client::MissionFile") );
  46. String terrainDirectory( Con::getVariable( "$pref::Directories::Terrain" ) );
  47. if ( terrainDirectory.isEmpty() )
  48. {
  49. terrainDirectory = "art/terrains/";
  50. }
  51. terrFileName.replace("tools/levels/", terrainDirectory);
  52. terrFileName.replace("levels/", terrainDirectory);
  53. TerrainFile::create( &terrFileName, resolution, materials );
  54. if( !terrain->setFile( terrFileName ) )
  55. {
  56. Con::errorf( "TerrainBlock::createNew - error creating '%s'", terrFileName.c_str() );
  57. return 0;
  58. }
  59. terrain->setPosition( Point3F( 0, 0, 0 ) );
  60. const U32 blockSize = terrain->getBlockSize();
  61. if ( genNoise )
  62. {
  63. TerrainFile *file = terrain->getFile();
  64. Vector<F32> floatHeights;
  65. floatHeights.setSize( blockSize * blockSize );
  66. Noise2D noise;
  67. noise.setSeed( 134208587 );
  68. // Set up some defaults.
  69. F32 octaves = 3.0f;
  70. U32 freq = 4;
  71. F32 roughness = 0.0f;
  72. noise.fBm( &floatHeights, blockSize, freq, 1.0f - roughness, octaves );
  73. F32 height = 0;
  74. F32 omax, omin;
  75. noise.getMinMax( &floatHeights, &omin, &omax, blockSize );
  76. F32 terrscale = 300.0f / (omax - omin);
  77. for ( S32 y = 0; y < blockSize; y++ )
  78. {
  79. for ( S32 x = 0; x < blockSize; x++ )
  80. {
  81. // Very important to subtract the min
  82. // noise value when using the noise functions
  83. // for terrain, otherwise floatToFixed() will
  84. // wrap negative values to U16_MAX, creating
  85. // a very ugly terrain.
  86. height = (floatHeights[ x + (y * blockSize) ] - omin) * terrscale + 30.0f;
  87. file->setHeight( x, y, floatToFixed( height ) );
  88. }
  89. }
  90. terrain->updateGrid( Point2I::Zero, Point2I( blockSize, blockSize ) );
  91. terrain->updateGridMaterials( Point2I::Zero, Point2I( blockSize, blockSize ) );
  92. }
  93. terrain->registerObject( terrainName );
  94. // Add to mission group!
  95. SimGroup *missionGroup;
  96. if( Sim::findObject( "MissionGroup", missionGroup ) )
  97. missionGroup->addObject( terrain );
  98. return terrain->getId();
  99. }
  100. ConsoleStaticMethod( TerrainBlock, import, S32, 7, 8,
  101. "( String terrainName, String heightMap, F32 metersPerPixel, F32 heightScale, String materials, String opacityLayers[, bool flipYAxis=true] )\n"
  102. "" )
  103. {
  104. // Get the parameters.
  105. const UTF8 *terrainName = argv[1];
  106. const UTF8 *hmap = argv[2];
  107. F32 metersPerPixel = dAtof(argv[3]);
  108. F32 heightScale = dAtof(argv[4]);
  109. const UTF8 *opacityFiles = argv[5];
  110. const UTF8 *materialsStr = argv[6];
  111. bool flipYAxis = argc == 8? dAtob(argv[7]) : true;
  112. // First load the height map and validate it.
  113. Resource<GBitmap> heightmap = GBitmap::load( hmap );
  114. if ( !heightmap )
  115. {
  116. Con::errorf( "Heightmap failed to load!" );
  117. return 0;
  118. }
  119. U32 terrSize = heightmap->getWidth();
  120. U32 hheight = heightmap->getHeight();
  121. if ( terrSize != hheight || !isPow2( terrSize ) )
  122. {
  123. Con::errorf( "Height map must be square and power of two in size!" );
  124. return 0;
  125. }
  126. else if ( terrSize < 128 || terrSize > 4096 )
  127. {
  128. Con::errorf( "Height map must be between 128 and 4096 in size!" );
  129. return 0;
  130. }
  131. U32 fileCount = StringUnit::getUnitCount( opacityFiles, "\n" );
  132. Vector<U8> layerMap;
  133. layerMap.setSize( terrSize * terrSize );
  134. {
  135. Vector<GBitmap*> bitmaps;
  136. for ( U32 i = 0; i < fileCount; i++ )
  137. {
  138. String fileNameWithChannel = StringUnit::getUnit( opacityFiles, i, "\n" );
  139. String fileName = StringUnit::getUnit( fileNameWithChannel, 0, "\t" );
  140. String channel = StringUnit::getUnit( fileNameWithChannel, 1, "\t" );
  141. if ( fileName.isEmpty() )
  142. continue;
  143. if ( !channel.isEmpty() )
  144. {
  145. // Load and push back the bitmap here.
  146. Resource<GBitmap> opacityMap = ResourceManager::get().load( fileName );
  147. if ( terrSize != opacityMap->getWidth() || terrSize != opacityMap->getHeight() )
  148. {
  149. Con::errorf( "The opacity map '%s' doesn't match height map size!", fileName.c_str() );
  150. return 0;
  151. }
  152. // Always going to be one channel.
  153. GBitmap *opacityMapChannel = new GBitmap( terrSize,
  154. terrSize,
  155. false,
  156. GFXFormatA8 );
  157. if ( opacityMap->getBytesPerPixel() > 1 )
  158. {
  159. if ( channel.equal( "R", 1 ) )
  160. opacityMap->copyChannel( 0, opacityMapChannel );
  161. else if ( channel.equal( "G", 1 ) )
  162. opacityMap->copyChannel( 1, opacityMapChannel );
  163. else if ( channel.equal( "B", 1 ) )
  164. opacityMap->copyChannel( 2, opacityMapChannel );
  165. else if ( channel.equal( "A", 1 ) )
  166. opacityMap->copyChannel( 3, opacityMapChannel );
  167. bitmaps.push_back( opacityMapChannel );
  168. }
  169. else
  170. {
  171. opacityMapChannel->copyRect( opacityMap, RectI( 0, 0, terrSize, terrSize ), Point2I( 0, 0 ) );
  172. bitmaps.push_back( opacityMapChannel );
  173. }
  174. }
  175. }
  176. // Ok... time to convert all this opacity layer
  177. // mess to the layer index map!
  178. U32 layerCount = bitmaps.size() - 1;
  179. U32 layer, lastValue;
  180. U8 value;
  181. for ( U32 i = 0; i < terrSize * terrSize; i++ )
  182. {
  183. // Find the greatest layer.
  184. layer = lastValue = 0;
  185. for ( U32 k=0; k < bitmaps.size(); k++ )
  186. {
  187. value = bitmaps[k]->getBits()[i];
  188. if ( value >= lastValue )
  189. {
  190. layer = k;
  191. lastValue = value;
  192. }
  193. }
  194. // Set the layer index.
  195. layerMap[i] = getMin( layer, layerCount );
  196. }
  197. // Cleanup the bitmaps.
  198. for ( U32 i=0; i < bitmaps.size(); i++ )
  199. delete bitmaps[i];
  200. }
  201. U32 matCount = StringUnit::getUnitCount( materialsStr, "\t\n" );
  202. if( matCount != fileCount)
  203. {
  204. Con::errorf("Number of Materials and Layer maps must be equal.");
  205. return 0;
  206. }
  207. Vector<String> materials;
  208. for ( U32 i = 0; i < matCount; i++ )
  209. {
  210. String matStr = StringUnit::getUnit( materialsStr, i, "\t\n" );
  211. // even if matStr is empty, insert it as a placeholder (will be replaced with warning material later)
  212. materials.push_back( matStr );
  213. }
  214. // Do we have an existing terrain with that name... then update it!
  215. TerrainBlock *terrain = dynamic_cast<TerrainBlock*>( Sim::findObject( terrainName ) );
  216. if ( terrain )
  217. terrain->import( (*heightmap), heightScale, metersPerPixel, layerMap, materials, flipYAxis );
  218. else
  219. {
  220. terrain = new TerrainBlock();
  221. terrain->assignName( terrainName );
  222. terrain->import( (*heightmap), heightScale, metersPerPixel, layerMap, materials, flipYAxis );
  223. terrain->registerObject();
  224. // Add to mission group!
  225. SimGroup *missionGroup;
  226. if ( Sim::findObject( "MissionGroup", missionGroup ) )
  227. missionGroup->addObject( terrain );
  228. }
  229. return terrain->getId();
  230. }
  231. bool TerrainBlock::import( const GBitmap &heightMap,
  232. F32 heightScale,
  233. F32 metersPerPixel,
  234. const Vector<U8> &layerMap,
  235. const Vector<String> &materials,
  236. bool flipYAxis)
  237. {
  238. AssertFatal( isServerObject(), "TerrainBlock::import - This should only be called on the server terrain!" );
  239. AssertFatal( heightMap.getWidth() == heightMap.getHeight(), "TerrainBlock::import - Height map is not square!" );
  240. AssertFatal( isPow2( heightMap.getWidth() ), "TerrainBlock::import - Height map is not power of two!" );
  241. // If we don't have a terrain file then add one.
  242. if ( !mFile )
  243. {
  244. // Get a unique file name for the terrain.
  245. String fileName( getName() );
  246. if ( fileName.isEmpty() )
  247. fileName = "terrain";
  248. mTerrFileName = FS::MakeUniquePath( "levels", fileName, "ter" );
  249. // TODO: We have to save and reload the file to get
  250. // it into the resource system. This creates lots
  251. // of temporary unused files when the terrain is
  252. // discarded because of undo or quit.
  253. TerrainFile *file = new TerrainFile;
  254. file->save( mTerrFileName );
  255. delete file;
  256. mFile = ResourceManager::get().load( mTerrFileName );
  257. }
  258. // The file does a bunch of the work.
  259. mFile->import( heightMap, heightScale, layerMap, materials, flipYAxis );
  260. // Set the square size.
  261. mSquareSize = metersPerPixel;
  262. if ( isProperlyAdded() )
  263. {
  264. // Update the server bounds.
  265. _updateBounds();
  266. // Make sure the client gets updated.
  267. setMaskBits( HeightMapChangeMask | SizeMask );
  268. }
  269. return true;
  270. }