Call the correct system rename
[Torque-3d.git] / Engine / source / terrain / terrExport.cpp
blob8941c8b261de310e48b6b012b1c65918843dd06c
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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 //-----------------------------------------------------------------------------
23 #include "platform/platform.h"
24 #include "terrain/terrData.h"
25 #include "gfx/bitmap/gBitmap.h"
26 #include "terrain/terrMaterial.h"
27 #include "core/stream/fileStream.h"
28 #include "console/engineAPI.h"
30 #ifdef TORQUE_TOOLS
32 bool TerrainBlock::exportHeightMap( const UTF8 *filePath, const String &format ) const
35 GBitmap output( mFile->mSize,
36 mFile->mSize,
37 false,
38 GFXFormatR5G6B5 );
40 // First capture the max height... we'll normalize
41 // everything to this value.
42 U16 maxHeight = 0;
44 Vector<const U16>::iterator iBits = mFile->mHeightMap.begin();
45 for ( S32 y = 0; y < mFile->mSize; y++ )
47 for ( S32 x = 0; x < mFile->mSize; x++ )
49 if ( *iBits > maxHeight )
50 maxHeight = *iBits;
51 ++iBits;
55 // Now write out the map.
56 iBits = mFile->mHeightMap.begin();
57 U16 *oBits = (U16*)output.getWritableBits();
58 for ( S32 y = 0; y < mFile->mSize; y++ )
60 for ( S32 x = 0; x < mFile->mSize; x++ )
62 // PNG expects big endian.
63 U16 height = (U16)( ( (F32)(*iBits) / (F32)maxHeight ) * (F32)U16_MAX );
64 *oBits = convertHostToBEndian( height );
65 ++oBits;
66 ++iBits;
70 FileStream stream;
71 if ( !stream.open( filePath, Torque::FS::File::Write ) )
73 Con::errorf( "TerrainBlock::exportHeightMap() - Error opening file for writing: %s !", filePath );
74 return false;
77 if ( !output.writeBitmap( format, stream ) )
79 Con::errorf( "TerrainBlock::exportHeightMap() - Error writing %s: %s !", format.c_str(), filePath );
80 return false;
83 // Print out the map size in meters, so that the user
84 // knows what values to use when importing it into
85 // another terrain tool.
86 S32 dim = mSquareSize * mFile->mSize;
87 S32 height = fixedToFloat( maxHeight );
88 Con::printf( "Saved heightmap with dimensions %d x %d x %d.", dim, dim, height );
90 return true;
93 bool TerrainBlock::exportLayerMaps( const UTF8 *filePrefix, const String &format ) const
95 for(S32 i = 0; i < mFile->mMaterials.size(); i++)
97 Vector<const U8>::iterator iBits = mFile->mLayerMap.begin();
99 GBitmap output( mFile->mSize,
100 mFile->mSize,
101 false,
102 GFXFormatA8 );
104 // Copy the layer data.
105 U8 *oBits = (U8*)output.getWritableBits();
106 dMemset( oBits, 0, mFile->mSize * mFile->mSize );
108 for ( S32 y = 0; y < mFile->mSize; y++ )
110 for ( S32 x = 0; x < mFile->mSize; x++ )
112 if(*iBits == i)
113 *oBits = 0xFF;
114 ++iBits;
115 ++oBits;
119 // Whats the full file name for this layer.
120 UTF8 filePath[1024];
121 dSprintf( filePath, 1024, "%s_%d_%s.%s", filePrefix, i, mFile->mMaterials[i]->getInternalName(), format.c_str() );
123 FileStream stream;
124 if ( !stream.open( filePath, Torque::FS::File::Write ) )
126 Con::errorf( "TerrainBlock::exportLayerMaps() - Error opening file for writing: %s !", filePath );
127 return false;
130 if ( !output.writeBitmap( format, stream ) )
132 Con::errorf( "TerrainBlock::exportLayerMaps() - Error writing %s: %s !", format.c_str(), filePath );
133 return false;
137 return true;
140 DefineConsoleMethod( 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 UTF8 fileName[1024];
143 Con::expandScriptFilename( fileName, sizeof( fileName ), fileNameStr );
145 return object->exportHeightMap( fileName, format );
148 DefineConsoleMethod( 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 UTF8 filePrefix[1024];
151 Con::expandScriptFilename( filePrefix, sizeof( filePrefix ), filePrefixStr );
153 return object->exportLayerMaps( filePrefix, format );
155 #endif