Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(spatial LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_library(spatial STATIC
|
||||
src/loose_octree.cpp
|
||||
)
|
||||
|
||||
target_include_directories(spatial PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
)
|
||||
|
||||
add_library(spatial::spatial ALIAS spatial)
|
||||
|
||||
option(SPATIAL_BUILD_TESTS "Build spatial SDK tests" ON)
|
||||
if(SPATIAL_BUILD_TESTS)
|
||||
add_executable(spatial_test tests/spatial_test.cpp)
|
||||
target_link_libraries(spatial_test PRIVATE spatial)
|
||||
add_test(NAME spatial_test COMMAND spatial_test)
|
||||
endif()
|
||||
@@ -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
|
||||
@@ -0,0 +1,439 @@
|
||||
#include "spatial/loose_octree.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
namespace metin2::spatial {
|
||||
|
||||
// Frustum Helpers
|
||||
Frustum Frustum::CreateFromViewProj(const float m[16]) {
|
||||
// Gribb-Hartmann Matrix Frustum Extraction
|
||||
// Assuming column-major or row-major standard layout
|
||||
Frustum f;
|
||||
|
||||
// Left plane: row 4 + row 1
|
||||
f.SetPlane(PlaneLeft, Plane({m[3] + m[0], m[7] + m[4], m[11] + m[8]}, m[15] + m[12]));
|
||||
// Right plane: row 4 - row 1
|
||||
f.SetPlane(PlaneRight, Plane({m[3] - m[0], m[7] - m[4], m[11] - m[8]}, m[15] - m[12]));
|
||||
// Bottom plane: row 4 + row 2
|
||||
f.SetPlane(PlaneBottom, Plane({m[3] + m[1], m[7] + m[5], m[11] + m[9]}, m[15] + m[13]));
|
||||
// Top plane: row 4 - row 2
|
||||
f.SetPlane(PlaneTop, Plane({m[3] - m[1], m[7] - m[5], m[11] - m[9]}, m[15] - m[13]));
|
||||
// Near plane: row 4 + row 3
|
||||
f.SetPlane(PlaneNear, Plane({m[3] + m[2], m[7] + m[6], m[11] + m[10]}, m[15] + m[14]));
|
||||
// Far plane: row 4 - row 3
|
||||
f.SetPlane(PlaneFar, Plane({m[3] - m[2], m[7] - m[6], m[11] - m[10]}, m[15] - m[14]));
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
Frustum Frustum::CreatePerspective(const Vector3& eye, const Vector3& look_dir, const Vector3& up,
|
||||
float fov_deg, float aspect, float near_z, float far_z) {
|
||||
Vector3 fwd = look_dir.Normalized();
|
||||
Vector3 cam_right = up.Cross(fwd).Normalized();
|
||||
Vector3 cam_up = fwd.Cross(cam_right).Normalized();
|
||||
|
||||
float half_fov_rad = (fov_deg * 0.5f) * (3.1415926535f / 180.0f);
|
||||
float tan_half_fov = std::tan(half_fov_rad);
|
||||
|
||||
float near_h = near_z * tan_half_fov;
|
||||
float near_w = near_h * aspect;
|
||||
|
||||
Vector3 near_center = eye + fwd * near_z;
|
||||
Vector3 far_center = eye + fwd * far_z;
|
||||
|
||||
Frustum f;
|
||||
// Near & Far (normals point inward into frustum)
|
||||
f.SetPlane(PlaneNear, Plane(fwd, -fwd.Dot(near_center)));
|
||||
f.SetPlane(PlaneFar, Plane(fwd * -1.0f, fwd.Dot(far_center)));
|
||||
|
||||
// Left & Right
|
||||
Vector3 left_normal = (cam_right * near_z + fwd * near_w).Normalized();
|
||||
Vector3 right_normal = (cam_right * -near_z + fwd * near_w).Normalized();
|
||||
f.SetPlane(PlaneLeft, Plane(left_normal, -left_normal.Dot(eye)));
|
||||
f.SetPlane(PlaneRight, Plane(right_normal, -right_normal.Dot(eye)));
|
||||
|
||||
// Top & Bottom
|
||||
Vector3 top_normal = (cam_up * -near_z + fwd * near_h).Normalized();
|
||||
Vector3 bottom_normal = (cam_up * near_z + fwd * near_h).Normalized();
|
||||
f.SetPlane(PlaneTop, Plane(top_normal, -top_normal.Dot(eye)));
|
||||
f.SetPlane(PlaneBottom, Plane(bottom_normal, -bottom_normal.Dot(eye)));
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
FrustumIntersection Frustum::TestAABB(const AABB& aabb) const {
|
||||
bool fully_inside = true;
|
||||
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
const auto& p = m_planes[i];
|
||||
|
||||
// p-vertex (most positive in plane normal direction)
|
||||
Vector3 p_vertex{
|
||||
p.normal.x >= 0 ? aabb.max_point.x : aabb.min_point.x,
|
||||
p.normal.y >= 0 ? aabb.max_point.y : aabb.min_point.y,
|
||||
p.normal.z >= 0 ? aabb.max_point.z : aabb.min_point.z
|
||||
};
|
||||
|
||||
// If p-vertex is outside, entire AABB is outside
|
||||
if (p.DistanceTo(p_vertex) < 0.0f) {
|
||||
return FrustumIntersection::Outside;
|
||||
}
|
||||
|
||||
// n-vertex (most negative in plane normal direction)
|
||||
Vector3 n_vertex{
|
||||
p.normal.x >= 0 ? aabb.min_point.x : aabb.max_point.x,
|
||||
p.normal.y >= 0 ? aabb.min_point.y : aabb.max_point.y,
|
||||
p.normal.z >= 0 ? aabb.min_point.z : aabb.max_point.z
|
||||
};
|
||||
|
||||
if (p.DistanceTo(n_vertex) < 0.0f) {
|
||||
fully_inside = false;
|
||||
}
|
||||
}
|
||||
|
||||
return fully_inside ? FrustumIntersection::Inside : FrustumIntersection::Intersects;
|
||||
}
|
||||
|
||||
// Ray-AABB Slab intersection
|
||||
bool Ray::IntersectAABB(const AABB& aabb, float& out_t) const {
|
||||
float tmin = 0.0f;
|
||||
float tmax = std::numeric_limits<float>::max();
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
float origin_val = (i == 0) ? origin.x : (i == 1 ? origin.y : origin.z);
|
||||
float dir_val = (i == 0) ? direction.x : (i == 1 ? direction.y : direction.z);
|
||||
float min_val = (i == 0) ? aabb.min_point.x : (i == 1 ? aabb.min_point.y : aabb.min_point.z);
|
||||
float max_val = (i == 0) ? aabb.max_point.x : (i == 1 ? aabb.max_point.y : aabb.max_point.z);
|
||||
|
||||
if (std::abs(dir_val) < 1e-8f) {
|
||||
if (origin_val < min_val || origin_val > max_val) return false;
|
||||
} else {
|
||||
float inv_d = 1.0f / dir_val;
|
||||
float t1 = (min_val - origin_val) * inv_d;
|
||||
float t2 = (max_val - origin_val) * inv_d;
|
||||
if (t1 > t2) std::swap(t1, t2);
|
||||
tmin = std::max(tmin, t1);
|
||||
tmax = std::min(tmax, t2);
|
||||
if (tmin > tmax) return false;
|
||||
}
|
||||
}
|
||||
|
||||
out_t = tmin;
|
||||
return true;
|
||||
}
|
||||
|
||||
// LooseOctree
|
||||
LooseOctree::LooseOctree(const AABB& world_bounds, float looseness, int max_depth, int max_objects_per_node)
|
||||
: m_world_bounds(world_bounds)
|
||||
, m_looseness(looseness)
|
||||
, m_max_depth(max_depth)
|
||||
, m_max_objects_per_node(max_objects_per_node) {
|
||||
m_root = std::make_unique<Node>();
|
||||
m_root->base_bounds = world_bounds;
|
||||
m_root->loose_bounds = world_bounds.Expanded(looseness);
|
||||
m_root->depth = 0;
|
||||
}
|
||||
|
||||
void LooseOctree::Subdivide(Node* node) {
|
||||
Vector3 center = node->base_bounds.GetCenter();
|
||||
Vector3 min_p = node->base_bounds.min_point;
|
||||
Vector3 max_p = node->base_bounds.max_point;
|
||||
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
Vector3 child_min{
|
||||
(i & 1) ? center.x : min_p.x,
|
||||
(i & 2) ? center.y : min_p.y,
|
||||
(i & 4) ? center.z : min_p.z
|
||||
};
|
||||
Vector3 child_max{
|
||||
(i & 1) ? max_p.x : center.x,
|
||||
(i & 2) ? max_p.y : center.y,
|
||||
(i & 4) ? max_p.z : center.z
|
||||
};
|
||||
|
||||
auto child = std::make_unique<Node>();
|
||||
child->base_bounds = AABB(child_min, child_max);
|
||||
child->loose_bounds = child->base_bounds.Expanded(m_looseness);
|
||||
child->depth = node->depth + 1;
|
||||
node->children[i] = std::move(child);
|
||||
}
|
||||
}
|
||||
|
||||
LooseOctree::Node* LooseOctree::FindBestNodeForBounds(Node* current, const AABB& bounds) {
|
||||
if (current->depth >= m_max_depth) {
|
||||
return current;
|
||||
}
|
||||
|
||||
if (current->IsLeaf()) {
|
||||
if (current->objects.size() >= static_cast<size_t>(m_max_objects_per_node)) {
|
||||
Subdivide(current);
|
||||
} else {
|
||||
return current;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if bounds completely fit inside any child's loose bounds
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
if (current->children[i] && current->children[i]->loose_bounds.Contains(bounds)) {
|
||||
return FindBestNodeForBounds(current->children[i].get(), bounds);
|
||||
}
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
bool LooseOctree::Insert(uint64_t id, SpatialObjectType type, const AABB& bounds, void* user_data) {
|
||||
if (m_object_map.find(id) != m_object_map.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SpatialObject obj;
|
||||
obj.id = id;
|
||||
obj.type = type;
|
||||
obj.bounds = bounds;
|
||||
obj.user_data = user_data;
|
||||
|
||||
Node* target = FindBestNodeForBounds(m_root.get(), bounds);
|
||||
target->objects.push_back(obj);
|
||||
m_object_map[id] = obj;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LooseOctree::RemoveRecursive(Node* node, uint64_t id) {
|
||||
auto it = std::remove_if(node->objects.begin(), node->objects.end(),
|
||||
[id](const SpatialObject& o) { return o.id == id; });
|
||||
if (it != node->objects.end()) {
|
||||
node->objects.erase(it, node->objects.end());
|
||||
return true;
|
||||
}
|
||||
|
||||
for (auto& child : node->children) {
|
||||
if (child && RemoveRecursive(child.get(), id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool LooseOctree::Remove(uint64_t id) {
|
||||
auto it = m_object_map.find(id);
|
||||
if (it == m_object_map.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RemoveRecursive(m_root.get(), id);
|
||||
m_object_map.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LooseOctree::Update(uint64_t id, const AABB& new_bounds) {
|
||||
auto it = m_object_map.find(id);
|
||||
if (it == m_object_map.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SpatialObjectType type = it->second.type;
|
||||
void* user_data = it->second.user_data;
|
||||
|
||||
Remove(id);
|
||||
return Insert(id, type, new_bounds, user_data);
|
||||
}
|
||||
|
||||
void LooseOctree::Clear() {
|
||||
m_object_map.clear();
|
||||
m_root = std::make_unique<Node>();
|
||||
m_root->base_bounds = m_world_bounds;
|
||||
m_root->loose_bounds = m_world_bounds.Expanded(m_looseness);
|
||||
m_root->depth = 0;
|
||||
}
|
||||
|
||||
void LooseOctree::CollectAllRecursive(const Node* node, const Vector3& camera_pos, std::vector<VisibleObject>& out_visible) const {
|
||||
for (const auto& obj : node->objects) {
|
||||
VisibleObject vis;
|
||||
vis.id = obj.id;
|
||||
vis.type = obj.type;
|
||||
vis.bounds = obj.bounds;
|
||||
vis.user_data = obj.user_data;
|
||||
vis.distance_sq = camera_pos.DistanceSq(obj.bounds.GetCenter());
|
||||
vis.distance = std::sqrt(vis.distance_sq);
|
||||
out_visible.push_back(vis);
|
||||
}
|
||||
|
||||
for (const auto& child : node->children) {
|
||||
if (child) {
|
||||
CollectAllRecursive(child.get(), camera_pos, out_visible);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LooseOctree::QueryFrustumRecursive(const Node* node, const Frustum& frustum, const Vector3& camera_pos,
|
||||
FrustumIntersection parent_status, std::vector<VisibleObject>& out_visible) const {
|
||||
FrustumIntersection status = parent_status;
|
||||
if (status != FrustumIntersection::Inside) {
|
||||
status = frustum.TestAABB(node->loose_bounds);
|
||||
}
|
||||
|
||||
if (status == FrustumIntersection::Outside) {
|
||||
return; // Culling entire node and children!
|
||||
}
|
||||
|
||||
if (status == FrustumIntersection::Inside) {
|
||||
// Entire subtree visible
|
||||
CollectAllRecursive(node, camera_pos, out_visible);
|
||||
return;
|
||||
}
|
||||
|
||||
// Intersects: test each object individually
|
||||
for (const auto& obj : node->objects) {
|
||||
if (frustum.Intersects(obj.bounds)) {
|
||||
VisibleObject vis;
|
||||
vis.id = obj.id;
|
||||
vis.type = obj.type;
|
||||
vis.bounds = obj.bounds;
|
||||
vis.user_data = obj.user_data;
|
||||
vis.distance_sq = camera_pos.DistanceSq(obj.bounds.GetCenter());
|
||||
vis.distance = std::sqrt(vis.distance_sq);
|
||||
out_visible.push_back(vis);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& child : node->children) {
|
||||
if (child) {
|
||||
QueryFrustumRecursive(child.get(), frustum, camera_pos, status, out_visible);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<VisibleObject> LooseOctree::QueryFrustum(const Frustum& frustum, const Vector3& camera_pos, bool sort_by_distance) const {
|
||||
std::vector<VisibleObject> visible;
|
||||
if (m_root) {
|
||||
QueryFrustumRecursive(m_root.get(), frustum, camera_pos, FrustumIntersection::Intersects, visible);
|
||||
}
|
||||
|
||||
if (sort_by_distance && !visible.empty()) {
|
||||
std::sort(visible.begin(), visible.end(), [](const VisibleObject& a, const VisibleObject& b) {
|
||||
return a.distance_sq < b.distance_sq;
|
||||
});
|
||||
}
|
||||
|
||||
return visible;
|
||||
}
|
||||
|
||||
void LooseOctree::QueryAABBRecursive(const Node* node, const AABB& query_box, std::vector<uint64_t>& out_ids) const {
|
||||
if (!node->loose_bounds.Intersects(query_box)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& obj : node->objects) {
|
||||
if (obj.bounds.Intersects(query_box)) {
|
||||
out_ids.push_back(obj.id);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& child : node->children) {
|
||||
if (child) {
|
||||
QueryAABBRecursive(child.get(), query_box, out_ids);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint64_t> LooseOctree::QueryAABB(const AABB& query_box) const {
|
||||
std::vector<uint64_t> result;
|
||||
if (m_root) {
|
||||
QueryAABBRecursive(m_root.get(), query_box, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void LooseOctree::QuerySphereRecursive(const Node* node, const Vector3& center, float radius_sq, std::vector<uint64_t>& out_ids) const {
|
||||
// AABB-Sphere test for node loose bounds
|
||||
Vector3 clamped{
|
||||
std::clamp(center.x, node->loose_bounds.min_point.x, node->loose_bounds.max_point.x),
|
||||
std::clamp(center.y, node->loose_bounds.min_point.y, node->loose_bounds.max_point.y),
|
||||
std::clamp(center.z, node->loose_bounds.min_point.z, node->loose_bounds.max_point.z)
|
||||
};
|
||||
if (center.DistanceSq(clamped) > radius_sq) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& obj : node->objects) {
|
||||
Vector3 obj_clamped{
|
||||
std::clamp(center.x, obj.bounds.min_point.x, obj.bounds.max_point.x),
|
||||
std::clamp(center.y, obj.bounds.min_point.y, obj.bounds.max_point.y),
|
||||
std::clamp(center.z, obj.bounds.min_point.z, obj.bounds.max_point.z)
|
||||
};
|
||||
if (center.DistanceSq(obj_clamped) <= radius_sq) {
|
||||
out_ids.push_back(obj.id);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& child : node->children) {
|
||||
if (child) {
|
||||
QuerySphereRecursive(child.get(), center, radius_sq, out_ids);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint64_t> LooseOctree::QuerySphere(const Vector3& center, float radius) const {
|
||||
std::vector<uint64_t> result;
|
||||
if (m_root && radius > 0.0f) {
|
||||
QuerySphereRecursive(m_root.get(), center, radius * radius, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void LooseOctree::RaycastRecursive(const Node* node, const Ray& ray, std::vector<RaycastHit>& hits) const {
|
||||
float node_t = 0.0f;
|
||||
if (!ray.IntersectAABB(node->loose_bounds, node_t)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& obj : node->objects) {
|
||||
float obj_t = 0.0f;
|
||||
if (ray.IntersectAABB(obj.bounds, obj_t)) {
|
||||
RaycastHit hit;
|
||||
hit.hit = true;
|
||||
hit.object_id = obj.id;
|
||||
hit.type = obj.type;
|
||||
hit.distance = obj_t;
|
||||
hit.point = ray.origin + ray.direction * obj_t;
|
||||
hit.user_data = obj.user_data;
|
||||
hits.push_back(hit);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& child : node->children) {
|
||||
if (child) {
|
||||
RaycastRecursive(child.get(), ray, hits);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool LooseOctree::Raycast(const Ray& ray, RaycastHit& out_hit) const {
|
||||
auto hits = RaycastAll(ray);
|
||||
if (hits.empty()) {
|
||||
out_hit.hit = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
out_hit = hits[0]; // Already sorted by distance ascending
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<RaycastHit> LooseOctree::RaycastAll(const Ray& ray) const {
|
||||
std::vector<RaycastHit> hits;
|
||||
if (m_root) {
|
||||
RaycastRecursive(m_root.get(), ray, hits);
|
||||
}
|
||||
|
||||
if (!hits.empty()) {
|
||||
std::sort(hits.begin(), hits.end(), [](const RaycastHit& a, const RaycastHit& b) {
|
||||
return a.distance < b.distance;
|
||||
});
|
||||
}
|
||||
|
||||
return hits;
|
||||
}
|
||||
|
||||
} // namespace metin2::spatial
|
||||
@@ -0,0 +1,134 @@
|
||||
#include "spatial/spatial.h"
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
|
||||
using namespace metin2::spatial;
|
||||
|
||||
void TestMathPrimitives() {
|
||||
std::cout << "[Test] Vector3 & AABB & Frustum Primitives...\n";
|
||||
Vector3 a(1, 2, 3);
|
||||
Vector3 b(4, 5, 6);
|
||||
assert(a.Dot(b) == 32.0f);
|
||||
|
||||
Vector3 cross = Vector3(1, 0, 0).Cross(Vector3(0, 1, 0));
|
||||
assert(std::abs(cross.z - 1.0f) < 1e-5f);
|
||||
|
||||
AABB box(Vector3(-10, -10, -10), Vector3(10, 10, 10));
|
||||
assert(box.Contains(Vector3(0, 0, 0)));
|
||||
assert(!box.Contains(Vector3(20, 0, 0)));
|
||||
|
||||
AABB other(Vector3(5, 5, 5), Vector3(15, 15, 15));
|
||||
assert(box.Intersects(other));
|
||||
|
||||
// Perspective Frustum
|
||||
Vector3 eye(0, 100, -500);
|
||||
Vector3 look(0, 0, 1);
|
||||
Vector3 up(0, 1, 0);
|
||||
Frustum frustum = Frustum::CreatePerspective(eye, look, up, 60.0f, 16.0f / 9.0f, 1.0f, 2000.0f);
|
||||
|
||||
AABB in_front(Vector3(-50, 50, 200), Vector3(50, 150, 300));
|
||||
assert(frustum.Intersects(in_front));
|
||||
|
||||
AABB behind_camera(Vector3(-50, 50, -1000), Vector3(50, 150, -800));
|
||||
assert(!frustum.Intersects(behind_camera));
|
||||
|
||||
std::cout << " -> Math primitives passed!\n";
|
||||
}
|
||||
|
||||
void TestLooseOctreeDynamicOperations() {
|
||||
std::cout << "[Test] Loose Octree Dynamic Insertion, Update & Removal...\n";
|
||||
AABB world(Vector3(-50000, -5000, -50000), Vector3(50000, 10000, 50000));
|
||||
LooseOctree octree(world, 2.0f, 5, 8);
|
||||
|
||||
// Insert 3 objects: Building (1), Tree (2), Monster (3)
|
||||
assert(octree.Insert(1, SpatialObjectType::Building, AABB(Vector3(100, 0, 100), Vector3(300, 400, 300))));
|
||||
assert(octree.Insert(2, SpatialObjectType::Tree, AABB(Vector3(-200, 0, 500), Vector3(-150, 600, 550))));
|
||||
assert(octree.Insert(3, SpatialObjectType::Monster, AABB(Vector3(10, 0, 10), Vector3(30, 80, 30))));
|
||||
|
||||
assert(octree.Size() == 3);
|
||||
assert(octree.Contains(1));
|
||||
assert(octree.Contains(2));
|
||||
assert(octree.Contains(3));
|
||||
|
||||
// Cannot insert duplicate ID
|
||||
assert(!octree.Insert(1, SpatialObjectType::Building, AABB(Vector3(0, 0, 0), Vector3(10, 10, 10))));
|
||||
|
||||
// Update object 3 position
|
||||
assert(octree.Update(3, AABB(Vector3(500, 0, 500), Vector3(520, 80, 520))));
|
||||
assert(octree.Size() == 3);
|
||||
|
||||
// Remove object 2
|
||||
assert(octree.Remove(2));
|
||||
assert(!octree.Contains(2));
|
||||
assert(octree.Size() == 2);
|
||||
|
||||
std::cout << " -> Loose Octree operations passed!\n";
|
||||
}
|
||||
|
||||
void TestFrustumCullingAndDistanceSort() {
|
||||
std::cout << "[Test] Frustum Culling & Distance Sorting...\n";
|
||||
AABB world(Vector3(-10000, -1000, -10000), Vector3(10000, 2000, 10000));
|
||||
LooseOctree octree(world);
|
||||
|
||||
// Insert objects along the Z-axis
|
||||
// Camera is at (0, 0, 0), looking forward (0, 0, 1)
|
||||
octree.Insert(10, SpatialObjectType::Tree, AABB(Vector3(-10, 0, 500), Vector3(10, 200, 520))); // In front (500 units)
|
||||
octree.Insert(20, SpatialObjectType::Tree, AABB(Vector3(-10, 0, 100), Vector3(10, 200, 120))); // In front (100 units, closer)
|
||||
octree.Insert(30, SpatialObjectType::Tree, AABB(Vector3(-10, 0, 1500), Vector3(10, 200, 1520)));// In front (1500 units, farther)
|
||||
octree.Insert(40, SpatialObjectType::Building, AABB(Vector3(-10, 0, -500), Vector3(10, 200, -480))); // Behind camera
|
||||
octree.Insert(50, SpatialObjectType::Building, AABB(Vector3(3000, 0, 200), Vector3(3200, 200, 220))); // Way off to the right
|
||||
|
||||
Vector3 cam_pos(0, 10, 0);
|
||||
Frustum frustum = Frustum::CreatePerspective(cam_pos, Vector3(0, 0, 1), Vector3(0, 1, 0), 60.0f, 1.0f, 10.0f, 2000.0f);
|
||||
|
||||
auto visible = octree.QueryFrustum(frustum, cam_pos, true);
|
||||
|
||||
// Expected: 20, 10, 30 are visible in order of distance; 40 and 50 are culled!
|
||||
assert(visible.size() == 3);
|
||||
assert(visible[0].id == 20); // ~110 units
|
||||
assert(visible[1].id == 10); // ~510 units
|
||||
assert(visible[2].id == 30); // ~1510 units
|
||||
assert(visible[0].distance < visible[1].distance);
|
||||
assert(visible[1].distance < visible[2].distance);
|
||||
|
||||
std::cout << " -> Frustum culling and distance sort verified!\n";
|
||||
}
|
||||
|
||||
void TestSphereQueryAndRaycast() {
|
||||
std::cout << "[Test] Sphere AOE Query & Raycast...\n";
|
||||
AABB world(Vector3(-10000, -1000, -10000), Vector3(10000, 2000, 10000));
|
||||
LooseOctree octree(world);
|
||||
|
||||
octree.Insert(100, SpatialObjectType::Monster, AABB(Vector3(0, 0, 100), Vector3(20, 50, 120)));
|
||||
octree.Insert(200, SpatialObjectType::Monster, AABB(Vector3(0, 0, 300), Vector3(20, 50, 320)));
|
||||
octree.Insert(300, SpatialObjectType::Building, AABB(Vector3(500, 0, 500), Vector3(600, 300, 600)));
|
||||
|
||||
// Sphere query around (0, 0, 100) with radius 150
|
||||
auto sphere_hits = octree.QuerySphere(Vector3(10, 25, 110), 100.0f);
|
||||
assert(sphere_hits.size() == 1);
|
||||
assert(sphere_hits[0] == 100);
|
||||
|
||||
// Raycast: shoot ray straight down Z axis from (10, 25, 0) towards +Z
|
||||
Ray ray;
|
||||
ray.origin = Vector3(10, 25, 0);
|
||||
ray.direction = Vector3(0, 0, 1);
|
||||
|
||||
RaycastHit hit;
|
||||
bool found = octree.Raycast(ray, hit);
|
||||
assert(found);
|
||||
assert(hit.object_id == 100);
|
||||
assert(hit.distance >= 99.0f && hit.distance <= 101.0f);
|
||||
|
||||
std::cout << " -> Sphere query and Raycast verified!\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << "=== Running Metin2 Spatial Octree SDK Tests ===\n";
|
||||
TestMathPrimitives();
|
||||
TestLooseOctreeDynamicOperations();
|
||||
TestFrustumCullingAndDistanceSort();
|
||||
TestSphereQueryAndRaycast();
|
||||
std::cout << "All Spatial Octree SDK Tests PASSED!\n";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user