feat: WOT doodad/WMO placements, WOB materials, deduplicate loader

Architecture fixes for open format data fidelity:

- WOT now serializes full doodad/WMO placement arrays (positions,
  rotations, scale, flags, doodad sets) — was only storing counts,
  causing all placed objects to be lost on WOT round-trip
- WOT loader parses placements back into ADTTerrain for client rendering
- WOB Material struct added: preserves WMO material flags, shader type,
  and blend mode during WMO→WOB conversion (was geometry-only)
- WOB doodad rotation: quaternion→euler conversion instead of hardcoded
  zero (placed doodads inside buildings now retain their orientation)
- importOpen() deduplicated: delegates to pipeline::WoweeTerrainLoader
  instead of duplicating 100 lines of parsing code
This commit is contained in:
Kelsi 2026-05-05 14:44:46 -07:00
parent d00ddd1c73
commit ca15da5e9b
4 changed files with 100 additions and 110 deletions

View file

@ -1,4 +1,5 @@
#include "wowee_terrain.hpp"
#include "pipeline/wowee_terrain_loader.hpp"
#include "core/logger.hpp"
#include "stb_image_write.h"
#include <nlohmann/json.hpp>
@ -86,8 +87,38 @@ bool WoweeTerrain::exportOpen(const pipeline::ADTTerrain& terrain,
}
}
j["water"] = waterArr;
j["doodadCount"] = terrain.doodadPlacements.size();
j["wmoCount"] = terrain.wmoPlacements.size();
// Doodad placements (M2 models on terrain)
nlohmann::json doodadNames = nlohmann::json::array();
for (const auto& n : terrain.doodadNames) doodadNames.push_back(n);
j["doodadNames"] = doodadNames;
nlohmann::json doodads = nlohmann::json::array();
for (const auto& dp : terrain.doodadPlacements) {
doodads.push_back({
{"nameId", dp.nameId}, {"uniqueId", dp.uniqueId},
{"pos", {dp.position[0], dp.position[1], dp.position[2]}},
{"rot", {dp.rotation[0], dp.rotation[1], dp.rotation[2]}},
{"scale", dp.scale}, {"flags", dp.flags}
});
}
j["doodads"] = doodads;
// WMO placements (buildings on terrain)
nlohmann::json wmoNames = nlohmann::json::array();
for (const auto& n : terrain.wmoNames) wmoNames.push_back(n);
j["wmoNames"] = wmoNames;
nlohmann::json wmos = nlohmann::json::array();
for (const auto& wp : terrain.wmoPlacements) {
wmos.push_back({
{"nameId", wp.nameId}, {"uniqueId", wp.uniqueId},
{"pos", {wp.position[0], wp.position[1], wp.position[2]}},
{"rot", {wp.rotation[0], wp.rotation[1], wp.rotation[2]}},
{"flags", wp.flags}, {"doodadSet", wp.doodadSet}
});
}
j["wmos"] = wmos;
std::ofstream f(jsonPath);
if (!f) return false;
@ -217,112 +248,7 @@ int WoweeTerrain::exportAlphaMaps(const pipeline::ADTTerrain& terrain,
}
bool WoweeTerrain::importOpen(const std::string& basePath, pipeline::ADTTerrain& terrain) {
// Load binary heightmap (.whm)
std::string hmPath = basePath + ".whm";
std::ifstream f(hmPath, std::ios::binary);
if (!f) return false;
uint32_t magic, chunks, verts;
f.read(reinterpret_cast<char*>(&magic), 4);
if (magic != 0x314D4857) return false;
f.read(reinterpret_cast<char*>(&chunks), 4);
f.read(reinterpret_cast<char*>(&verts), 4);
if (chunks != 256 || verts != 145) return false;
terrain.loaded = true;
terrain.version = 18;
for (int ci = 0; ci < 256; ci++) {
auto& chunk = terrain.chunks[ci];
chunk.heightMap.loaded = true;
chunk.indexX = ci % 16;
chunk.indexY = ci / 16;
float base;
f.read(reinterpret_cast<char*>(&base), 4);
chunk.position[2] = base;
f.read(reinterpret_cast<char*>(chunk.heightMap.heights.data()), 145 * 4);
uint32_t alphaSize = 0;
if (f.read(reinterpret_cast<char*>(&alphaSize), 4) && alphaSize > 0 && alphaSize <= 65536) {
chunk.alphaMap.resize(alphaSize);
f.read(reinterpret_cast<char*>(chunk.alphaMap.data()), alphaSize);
}
for (int i = 0; i < 145; i++) {
chunk.normals[i * 3 + 0] = 0;
chunk.normals[i * 3 + 1] = 0;
chunk.normals[i * 3 + 2] = 127;
}
}
// Load JSON metadata (.wot)
std::string wotPath = basePath + ".wot";
std::ifstream wf(wotPath);
if (wf) {
try {
auto j = nlohmann::json::parse(wf);
terrain.coord.x = j.value("tileX", 0);
terrain.coord.y = j.value("tileY", 0);
float tileSize = 533.33333f;
float chunkSize = tileSize / 16.0f;
for (int cy = 0; cy < 16; cy++) {
for (int cx = 0; cx < 16; cx++) {
auto& chunk = terrain.chunks[cy * 16 + cx];
chunk.position[0] = (32.0f - terrain.coord.x) * tileSize - cx * chunkSize;
chunk.position[1] = (32.0f - terrain.coord.y) * tileSize - cy * chunkSize;
}
}
if (j.contains("textures") && j["textures"].is_array()) {
for (const auto& tex : j["textures"]) {
if (tex.is_string() && !tex.get<std::string>().empty())
terrain.textures.push_back(tex.get<std::string>());
}
}
if (j.contains("chunkLayers") && j["chunkLayers"].is_array()) {
const auto& layers = j["chunkLayers"];
for (int ci = 0; ci < std::min(256, static_cast<int>(layers.size())); ci++) {
const auto& cl = layers[ci];
if (cl.contains("layers") && cl["layers"].is_array()) {
for (const auto& texId : cl["layers"]) {
pipeline::TextureLayer layer{};
layer.textureId = texId.get<uint32_t>();
layer.flags = terrain.chunks[ci].layers.empty() ? 0 : 0x100;
terrain.chunks[ci].layers.push_back(layer);
}
}
if (cl.contains("holes"))
terrain.chunks[ci].holes = cl["holes"].get<uint16_t>();
}
}
if (j.contains("water") && j["water"].is_array()) {
for (const auto& w : j["water"]) {
if (w.is_null()) continue;
int wci = w.value("chunk", -1);
if (wci < 0 || wci >= 256) continue;
pipeline::ADTTerrain::WaterLayer wl;
wl.liquidType = w.value("type", 0u);
wl.maxHeight = w.value("height", 0.0f);
wl.minHeight = wl.maxHeight;
wl.x = 0; wl.y = 0; wl.width = 9; wl.height = 9;
wl.heights.assign(81, wl.maxHeight);
wl.mask.assign(8, 0xFF);
terrain.waterData[wci].layers.push_back(wl);
}
}
LOG_INFO("WOT metadata loaded: tile [", terrain.coord.x, ",", terrain.coord.y,
"], ", terrain.textures.size(), " textures");
} catch (const std::exception& e) {
LOG_WARNING("Could not parse WOT metadata: ", e.what());
}
}
LOG_INFO("Open terrain imported: ", basePath);
return true;
return pipeline::WoweeTerrainLoader::load(basePath, terrain);
}
} // namespace editor