| 361 | } |
| 362 | |
| 363 | bool TerrainTools::ExportTerrain(Terrain* terrain, String outputFolder) |
| 364 | { |
| 365 | PROFILE_CPU_NAMED("Terrain.ExportTerrain"); |
| 366 | CHECK_RETURN(terrain && terrain->GetPatchesCount() != 0, true); |
| 367 | |
| 368 | // Find size of heightmap in patches |
| 369 | const auto firstPatch = terrain->GetPatch(0); |
| 370 | Int2 start(firstPatch->GetX(), firstPatch->GetZ()); |
| 371 | Int2 end(start); |
| 372 | for (int32 patchIndex = 0; patchIndex < terrain->GetPatchesCount(); patchIndex++) |
| 373 | { |
| 374 | const auto patch = terrain->GetPatch(patchIndex); |
| 375 | const Int2 pos(patch->GetX(), patch->GetZ()); |
| 376 | start = Int2::Min(start, pos); |
| 377 | end = Int2::Max(end, pos); |
| 378 | } |
| 379 | const Int2 size = (end + 1) - start; |
| 380 | |
| 381 | // Allocate heightmap for a whole terrain (NumberOfPatches * 4x4 * ChunkSize + 1) |
| 382 | const Int2 heightmapSize = size * Terrain::ChunksCountEdge * terrain->GetChunkSize() + 1; |
| 383 | Array<float> heightmap; |
| 384 | heightmap.Resize(heightmapSize.X * heightmapSize.Y); |
| 385 | if (const float* heightmapData = firstPatch->GetHeightmapData()) |
| 386 | heightmap.SetAll(heightmapData[0]); |
| 387 | |
| 388 | // Fill heightmap with data from all patches |
| 389 | const int32 rowSize = terrain->GetChunkSize() * Terrain::ChunksCountEdge + 1; |
| 390 | for (int32 patchIndex = 0; patchIndex < terrain->GetPatchesCount(); patchIndex++) |
| 391 | { |
| 392 | const auto patch = terrain->GetPatch(patchIndex); |
| 393 | const Int2 pos(patch->GetX() - start.X, patch->GetZ() - start.Y); |
| 394 | const float* src = patch->GetHeightmapData(); |
| 395 | float* dst = heightmap.Get() + pos.X * (rowSize - 1) + pos.Y * heightmapSize.X * (rowSize - 1); |
| 396 | if (src) |
| 397 | { |
| 398 | for (int32 row = 0; row < rowSize; row++) |
| 399 | Platform::MemoryCopy(dst + row * heightmapSize.X, src + row * rowSize, rowSize * sizeof(float)); |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | for (int32 row = 0; row < rowSize; row++) |
| 404 | Platform::MemoryClear(dst + row * heightmapSize.X, rowSize * sizeof(float)); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | // Interpolate to 16-bit int |
| 409 | float maxHeight, minHeight; |
| 410 | maxHeight = minHeight = heightmap[0]; |
| 411 | for (int32 i = 1; i < heightmap.Count(); i++) |
| 412 | { |
| 413 | float h = heightmap.Get()[i]; |
| 414 | if (maxHeight < h) |
| 415 | maxHeight = h; |
| 416 | else if (minHeight > h) |
| 417 | minHeight = h; |
| 418 | } |
| 419 | const float alpha = MAX_uint16 / (maxHeight - minHeight); |
| 420 |
no test coverage detected