terrExport.cpp 5.3 KB

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