Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
|
||||
namespace metin2::terrain {
|
||||
|
||||
class TerrainHeightmap {
|
||||
public:
|
||||
static constexpr int RAW_XSIZE = 131;
|
||||
static constexpr int RAW_YSIZE = 131;
|
||||
static constexpr int MAP_XSIZE = 128;
|
||||
static constexpr int MAP_YSIZE = 128;
|
||||
static constexpr int TILEMAP_RAW_XSIZE = 258;
|
||||
static constexpr int TILEMAP_RAW_YSIZE = 258;
|
||||
static constexpr int ATTRMAP_XSIZE = 256;
|
||||
static constexpr int ATTRMAP_YSIZE = 256;
|
||||
static constexpr int PATCH_SIZE = 16;
|
||||
static constexpr int PATCH_COUNT = MAP_XSIZE / PATCH_SIZE; // 8x8 = 64
|
||||
static constexpr float DEFAULT_CELL_SCALE = 200.0f;
|
||||
static constexpr float DEFAULT_HEIGHT_SCALE = 0.5f;
|
||||
|
||||
TerrainHeightmap();
|
||||
~TerrainHeightmap() = default;
|
||||
|
||||
// Load from Metin2 height.raw file (131x131 uint16_t, 34322 bytes) or memory buffer
|
||||
bool LoadRaw(const std::string& raw_path, float height_scale = DEFAULT_HEIGHT_SCALE, float cell_scale = DEFAULT_CELL_SCALE);
|
||||
bool LoadFromBuffer(const uint16_t* raw_data, size_t count, float height_scale = DEFAULT_HEIGHT_SCALE, float cell_scale = DEFAULT_CELL_SCALE);
|
||||
|
||||
// Load from Metin2 tile.raw file (258x258 uint8_t, 66564 bytes) or memory buffer
|
||||
bool LoadTileRaw(const std::string& tile_path);
|
||||
bool LoadTileFromBuffer(const uint8_t* tile_data, size_t count);
|
||||
|
||||
// Load from Metin2 attr.atr file (65542 bytes = 6 bytes header + 256*256 bytes) or raw 65536 bytes
|
||||
bool LoadAttr(const std::string& attr_path);
|
||||
bool LoadAttrFromBuffer(const uint8_t* attr_data, size_t count);
|
||||
|
||||
// Load TextureSet file (e.g. textureset/metin2_map1.txt)
|
||||
bool LoadTextureSet(const std::string& textureset_path);
|
||||
bool LoadTextureSetFromString(const std::string& content);
|
||||
|
||||
// Procedural terrain generation for testing & headless environments
|
||||
void GenerateProcedural(int width = 64, int height = 64, float cell_scale = 100.0f, float hill_amplitude = 80.0f);
|
||||
|
||||
// Query height at continuous world coordinates (bilinear interpolation)
|
||||
float GetHeightAtWorld(float world_x, float world_z) const;
|
||||
float GetHeightAtGrid(int x, int y) const;
|
||||
|
||||
// Query normal at continuous world coordinates (normalized, Y-up)
|
||||
void GetNormalAtWorld(float world_x, float world_z, float out_normal[3]) const;
|
||||
|
||||
// Query tile type at grid or world coordinates
|
||||
uint8_t GetTileAtGrid(int x, int y) const;
|
||||
uint8_t GetTileAtWorld(float world_x, float world_z) const;
|
||||
std::string GetTexturePathForTile(uint8_t tile_id) const;
|
||||
|
||||
// Query attribute flag (ATTR_BLOCK, ATTR_WATER, ATTR_BANPK, ATTR_OBJECT)
|
||||
bool HasAttrMap() const { return !m_attr.empty(); }
|
||||
uint8_t GetAttrAtGrid(int x, int y) const;
|
||||
uint8_t GetAttrAtWorld(float world_x, float world_z) const;
|
||||
bool IsAttrOn(float world_x, float world_z, uint8_t flag) const;
|
||||
|
||||
// CTerrain block placement world origin
|
||||
void SetWorldOrigin(float min_x, float min_z);
|
||||
float GetWorldMinX() const { return m_world_min_x; }
|
||||
float GetWorldMaxX() const { return m_world_max_x; }
|
||||
float GetWorldMinZ() const { return m_world_min_z; }
|
||||
float GetWorldMaxZ() const { return m_world_max_z; }
|
||||
float GetMinHeight() const { return m_min_height; }
|
||||
float GetMaxHeight() const { return m_max_height; }
|
||||
int GetWidth() const { return m_width; }
|
||||
int GetHeight() const { return m_height; }
|
||||
|
||||
// Build unified 3D draw command without host graphic API bindings
|
||||
TerrainDrawCommand BuildDrawCommand(const float world[16] = nullptr, int step = 1) const;
|
||||
|
||||
// Quadtree frustum culling query
|
||||
std::vector<TerrainDrawCommand> BuildCulledDraws(const float view[16], const float proj[16], float max_distance = 15000.0f) const;
|
||||
|
||||
// Direct access to patches & quadtree
|
||||
const std::vector<TerrainPatch>& GetPatches() const { return m_patches; }
|
||||
const TerrainQuadNode* GetQuadtreeRoot() const { return m_quadtreeRoot.get(); }
|
||||
|
||||
private:
|
||||
void UpdateBounds();
|
||||
void CalculateNormals();
|
||||
void BuildPatchesAndQuadtree();
|
||||
std::unique_ptr<TerrainQuadNode> BuildQuadtreeNode(float min_x, float max_x, float min_z, float max_z, int depth);
|
||||
void QueryQuadtree(const TerrainQuadNode* node, const float planes[6][4], const float eye[3], float max_distance, std::vector<int>& out_visible_patches) const;
|
||||
|
||||
int m_width = 0;
|
||||
int m_height = 0;
|
||||
float m_cell_scale = DEFAULT_CELL_SCALE;
|
||||
float m_height_scale = DEFAULT_HEIGHT_SCALE;
|
||||
float m_world_min_x = 0.0f;
|
||||
float m_world_max_x = 0.0f;
|
||||
float m_world_min_z = 0.0f;
|
||||
float m_world_max_z = 0.0f;
|
||||
float m_min_height = 0.0f;
|
||||
float m_max_height = 0.0f;
|
||||
bool m_has_origin = false;
|
||||
float m_origin_x = 0.0f;
|
||||
float m_origin_z = 0.0f;
|
||||
|
||||
std::vector<float> m_heights;
|
||||
std::vector<float> m_normals; // 3 floats per vertex
|
||||
|
||||
int m_tile_width = 0;
|
||||
int m_tile_height = 0;
|
||||
std::vector<uint8_t> m_tiles;
|
||||
std::unordered_map<uint8_t, std::string> m_textureSet;
|
||||
|
||||
int m_attr_width = 0;
|
||||
int m_attr_height = 0;
|
||||
std::vector<uint8_t> m_attr;
|
||||
|
||||
std::vector<TerrainPatch> m_patches;
|
||||
std::unique_ptr<TerrainQuadNode> m_quadtreeRoot;
|
||||
};
|
||||
|
||||
} // namespace metin2::terrain
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include "heightmap.h"
|
||||
#include "water.h"
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace metin2::terrain {
|
||||
|
||||
// Metin2 collision and map attribute flags (aligned with CTerrainImpl)
|
||||
enum TerrainAttr : uint8_t {
|
||||
ATTR_NONE = 0,
|
||||
ATTR_BLOCK = (1 << 0), // Movement collision blocking (cannot walk)
|
||||
ATTR_WATER = (1 << 1), // Water surface / swimming
|
||||
ATTR_BANPK = (1 << 2), // Safe zone / peace zone (PvP disabled)
|
||||
ATTR_OBJECT = (1 << 3), // Occupied by static world object
|
||||
};
|
||||
|
||||
struct TerrainPatch {
|
||||
int patch_x = 0; // 0..7
|
||||
int patch_y = 0; // 0..7
|
||||
float aabb_min[3] = {0.0f, 0.0f, 0.0f};
|
||||
float aabb_max[3] = {0.0f, 0.0f, 0.0f};
|
||||
uint8_t dominant_tile = 0;
|
||||
};
|
||||
|
||||
struct TerrainQuadNode {
|
||||
float aabb_min[3] = {0.0f, 0.0f, 0.0f};
|
||||
float aabb_max[3] = {0.0f, 0.0f, 0.0f};
|
||||
int depth = 0;
|
||||
int patch_idx = -1; // -1 if internal, >=0 if leaf (0..63)
|
||||
std::unique_ptr<TerrainQuadNode> children[4];
|
||||
};
|
||||
|
||||
struct TerrainDrawCommand {
|
||||
std::vector<float> positions; // x, y, z
|
||||
std::vector<float> normals; // nx, ny, nz
|
||||
std::vector<float> uv0; // u, v
|
||||
std::vector<uint32_t> diffuse;// RGBA8888 packed
|
||||
std::vector<uint32_t> indices;// 3 indices per triangle
|
||||
uint8_t dominant_tile = 0;
|
||||
std::string texture_path;
|
||||
float aabb_min[3] = {0.0f, 0.0f, 0.0f};
|
||||
float aabb_max[3] = {0.0f, 0.0f, 0.0f};
|
||||
};
|
||||
|
||||
} // namespace metin2::terrain
|
||||
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include "types.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
namespace metin2::terrain {
|
||||
|
||||
class WaterManager {
|
||||
public:
|
||||
WaterManager();
|
||||
~WaterManager() = default;
|
||||
|
||||
// Load Metin2 binary water.wtr file (Magic: 5426)
|
||||
bool LoadWater(const std::string& filepath, float cell_scale = 40.0f, float height_scale = 0.5f, float height_bias = 0.0f,
|
||||
float world_min_x = -2560.0f, float world_min_z = -2560.0f);
|
||||
bool LoadWaterFromBuffer(const uint8_t* data, size_t size, float cell_scale = 40.0f, float height_scale = 0.5f, float height_bias = 0.0f,
|
||||
float world_min_x = -2560.0f, float world_min_z = -2560.0f);
|
||||
|
||||
// Generate procedural water plane (for valleys, rivers, or lakes)
|
||||
void GenerateProceduralWater(float water_level, float min_x, float max_x, float min_z, float max_z, float cell_size = 60.0f);
|
||||
|
||||
// Build unified 3D draw command for water rasterization
|
||||
TerrainDrawCommand BuildDrawCommand(float anim_time = 0.0f) const;
|
||||
|
||||
bool HasWater() const { return m_has_water; }
|
||||
size_t GetWaterFaceCount() const { return m_face_count; }
|
||||
float GetWaterLevel(size_t layer = 0) const;
|
||||
|
||||
void SetWaterColor(uint32_t color) { m_water_color = color; }
|
||||
uint32_t GetWaterColor() const { return m_water_color; }
|
||||
|
||||
private:
|
||||
bool m_has_water = false;
|
||||
uint16_t m_width = 0;
|
||||
uint16_t m_height = 0;
|
||||
std::vector<uint8_t> m_water_grid;
|
||||
std::vector<int32_t> m_water_heights;
|
||||
float m_cell_scale = 40.0f;
|
||||
float m_height_scale = 0.5f;
|
||||
float m_height_bias = 0.0f;
|
||||
float m_world_min_x = 0.0f;
|
||||
float m_world_min_z = 0.0f;
|
||||
size_t m_face_count = 0;
|
||||
uint32_t m_water_color = 0xAA2C7CA3; // translucent aqua cyan
|
||||
|
||||
struct ProceduralWaterPlane {
|
||||
float water_level = 0.0f;
|
||||
float min_x = 0.0f, max_x = 0.0f;
|
||||
float min_z = 0.0f, max_z = 0.0f;
|
||||
float cell_size = 60.0f;
|
||||
};
|
||||
ProceduralWaterPlane m_proc_plane;
|
||||
bool m_procedural = false;
|
||||
};
|
||||
|
||||
} // namespace metin2::terrain
|
||||
Reference in New Issue
Block a user