#pragma once #include #include #include #include #include #include namespace gr2_render { // Engine-agnostic 3D mesh draw data for animated/static Gr2 models struct Gr2MeshDraw { std::shared_ptr 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 positions; // 3 floats (x, y, z) per vertex std::vector normals; // 3 floats (nx, ny, nz) per vertex std::vector uv0; // 2 floats (u, v) per vertex std::vector diffuse; // 0xAARRGGBB per vertex std::vector 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 bone_indices; // 4 uint8 per vertex std::vector 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> 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 Load(const std::string& filepath); static std::unique_ptr 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 attachment, const float* offset_matrix = nullptr); void AttachPart(std::shared_ptr part); void DetachAll(); void DetachModel(const std::string& bone_name); void DetachPart(const std::shared_ptr& 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 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 BuildDrawCommandsWithPalette(const std::vector& 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 model; float offset[16]; bool animated = false; bool scaled = false; float scale[3] = {1.0f, 1.0f, 1.0f}; }; std::vector 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 m_gpuMainBaseTex, m_gpuPartsBaseTex; size_t m_gpuMainSkinGen = 0, m_gpuPartsSkinGen = 0, m_skinGeneration = 1; void CacheGpuDraws(std::vector& cache, std::vector& base, size_t& gen, const std::vector& draws) const; void RefreshGpuSkins(std::vector& cache, const std::vector& 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 m_partToParent; std::vector 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 m_worldPose, m_skinPose, m_trackPose, m_localPose; mutable std::vector m_trackBones; mutable size_t m_poseEvaluations = 0; void ApplySkins(std::vector& draws, size_t first) const; mutable std::unordered_map m_boneIndex; mutable bool m_boneIndexBuilt = false; std::shared_ptr m_file; std::string m_filepath; std::shared_ptr m_anim_file; std::string m_anim_filepath; std::vector m_attached_models; std::vector> m_attached_parts; std::unordered_map m_skins; }; } // namespace gr2_render