FindBoneIndex builds a name->index map once per instance (first match wins, as the linear scan did); GetBoneWorldTransform gains an index overload and attached models cache their bone index at attach time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
7.0 KiB
C++
146 lines
7.0 KiB
C++
#pragma once
|
||
|
||
#include <gr2/gr2.h>
|
||
#include <cstdint>
|
||
#include <string>
|
||
#include <vector>
|
||
#include <unordered_map>
|
||
#include <memory>
|
||
|
||
namespace gr2_render {
|
||
|
||
// Engine-agnostic 3D mesh draw data for animated/static Gr2 models
|
||
struct Gr2MeshDraw {
|
||
std::shared_ptr<const Gr2MeshDraw> geometry;
|
||
bool gpu_skinning = false;
|
||
// D3D/Engine row-vector world transform matrix (translation in elements 12..14)
|
||
float world[16] = {
|
||
1.0f, 0.0f, 0.0f, 0.0f,
|
||
0.0f, 1.0f, 0.0f, 0.0f,
|
||
0.0f, 0.0f, 1.0f, 0.0f,
|
||
0.0f, 0.0f, 0.0f, 1.0f
|
||
};
|
||
|
||
std::string texture0;
|
||
std::string texture1;
|
||
|
||
// Per-vertex attributes
|
||
std::vector<float> positions; // 3 floats (x, y, z) per vertex
|
||
std::vector<float> normals; // 3 floats (nx, ny, nz) per vertex
|
||
std::vector<float> uv0; // 2 floats (u, v) per vertex
|
||
std::vector<uint32_t> diffuse; // 0xAARRGGBB per vertex
|
||
std::vector<uint32_t> indices; // Triangle indices
|
||
// Sub-range of the indices (of geometry when shared) this draw covers. Tri-groups of one
|
||
// GPU mesh share a single geometry and differ only by range and material.
|
||
uint32_t first_index = 0;
|
||
uint32_t index_count = UINT32_MAX; // UINT32_MAX = through the end
|
||
|
||
// Skeletal skinning palette & vertex weights (for GPU skinning)
|
||
std::vector<uint8_t> bone_indices; // 4 uint8 per vertex
|
||
std::vector<float> bone_weights; // 4 floats per vertex
|
||
// GPU skinning palette, 16 floats per bone; one per instance per frame, shared by its draws.
|
||
std::shared_ptr<const std::vector<float>> palette;
|
||
|
||
// Render states
|
||
bool alpha_test = false;
|
||
uint32_t alpha_ref = 70;
|
||
bool double_sided = false;
|
||
bool z_write = true;
|
||
bool lighting = true;
|
||
};
|
||
|
||
class Gr2ModelInstance {
|
||
public:
|
||
static std::unique_ptr<Gr2ModelInstance> Load(const std::string& filepath);
|
||
static std::unique_ptr<Gr2ModelInstance> LoadFromMemory(const uint8_t* data, size_t size, const std::string& name = "");
|
||
|
||
// Load external animation (.gr2) to animate this model
|
||
bool LoadExternalAnimation(const std::string& anim_filepath);
|
||
bool LoadExternalAnimationFromMemory(const uint8_t* data, size_t size);
|
||
float GetAnimationDuration() const;
|
||
// granny Control 的循环状态(gr2::control_loop_state)。循环播放的动作传 (true, true):
|
||
// 首尾附近的 B 样条窗口跨到上一遍/下一遍取 knot,接缝处不再夹到端点。默认都不循环。
|
||
void SetAnimationLoop(bool underflow_loop, bool overflow_loop);
|
||
|
||
// Bone queries & attachments
|
||
bool GetBoneWorldTransform(const std::string& bone_name, float* out_matrix, float animation_time = -1.0f) const;
|
||
// Bone index in skeleton 0 (first bone with that name), or -1. Callers that query the same bone
|
||
// every frame can cache it and use the index overload instead of a name lookup.
|
||
int FindBoneIndex(const std::string& bone_name) const;
|
||
bool GetBoneWorldTransform(int bone_index, float* out_matrix, float animation_time = -1.0f) const;
|
||
void AttachModel(const std::string& bone_name, std::shared_ptr<Gr2ModelInstance> attachment, const float* offset_matrix = nullptr);
|
||
void AttachPart(std::shared_ptr<Gr2ModelInstance> part);
|
||
void DetachAll();
|
||
void DetachModel(const std::string& bone_name);
|
||
void DetachPart(const std::shared_ptr<Gr2ModelInstance>& part);
|
||
|
||
void SetAttachmentAnimated(const std::string& bone_name, bool animated);
|
||
void SetAttachmentScale(const std::string& bone_name, float sx, float sy, float sz);
|
||
void SetSkin(const std::string& src_file_name, const std::string& dst_file_name);
|
||
void ClearSkins() { m_skins.clear(); ++m_skinGeneration; }
|
||
const std::string& GetFilePath() const { return m_filepath; }
|
||
|
||
// Build draw commands for this model and all attached components (weapons, hair, armor parts)
|
||
std::vector<Gr2MeshDraw> BuildDrawCommands(float animation_time = -1.0f, const float* world_matrix = nullptr, bool hardwareSkinning = false);
|
||
|
||
// Build draw commands for a skinned part using an external bone palette
|
||
std::vector<Gr2MeshDraw> BuildDrawCommandsWithPalette(const std::vector<gr2::Mat4>& skin_matrices,
|
||
const float* world_matrix = nullptr,
|
||
const gr2::Skeleton* parent_skeleton = nullptr, bool hardwareSkinning = false);
|
||
|
||
bool GetLocalBounds(float& minX, float& minY, float& minZ, float& maxX, float& maxY, float& maxZ) const;
|
||
|
||
size_t GetPoseEvaluationCount() const { return m_poseEvaluations; }
|
||
const gr2::File* GetAnimationResource() const { return m_anim_file.get(); }
|
||
const gr2::File& GetFile() const { return *m_file; }
|
||
const gr2::FileInfo& GetInfo() const { return m_file->file_info(); }
|
||
|
||
private:
|
||
struct AttachedModel {
|
||
std::string bone_name;
|
||
int bone_index = -1;
|
||
std::shared_ptr<Gr2ModelInstance> model;
|
||
float offset[16];
|
||
bool animated = false;
|
||
bool scaled = false;
|
||
float scale[3] = {1.0f, 1.0f, 1.0f};
|
||
};
|
||
std::vector<Gr2MeshDraw> m_gpuMain, m_gpuParts;
|
||
// The GPU caches hold skinned texture names; the unskinned originals are kept so a skin change
|
||
// (SetSkin/ClearSkins bump m_skinGeneration) re-resolves them once instead of every frame.
|
||
std::vector<std::string> m_gpuMainBaseTex, m_gpuPartsBaseTex;
|
||
size_t m_gpuMainSkinGen = 0, m_gpuPartsSkinGen = 0, m_skinGeneration = 1;
|
||
void CacheGpuDraws(std::vector<Gr2MeshDraw>& cache, std::vector<std::string>& base, size_t& gen, const std::vector<Gr2MeshDraw>& draws) const;
|
||
void RefreshGpuSkins(std::vector<Gr2MeshDraw>& cache, const std::vector<std::string>& base, size_t& gen) const;
|
||
const gr2::Skeleton* m_gpuPartParent = nullptr;
|
||
size_t m_gpuPartPaletteSize = 0;
|
||
// Part bone -> parent bone by name, and the parent bind world per part bone; both depend only
|
||
// on the two skeletons, so they are rebuilt when the parent skeleton changes, not every frame.
|
||
const gr2::Skeleton* m_partMapParent = nullptr;
|
||
size_t m_partMapParentBones = 0;
|
||
std::vector<int> m_partToParent;
|
||
std::vector<gr2::Mat4> m_partParentBind, m_partSkin;
|
||
void EvaluatePose(float animation_time) const;
|
||
mutable bool m_poseValid = false;
|
||
mutable float m_poseTime = -1;
|
||
mutable const gr2::Animation* m_poseAnimation = nullptr;
|
||
bool m_loopUnderflow = false, m_loopOverflow = false;
|
||
mutable std::vector<gr2::Mat4> m_worldPose, m_skinPose, m_trackPose, m_localPose;
|
||
mutable std::vector<int> m_trackBones;
|
||
mutable size_t m_poseEvaluations = 0;
|
||
void ApplySkins(std::vector<Gr2MeshDraw>& draws, size_t first) const;
|
||
mutable std::unordered_map<std::string, int> m_boneIndex;
|
||
mutable bool m_boneIndexBuilt = false;
|
||
|
||
std::shared_ptr<const gr2::File> m_file;
|
||
std::string m_filepath;
|
||
std::shared_ptr<const gr2::File> m_anim_file;
|
||
std::string m_anim_filepath;
|
||
|
||
std::vector<AttachedModel> m_attached_models;
|
||
std::vector<std::shared_ptr<Gr2ModelInstance>> m_attached_parts;
|
||
std::unordered_map<std::string, std::string> m_skins;
|
||
};
|
||
|
||
} // namespace gr2_render
|