fix(nif): read node rotations row by row, as the client does

Gamebryo stores a node's rotation in the order it keeps it in memory, row by row for column vectors, and the client reads the nine floats as they are. Reading them column by column turned every rotated node the wrong way: parts of Nimbus Station's buildings stood in the wrong places and the minifig skeleton stood on its head.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-23 16:41:39 -06:00
parent 8ce02a7b65
commit 1c5d2e49c4
2 changed files with 23 additions and 8 deletions

View File

@@ -337,12 +337,9 @@ namespace {
av.name = ReadNet(reader).name;
av.flags = reader.U16();
for (auto& value : av.transform.t) value = reader.Float();
// Matrix33 is stored m11, m21, m31, m12, ... (nif.xml): column by column
std::array<float, 9> stored{};
for (auto& value : stored) value = reader.Float();
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) av.transform.r[row * 3 + col] = stored[col * 3 + row];
}
// The rotation is stored row by row, the way Gamebryo keeps it in memory (the client reads the nine floats
// in order), for column vectors: p' = R p
for (auto& value : av.transform.r) value = reader.Float();
av.transform.s = reader.Float();
const auto count = reader.U32();
av.properties = reader.Array<int32_t>(count);

View File

@@ -78,11 +78,11 @@ namespace {
return bytes.Put<uint32_t>(0xFFFFFFFF).Put<uint32_t>(0).Put<int32_t>(-1);
}
// NiAVObject; `rotation` row-major (for column vectors), written the way the file stores it (column by column)
// NiAVObject; `rotation` row-major (for column vectors), written the way the file stores it (row by row)
Bytes Av(uint16_t flags, std::array<float, 3> translation, std::array<float, 9> rotation, float scale, std::vector<int32_t> properties) {
auto bytes = Net();
bytes.Put(flags).Floats({ translation[0], translation[1], translation[2] });
for (int col = 0; col < 3; col++) for (int row = 0; row < 3; row++) bytes.Put(rotation[row * 3 + col]);
for (const auto value : rotation) bytes.Put(value);
bytes.Put(scale).Put<uint32_t>(static_cast<uint32_t>(properties.size()));
for (const auto p : properties) bytes.Put(p);
return bytes.Put<int32_t>(-1); // collision
@@ -758,3 +758,21 @@ TEST(NifFileTests, PassesMultishaderTagsDownToMeshes) {
std::memcpy(&length, encoded.data(), 4);
EXPECT_EQ(nlohmann::json::parse(encoded.substr(4, length))["meshes"][0]["shaderTag"], 30);
}
TEST(NifFileTests, TurnsNodesAsTheClientDoes) {
// The minifigure's skeleton: each bone turned from its parent, so reading the rotations transposed stands it on
// its head (the hair below the hips)
const auto path = ClientRes() / "mesh" / "minifig" / "mf_main_nolod.nif";
std::ifstream file(path, std::ios::binary);
if (!file) GTEST_SKIP() << "No game client configured";
const std::string data((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
std::string error;
const auto model = NifFile::Parse(data, 0, error);
ASSERT_TRUE(model) << error;
const auto y = [&model](const std::string& node) { return model->nodes.at(node).translation[1]; };
EXPECT_GT(y("hair"), y("head"));
EXPECT_GT(y("head"), y("hip_l"));
EXPECT_GT(y("hip_l"), y("knee_l"));
EXPECT_GT(y("knee_l"), y("ankle_l"));
EXPECT_NEAR(y("hair") - y("ankle_l"), 3.8f, 0.2f);
}