#pragma once #include #include namespace game { inline void multiply_matrix(const float* a, const float* b, float* out) { float result[16]{}; for (int col = 0; col < 4; ++col) for (int row = 0; row < 4; ++row) for (int k = 0; k < 4; ++k) result[col*4+row] += a[k*4+row] * b[col*4+k]; std::memcpy(out, result, sizeof(result)); } inline bool inverse_matrix(const float* matrix, float* inverse) { double rows[4][8]{}; for (int r = 0; r < 4; ++r) { for (int c = 0; c < 4; ++c) { if (!std::isfinite(matrix[c*4+r])) return false; rows[r][c] = matrix[c*4+r]; } rows[r][4+r] = 1; } for (int col = 0; col < 4; ++col) { int pivot = col; for (int r = col+1; r < 4; ++r) if (std::fabs(rows[r][col]) > std::fabs(rows[pivot][col])) pivot = r; if (std::fabs(rows[pivot][col]) < 1.e-10) return false; for (int c = 0; c < 8; ++c) { const double v = rows[col][c]; rows[col][c] = rows[pivot][c]; rows[pivot][c] = v; } const double divisor = rows[col][col]; for (double& v : rows[col]) v /= divisor; for (int r = 0; r < 4; ++r) { if (r == col) continue; const double factor = rows[r][col]; for (int c = 0; c < 8; ++c) rows[r][c] -= factor * rows[col][c]; } } for (int r = 0; r < 4; ++r) for (int c = 0; c < 4; ++c) inverse[c*4+r] = static_cast(rows[r][4+c]); return true; } // Verified 26.52: BaseActorRenderContext+0x28 -> render context+0x18 -> // ScreenContext. World MatrixStack has a deque of one matrix per block at +0x80. inline float* viewmodel_world_matrix(void* actor_context, unsigned char** screen_out = nullptr) { if (!actor_context) return nullptr; auto* render = *reinterpret_cast(static_cast(actor_context) + 0x28); if (!render) return nullptr; auto* screen = *reinterpret_cast(render + 0x18); if (!screen) return nullptr; auto** map = *reinterpret_cast(screen + 0x88); const size_t capacity = *reinterpret_cast(screen + 0x90); const size_t first = *reinterpret_cast(screen + 0x98); const size_t size = *reinterpret_cast(screen + 0xa0); if (!map || !capacity || capacity > 65536 || (capacity & (capacity-1)) || !size) return nullptr; if (screen_out) *screen_out = screen; return map[(first + size - 1) & (capacity - 1)]; } class viewmodel_matrix_scope { public: explicit viewmodel_matrix_scope(void* context) : matrix(viewmodel_world_matrix(context, &screen_)) { if (matrix) std::memcpy(saved, matrix, sizeof(saved)); } ~viewmodel_matrix_scope() { if (matrix) { std::memcpy(matrix, saved, sizeof(saved)); dirty(); } } void dirty() { if (screen_) screen_[0xb8] = 1; } float* matrix; float saved[16]{}; private: unsigned char* screen_ = nullptr; }; // Render matrices are column-major float[16]. inline void transform_viewmodel(float* matrix, const float* translation, const float* rotation) { if (!matrix) return; // Translate in the current local basis (M * T). for (int row = 0; row < 4; ++row) matrix[12 + row] += matrix[row] * translation[0] + matrix[4 + row] * translation[1] + matrix[8 + row] * translation[2]; constexpr float radians = 0.017453292519943295f; // Local pitch, yaw, roll: M * Rx * Ry * Rz. Translation remains the pivot. const int first[] = {1, 2, 0}, second[] = {2, 0, 1}; for (int axis = 0; axis < 3; ++axis) { if (rotation[axis] == 0.f) continue; const float c = std::cos(rotation[axis] * radians), s = std::sin(rotation[axis] * radians); for (int row = 0; row < 4; ++row) { const int a = first[axis] * 4 + row, b = second[axis] * 4 + row; const float old_a = matrix[a], old_b = matrix[b]; matrix[a] = old_a * c + old_b * s; matrix[b] = old_b * c - old_a * s; } } } }