Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace metin2::spatial {
|
||||
|
||||
class LooseOctree {
|
||||
public:
|
||||
struct Node {
|
||||
AABB base_bounds; // Strict bounds of this octree cell
|
||||
AABB loose_bounds; // Expanded bounds (by looseness factor, default 2.0)
|
||||
int depth = 0;
|
||||
std::array<std::unique_ptr<Node>, 8> children;
|
||||
std::vector<SpatialObject> objects;
|
||||
|
||||
bool IsLeaf() const {
|
||||
for (const auto& c : children) {
|
||||
if (c) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
LooseOctree(const AABB& world_bounds, float looseness = 2.0f, int max_depth = 6, int max_objects_per_node = 16);
|
||||
~LooseOctree() = default;
|
||||
|
||||
// Core Dynamic Operations
|
||||
bool Insert(uint64_t id, SpatialObjectType type, const AABB& bounds, void* user_data = nullptr);
|
||||
bool Update(uint64_t id, const AABB& new_bounds);
|
||||
bool Remove(uint64_t id);
|
||||
void Clear();
|
||||
|
||||
size_t Size() const { return m_object_map.size(); }
|
||||
bool Contains(uint64_t id) const { return m_object_map.find(id) != m_object_map.end(); }
|
||||
const AABB& GetWorldBounds() const { return m_world_bounds; }
|
||||
|
||||
// Acceleration Queries
|
||||
std::vector<VisibleObject> QueryFrustum(const Frustum& frustum, const Vector3& camera_pos, bool sort_by_distance = true) const;
|
||||
std::vector<uint64_t> QueryAABB(const AABB& query_box) const;
|
||||
std::vector<uint64_t> QuerySphere(const Vector3& center, float radius) const;
|
||||
|
||||
bool Raycast(const Ray& ray, RaycastHit& out_hit) const;
|
||||
std::vector<RaycastHit> RaycastAll(const Ray& ray) const;
|
||||
|
||||
private:
|
||||
void Subdivide(Node* node);
|
||||
Node* FindBestNodeForBounds(Node* current, const AABB& bounds);
|
||||
void QueryFrustumRecursive(const Node* node, const Frustum& frustum, const Vector3& camera_pos,
|
||||
FrustumIntersection parent_status, std::vector<VisibleObject>& out_visible) const;
|
||||
void CollectAllRecursive(const Node* node, const Vector3& camera_pos, std::vector<VisibleObject>& out_visible) const;
|
||||
void QueryAABBRecursive(const Node* node, const AABB& query_box, std::vector<uint64_t>& out_ids) const;
|
||||
void QuerySphereRecursive(const Node* node, const Vector3& center, float radius_sq, std::vector<uint64_t>& out_ids) const;
|
||||
void RaycastRecursive(const Node* node, const Ray& ray, std::vector<RaycastHit>& hits) const;
|
||||
|
||||
bool RemoveRecursive(Node* node, uint64_t id);
|
||||
|
||||
AABB m_world_bounds;
|
||||
float m_looseness = 2.0f;
|
||||
int m_max_depth = 6;
|
||||
int m_max_objects_per_node = 16;
|
||||
|
||||
std::unique_ptr<Node> m_root;
|
||||
std::unordered_map<uint64_t, SpatialObject> m_object_map;
|
||||
};
|
||||
|
||||
} // namespace metin2::spatial
|
||||
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include "loose_octree.h"
|
||||
@@ -0,0 +1,187 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
|
||||
namespace metin2::spatial {
|
||||
|
||||
struct Vector3 {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
|
||||
constexpr Vector3() = default;
|
||||
constexpr Vector3(float in_x, float in_y, float in_z) : x(in_x), y(in_y), z(in_z) {}
|
||||
|
||||
Vector3 operator+(const Vector3& o) const { return {x + o.x, y + o.y, z + o.z}; }
|
||||
Vector3 operator-(const Vector3& o) const { return {x - o.x, y - o.y, z - o.z}; }
|
||||
Vector3 operator*(float s) const { return {x * s, y * s, z * s}; }
|
||||
Vector3 operator/(float s) const { float inv = 1.0f / s; return {x * inv, y * inv, z * inv}; }
|
||||
Vector3& operator+=(const Vector3& o) { x += o.x; y += o.y; z += o.z; return *this; }
|
||||
Vector3& operator-=(const Vector3& o) { x -= o.x; y -= o.y; z -= o.z; return *this; }
|
||||
|
||||
float Dot(const Vector3& o) const { return x * o.x + y * o.y + z * o.z; }
|
||||
Vector3 Cross(const Vector3& o) const {
|
||||
return {
|
||||
y * o.z - z * o.y,
|
||||
z * o.x - x * o.z,
|
||||
x * o.y - y * o.x
|
||||
};
|
||||
}
|
||||
|
||||
float LengthSq() const { return x * x + y * y + z * z; }
|
||||
float Length() const { return std::sqrt(LengthSq()); }
|
||||
float Distance(const Vector3& o) const { return (*this - o).Length(); }
|
||||
float DistanceSq(const Vector3& o) const { return (*this - o).LengthSq(); }
|
||||
|
||||
Vector3 Normalized() const {
|
||||
float len = Length();
|
||||
if (len > 1e-6f) return *this / len;
|
||||
return {0.0f, 0.0f, 0.0f};
|
||||
}
|
||||
};
|
||||
|
||||
struct AABB {
|
||||
Vector3 min_point{0.0f, 0.0f, 0.0f};
|
||||
Vector3 max_point{0.0f, 0.0f, 0.0f};
|
||||
|
||||
constexpr AABB() = default;
|
||||
constexpr AABB(const Vector3& min_p, const Vector3& max_p) : min_point(min_p), max_point(max_p) {}
|
||||
|
||||
Vector3 GetCenter() const { return (min_point + max_point) * 0.5f; }
|
||||
Vector3 GetExtents() const { return (max_point - min_point) * 0.5f; }
|
||||
Vector3 GetSize() const { return max_point - min_point; }
|
||||
|
||||
bool Contains(const Vector3& p) const {
|
||||
return (p.x >= min_point.x && p.x <= max_point.x) &&
|
||||
(p.y >= min_point.y && p.y <= max_point.y) &&
|
||||
(p.z >= min_point.z && p.z <= max_point.z);
|
||||
}
|
||||
|
||||
bool Contains(const AABB& o) const {
|
||||
return (o.min_point.x >= min_point.x && o.max_point.x <= max_point.x) &&
|
||||
(o.min_point.y >= min_point.y && o.max_point.y <= max_point.y) &&
|
||||
(o.min_point.z >= min_point.z && o.max_point.z <= max_point.z);
|
||||
}
|
||||
|
||||
bool Intersects(const AABB& o) const {
|
||||
return (min_point.x <= o.max_point.x && max_point.x >= o.min_point.x) &&
|
||||
(min_point.y <= o.max_point.y && max_point.y >= o.min_point.y) &&
|
||||
(min_point.z <= o.max_point.z && max_point.z >= o.min_point.z);
|
||||
}
|
||||
|
||||
AABB Expanded(float factor) const {
|
||||
Vector3 c = GetCenter();
|
||||
Vector3 e = GetExtents() * factor;
|
||||
return {c - e, c + e};
|
||||
}
|
||||
};
|
||||
|
||||
struct Plane {
|
||||
Vector3 normal{0.0f, 1.0f, 0.0f};
|
||||
float d = 0.0f; // ax + by + cz + d = 0
|
||||
|
||||
constexpr Plane() = default;
|
||||
constexpr Plane(const Vector3& n, float in_d) : normal(n), d(in_d) {}
|
||||
|
||||
void Normalize() {
|
||||
float len = normal.Length();
|
||||
if (len > 1e-6f) {
|
||||
normal = normal / len;
|
||||
d /= len;
|
||||
}
|
||||
}
|
||||
|
||||
float DistanceTo(const Vector3& p) const {
|
||||
return normal.Dot(p) + d;
|
||||
}
|
||||
};
|
||||
|
||||
enum class FrustumIntersection {
|
||||
Outside = 0,
|
||||
Inside,
|
||||
Intersects
|
||||
};
|
||||
|
||||
class Frustum {
|
||||
public:
|
||||
enum PlaneIndex {
|
||||
PlaneLeft = 0,
|
||||
PlaneRight,
|
||||
PlaneBottom,
|
||||
PlaneTop,
|
||||
PlaneNear,
|
||||
PlaneFar,
|
||||
NumPlanes = 6
|
||||
};
|
||||
|
||||
Frustum() = default;
|
||||
|
||||
void SetPlane(PlaneIndex idx, const Plane& plane) {
|
||||
m_planes[idx] = plane;
|
||||
m_planes[idx].Normalize();
|
||||
}
|
||||
|
||||
const Plane& GetPlane(PlaneIndex idx) const { return m_planes[idx]; }
|
||||
|
||||
static Frustum CreateFromViewProj(const float m[16]);
|
||||
static Frustum CreatePerspective(const Vector3& eye, const Vector3& look_dir, const Vector3& up,
|
||||
float fov_deg, float aspect, float near_z, float far_z);
|
||||
|
||||
FrustumIntersection TestAABB(const AABB& aabb) const;
|
||||
bool Intersects(const AABB& aabb) const {
|
||||
return TestAABB(aabb) != FrustumIntersection::Outside;
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<Plane, 6> m_planes;
|
||||
};
|
||||
|
||||
struct Ray {
|
||||
Vector3 origin;
|
||||
Vector3 direction; // should be normalized
|
||||
|
||||
bool IntersectAABB(const AABB& aabb, float& out_t) const;
|
||||
};
|
||||
|
||||
enum class SpatialObjectType : uint8_t {
|
||||
Unknown = 0,
|
||||
Building,
|
||||
Tree,
|
||||
Character,
|
||||
Monster,
|
||||
NPC,
|
||||
Effect,
|
||||
ItemDrop
|
||||
};
|
||||
|
||||
struct SpatialObject {
|
||||
uint64_t id = 0;
|
||||
SpatialObjectType type = SpatialObjectType::Unknown;
|
||||
AABB bounds;
|
||||
void* user_data = nullptr;
|
||||
};
|
||||
|
||||
struct VisibleObject {
|
||||
uint64_t id = 0;
|
||||
SpatialObjectType type = SpatialObjectType::Unknown;
|
||||
AABB bounds;
|
||||
float distance_sq = 0.0f;
|
||||
float distance = 0.0f;
|
||||
void* user_data = nullptr;
|
||||
};
|
||||
|
||||
struct RaycastHit {
|
||||
bool hit = false;
|
||||
uint64_t object_id = 0;
|
||||
SpatialObjectType type = SpatialObjectType::Unknown;
|
||||
float distance = 0.0f;
|
||||
Vector3 point;
|
||||
Vector3 normal;
|
||||
void* user_data = nullptr;
|
||||
};
|
||||
|
||||
} // namespace metin2::spatial
|
||||
Reference in New Issue
Block a user