diff --git a/dCommon/NifFile.cpp b/dCommon/NifFile.cpp index 017f27ed9..b56241591 100644 --- a/dCommon/NifFile.cpp +++ b/dCommon/NifFile.cpp @@ -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 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(count); diff --git a/tests/dWebTests/NifFileTests.cpp b/tests/dWebTests/NifFileTests.cpp index e1455a3b2..1bffc65c5 100644 --- a/tests/dWebTests/NifFileTests.cpp +++ b/tests/dWebTests/NifFileTests.cpp @@ -78,11 +78,11 @@ namespace { return bytes.Put(0xFFFFFFFF).Put(0).Put(-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 translation, std::array rotation, float scale, std::vector 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(static_cast(properties.size())); for (const auto p : properties) bytes.Put(p); return bytes.Put(-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(file)), std::istreambuf_iterator()); + 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); +}