From b2d376ee5aa4c6e8ed3064de9b68c493cd812416 Mon Sep 17 00:00:00 2001 From: ihm-tswow Date: Sun, 7 May 2023 10:49:03 +0200 Subject: [PATCH] add tiled edges option for heightmap image import - allows importing heightmaps from 256x256 images (e.g. without duplicate edges) --- src/noggit/MapTile.cpp | 70 ++++++++++++++++++- src/noggit/MapTile.h | 2 +- src/noggit/MapView.cpp | 7 +- src/noggit/World.cpp | 17 ++--- src/noggit/World.h | 2 +- .../World/Tile/TileSetHeightmapImage.cpp | 2 +- 6 files changed, 84 insertions(+), 16 deletions(-) diff --git a/src/noggit/MapTile.cpp b/src/noggit/MapTile.cpp index d6b39ffc..3d0784e1 100755 --- a/src/noggit/MapTile.cpp +++ b/src/noggit/MapTile.cpp @@ -19,7 +19,7 @@ #include #include - +#include #include #include @@ -1203,10 +1203,9 @@ QImage MapTile::getAlphamapImage(std::string const& filename) return std::move(image); } -void MapTile::setHeightmapImage(QImage const& image, float multiplier, int mode) +void MapTile::setHeightmapImage(QImage const& image, float multiplier, int mode, bool tiledEdges) { unsigned const LONG{9}, SHORT{8}, SUM{LONG + SHORT}, DSUM{SUM * 2}; - for (int k = 0; k < 16; ++k) { for (int l = 0; l < 16; ++l) @@ -1229,6 +1228,11 @@ void MapTile::setHeightmapImage(QImage const& image, float multiplier, int mode) bool const erp = plain % DSUM / SUM; unsigned const idx {(plain - (is_virtual ? (erp ? SUM : 1) : 0)) / 2}; + if (tiledEdges && ((y == 16 && l == 15) || (x == 16 && k == 15))) + { + continue; + } + switch (image.depth()) { case 8: @@ -1287,6 +1291,66 @@ void MapTile::setHeightmapImage(QImage const& image, float multiplier, int mode) registerChunkUpdate(ChunkUpdateFlags::VERTEX); } } + + if (tiledEdges) // resize + fit + { + if (index.z > 0) + { + getWorld()->for_tile_at(TileIndex{ index.x, index.z-1} + , [&](MapTile* tile) + { + for (int chunk_x = 0; chunk_x < 16; ++chunk_x) + { + MapChunk* targetChunk = tile->getChunk(chunk_x, 15); + MapChunk* sourceChunk = this->getChunk(chunk_x, 0); + targetChunk->registerChunkUpdate(ChunkUpdateFlags::VERTEX); + for (int vert_x = 0; vert_x < 9; ++vert_x) + { + int target_vert = 136 + vert_x; + int source_vert = vert_x; + targetChunk->getHeightmap()[target_vert].y = sourceChunk->getHeightmap()[source_vert].y; + } + } + tile->registerChunkUpdate(ChunkUpdateFlags::VERTEX); + } + ); + } + + if (index.x > 1) + { + getWorld()->for_tile_at(TileIndex{ index.x-1, index.z} + , [&](MapTile* tile) + { + for (int chunk_y = 0; chunk_y < 16; ++chunk_y) + { + MapChunk* targetChunk = tile->getChunk(15, chunk_y); + MapChunk* sourceChunk = this->getChunk(0, chunk_y); + targetChunk->registerChunkUpdate(ChunkUpdateFlags::VERTEX); + for (int vert_y = 0; vert_y < 9; ++vert_y) + { + int target_vert = vert_y * 17 + 8; + int source_vert = vert_y * 17; + targetChunk->getHeightmap()[target_vert].y = sourceChunk->getHeightmap()[source_vert].y; + } + } + tile->registerChunkUpdate(ChunkUpdateFlags::VERTEX); + } + ); + } + + if (index.x > 1 && index.z > 1) + { + getWorld()->for_tile_at(TileIndex { index.x-1, index.z-1 } + , [&] (MapTile* tile) + { + MapChunk* targetChunk = tile->getChunk(15, 15); + targetChunk->registerChunkUpdate(ChunkUpdateFlags::VERTEX); + tile->getChunk(15,15)->getHeightmap()[144].y = this->getChunk(0,0)->getHeightmap()[0].y; + tile->registerChunkUpdate(ChunkUpdateFlags::VERTEX); + } + ); + } + } } void MapTile::setAlphaImage(QImage const& image, unsigned layer) diff --git a/src/noggit/MapTile.h b/src/noggit/MapTile.h index 332b14d2..c9d28f18 100755 --- a/src/noggit/MapTile.h +++ b/src/noggit/MapTile.h @@ -141,7 +141,7 @@ public: QImage getAlphamapImage(std::string const& filename); QImage getVertexColorsImage(); QImage getNormalmapImage(); - void setHeightmapImage(QImage const& image, float multiplier, int mode); + void setHeightmapImage(QImage const& image, float multiplier, int mode, bool tiledEdges); void setAlphaImage(QImage const& image, unsigned layer); void setVertexColorImage(QImage const& image, int mode); void registerChunkUpdate(unsigned flags) { _chunk_update_flags |= flags; }; diff --git a/src/noggit/MapView.cpp b/src/noggit/MapView.cpp index 7ae78d1a..210cd6d8 100755 --- a/src/noggit/MapView.cpp +++ b/src/noggit/MapView.cpp @@ -1540,7 +1540,10 @@ void MapView::setupAssistMenu() adt_import_height_params_layout->addWidget(new QLabel("Mode:", adt_import_height_params)); QComboBox* adt_import_height_params_mode = new QComboBox(adt_import_height_params); adt_import_height_params_layout->addWidget(adt_import_height_params_mode); - adt_import_height_params_mode->addItems({"Set", "Add", "Subtract", "Multiply"}); + adt_import_height_params_mode->addItems({"Set", "Add", "Subtract", "Multiply" }); + + QCheckBox* adt_import_height_tiled_edges = new QCheckBox("Tiled Edges", adt_import_height_params); + adt_import_height_params_layout->addWidget(adt_import_height_tiled_edges); QPushButton* adt_import_height_params_okay = new QPushButton("Okay", adt_import_height_params); adt_import_height_params_layout->addWidget(adt_import_height_params_okay); @@ -1576,7 +1579,7 @@ void MapView::setupAssistMenu() NOGGIT_ACTION_MGR->beginAction(this, Noggit::ActionFlags::eCHUNKS_TERRAIN); _world->importADTHeightmap(_camera.position, img, adt_import_height_params_multiplier->value(), - adt_import_height_params_mode->currentIndex()); + adt_import_height_params_mode->currentIndex(), adt_import_height_tiled_edges->isChecked()); NOGGIT_ACTION_MGR->endAction(); } } diff --git a/src/noggit/World.cpp b/src/noggit/World.cpp index 7de4606b..fac541cf 100755 --- a/src/noggit/World.cpp +++ b/src/noggit/World.cpp @@ -1834,22 +1834,23 @@ void World::importADTAlphamap(glm::vec3 const& pos) ); } -void World::importADTHeightmap(glm::vec3 const& pos, QImage const& image, float multiplier, unsigned mode) +void World::importADTHeightmap(glm::vec3 const& pos, QImage const& image, float multiplier, unsigned mode, bool tiledEdges) { ZoneScoped; + int desired_dimensions = tiledEdges ? 256 : 257; for_all_chunks_on_tile(pos, [](MapChunk* chunk) { NOGGIT_CUR_ACTION->registerChunkTerrainChange(chunk); }); - if (image.width() != 257 || image.height() != 257) + if (image.width() != desired_dimensions || image.height() != desired_dimensions) { - QImage scaled = image.scaled(257, 257, Qt::AspectRatioMode::IgnoreAspectRatio); + QImage scaled = image.scaled(desired_dimensions, desired_dimensions, Qt::AspectRatioMode::IgnoreAspectRatio); for_tile_at ( pos , [&] (MapTile* tile) { - tile->setHeightmapImage(scaled, multiplier, mode); + tile->setHeightmapImage(scaled, multiplier, mode, tiledEdges); } ); @@ -1859,7 +1860,7 @@ void World::importADTHeightmap(glm::vec3 const& pos, QImage const& image, float for_tile_at ( pos , [&] (MapTile* tile) { - tile->setHeightmapImage(image, multiplier, mode); + tile->setHeightmapImage(image, multiplier, mode, tiledEdges); } ); } @@ -1896,7 +1897,7 @@ void World::importADTHeightmap(glm::vec3 const& pos, float multiplier, unsigned if (img.width() != 257 || img.height() != 257) img = img.scaled(257, 257, Qt::AspectRatioMode::IgnoreAspectRatio); - tile->setHeightmapImage(img, multiplier, mode); + tile->setHeightmapImage(img, multiplier, mode, false); } ); @@ -2839,11 +2840,11 @@ void World::importAllADTsHeightmaps(float multiplier, unsigned int mode) if (img.width() != 257 || img.height() != 257) { QImage scaled = img.scaled(257, 257, Qt::IgnoreAspectRatio); - mTile->setHeightmapImage(scaled, multiplier, mode); + mTile->setHeightmapImage(scaled, multiplier, mode, false); } else { - mTile->setHeightmapImage(img, multiplier, mode); + mTile->setHeightmapImage(img, multiplier, mode, false); } mTile->saveTile(this); diff --git a/src/noggit/World.h b/src/noggit/World.h index 1a26da4b..5547698d 100755 --- a/src/noggit/World.h +++ b/src/noggit/World.h @@ -235,7 +235,7 @@ public: void importADTAlphamap(glm::vec3 const& pos, QImage const& image, unsigned layer); void importADTAlphamap(glm::vec3 const& pos); - void importADTHeightmap(glm::vec3 const& pos, QImage const& image, float multiplier, unsigned mode); + void importADTHeightmap(glm::vec3 const& pos, QImage const& image, float multiplier, unsigned mode, bool tiledEdges); void importADTHeightmap(glm::vec3 const& pos, float multiplier, unsigned mode); void importADTVertexColorMap(glm::vec3 const& pos, int mode); void importADTVertexColorMap(glm::vec3 const& pos, QImage const& image, int mode); diff --git a/src/noggit/ui/tools/NodeEditor/Nodes/World/Tile/TileSetHeightmapImage.cpp b/src/noggit/ui/tools/NodeEditor/Nodes/World/Tile/TileSetHeightmapImage.cpp index fafe19dd..67ac1ad6 100755 --- a/src/noggit/ui/tools/NodeEditor/Nodes/World/Tile/TileSetHeightmapImage.cpp +++ b/src/noggit/ui/tools/NodeEditor/Nodes/World/Tile/TileSetHeightmapImage.cpp @@ -59,7 +59,7 @@ void TileSetHeightmapImageNode::compute() return; } - tile->setHeightmapImage(*image_to_use, static_cast(multiplier), _operation->currentIndex()); + tile->setHeightmapImage(*image_to_use, static_cast(multiplier), _operation->currentIndex(), false); _out_ports[0].out_value = std::make_shared(true); _node->onDataUpdated(0);