Merge exp/nif-rotations: node rotations are read row by row, scenery models converted again

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-23 21:03:06 -06:00
5 changed files with 27 additions and 12 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

@@ -279,9 +279,9 @@ namespace {
* isn't drawn with newer code. 2: meshes carry their multishader tag; conversions without it drew glom parts with
* the LEGO shader. 3: dark textures and the UV set each texture names. 4: the game's shaders draw the models
* (manifest "techniques"), vertex colors go to them as stored. 5: an animated material alpha is its highest key. 6: flair tints are bytes over
* 255 (they were over 63).
* 255 (they were over 63). 7: node rotations are read row by row, so models with turned nodes are converted again.
*/
constexpr uint32_t FORMAT_VERSION = 6;
constexpr uint32_t FORMAT_VERSION = 7;
// A zone's lighting (WorldScene::Lighting) for the viewers' shaders
nlohmann::json LightingJson(const WorldScene::Lighting& lighting) {

View File

@@ -183,7 +183,7 @@ export const TECHNIQUE = {
* The conversion format (Scenery.cpp FORMAT_VERSION) these views are written for. Manifest URLs carry it, so a
* browser never draws with a manifest it kept from an older server (they are cached for up to a day).
*/
export const SCENERY_FORMAT = 6;
export const SCENERY_FORMAT = 7;
/** A manifest URL asking for SCENERY_FORMAT's manifest (the server ignores the parameter; browsers cache by it). */
export function manifestUrl(url) {

View File

@@ -1530,7 +1530,7 @@ Powerups, Orb, TV Screen and Head Icon effects (drawn as LEGO or unlit), Flair's
client's environment textures come from `/api/scenery/env/:name` (`reflection`, `polished`, `brushed`,
`brushedNoise`). Placed player models are drawn by the viewer's own lighting.
The views ask for a zone's manifest with the conversion format they're written for (`?format=6`,
The views ask for a zone's manifest with the conversion format they're written for (`?format=7`,
`scenery-core.js` `SCENERY_FORMAT`, which follows `Scenery.cpp` `FORMAT_VERSION`), since browsers keep manifests for up
to a day. A manifest without `techniques` (an older server's) is drawn with the viewer's own lights and its
`textureAlpha` table instead of guessing every shader as LEGO, which laid see-through textures over white vertex

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);
}