Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,707 @@
|
||||
#include "terrain/heightmap.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace metin2::terrain {
|
||||
|
||||
namespace {
|
||||
|
||||
inline void Normalize3(float v[3]) {
|
||||
float len = std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
|
||||
if (len > 1e-6f) {
|
||||
float inv = 1.0f / len;
|
||||
v[0] *= inv;
|
||||
v[1] *= inv;
|
||||
v[2] *= inv;
|
||||
} else {
|
||||
v[0] = 0.0f; v[1] = 1.0f; v[2] = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
inline void ExtractFrustumPlanes(const float view[16], const float proj[16], float planes[6][4]) {
|
||||
float vp[16];
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
vp[i * 4 + j] = view[i * 4 + 0] * proj[0 * 4 + j] +
|
||||
view[i * 4 + 1] * proj[1 * 4 + j] +
|
||||
view[i * 4 + 2] * proj[2 * 4 + j] +
|
||||
view[i * 4 + 3] * proj[3 * 4 + j];
|
||||
}
|
||||
}
|
||||
|
||||
// Left, Right, Bottom, Top, Near, Far
|
||||
planes[0][0] = vp[3] + vp[0]; planes[0][1] = vp[7] + vp[4]; planes[0][2] = vp[11] + vp[8]; planes[0][3] = vp[15] + vp[12];
|
||||
planes[1][0] = vp[3] - vp[0]; planes[1][1] = vp[7] - vp[4]; planes[1][2] = vp[11] - vp[8]; planes[1][3] = vp[15] - vp[12];
|
||||
planes[2][0] = vp[3] + vp[1]; planes[2][1] = vp[7] + vp[5]; planes[2][2] = vp[11] + vp[9]; planes[2][3] = vp[15] + vp[13];
|
||||
planes[3][0] = vp[3] - vp[1]; planes[3][1] = vp[7] - vp[5]; planes[3][2] = vp[11] - vp[9]; planes[3][3] = vp[15] - vp[13];
|
||||
planes[4][0] = vp[2]; planes[4][1] = vp[6]; planes[4][2] = vp[10]; planes[4][3] = vp[14];
|
||||
planes[5][0] = vp[3] - vp[2]; planes[5][1] = vp[7] - vp[6]; planes[5][2] = vp[11] - vp[10]; planes[5][3] = vp[15] - vp[14];
|
||||
|
||||
for (int p = 0; p < 6; ++p) {
|
||||
float len = std::sqrt(planes[p][0] * planes[p][0] + planes[p][1] * planes[p][1] + planes[p][2] * planes[p][2]);
|
||||
if (len > 1e-6f) {
|
||||
float inv = 1.0f / len;
|
||||
planes[p][0] *= inv;
|
||||
planes[p][1] *= inv;
|
||||
planes[p][2] *= inv;
|
||||
planes[p][3] *= inv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline bool IsBoxInFrustum(const float aabb_min[3], const float aabb_max[3], const float planes[6][4]) {
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
float px = (planes[i][0] > 0.0f) ? aabb_max[0] : aabb_min[0];
|
||||
float py = (planes[i][1] > 0.0f) ? aabb_max[1] : aabb_min[1];
|
||||
float pz = (planes[i][2] > 0.0f) ? aabb_max[2] : aabb_min[2];
|
||||
|
||||
if (planes[i][0] * px + planes[i][1] * py + planes[i][2] * pz + planes[i][3] < 0.0f) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TerrainHeightmap::TerrainHeightmap() = default;
|
||||
|
||||
bool TerrainHeightmap::LoadRaw(const std::string& raw_path, float height_scale, float cell_scale) {
|
||||
std::ifstream file(raw_path, std::ios::binary);
|
||||
if (!file.is_open()) return false;
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
size_t file_size = static_cast<size_t>(file.tellg());
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
if (file_size < sizeof(uint16_t) * RAW_XSIZE * RAW_YSIZE) return false;
|
||||
|
||||
std::vector<uint16_t> buffer(RAW_XSIZE * RAW_YSIZE);
|
||||
file.read(reinterpret_cast<char*>(buffer.data()), buffer.size() * sizeof(uint16_t));
|
||||
if (!file) return false;
|
||||
|
||||
return LoadFromBuffer(buffer.data(), buffer.size(), height_scale, cell_scale);
|
||||
}
|
||||
|
||||
bool TerrainHeightmap::LoadFromBuffer(const uint16_t* raw_data, size_t count, float height_scale, float cell_scale) {
|
||||
if (!raw_data || count < static_cast<size_t>(RAW_XSIZE * RAW_YSIZE)) return false;
|
||||
|
||||
m_width = RAW_XSIZE;
|
||||
m_height = RAW_YSIZE;
|
||||
m_cell_scale = cell_scale;
|
||||
m_height_scale = height_scale;
|
||||
|
||||
m_heights.resize(m_width * m_height);
|
||||
m_min_height = 1e9f;
|
||||
m_max_height = -1e9f;
|
||||
|
||||
for (size_t i = 0; i < m_heights.size(); ++i) {
|
||||
float h = static_cast<float>(raw_data[i]) * m_height_scale;
|
||||
m_heights[i] = h;
|
||||
if (h < m_min_height) m_min_height = h;
|
||||
if (h > m_max_height) m_max_height = h;
|
||||
}
|
||||
|
||||
UpdateBounds();
|
||||
CalculateNormals();
|
||||
BuildPatchesAndQuadtree();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TerrainHeightmap::LoadTileRaw(const std::string& tile_path) {
|
||||
std::ifstream file(tile_path, std::ios::binary);
|
||||
if (!file.is_open()) return false;
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
size_t file_size = static_cast<size_t>(file.tellg());
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
if (file_size < static_cast<size_t>(TILEMAP_RAW_XSIZE * TILEMAP_RAW_YSIZE)) return false;
|
||||
|
||||
std::vector<uint8_t> buffer(TILEMAP_RAW_XSIZE * TILEMAP_RAW_YSIZE);
|
||||
file.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
|
||||
if (!file) return false;
|
||||
|
||||
return LoadTileFromBuffer(buffer.data(), buffer.size());
|
||||
}
|
||||
|
||||
bool TerrainHeightmap::LoadTileFromBuffer(const uint8_t* tile_data, size_t count) {
|
||||
if (!tile_data || count < static_cast<size_t>(TILEMAP_RAW_XSIZE * TILEMAP_RAW_YSIZE)) return false;
|
||||
|
||||
m_tile_width = TILEMAP_RAW_XSIZE;
|
||||
m_tile_height = TILEMAP_RAW_YSIZE;
|
||||
m_tiles.assign(tile_data, tile_data + count);
|
||||
|
||||
if (!m_heights.empty()) {
|
||||
BuildPatchesAndQuadtree();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TerrainHeightmap::LoadAttr(const std::string& attr_path) {
|
||||
std::ifstream file(attr_path, std::ios::binary);
|
||||
if (!file.is_open()) return false;
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
size_t file_size = static_cast<size_t>(file.tellg());
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<uint8_t> buffer(file_size);
|
||||
file.read(reinterpret_cast<char*>(buffer.data()), file_size);
|
||||
if (!file) return false;
|
||||
|
||||
return LoadAttrFromBuffer(buffer.data(), buffer.size());
|
||||
}
|
||||
|
||||
bool TerrainHeightmap::LoadAttrFromBuffer(const uint8_t* attr_data, size_t count) {
|
||||
if (!attr_data || count < static_cast<size_t>(ATTRMAP_XSIZE * ATTRMAP_YSIZE)) return false;
|
||||
|
||||
m_attr_width = ATTRMAP_XSIZE;
|
||||
m_attr_height = ATTRMAP_YSIZE;
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct SAttrMapHeader {
|
||||
uint16_t m_wMagic;
|
||||
uint16_t m_wWidth;
|
||||
uint16_t m_wHeight;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
const size_t expected_data_size = ATTRMAP_XSIZE * ATTRMAP_YSIZE;
|
||||
if (count >= sizeof(SAttrMapHeader) + expected_data_size) {
|
||||
const auto* hdr = reinterpret_cast<const SAttrMapHeader*>(attr_data);
|
||||
if (hdr->m_wMagic == 2634 && hdr->m_wWidth == ATTRMAP_XSIZE && hdr->m_wHeight == ATTRMAP_YSIZE) {
|
||||
const uint8_t* src = attr_data + sizeof(SAttrMapHeader);
|
||||
m_attr.assign(src, src + expected_data_size);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
m_attr.assign(attr_data, attr_data + expected_data_size);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TerrainHeightmap::LoadTextureSet(const std::string& textureset_path) {
|
||||
std::ifstream file(textureset_path);
|
||||
if (!file.is_open()) return false;
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
return LoadTextureSetFromString(buffer.str());
|
||||
}
|
||||
|
||||
bool TerrainHeightmap::LoadTextureSetFromString(const std::string& content) {
|
||||
std::istringstream file(content);
|
||||
std::string line;
|
||||
int current_id = 0;
|
||||
bool in_texture = false;
|
||||
|
||||
while (std::getline(file, line)) {
|
||||
while (!line.empty() && (line.back() == '\r' || line.back() == ' ' || line.back() == '\t')) {
|
||||
line.pop_back();
|
||||
}
|
||||
size_t start = line.find_first_not_of(" \t");
|
||||
if (start == std::string::npos) continue;
|
||||
std::string trim = line.substr(start);
|
||||
|
||||
if (trim.rfind("Start Texture", 0) == 0) {
|
||||
std::string id_str = trim.substr(13);
|
||||
current_id = std::atoi(id_str.c_str());
|
||||
in_texture = true;
|
||||
} else if (trim.rfind("End Texture", 0) == 0) {
|
||||
in_texture = false;
|
||||
current_id = 0;
|
||||
} else if (in_texture && current_id > 0) {
|
||||
if (trim.front() == '"' || trim.find(".dds") != std::string::npos || trim.find(".tga") != std::string::npos) {
|
||||
std::string path = trim;
|
||||
if (path.front() == '"' && path.back() == '"') {
|
||||
path = path.substr(1, path.size() - 2);
|
||||
}
|
||||
if (path.size() >= 3 && (path[0] == 'd' || path[0] == 'D') && path[1] == ':') {
|
||||
path = path.substr(3);
|
||||
}
|
||||
for (char& c : path) {
|
||||
if (c == '\\') c = '/';
|
||||
}
|
||||
while (!path.empty() && path.front() == '/') {
|
||||
path = path.substr(1);
|
||||
}
|
||||
m_textureSet[static_cast<uint8_t>(current_id)] = path;
|
||||
in_texture = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return !m_textureSet.empty();
|
||||
}
|
||||
|
||||
void TerrainHeightmap::GenerateProcedural(int width, int height, float cell_scale, float hill_amplitude) {
|
||||
m_width = std::max(width, 16);
|
||||
m_height = std::max(height, 16);
|
||||
m_cell_scale = cell_scale;
|
||||
m_height_scale = 1.0f;
|
||||
|
||||
m_heights.resize(m_width * m_height);
|
||||
m_min_height = 1e9f;
|
||||
m_max_height = -1e9f;
|
||||
|
||||
for (int j = 0; j < m_height; ++j) {
|
||||
for (int i = 0; i < m_width; ++i) {
|
||||
float fx = static_cast<float>(i);
|
||||
float fz = static_cast<float>(j);
|
||||
|
||||
float h1 = std::sin(fx * 0.12f) * std::cos(fz * 0.12f) * (hill_amplitude * 0.6f);
|
||||
float h2 = std::sin(fx * 0.05f + 1.2f) * std::cos(fz * 0.06f + 0.8f) * (hill_amplitude * 0.4f);
|
||||
float h3 = std::sin(fx * 0.25f) * (hill_amplitude * 0.1f);
|
||||
float h = h1 + h2 + h3;
|
||||
|
||||
int idx = j * m_width + i;
|
||||
m_heights[idx] = h;
|
||||
if (h < m_min_height) m_min_height = h;
|
||||
if (h > m_max_height) m_max_height = h;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateBounds();
|
||||
CalculateNormals();
|
||||
BuildPatchesAndQuadtree();
|
||||
}
|
||||
|
||||
void TerrainHeightmap::UpdateBounds() {
|
||||
float total_w = static_cast<float>(m_width - 1) * m_cell_scale;
|
||||
float total_h = static_cast<float>(m_height - 1) * m_cell_scale;
|
||||
m_world_min_x = m_has_origin ? m_origin_x : -total_w * 0.5f;
|
||||
m_world_max_x = m_world_min_x + total_w;
|
||||
m_world_min_z = m_has_origin ? m_origin_z : -total_h * 0.5f;
|
||||
m_world_max_z = m_world_min_z + total_h;
|
||||
}
|
||||
|
||||
void TerrainHeightmap::SetWorldOrigin(float min_x, float min_z) {
|
||||
m_has_origin = true;
|
||||
m_origin_x = min_x;
|
||||
m_origin_z = min_z;
|
||||
if (m_heights.empty()) return;
|
||||
UpdateBounds();
|
||||
BuildPatchesAndQuadtree();
|
||||
}
|
||||
|
||||
void TerrainHeightmap::CalculateNormals() {
|
||||
m_normals.resize(m_width * m_height * 3);
|
||||
|
||||
for (int j = 0; j < m_height; ++j) {
|
||||
for (int i = 0; i < m_width; ++i) {
|
||||
int x_prev = std::max(0, i - 1);
|
||||
int x_next = std::min(m_width - 1, i + 1);
|
||||
int z_prev = std::max(0, j - 1);
|
||||
int z_next = std::min(m_height - 1, j + 1);
|
||||
|
||||
float h_left = m_heights[j * m_width + x_prev];
|
||||
float h_right = m_heights[j * m_width + x_next];
|
||||
float h_down = m_heights[z_prev * m_width + i];
|
||||
float h_up = m_heights[z_next * m_width + i];
|
||||
|
||||
float dx = (h_right - h_left);
|
||||
float dz = (h_up - h_down);
|
||||
float scale_x = static_cast<float>(x_next - x_prev) * m_cell_scale;
|
||||
float scale_z = static_cast<float>(z_next - z_prev) * m_cell_scale;
|
||||
|
||||
float norm[3] = { -dx / scale_x, 1.0f, -dz / scale_z };
|
||||
Normalize3(norm);
|
||||
|
||||
int n_idx = (j * m_width + i) * 3;
|
||||
m_normals[n_idx + 0] = norm[0];
|
||||
m_normals[n_idx + 1] = norm[1];
|
||||
m_normals[n_idx + 2] = norm[2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float TerrainHeightmap::GetHeightAtGrid(int x, int y) const {
|
||||
if (m_heights.empty()) return 0.0f;
|
||||
x = std::clamp(x, 0, m_width - 1);
|
||||
y = std::clamp(y, 0, m_height - 1);
|
||||
return m_heights[y * m_width + x];
|
||||
}
|
||||
|
||||
float TerrainHeightmap::GetHeightAtWorld(float world_x, float world_z) const {
|
||||
if (m_heights.empty() || m_width < 2 || m_height < 2) return 0.0f;
|
||||
|
||||
float grid_x = (world_x - m_world_min_x) / m_cell_scale;
|
||||
float grid_z = (world_z - m_world_min_z) / m_cell_scale;
|
||||
|
||||
grid_x = std::clamp(grid_x, 0.0f, static_cast<float>(m_width - 1));
|
||||
grid_z = std::clamp(grid_z, 0.0f, static_cast<float>(m_height - 1));
|
||||
|
||||
int x0 = static_cast<int>(std::floor(grid_x));
|
||||
int z0 = static_cast<int>(std::floor(grid_z));
|
||||
int x1 = std::min(x0 + 1, m_width - 1);
|
||||
int z1 = std::min(z0 + 1, m_height - 1);
|
||||
|
||||
float tx = grid_x - static_cast<float>(x0);
|
||||
float tz = grid_z - static_cast<float>(z0);
|
||||
|
||||
float h00 = m_heights[z0 * m_width + x0];
|
||||
float h10 = m_heights[z0 * m_width + x1];
|
||||
float h01 = m_heights[z1 * m_width + x0];
|
||||
float h11 = m_heights[z1 * m_width + x1];
|
||||
|
||||
float h0 = h00 * (1.0f - tx) + h10 * tx;
|
||||
float h1 = h01 * (1.0f - tx) + h11 * tx;
|
||||
return h0 * (1.0f - tz) + h1 * tz;
|
||||
}
|
||||
|
||||
void TerrainHeightmap::GetNormalAtWorld(float world_x, float world_z, float out_normal[3]) const {
|
||||
if (m_normals.empty() || m_width < 2 || m_height < 2) {
|
||||
out_normal[0] = 0.0f; out_normal[1] = 1.0f; out_normal[2] = 0.0f;
|
||||
return;
|
||||
}
|
||||
|
||||
float grid_x = (world_x - m_world_min_x) / m_cell_scale;
|
||||
float grid_z = (world_z - m_world_min_z) / m_cell_scale;
|
||||
|
||||
grid_x = std::clamp(grid_x, 0.0f, static_cast<float>(m_width - 1));
|
||||
grid_z = std::clamp(grid_z, 0.0f, static_cast<float>(m_height - 1));
|
||||
|
||||
int x0 = static_cast<int>(std::round(grid_x));
|
||||
int z0 = static_cast<int>(std::round(grid_z));
|
||||
x0 = std::clamp(x0, 0, m_width - 1);
|
||||
z0 = std::clamp(z0, 0, m_height - 1);
|
||||
|
||||
int idx = (z0 * m_width + x0) * 3;
|
||||
out_normal[0] = m_normals[idx + 0];
|
||||
out_normal[1] = m_normals[idx + 1];
|
||||
out_normal[2] = m_normals[idx + 2];
|
||||
}
|
||||
|
||||
uint8_t TerrainHeightmap::GetTileAtGrid(int x, int y) const {
|
||||
if (m_tiles.empty() || m_tile_width <= 0 || m_tile_height <= 0) return 0;
|
||||
x = std::clamp(x, 0, m_tile_width - 1);
|
||||
y = std::clamp(y, 0, m_tile_height - 1);
|
||||
return m_tiles[y * m_tile_width + x];
|
||||
}
|
||||
|
||||
uint8_t TerrainHeightmap::GetTileAtWorld(float world_x, float world_z) const {
|
||||
if (m_tiles.empty() || m_tile_width <= 0 || m_tile_height <= 0) return 0;
|
||||
float grid_x = (world_x - m_world_min_x) / (static_cast<float>(m_width - 1) * m_cell_scale) * static_cast<float>(m_tile_width - 1);
|
||||
float grid_z = (world_z - m_world_min_z) / (static_cast<float>(m_height - 1) * m_cell_scale) * static_cast<float>(m_tile_height - 1);
|
||||
int tx = std::clamp(static_cast<int>(std::round(grid_x)), 0, m_tile_width - 1);
|
||||
int tz = std::clamp(static_cast<int>(std::round(grid_z)), 0, m_tile_height - 1);
|
||||
return m_tiles[tz * m_tile_width + tx];
|
||||
}
|
||||
|
||||
std::string TerrainHeightmap::GetTexturePathForTile(uint8_t tile_id) const {
|
||||
auto it = m_textureSet.find(tile_id);
|
||||
if (it != m_textureSet.end()) return it->second;
|
||||
return "ymir work/terrainmaps/map1/grass/grass 01.dds";
|
||||
}
|
||||
|
||||
uint8_t TerrainHeightmap::GetAttrAtGrid(int x, int y) const {
|
||||
if (m_attr.empty() || m_attr_width <= 0 || m_attr_height <= 0) return 0;
|
||||
x = std::clamp(x, 0, m_attr_width - 1);
|
||||
y = std::clamp(y, 0, m_attr_height - 1);
|
||||
return m_attr[y * m_attr_width + x];
|
||||
}
|
||||
|
||||
uint8_t TerrainHeightmap::GetAttrAtWorld(float world_x, float world_z) const {
|
||||
if (m_attr.empty() || m_attr_width <= 0 || m_attr_height <= 0 || m_width < 2 || m_height < 2) return 0;
|
||||
float grid_x = (world_x - m_world_min_x) / (static_cast<float>(m_width - 1) * m_cell_scale) * static_cast<float>(m_attr_width - 1);
|
||||
float grid_z = (world_z - m_world_min_z) / (static_cast<float>(m_height - 1) * m_cell_scale) * static_cast<float>(m_attr_height - 1);
|
||||
int ax = std::clamp(static_cast<int>(std::round(grid_x)), 0, m_attr_width - 1);
|
||||
int az = std::clamp(static_cast<int>(std::round(grid_z)), 0, m_attr_height - 1);
|
||||
return m_attr[az * m_attr_width + ax];
|
||||
}
|
||||
|
||||
bool TerrainHeightmap::IsAttrOn(float world_x, float world_z, uint8_t flag) const {
|
||||
return (GetAttrAtWorld(world_x, world_z) & flag) != 0;
|
||||
}
|
||||
|
||||
void TerrainHeightmap::BuildPatchesAndQuadtree() {
|
||||
if (m_width < 16 || m_height < 16) return;
|
||||
|
||||
m_patches.resize(PATCH_COUNT * PATCH_COUNT);
|
||||
int patch_cells_x = (m_width - 1) / PATCH_COUNT;
|
||||
int patch_cells_z = (m_height - 1) / PATCH_COUNT;
|
||||
|
||||
for (int py = 0; py < PATCH_COUNT; ++py) {
|
||||
for (int px = 0; px < PATCH_COUNT; ++px) {
|
||||
int p_idx = py * PATCH_COUNT + px;
|
||||
auto& patch = m_patches[p_idx];
|
||||
patch.patch_x = px;
|
||||
patch.patch_y = py;
|
||||
|
||||
int x0 = px * patch_cells_x;
|
||||
int x1 = (px + 1) * patch_cells_x;
|
||||
int z0 = py * patch_cells_z;
|
||||
int z1 = (py + 1) * patch_cells_z;
|
||||
|
||||
patch.aabb_min[0] = m_world_min_x + static_cast<float>(x0) * m_cell_scale;
|
||||
patch.aabb_max[0] = m_world_min_x + static_cast<float>(x1) * m_cell_scale;
|
||||
patch.aabb_min[2] = m_world_min_z + static_cast<float>(z0) * m_cell_scale;
|
||||
patch.aabb_max[2] = m_world_min_z + static_cast<float>(z1) * m_cell_scale;
|
||||
|
||||
float min_y = 1e9f, max_y = -1e9f;
|
||||
std::unordered_map<uint8_t, int> tile_freq;
|
||||
|
||||
for (int j = z0; j <= z1 && j < m_height; ++j) {
|
||||
for (int i = x0; i <= x1 && i < m_width; ++i) {
|
||||
float h = m_heights[j * m_width + i];
|
||||
if (h < min_y) min_y = h;
|
||||
if (h > max_y) max_y = h;
|
||||
|
||||
if (!m_tiles.empty() && m_tile_width > 0 && m_tile_height > 0) {
|
||||
int tx = (i * (m_tile_width - 1)) / (m_width - 1);
|
||||
int tz = (j * (m_tile_height - 1)) / (m_height - 1);
|
||||
uint8_t t = m_tiles[tz * m_tile_width + tx];
|
||||
tile_freq[t]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
patch.aabb_min[1] = min_y;
|
||||
patch.aabb_max[1] = max_y;
|
||||
|
||||
uint8_t dom_tile = 4; // default grass
|
||||
int max_c = 0;
|
||||
for (const auto& [t, c] : tile_freq) {
|
||||
if (c > max_c) {
|
||||
max_c = c;
|
||||
dom_tile = t;
|
||||
}
|
||||
}
|
||||
patch.dominant_tile = dom_tile;
|
||||
}
|
||||
}
|
||||
|
||||
m_quadtreeRoot = BuildQuadtreeNode(m_world_min_x, m_world_max_x, m_world_min_z, m_world_max_z, 0);
|
||||
}
|
||||
|
||||
std::unique_ptr<TerrainQuadNode> TerrainHeightmap::BuildQuadtreeNode(float min_x, float max_x, float min_z, float max_z, int depth) {
|
||||
auto node = std::make_unique<TerrainQuadNode>();
|
||||
node->depth = depth;
|
||||
node->aabb_min[0] = min_x;
|
||||
node->aabb_max[0] = max_x;
|
||||
node->aabb_min[2] = min_z;
|
||||
node->aabb_max[2] = max_z;
|
||||
node->aabb_min[1] = m_min_height;
|
||||
node->aabb_max[1] = m_max_height;
|
||||
|
||||
if (depth >= 3) {
|
||||
float cx = (min_x + max_x) * 0.5f;
|
||||
float cz = (min_z + max_z) * 0.5f;
|
||||
|
||||
float u = (cx - m_world_min_x) / (m_world_max_x - m_world_min_x);
|
||||
float v = (cz - m_world_min_z) / (m_world_max_z - m_world_min_z);
|
||||
int px = std::clamp(static_cast<int>(u * static_cast<float>(PATCH_COUNT)), 0, PATCH_COUNT - 1);
|
||||
int py = std::clamp(static_cast<int>(v * static_cast<float>(PATCH_COUNT)), 0, PATCH_COUNT - 1);
|
||||
int idx = py * PATCH_COUNT + px;
|
||||
|
||||
node->patch_idx = idx;
|
||||
if (idx < static_cast<int>(m_patches.size())) {
|
||||
node->aabb_min[1] = m_patches[idx].aabb_min[1];
|
||||
node->aabb_max[1] = m_patches[idx].aabb_max[1];
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
float mid_x = (min_x + max_x) * 0.5f;
|
||||
float mid_z = (min_z + max_z) * 0.5f;
|
||||
|
||||
node->children[0] = BuildQuadtreeNode(min_x, mid_x, min_z, mid_z, depth + 1);
|
||||
node->children[1] = BuildQuadtreeNode(mid_x, max_x, min_z, mid_z, depth + 1);
|
||||
node->children[2] = BuildQuadtreeNode(min_x, mid_x, mid_z, max_z, depth + 1);
|
||||
node->children[3] = BuildQuadtreeNode(mid_x, max_x, mid_z, max_z, depth + 1);
|
||||
|
||||
float cmin_y = 1e9f, cmax_y = -1e9f;
|
||||
for (int k = 0; k < 4; ++k) {
|
||||
if (node->children[k]) {
|
||||
cmin_y = std::min(cmin_y, node->children[k]->aabb_min[1]);
|
||||
cmax_y = std::max(cmax_y, node->children[k]->aabb_max[1]);
|
||||
}
|
||||
}
|
||||
node->aabb_min[1] = cmin_y;
|
||||
node->aabb_max[1] = cmax_y;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
void TerrainHeightmap::QueryQuadtree(const TerrainQuadNode* node, const float planes[6][4], const float eye[3], float max_distance, std::vector<int>& out_visible_patches) const {
|
||||
if (!node) return;
|
||||
|
||||
if (!IsBoxInFrustum(node->aabb_min, node->aabb_max, planes)) return;
|
||||
|
||||
if (node->patch_idx >= 0) {
|
||||
float cx = (node->aabb_min[0] + node->aabb_max[0]) * 0.5f;
|
||||
float cy = (node->aabb_min[1] + node->aabb_max[1]) * 0.5f;
|
||||
float cz = (node->aabb_min[2] + node->aabb_max[2]) * 0.5f;
|
||||
float dx = cx - eye[0];
|
||||
float dy = cy - eye[1];
|
||||
float dz = cz - eye[2];
|
||||
float dist_sq = dx * dx + dy * dy + dz * dz;
|
||||
|
||||
if (dist_sq <= max_distance * max_distance) {
|
||||
out_visible_patches.push_back(node->patch_idx);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (int k = 0; k < 4; ++k) {
|
||||
if (node->children[k]) {
|
||||
QueryQuadtree(node->children[k].get(), planes, eye, max_distance, out_visible_patches);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TerrainDrawCommand TerrainHeightmap::BuildDrawCommand(const float /*world*/[16], int step) const {
|
||||
TerrainDrawCommand cmd;
|
||||
if (m_heights.empty() || m_width < 2 || m_height < 2) return cmd;
|
||||
|
||||
step = std::max(1, step);
|
||||
int verts_x = (m_width - 1) / step + 1;
|
||||
int verts_z = (m_height - 1) / step + 1;
|
||||
|
||||
cmd.positions.reserve(verts_x * verts_z * 3);
|
||||
cmd.normals.reserve(verts_x * verts_z * 3);
|
||||
cmd.uv0.reserve(verts_x * verts_z * 2);
|
||||
cmd.diffuse.reserve(verts_x * verts_z);
|
||||
cmd.indices.reserve((verts_x - 1) * (verts_z - 1) * 6);
|
||||
|
||||
for (int j = 0; j < m_height; j += step) {
|
||||
for (int i = 0; i < m_width; i += step) {
|
||||
float x = m_world_min_x + static_cast<float>(i) * m_cell_scale;
|
||||
float z = m_world_min_z + static_cast<float>(j) * m_cell_scale;
|
||||
float y = m_heights[j * m_width + i];
|
||||
|
||||
cmd.positions.push_back(x);
|
||||
cmd.positions.push_back(y);
|
||||
cmd.positions.push_back(z);
|
||||
|
||||
if (!m_normals.empty()) {
|
||||
int n_idx = (j * m_width + i) * 3;
|
||||
cmd.normals.push_back(m_normals[n_idx + 0]);
|
||||
cmd.normals.push_back(m_normals[n_idx + 1]);
|
||||
cmd.normals.push_back(m_normals[n_idx + 2]);
|
||||
} else {
|
||||
cmd.normals.push_back(0.0f);
|
||||
cmd.normals.push_back(1.0f);
|
||||
cmd.normals.push_back(0.0f);
|
||||
}
|
||||
|
||||
cmd.uv0.push_back(static_cast<float>(i) * 0.125f);
|
||||
cmd.uv0.push_back(static_cast<float>(j) * 0.125f);
|
||||
cmd.diffuse.push_back(0xFFFFFFFF);
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < verts_z - 1; ++j) {
|
||||
for (int i = 0; i < verts_x - 1; ++i) {
|
||||
uint32_t i0 = j * verts_x + i;
|
||||
uint32_t i1 = j * verts_x + (i + 1);
|
||||
uint32_t i2 = (j + 1) * verts_x + i;
|
||||
uint32_t i3 = (j + 1) * verts_x + (i + 1);
|
||||
|
||||
cmd.indices.push_back(i0);
|
||||
cmd.indices.push_back(i2);
|
||||
cmd.indices.push_back(i1);
|
||||
|
||||
cmd.indices.push_back(i1);
|
||||
cmd.indices.push_back(i2);
|
||||
cmd.indices.push_back(i3);
|
||||
}
|
||||
}
|
||||
|
||||
cmd.aabb_min[0] = m_world_min_x; cmd.aabb_min[1] = m_min_height; cmd.aabb_min[2] = m_world_min_z;
|
||||
cmd.aabb_max[0] = m_world_max_x; cmd.aabb_max[1] = m_max_height; cmd.aabb_max[2] = m_world_max_z;
|
||||
return cmd;
|
||||
}
|
||||
|
||||
std::vector<TerrainDrawCommand> TerrainHeightmap::BuildCulledDraws(const float view[16], const float proj[16], float max_distance) const {
|
||||
std::vector<TerrainDrawCommand> draws;
|
||||
if (m_heights.empty() || m_patches.empty() || !m_quadtreeRoot) return draws;
|
||||
|
||||
float planes[6][4];
|
||||
ExtractFrustumPlanes(view, proj, planes);
|
||||
|
||||
float eye[3] = {
|
||||
-(view[0] * view[12] + view[1] * view[13] + view[2] * view[14]),
|
||||
-(view[4] * view[12] + view[5] * view[13] + view[6] * view[14]),
|
||||
-(view[8] * view[12] + view[9] * view[13] + view[10] * view[14])
|
||||
};
|
||||
|
||||
std::vector<int> visible_patches;
|
||||
visible_patches.reserve(64);
|
||||
QueryQuadtree(m_quadtreeRoot.get(), planes, eye, max_distance, visible_patches);
|
||||
|
||||
int patch_cells_x = (m_width - 1) / PATCH_COUNT;
|
||||
int patch_cells_z = (m_height - 1) / PATCH_COUNT;
|
||||
|
||||
for (int p_idx : visible_patches) {
|
||||
if (p_idx < 0 || p_idx >= static_cast<int>(m_patches.size())) continue;
|
||||
const auto& patch = m_patches[p_idx];
|
||||
|
||||
TerrainDrawCommand cmd;
|
||||
cmd.dominant_tile = patch.dominant_tile;
|
||||
cmd.texture_path = GetTexturePathForTile(patch.dominant_tile);
|
||||
cmd.aabb_min[0] = patch.aabb_min[0]; cmd.aabb_min[1] = patch.aabb_min[1]; cmd.aabb_min[2] = patch.aabb_min[2];
|
||||
cmd.aabb_max[0] = patch.aabb_max[0]; cmd.aabb_max[1] = patch.aabb_max[1]; cmd.aabb_max[2] = patch.aabb_max[2];
|
||||
|
||||
int x0 = patch.patch_x * patch_cells_x;
|
||||
int x1 = (patch.patch_x + 1) * patch_cells_x;
|
||||
int z0 = patch.patch_y * patch_cells_z;
|
||||
int z1 = (patch.patch_y + 1) * patch_cells_z;
|
||||
|
||||
int step = 1;
|
||||
int verts_x = (x1 - x0) / step + 1;
|
||||
int verts_z = (z1 - z0) / step + 1;
|
||||
|
||||
cmd.positions.reserve(verts_x * verts_z * 3);
|
||||
cmd.normals.reserve(verts_x * verts_z * 3);
|
||||
cmd.uv0.reserve(verts_x * verts_z * 2);
|
||||
cmd.diffuse.reserve(verts_x * verts_z);
|
||||
cmd.indices.reserve((verts_x - 1) * (verts_z - 1) * 6);
|
||||
|
||||
for (int j = z0; j <= z1; j += step) {
|
||||
for (int i = x0; i <= x1; i += step) {
|
||||
float x = m_world_min_x + static_cast<float>(i) * m_cell_scale;
|
||||
float z = m_world_min_z + static_cast<float>(j) * m_cell_scale;
|
||||
float y = m_heights[j * m_width + i];
|
||||
|
||||
cmd.positions.push_back(x);
|
||||
cmd.positions.push_back(y);
|
||||
cmd.positions.push_back(z);
|
||||
|
||||
int n_idx = (j * m_width + i) * 3;
|
||||
cmd.normals.push_back(m_normals[n_idx + 0]);
|
||||
cmd.normals.push_back(m_normals[n_idx + 1]);
|
||||
cmd.normals.push_back(m_normals[n_idx + 2]);
|
||||
|
||||
cmd.uv0.push_back(static_cast<float>(i) * 0.125f);
|
||||
cmd.uv0.push_back(static_cast<float>(j) * 0.125f);
|
||||
cmd.diffuse.push_back(0xFFFFFFFF);
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < verts_z - 1; ++j) {
|
||||
for (int i = 0; i < verts_x - 1; ++i) {
|
||||
uint32_t i0 = j * verts_x + i;
|
||||
uint32_t i1 = j * verts_x + (i + 1);
|
||||
uint32_t i2 = (j + 1) * verts_x + i;
|
||||
uint32_t i3 = (j + 1) * verts_x + (i + 1);
|
||||
|
||||
cmd.indices.push_back(i0);
|
||||
cmd.indices.push_back(i2);
|
||||
cmd.indices.push_back(i1);
|
||||
|
||||
cmd.indices.push_back(i1);
|
||||
cmd.indices.push_back(i2);
|
||||
cmd.indices.push_back(i3);
|
||||
}
|
||||
}
|
||||
|
||||
draws.push_back(std::move(cmd));
|
||||
}
|
||||
|
||||
return draws;
|
||||
}
|
||||
|
||||
} // namespace metin2::terrain
|
||||
@@ -0,0 +1,213 @@
|
||||
#include "terrain/water.h"
|
||||
#include <fstream>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
namespace metin2::terrain {
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct SWaterMapHeader {
|
||||
uint16_t magic; // 5426
|
||||
uint16_t width; // 128
|
||||
uint16_t height; // 128
|
||||
uint8_t layer_count;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
WaterManager::WaterManager() = default;
|
||||
|
||||
bool WaterManager::LoadWater(const std::string& filepath, float cell_scale, float height_scale, float height_bias,
|
||||
float world_min_x, float world_min_z) {
|
||||
std::ifstream file(filepath, std::ios::binary);
|
||||
if (!file) return false;
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
std::streamsize file_size = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<uint8_t> buffer(file_size);
|
||||
file.read(reinterpret_cast<char*>(buffer.data()), file_size);
|
||||
if (!file) return false;
|
||||
|
||||
return LoadWaterFromBuffer(buffer.data(), buffer.size(), cell_scale, height_scale, height_bias, world_min_x, world_min_z);
|
||||
}
|
||||
|
||||
bool WaterManager::LoadWaterFromBuffer(const uint8_t* data, size_t size, float cell_scale, float height_scale, float height_bias,
|
||||
float world_min_x, float world_min_z) {
|
||||
if (!data || size < sizeof(SWaterMapHeader)) return false;
|
||||
|
||||
const auto* header = reinterpret_cast<const SWaterMapHeader*>(data);
|
||||
constexpr uint16_t WATER_MAGIC = 5426;
|
||||
if (header->magic != WATER_MAGIC || header->width == 0 || header->height == 0) return false;
|
||||
|
||||
m_width = header->width;
|
||||
m_height = header->height;
|
||||
size_t grid_size = static_cast<size_t>(m_width) * m_height;
|
||||
|
||||
if (size < sizeof(SWaterMapHeader) + grid_size) return false;
|
||||
|
||||
const uint8_t* grid_ptr = data + sizeof(SWaterMapHeader);
|
||||
m_water_grid.assign(grid_ptr, grid_ptr + grid_size);
|
||||
|
||||
size_t rest_bytes = size - sizeof(SWaterMapHeader) - grid_size;
|
||||
m_water_heights.clear();
|
||||
|
||||
const uint8_t* rest_ptr = grid_ptr + grid_size;
|
||||
if (header->layer_count > 0) {
|
||||
if (rest_bytes == header->layer_count * sizeof(int32_t)) {
|
||||
m_water_heights.resize(header->layer_count);
|
||||
std::copy(reinterpret_cast<const int32_t*>(rest_ptr),
|
||||
reinterpret_cast<const int32_t*>(rest_ptr) + header->layer_count,
|
||||
m_water_heights.begin());
|
||||
} else if (rest_bytes == header->layer_count * sizeof(uint16_t)) {
|
||||
const auto* h16 = reinterpret_cast<const uint16_t*>(rest_ptr);
|
||||
for (size_t l = 0; l < header->layer_count; ++l) {
|
||||
m_water_heights.push_back(static_cast<int32_t>(h16[l]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_cell_scale = cell_scale;
|
||||
m_height_scale = height_scale;
|
||||
m_height_bias = height_bias;
|
||||
m_world_min_x = world_min_x;
|
||||
m_world_min_z = world_min_z;
|
||||
|
||||
size_t valid_cells = 0;
|
||||
for (uint8_t b : m_water_grid) {
|
||||
if (b != 0xFF && b < m_water_heights.size()) {
|
||||
valid_cells++;
|
||||
}
|
||||
}
|
||||
|
||||
m_face_count = valid_cells * 2;
|
||||
m_has_water = (valid_cells > 0);
|
||||
m_procedural = false;
|
||||
return m_has_water;
|
||||
}
|
||||
|
||||
void WaterManager::GenerateProceduralWater(float water_level, float min_x, float max_x, float min_z, float max_z, float cell_size) {
|
||||
m_procedural = true;
|
||||
m_proc_plane.water_level = water_level;
|
||||
m_proc_plane.min_x = min_x;
|
||||
m_proc_plane.max_x = max_x;
|
||||
m_proc_plane.min_z = min_z;
|
||||
m_proc_plane.max_z = max_z;
|
||||
m_proc_plane.cell_size = std::max(10.0f, cell_size);
|
||||
|
||||
int count_x = static_cast<int>(std::ceil((max_x - min_x) / m_proc_plane.cell_size));
|
||||
int count_z = static_cast<int>(std::ceil((max_z - min_z) / m_proc_plane.cell_size));
|
||||
m_face_count = static_cast<size_t>(count_x * count_z * 2);
|
||||
m_has_water = true;
|
||||
}
|
||||
|
||||
float WaterManager::GetWaterLevel(size_t layer) const {
|
||||
if (m_procedural) return m_proc_plane.water_level;
|
||||
if (layer < m_water_heights.size()) {
|
||||
return static_cast<float>(m_water_heights[layer]) * m_height_scale - m_height_bias;
|
||||
}
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
TerrainDrawCommand WaterManager::BuildDrawCommand(float anim_time) const {
|
||||
TerrainDrawCommand draw;
|
||||
if (!m_has_water) return draw;
|
||||
|
||||
float wave_offset = std::sin(anim_time * 2.0f) * 1.5f;
|
||||
|
||||
if (m_procedural) {
|
||||
int nx = static_cast<int>(std::ceil((m_proc_plane.max_x - m_proc_plane.min_x) / m_proc_plane.cell_size));
|
||||
int nz = static_cast<int>(std::ceil((m_proc_plane.max_z - m_proc_plane.min_z) / m_proc_plane.cell_size));
|
||||
int verts_x = nx + 1;
|
||||
int verts_z = nz + 1;
|
||||
|
||||
draw.positions.reserve(verts_x * verts_z * 3);
|
||||
draw.normals.reserve(verts_x * verts_z * 3);
|
||||
draw.uv0.reserve(verts_x * verts_z * 2);
|
||||
draw.diffuse.reserve(verts_x * verts_z);
|
||||
draw.indices.reserve(nx * nz * 6);
|
||||
|
||||
for (int j = 0; j <= nz; ++j) {
|
||||
float z = m_proc_plane.min_z + static_cast<float>(j) * m_proc_plane.cell_size;
|
||||
for (int i = 0; i <= nx; ++i) {
|
||||
float x = m_proc_plane.min_x + static_cast<float>(i) * m_proc_plane.cell_size;
|
||||
float y = m_proc_plane.water_level + wave_offset;
|
||||
|
||||
draw.positions.push_back(x);
|
||||
draw.positions.push_back(y);
|
||||
draw.positions.push_back(z);
|
||||
|
||||
draw.normals.push_back(0.0f);
|
||||
draw.normals.push_back(1.0f);
|
||||
draw.normals.push_back(0.0f);
|
||||
|
||||
draw.uv0.push_back(x * 0.005f + anim_time * 0.02f);
|
||||
draw.uv0.push_back(z * 0.005f + anim_time * 0.02f);
|
||||
draw.diffuse.push_back(m_water_color);
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = 0; j < nz; ++j) {
|
||||
for (int i = 0; i < nx; ++i) {
|
||||
uint32_t i0 = j * verts_x + i;
|
||||
uint32_t i1 = j * verts_x + (i + 1);
|
||||
uint32_t i2 = (j + 1) * verts_x + i;
|
||||
uint32_t i3 = (j + 1) * verts_x + (i + 1);
|
||||
|
||||
draw.indices.push_back(i0);
|
||||
draw.indices.push_back(i2);
|
||||
draw.indices.push_back(i1);
|
||||
|
||||
draw.indices.push_back(i1);
|
||||
draw.indices.push_back(i2);
|
||||
draw.indices.push_back(i3);
|
||||
}
|
||||
}
|
||||
return draw;
|
||||
}
|
||||
|
||||
const int step = 2;
|
||||
for (int j = 0; j < m_height; j += step) {
|
||||
for (int i = 0; i < m_width; i += step) {
|
||||
uint8_t layer = m_water_grid[j * m_width + i];
|
||||
if (layer == 0xFF || layer >= m_water_heights.size()) continue;
|
||||
|
||||
float base_y = static_cast<float>(m_water_heights[layer]) * m_height_scale - m_height_bias + wave_offset;
|
||||
float x0 = m_world_min_x + static_cast<float>(i) * m_cell_scale;
|
||||
float x1 = x0 + m_cell_scale * static_cast<float>(step);
|
||||
float z0 = m_world_min_z + static_cast<float>(j) * m_cell_scale;
|
||||
float z1 = z0 + m_cell_scale * static_cast<float>(step);
|
||||
|
||||
uint32_t start_idx = static_cast<uint32_t>(draw.positions.size() / 3);
|
||||
|
||||
draw.positions.push_back(x0); draw.positions.push_back(base_y); draw.positions.push_back(z0);
|
||||
draw.positions.push_back(x1); draw.positions.push_back(base_y); draw.positions.push_back(z0);
|
||||
draw.positions.push_back(x0); draw.positions.push_back(base_y); draw.positions.push_back(z1);
|
||||
draw.positions.push_back(x1); draw.positions.push_back(base_y); draw.positions.push_back(z1);
|
||||
|
||||
for (int k = 0; k < 4; ++k) {
|
||||
draw.normals.push_back(0.0f);
|
||||
draw.normals.push_back(1.0f);
|
||||
draw.normals.push_back(0.0f);
|
||||
draw.diffuse.push_back(m_water_color);
|
||||
}
|
||||
|
||||
draw.uv0.push_back(x0 * 0.005f); draw.uv0.push_back(z0 * 0.005f);
|
||||
draw.uv0.push_back(x1 * 0.005f); draw.uv0.push_back(z0 * 0.005f);
|
||||
draw.uv0.push_back(x0 * 0.005f); draw.uv0.push_back(z1 * 0.005f);
|
||||
draw.uv0.push_back(x1 * 0.005f); draw.uv0.push_back(z1 * 0.005f);
|
||||
|
||||
draw.indices.push_back(start_idx + 0);
|
||||
draw.indices.push_back(start_idx + 2);
|
||||
draw.indices.push_back(start_idx + 1);
|
||||
|
||||
draw.indices.push_back(start_idx + 1);
|
||||
draw.indices.push_back(start_idx + 2);
|
||||
draw.indices.push_back(start_idx + 3);
|
||||
}
|
||||
}
|
||||
|
||||
return draw;
|
||||
}
|
||||
|
||||
} // namespace metin2::terrain
|
||||
Reference in New Issue
Block a user