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