MCPcopy Create free account
hub / github.com/TorqueGameEngines/Torque3D / exportHeightMap

Method exportHeightMap

Engine/source/terrain/terrExport.cpp:32–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30#ifdef TORQUE_TOOLS
31
32bool TerrainBlock::exportHeightMap( const UTF8 *filePath, const String &format ) const
33{
34
35 GBitmap output( mFile->mSize,
36 mFile->mSize,
37 false,
38 GFXFormatR5G6B5 );
39
40 // First capture the max height... we'll normalize
41 // everything to this value.
42 U16 maxHeight = 0;
43
44 Vector<const U16>::iterator iBits = mFile->mHeightMap.begin();
45 for ( S32 y = 0; y < mFile->mSize; y++ )
46 {
47 for ( S32 x = 0; x < mFile->mSize; x++ )
48 {
49 if ( *iBits > maxHeight )
50 maxHeight = *iBits;
51 ++iBits;
52 }
53 }
54
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++ )
59 {
60 for ( S32 x = 0; x < mFile->mSize; x++ )
61 {
62 // PNG expects big endian.
63 U16 height = (U16)( ( (F32)(*iBits) / (F32)maxHeight ) * (F32)U16_MAX );
64 *oBits = convertHostToBEndian( height );
65 ++oBits;
66 ++iBits;
67 }
68 }
69
70 FileStream stream;
71 if ( !stream.open( filePath, Torque::FS::File::Write ) )
72 {
73 Con::errorf( "TerrainBlock::exportHeightMap() - Error opening file for writing: %s !", filePath );
74 return false;
75 }
76
77 if ( !output.writeBitmap( format, stream ) )
78 {
79 Con::errorf( "TerrainBlock::exportHeightMap() - Error writing %s: %s !", format.c_str(), filePath );
80 return false;
81 }
82
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 );
89

Callers

nothing calls this directly

Calls 8

fixedToFloatFunction · 0.85
printfFunction · 0.85
getWritableBitsMethod · 0.80
writeBitmapMethod · 0.80
openMethod · 0.65
errorfFunction · 0.50
beginMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected