diff --git a/src/noggit/MapChunk.cpp b/src/noggit/MapChunk.cpp index 7ce52c79..77119e5c 100755 --- a/src/noggit/MapChunk.cpp +++ b/src/noggit/MapChunk.cpp @@ -1280,10 +1280,13 @@ int MapChunk::addTexture(scoped_blp_texture_reference texture) return texture_set->addTexture(std::move (texture)); } -void MapChunk::switchTexture(scoped_blp_texture_reference const& oldTexture, scoped_blp_texture_reference newTexture) +bool MapChunk::switchTexture(scoped_blp_texture_reference const& oldTexture, scoped_blp_texture_reference newTexture) { - texture_set->replace_texture(oldTexture, std::move (newTexture)); - registerChunkUpdate(ChunkUpdateFlags::ALPHAMAP); + bool changed = texture_set->replace_texture(oldTexture, std::move (newTexture)); + if (changed) + registerChunkUpdate(ChunkUpdateFlags::ALPHAMAP); + + return changed; } bool MapChunk::paintTexture(glm::vec3 const& pos, Brush* brush, float strength, float pressure, scoped_blp_texture_reference texture) diff --git a/src/noggit/MapChunk.h b/src/noggit/MapChunk.h index 03f62e10..3237ea09 100755 --- a/src/noggit/MapChunk.h +++ b/src/noggit/MapChunk.h @@ -160,7 +160,7 @@ public: bool replaceTexture(glm::vec3 const& pos, float radius, scoped_blp_texture_reference const& old_texture, scoped_blp_texture_reference new_texture, bool entire_chunk = false); bool canPaintTexture(scoped_blp_texture_reference texture); int addTexture(scoped_blp_texture_reference texture); - void switchTexture(scoped_blp_texture_reference const& oldTexture, scoped_blp_texture_reference newTexture); + bool switchTexture(scoped_blp_texture_reference const& oldTexture, scoped_blp_texture_reference newTexture); void eraseTextures(); void eraseTexture(scoped_blp_texture_reference const& tex); void change_texture_flag(scoped_blp_texture_reference const& tex, std::size_t flag, bool add); diff --git a/src/noggit/MapTile.cpp b/src/noggit/MapTile.cpp index cadc4776..bee9975d 100755 --- a/src/noggit/MapTile.cpp +++ b/src/noggit/MapTile.cpp @@ -1049,7 +1049,7 @@ void MapTile::initEmptyChunks() QImage MapTile::getHeightmapImage(float min_height, float max_height) { - QImage image(257, 257, QImage::Format_RGBA64); + QImage image(257, 257, QImage::Format_Grayscale16); unsigned const LONG{9}, SHORT{8}, SUM{LONG + SHORT}, DSUM{SUM * 2}; @@ -1121,7 +1121,7 @@ QImage MapTile::getNormalmapImage() QImage MapTile::getAlphamapImage(unsigned layer) { - QImage image(1024, 1024, QImage::Format_RGBA8888); + QImage image(1024, 1024, QImage::Format_Grayscale8); image.fill(Qt::black); for (int i = 0; i < 16; ++i) @@ -1154,7 +1154,7 @@ QImage MapTile::getAlphamapImage(unsigned layer) QImage MapTile::getAlphamapImage(std::string const& filename) { - QImage image(1024, 1024, QImage::Format_RGBA8888); + QImage image(1024, 1024, QImage::Format_Grayscale8); image.fill(Qt::black); for (int i = 0; i < 16; ++i) @@ -1164,20 +1164,25 @@ QImage MapTile::getAlphamapImage(std::string const& filename) MapChunk *chunk = getChunk(i, j); unsigned layer = 0; + bool chunk_has_texture = false; for (int k = 0; k < chunk->texture_set->num(); ++k) { - if (chunk->texture_set->filename(k) == filename) - layer = k; + if (chunk->texture_set->filename(k) == filename) + { + layer = k; + chunk_has_texture = true; + } } - if (!layer) + if (!chunk_has_texture) { for (int k = 0; k < 64; ++k) { for (int l = 0; l < 64; ++l) { - image.setPixelColor((i * 64) + k, (j * 64) + l, QColor(255, 255, 255, 255)); + // if texture is not in the chunk, set chunk to black + image.setPixelColor((i * 64) + k, (j * 64) + l, QColor(0, 0, 0, 255)); } } } @@ -1186,14 +1191,31 @@ QImage MapTile::getAlphamapImage(std::string const& filename) chunk->texture_set->apply_alpha_changes(); auto alphamaps = chunk->texture_set->getAlphamaps(); - auto alpha_layer = alphamaps->at(layer - 1).value(); - for (int k = 0; k < 64; ++k) { for (int l = 0; l < 64; ++l) { - int value = alpha_layer.getAlpha(64 * l + k); - image.setPixelColor((i * 64) + k, (j * 64) + l, QColor(value, value, value, 255)); + if (layer == 0) // titi test + { + // WoW calculates layer 0 as 255 - sum(Layer[1]...Layer[3]) + int layers_sum = 0; + if (alphamaps->at(0).has_value()) + layers_sum += alphamaps->at(0).value().getAlpha(64 * l + k); + if (alphamaps->at(1).has_value()) + layers_sum += alphamaps->at(1).value().getAlpha(64 * l + k); + if (alphamaps->at(2).has_value()) + layers_sum += alphamaps->at(2).value().getAlpha(64 * l + k); + + int value = std::clamp((255 - layers_sum), 0, 255); + image.setPixelColor((i * 64) + k, (j * 64) + l, QColor(value, value, value, 255)); + } + else // layer 1-3 + { + auto alpha_layer = alphamaps->at(layer - 1).value(); + + int value = alpha_layer.getAlpha(64 * l + k); + image.setPixelColor((i * 64) + k, (j * 64) + l, QColor(value, value, value, 255)); + } } } } @@ -1203,8 +1225,10 @@ QImage MapTile::getAlphamapImage(std::string const& filename) return std::move(image); } -void MapTile::setHeightmapImage(QImage const& image, float multiplier, int mode, bool tiledEdges) +void MapTile::setHeightmapImage(QImage const& baseimage, float multiplier, int mode, bool tiledEdges) // image { + auto image = baseimage.convertToFormat(QImage::Format_Grayscale16); + unsigned const LONG{9}, SHORT{8}, SUM{LONG + SHORT}, DSUM{SUM * 2}; for (int k = 0; k < 16; ++k) { @@ -1353,8 +1377,10 @@ void MapTile::setHeightmapImage(QImage const& image, float multiplier, int mode, } } -void MapTile::setAlphaImage(QImage const& image, unsigned layer) +void MapTile::setAlphaImage(QImage const& baseimage, unsigned layer) { + auto image = baseimage.convertToFormat(QImage::Format_Grayscale8); + for (int k = 0; k < 16; ++k) { for (int l = 0; l < 16; ++l) @@ -1364,6 +1390,8 @@ void MapTile::setAlphaImage(QImage const& image, unsigned layer) if (layer >= chunk->texture_set->num()) continue; + chunk->registerChunkUpdate(ChunkUpdateFlags::ALPHAMAP); + chunk->texture_set->create_temporary_alphamaps_if_needed(); auto& temp_alphamaps = chunk->texture_set->getTempAlphamaps()->value(); @@ -1420,8 +1448,10 @@ QImage MapTile::getVertexColorsImage() return std::move(image); } -void MapTile::setVertexColorImage(QImage const& image, int mode, bool tiledEdges) +void MapTile::setVertexColorImage(QImage const& baseimage, int mode, bool tiledEdges) { + QImage image = baseimage.convertToFormat(QImage::Format_RGBA8888); + unsigned const LONG{9}, SHORT{8}, SUM{LONG + SHORT}, DSUM{SUM * 2}; for (int k = 0; k < 16; ++k) @@ -1430,6 +1460,8 @@ void MapTile::setVertexColorImage(QImage const& image, int mode, bool tiledEdges { MapChunk* chunk = getChunk(k, l); + chunk->registerChunkUpdate(ChunkUpdateFlags::MCCV); + glm::vec3* colors = chunk->getVertexColors(); for (unsigned y = 0; y < SUM; ++y) @@ -1554,9 +1586,6 @@ void MapTile::setVertexColorImage(QImage const& image, int mode, bool tiledEdges } } - - - void MapTile::recalcExtents() { if (!_extents_dirty) diff --git a/src/noggit/MapTile.h b/src/noggit/MapTile.h index bb04538d..0e8c5d8b 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, bool tiledEdges); + void setHeightmapImage(QImage const& baseimage, float multiplier, int mode, bool tiledEdges); void setAlphaImage(QImage const& image, unsigned layer); void setVertexColorImage(QImage const& image, int mode, bool tiledEdges); void registerChunkUpdate(unsigned flags) { _chunk_update_flags |= flags; }; diff --git a/src/noggit/MapView.cpp b/src/noggit/MapView.cpp index eb039871..2fbc2fba 100755 --- a/src/noggit/MapView.cpp +++ b/src/noggit/MapView.cpp @@ -1826,7 +1826,7 @@ void MapView::setupAssistMenu() ( makeCurrent(); OpenGL::context::scoped_setter const _(::gl, context()); - NOGGIT_ACTION_MGR->beginAction(this, Noggit::ActionFlags::eCHUNKS_TEXTURE); + NOGGIT_ACTION_MGR->beginAction(this, Noggit::ActionFlags::eCHUNKS_TERRAIN); _world->importAllADTsHeightmaps(adt_import_height_params_multiplier->value(), adt_import_height_params_mode->currentIndex(), adt_import_height_tiled_edges->isChecked()); NOGGIT_ACTION_MGR->endAction(); ) @@ -1845,7 +1845,7 @@ void MapView::setupAssistMenu() ( makeCurrent(); OpenGL::context::scoped_setter const _(::gl, context()); - NOGGIT_ACTION_MGR->beginAction(this, Noggit::ActionFlags::eCHUNKS_TEXTURE); + NOGGIT_ACTION_MGR->beginAction(this, Noggit::ActionFlags::eCHUNKS_VERTEX_COLOR); _world->importAllADTVertexColorMaps(adt_import_vcol_params_mode->currentIndex(), adt_import_vcol_params_mode_tiled_edges->isChecked()); NOGGIT_ACTION_MGR->endAction(); ) diff --git a/src/noggit/World.cpp b/src/noggit/World.cpp index 9cb564df..0eac6311 100755 --- a/src/noggit/World.cpp +++ b/src/noggit/World.cpp @@ -2278,6 +2278,51 @@ void World::swapTexture(glm::vec3 const& pos, scoped_blp_texture_reference tex) } } +void World::swapTextureGlobal(scoped_blp_texture_reference tex) +{ + ZoneScoped; + if (!!Noggit::Ui::selected_texture::get()) + { + + for (size_t z = 0; z < 64; z++) + { + for (size_t x = 0; x < 64; x++) + { + TileIndex tile(x, z); + + bool unload = !mapIndex.tileLoaded(tile) && !mapIndex.tileAwaitingLoading(tile); + MapTile* mTile = mapIndex.loadTile(tile); + + if (mTile) + { + mTile->wait_until_loaded(); + + bool tile_changed = false; + for_all_chunks_on_tile(mTile, [&](MapChunk* chunk) + { + // NOGGIT_CUR_ACTION->registerChunkTextureChange(chunk); + bool swapped = chunk->switchTexture(tex, *Noggit::Ui::selected_texture::get()); + if (swapped) + tile_changed = true; + }); + + if (tile_changed) + { + mTile->saveTile(this); + mapIndex.markOnDisc(tile, true); + mapIndex.unsetChanged(tile); + } + + if (unload) + { + mapIndex.unloadTile(tile); + } + } + } + } + } +} + void World::removeTexture(glm::vec3 const& pos, scoped_blp_texture_reference tex) { ZoneScoped; diff --git a/src/noggit/World.h b/src/noggit/World.h index 3bec7db9..cc17d9a4 100755 --- a/src/noggit/World.h +++ b/src/noggit/World.h @@ -197,6 +197,9 @@ public: template void for_all_chunks_on_tile (glm::vec3 const& pos, Fun&&); + template + void for_all_chunks_on_tile(MapTile* tile, Fun&& fun); + template void for_chunk_at(glm::vec3 const& pos, Fun&& fun); template @@ -228,6 +231,7 @@ public: void clear_shadows(glm::vec3 const& pos); void clearTextures(glm::vec3 const& pos); void swapTexture(glm::vec3 const& pos, scoped_blp_texture_reference tex); + void swapTextureGlobal(scoped_blp_texture_reference tex); void removeTexture(glm::vec3 const& pos, scoped_blp_texture_reference tex); void removeTexDuplicateOnADT(glm::vec3 const& pos); void change_texture_flag(glm::vec3 const& pos, scoped_blp_texture_reference const& tex, std::size_t flag, bool add); diff --git a/src/noggit/World.inl b/src/noggit/World.inl index 4b50f0b9..aa258ee1 100755 --- a/src/noggit/World.inl +++ b/src/noggit/World.inl @@ -25,6 +25,23 @@ void World::for_all_chunks_on_tile (glm::vec3 const& pos, Fun&& fun) } } +template +void World::for_all_chunks_on_tile(MapTile* tile, Fun&& fun) +{ + if (tile && tile->finishedLoading()) + { + mapIndex.setChanged(tile); + + for (size_t ty = 0; ty < 16; ++ty) + { + for (size_t tx = 0; tx < 16; ++tx) + { + fun(tile->getChunk(ty, tx)); + } + } + } +} + template void World::for_chunk_at(glm::vec3 const& pos, Fun&& fun) { diff --git a/src/noggit/map_horizon.cpp b/src/noggit/map_horizon.cpp index 68afb944..b70abf7e 100755 --- a/src/noggit/map_horizon.cpp +++ b/src/noggit/map_horizon.cpp @@ -189,13 +189,15 @@ map_horizon::map_horizon(const std::string& basename, const MapIndex * const ind wdl_file.read(_tiles[y][x]->height_17, 17 * 17 * sizeof(int16_t)); wdl_file.read(_tiles[y][x]->height_16, 16 * 16 * sizeof(int16_t)); - - wdl_file.read(&fourcc, 4); - if (fourcc == 'MAHO') + if (wdl_file.getPos() < wdl_file.getSize()) { - wdl_file.read(&size, 4); - assert(size == 0x20); - wdl_file.read(_tiles[y][x]->holes, 16 * sizeof(int16_t)); + wdl_file.read(&fourcc, 4); + if (fourcc == 'MAHO') + { + wdl_file.read(&size, 4); + assert(size == 0x20); + wdl_file.read(_tiles[y][x]->holes, 16 * sizeof(int16_t)); + } } } diff --git a/src/noggit/map_index.cpp b/src/noggit/map_index.cpp index 624212bc..130d44bd 100755 --- a/src/noggit/map_index.cpp +++ b/src/noggit/map_index.cpp @@ -1098,6 +1098,13 @@ void MapIndex::loadMinimapMD5translate() QStringList line_split = line.split(QRegExp("[\t]")); + if (line_split.length() < 2) + { + std::string text = "Failed to read md5translate.trs.\nLine \"" + line.toStdString() + "\n has no tab spacing. Spacing must be only a tab character and not spaces."; + LogError << text << std::endl; + throw std::logic_error(text); + } + if (cur_dir.length()) { _minimap_md5translate[cur_dir.toStdString()][line_split[0].toStdString()] = line_split[1].toStdString(); diff --git a/src/noggit/texture_set.cpp b/src/noggit/texture_set.cpp index 17630741..cf76b965 100755 --- a/src/noggit/texture_set.cpp +++ b/src/noggit/texture_set.cpp @@ -87,7 +87,7 @@ int TextureSet::addTexture (scoped_blp_texture_reference texture) return texLevel; } -void TextureSet::replace_texture (scoped_blp_texture_reference const& texture_to_replace, scoped_blp_texture_reference replacement_texture) +bool TextureSet::replace_texture (scoped_blp_texture_reference const& texture_to_replace, scoped_blp_texture_reference replacement_texture) { int texture_to_replace_level = -1, replacement_texture_level = -1; @@ -122,6 +122,11 @@ void TextureSet::replace_texture (scoped_blp_texture_reference const& texture_to // merge_layers(texture_to_replace_level, replacement_texture_level); } } + + if (texture_to_replace_level != -1 || replacement_texture_level != -1) + return true; + else + return false; } void TextureSet::swap_layers(int layer_1, int layer_2) diff --git a/src/noggit/texture_set.hpp b/src/noggit/texture_set.hpp index 3e576836..0f58104a 100755 --- a/src/noggit/texture_set.hpp +++ b/src/noggit/texture_set.hpp @@ -45,7 +45,7 @@ public: // return true if at least 1 texture has been erased bool eraseUnusedTextures(); void swap_layers(int layer_1, int layer_2); - void replace_texture(scoped_blp_texture_reference const& texture_to_replace, scoped_blp_texture_reference replacement_texture); + bool replace_texture(scoped_blp_texture_reference const& texture_to_replace, scoped_blp_texture_reference replacement_texture); bool paintTexture(float xbase, float zbase, float x, float z, Brush* brush, float strength, float pressure, scoped_blp_texture_reference texture); bool stampTexture(float xbase, float zbase, float x, float z, Brush* brush, float strength, float pressure, scoped_blp_texture_reference texture, QImage* image, bool paint); bool replace_texture( float xbase diff --git a/src/noggit/ui/texture_swapper.cpp b/src/noggit/ui/texture_swapper.cpp index 7b4b2718..6465a673 100755 --- a/src/noggit/ui/texture_swapper.cpp +++ b/src/noggit/ui/texture_swapper.cpp @@ -36,12 +36,14 @@ namespace Noggit QPushButton* select = new QPushButton("Select", this); QPushButton* swap_adt = new QPushButton("Swap ADT", this); + QPushButton* swap_global = new QPushButton("Swap Global(All ADTs)", this); QPushButton* remove_text_adt = new QPushButton(tr("Remove this texture from ADT"), this); layout->addRow(new QLabel("Texture to swap")); layout->addRow(_texture_to_swap_display); layout->addRow(select); layout->addRow(swap_adt); + layout->addRow(swap_global); layout->addRow(remove_text_adt); _brush_mode_group = new QGroupBox("Brush mode", this); @@ -89,6 +91,14 @@ namespace Noggit } }); + connect(swap_global, &QPushButton::clicked, [this, camera_pos, map_view]() { + if (_texture_to_swap) + { + // TODO : action manager + _world->swapTextureGlobal(_texture_to_swap.value()); + } + }); + connect(remove_text_adt, &QPushButton::clicked, [this, camera_pos, map_view]() { if (_texture_to_swap) {