Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
#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
|
||||
|
||||
// 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
|
||||
std::vector<float> bone_matrices; // 16 floats per bone (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;
|
||||
|
||||
// Bone queries & attachments
|
||||
bool GetBoneWorldTransform(const std::string& bone_name, 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(); }
|
||||
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;
|
||||
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;
|
||||
const gr2::Skeleton* m_gpuPartParent = nullptr;
|
||||
size_t m_gpuPartPaletteSize = 0;
|
||||
void EvaluatePose(float animation_time) const;
|
||||
mutable bool m_poseValid = false;
|
||||
mutable float m_poseTime = -1;
|
||||
mutable const gr2::Animation* m_poseAnimation = nullptr;
|
||||
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;
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user