terrExport.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "terrain/terrMaterial.h"
  26. #include "core/stream/fileStream.h"
  27. #ifdef TORQUE_TOOLS
  28. bool TerrainBlock::exportHeightMap( const UTF8 *filePath, const String &format ) const
  29. {
  30. GBitmap output( mFile->mSize,
  31. mFile->mSize,
  32. false,
  33. GFXFormatR5G6B5 );
  34. // First capture the max height... we'll normalize
  35. // everything to this value.
  36. U16 maxHeight = 0;
  37. Vector<const U16>::iterator iBits = mFile->mHeightMap.begin();
  38. for ( S32 y = 0; y < mFile->mSize; y++ )
  39. {
  40. for ( S32 x = 0; x < mFile->mSize; x++ )
  41. {
  42. if ( *iBits > maxHeight )
  43. maxHeight = *iBits;
  44. ++iBits;
  45. }
  46. }
  47. // Now write out the map.
  48. iBits = mFile->mHeightMap.begin();
  49. U16 *oBits = (U16*)output.getWritableBits();
  50. for ( S32 y = 0; y < mFile->mSize; y++ )
  51. {
  52. for ( S32 x = 0; x < mFile->mSize; x++ )
  53. {
  54. // PNG expects big endian.
  55. U16 height = (U16)( ( (F32)(*iBits) / (F32)maxHeight ) * (F32)U16_MAX );
  56. *oBits = convertHostToBEndian( height );
  57. ++oBits;
  58. ++iBits;
  59. }
  60. }
  61. FileStream stream;
  62. if ( !stream.open( filePath, Torque::FS::File::Write ) )
  63. {
  64. Con::errorf( "TerrainBlock::exportHeightMap() - Error opening file for writing: %s !", filePath );
  65. return false;
  66. }
  67. if ( !output.writeBitmap( format, stream ) )
  68. {
  69. Con::errorf( "TerrainBlock::exportHeightMap() - Error writing %s: %s !", format.c_str(), filePath );
  70. return false;
  71. }
  72. // Print out the map size in meters, so that the user
  73. // knows what values to use when importing it into
  74. // another terrain tool.
  75. S32 dim = mSquareSize * mFile->mSize;
  76. S32 height = fixedToFloat( maxHeight );
  77. Con::printf( "Saved heightmap with dimensions %d x %d x %d.", dim, dim, height );
  78. return true;
  79. }
  80. bool TerrainBlock::exportLayerMaps( const UTF8 *filePrefix, const String &format ) const
  81. {
  82. for(S32 i = 0; i < mFile->mMaterials.size(); i++)
  83. {
  84. Vector<const U8>::iterator iBits = mFile->mLayerMap.begin();
  85. GBitmap output( mFile->mSize,
  86. mFile->mSize,
  87. false,
  88. GFXFormatA8 );
  89. // Copy the layer data.
  90. U8 *oBits = (U8*)output.getWritableBits();
  91. dMemset( oBits, 0, mFile->mSize * mFile->mSize );
  92. for ( S32 y = 0; y < mFile->mSize; y++ )
  93. {
  94. for ( S32 x = 0; x < mFile->mSize; x++ )
  95. {
  96. if(*iBits == i)
  97. *oBits = 0xFF;
  98. ++iBits;
  99. ++oBits;
  100. }
  101. }
  102. // Whats the full file name for this layer.
  103. UTF8 filePath[1024];
  104. dSprintf( filePath, 1024, "%s_%d_%s.%s", filePrefix, i, mFile->mMaterials[i]->getInternalName(), format.c_str() );
  105. FileStream stream;
  106. if ( !stream.open( filePath, Torque::FS::File::Write ) )
  107. {
  108. Con::errorf( "TerrainBlock::exportLayerMaps() - Error opening file for writing: %s !", filePath );
  109. return false;
  110. }
  111. if ( !output.writeBitmap( format, stream ) )
  112. {
  113. Con::errorf( "TerrainBlock::exportLayerMaps() - Error writing %s: %s !", format.c_str(), filePath );
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. ConsoleMethod( TerrainBlock, exportHeightMap, bool, 3, 4, "(string filename, [string format]) - export the terrain block's heightmap to a bitmap file (default: png)" )
  120. {
  121. UTF8 fileName[1024];
  122. String format = "png";
  123. if( argc > 3 )
  124. format = argv[ 3 ];
  125. Con::expandScriptFilename( fileName, sizeof( fileName ), argv[2] );
  126. return object->exportHeightMap( fileName, format );
  127. }
  128. ConsoleMethod( TerrainBlock, exportLayerMaps, bool, 3, 4, "(string filePrefix, [string format]) - export the terrain block's layer maps to bitmap files (default: png)" )
  129. {
  130. UTF8 filePrefix[1024];
  131. String format = "png";
  132. if( argc > 3 )
  133. format = argv[3];
  134. Con::expandScriptFilename( filePrefix, sizeof( filePrefix ), argv[2] );
  135. return object->exportLayerMaps( filePrefix, format );
  136. }
  137. #endif