444 lines
15 KiB
C++
444 lines
15 KiB
C++
// Terrain and object geometry of the simulated level (see sim_geo.h). Objects are indexed on an
|
||
// 8 m grid in XZ, with each cell's highest triangle and highest ground; a ray walks the cells it
|
||
// crosses, tests their triangles and marches the terrain only where it dips low enough.
|
||
#include "sim_geo.h"
|
||
|
||
#include <algorithm>
|
||
#include <cmath>
|
||
#include <cstring>
|
||
#include <fstream>
|
||
#include <vector>
|
||
|
||
namespace wto_sim {
|
||
|
||
namespace {
|
||
|
||
const char GEO_MAGIC[8] = { 'W', 'T', 'O', 'G', 'E', 'O', '\0', '\x01' };
|
||
const uint32_t GEO_MAX_VERSION = 3;
|
||
const float CELL_M = 8.0f;
|
||
const float BURIED_M = 0.3f;
|
||
|
||
#pragma pack(push, 1)
|
||
struct GeoHeader {
|
||
char magic[8];
|
||
uint32_t version;
|
||
uint32_t flags;
|
||
uint8_t everSha1[20];
|
||
float waterLevel;
|
||
float boundsMin[2];
|
||
float boundsMax[2];
|
||
uint32_t hmWidth;
|
||
uint32_t hmHeight;
|
||
float hmCellSize;
|
||
float hmOrigin[2];
|
||
uint32_t obsVertCount;
|
||
uint32_t obsTriCount;
|
||
};
|
||
#pragma pack(pop)
|
||
|
||
// One triangle ready for the ray test: a corner, two edges, and the group it belongs to.
|
||
struct Tri {
|
||
float v0[3];
|
||
float e1[3];
|
||
float e2[3];
|
||
int kind;
|
||
};
|
||
|
||
// Obstacle class (WtoObstacleClass) to ray group.
|
||
const int KIND_OF_CLASS[8] = { RAY_HARD, RAY_BRIDGE, RAY_CRUSHABLE, RAY_TREE, RAY_TREE, RAY_HARD, RAY_PLATFORM, RAY_PLATFORM };
|
||
|
||
struct Geo {
|
||
bool loaded = false;
|
||
float water = -1.0e6f;
|
||
uint32_t hmW = 0;
|
||
uint32_t hmH = 0;
|
||
float hmCell = 1.0f;
|
||
float hmOrigin[2] = { 0.0f, 0.0f };
|
||
std::vector<float> heights;
|
||
std::vector<Tri> tris;
|
||
// Grid over the objects: cell (c, r) holds items[offsets[i] .. offsets[i + 1]).
|
||
float gridOrigin[2] = { 0.0f, 0.0f };
|
||
int gridCols = 0;
|
||
int gridRows = 0;
|
||
std::vector<uint32_t> offsets;
|
||
std::vector<uint32_t> items;
|
||
std::vector<float> cellTop;
|
||
// Highest terrain sample in each cell (with its border), so a ray above it skips the march.
|
||
std::vector<float> cellGround;
|
||
// Ray id stamped on each triangle already tested by the current ray.
|
||
std::vector<uint32_t> stamp;
|
||
uint32_t rayId = 0;
|
||
};
|
||
|
||
Geo g_geo;
|
||
|
||
float height_at(const Geo & g, float x, float z)
|
||
{
|
||
if (g.hmW < 2 || g.hmH < 2)
|
||
return 0.0f;
|
||
const float fx = std::min(float(g.hmW) - 1.001f, std::max(0.0f, (x - g.hmOrigin[0]) / g.hmCell));
|
||
const float fz = std::min(float(g.hmH) - 1.001f, std::max(0.0f, (z - g.hmOrigin[1]) / g.hmCell));
|
||
const uint32_t x0 = uint32_t(fx);
|
||
const uint32_t z0 = uint32_t(fz);
|
||
const float tx = fx - float(x0);
|
||
const float tz = fz - float(z0);
|
||
const float * row0 = &g.heights[size_t(z0) * g.hmW + x0];
|
||
const float * row1 = row0 + g.hmW;
|
||
const float a = row0[0] + (row0[1] - row0[0]) * tx;
|
||
const float b = row1[0] + (row1[1] - row1[0]) * tx;
|
||
return a + (b - a) * tz;
|
||
}
|
||
|
||
void sub(const float a[3], const float b[3], float out[3])
|
||
{
|
||
out[0] = a[0] - b[0];
|
||
out[1] = a[1] - b[1];
|
||
out[2] = a[2] - b[2];
|
||
}
|
||
|
||
void cross(const float a[3], const float b[3], float out[3])
|
||
{
|
||
out[0] = a[1] * b[2] - a[2] * b[1];
|
||
out[1] = a[2] * b[0] - a[0] * b[2];
|
||
out[2] = a[0] * b[1] - a[1] * b[0];
|
||
}
|
||
|
||
float dot(const float a[3], const float b[3])
|
||
{
|
||
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
||
}
|
||
|
||
bool read_bytes(std::ifstream & in, void * out, size_t size)
|
||
{
|
||
in.read(static_cast<char *>(out), std::streamsize(size));
|
||
return bool(in);
|
||
}
|
||
|
||
bool buried(const Geo & g, const float * v)
|
||
{
|
||
return v[1] < height_at(g, v[0], v[2]) - BURIED_M;
|
||
}
|
||
|
||
// Keeps the triangles that are not wholly under the terrain, as ready-to-test records.
|
||
void keep_triangles(Geo & g, const std::vector<float> & verts, const std::vector<uint8_t> & raw, uint32_t count)
|
||
{
|
||
g.tris.clear();
|
||
g.tris.reserve(count);
|
||
const uint32_t vertCount = uint32_t(verts.size() / 3);
|
||
for (uint32_t i = 0; i < count; i++) {
|
||
const uint8_t * rec = &raw[size_t(i) * 13];
|
||
uint32_t idx[3];
|
||
std::memcpy(idx, rec, sizeof(idx));
|
||
if (idx[0] >= vertCount || idx[1] >= vertCount || idx[2] >= vertCount)
|
||
continue;
|
||
const float * a = &verts[size_t(idx[0]) * 3];
|
||
const float * b = &verts[size_t(idx[1]) * 3];
|
||
const float * c = &verts[size_t(idx[2]) * 3];
|
||
if (buried(g, a) && buried(g, b) && buried(g, c))
|
||
continue;
|
||
Tri t;
|
||
std::memcpy(t.v0, a, sizeof(t.v0));
|
||
sub(b, a, t.e1);
|
||
sub(c, a, t.e2);
|
||
t.kind = KIND_OF_CLASS[rec[12] & 7];
|
||
g.tris.push_back(t);
|
||
}
|
||
}
|
||
|
||
void tri_bounds(const Tri & t, float lo[3], float hi[3])
|
||
{
|
||
for (int k = 0; k < 3; k++) {
|
||
const float b = t.v0[k] + t.e1[k];
|
||
const float c = t.v0[k] + t.e2[k];
|
||
lo[k] = std::min(t.v0[k], std::min(b, c));
|
||
hi[k] = std::max(t.v0[k], std::max(b, c));
|
||
}
|
||
}
|
||
|
||
void cell_span(const Geo & g, const float lo[3], const float hi[3], int & c0, int & c1, int & r0, int & r1)
|
||
{
|
||
c0 = std::clamp(int(std::floor((lo[0] - g.gridOrigin[0]) / CELL_M)), 0, g.gridCols - 1);
|
||
c1 = std::clamp(int(std::floor((hi[0] - g.gridOrigin[0]) / CELL_M)), 0, g.gridCols - 1);
|
||
r0 = std::clamp(int(std::floor((lo[2] - g.gridOrigin[1]) / CELL_M)), 0, g.gridRows - 1);
|
||
r1 = std::clamp(int(std::floor((hi[2] - g.gridOrigin[1]) / CELL_M)), 0, g.gridRows - 1);
|
||
}
|
||
|
||
// Two passes over the triangles: count per cell, then fill the cells' item lists.
|
||
void build_grid(Geo & g, const float boundsMin[2], const float boundsMax[2])
|
||
{
|
||
g.gridOrigin[0] = boundsMin[0];
|
||
g.gridOrigin[1] = boundsMin[1];
|
||
g.gridCols = std::max(1, int(std::ceil((boundsMax[0] - boundsMin[0]) / CELL_M)));
|
||
g.gridRows = std::max(1, int(std::ceil((boundsMax[1] - boundsMin[1]) / CELL_M)));
|
||
const size_t cells = size_t(g.gridCols) * size_t(g.gridRows);
|
||
std::vector<uint32_t> counts(cells, 0);
|
||
g.cellTop.assign(cells, -1.0e9f);
|
||
float lo[3], hi[3];
|
||
int c0, c1, r0, r1;
|
||
for (const Tri & t : g.tris) {
|
||
tri_bounds(t, lo, hi);
|
||
cell_span(g, lo, hi, c0, c1, r0, r1);
|
||
for (int r = r0; r <= r1; r++) {
|
||
for (int c = c0; c <= c1; c++) {
|
||
const size_t i = size_t(r) * g.gridCols + c;
|
||
counts[i]++;
|
||
g.cellTop[i] = std::max(g.cellTop[i], hi[1]);
|
||
}
|
||
}
|
||
}
|
||
g.offsets.assign(cells + 1, 0);
|
||
for (size_t i = 0; i < cells; i++)
|
||
g.offsets[i + 1] = g.offsets[i] + counts[i];
|
||
g.items.assign(g.offsets[cells], 0);
|
||
std::fill(counts.begin(), counts.end(), 0);
|
||
for (uint32_t ti = 0; ti < uint32_t(g.tris.size()); ti++) {
|
||
tri_bounds(g.tris[ti], lo, hi);
|
||
cell_span(g, lo, hi, c0, c1, r0, r1);
|
||
for (int r = r0; r <= r1; r++) {
|
||
for (int c = c0; c <= c1; c++) {
|
||
const size_t i = size_t(r) * g.gridCols + c;
|
||
g.items[g.offsets[i] + counts[i]++] = ti;
|
||
}
|
||
}
|
||
}
|
||
g.stamp.assign(g.tris.size(), 0);
|
||
g.rayId = 0;
|
||
}
|
||
|
||
void build_ground_tops(Geo & g)
|
||
{
|
||
g.cellGround.assign(size_t(g.gridCols) * g.gridRows, -1.0e9f);
|
||
for (int r = 0; r < g.gridRows; r++) {
|
||
for (int c = 0; c < g.gridCols; c++) {
|
||
const float x0 = g.gridOrigin[0] + float(c) * CELL_M;
|
||
const float z0 = g.gridOrigin[1] + float(r) * CELL_M;
|
||
float top = -1.0e9f;
|
||
for (float z = z0 - g.hmCell; z <= z0 + CELL_M + g.hmCell; z += g.hmCell)
|
||
for (float x = x0 - g.hmCell; x <= x0 + CELL_M + g.hmCell; x += g.hmCell)
|
||
top = std::max(top, height_at(g, x, z));
|
||
g.cellGround[size_t(r) * g.gridCols + c] = top;
|
||
}
|
||
}
|
||
}
|
||
|
||
// Möller–Trumbore, both faces. Returns the distance along the ray, or -1.
|
||
float ray_tri(const Tri & t, const float p[3], const float dir[3])
|
||
{
|
||
float pv[3], tv[3], qv[3];
|
||
cross(dir, t.e2, pv);
|
||
const float det = dot(t.e1, pv);
|
||
if (std::fabs(det) < 1.0e-9f)
|
||
return -1.0f;
|
||
const float inv = 1.0f / det;
|
||
sub(p, t.v0, tv);
|
||
const float u = dot(tv, pv) * inv;
|
||
if (u < 0.0f || u > 1.0f)
|
||
return -1.0f;
|
||
cross(tv, t.e1, qv);
|
||
const float v = dot(dir, qv) * inv;
|
||
if (v < 0.0f || u + v > 1.0f)
|
||
return -1.0f;
|
||
return dot(t.e2, qv) * inv;
|
||
}
|
||
|
||
void tri_normal(const Tri & t, const float dir[3], float out[3])
|
||
{
|
||
cross(t.e1, t.e2, out);
|
||
const float len = std::sqrt(dot(out, out));
|
||
const float s = (len > 0.0f ? 1.0f / len : 0.0f) * (dot(out, dir) > 0.0f ? -1.0f : 1.0f);
|
||
out[0] *= s;
|
||
out[1] *= s;
|
||
out[2] *= s;
|
||
}
|
||
|
||
// Tests the cell's triangles of the selected groups; keeps the nearest hit within maxT.
|
||
void test_cell(Geo & g, size_t cell, const float p[3], const float dir[3], float maxT, int mask, RayHit & best)
|
||
{
|
||
for (uint32_t k = g.offsets[cell]; k < g.offsets[cell + 1]; k++) {
|
||
const uint32_t ti = g.items[k];
|
||
if (g.stamp[ti] == g.rayId)
|
||
continue;
|
||
g.stamp[ti] = g.rayId;
|
||
const Tri & t = g.tris[ti];
|
||
if ((t.kind & mask) == 0)
|
||
continue;
|
||
const float d = ray_tri(t, p, dir);
|
||
if (d >= 0.0f && d <= maxT && (best.dist < 0.0f || d < best.dist)) {
|
||
best.dist = d;
|
||
best.kind = t.kind;
|
||
tri_normal(t, dir, best.normal);
|
||
}
|
||
}
|
||
}
|
||
|
||
void terrain_normal(const Geo & g, float x, float z, float out[3])
|
||
{
|
||
const float e = g.hmCell;
|
||
const float dx = height_at(g, x + e, z) - height_at(g, x - e, z);
|
||
const float dz = height_at(g, x, z + e) - height_at(g, x, z - e);
|
||
out[0] = -dx;
|
||
out[1] = 2.0f * e;
|
||
out[2] = -dz;
|
||
const float len = std::sqrt(dot(out, out));
|
||
out[0] /= len;
|
||
out[1] /= len;
|
||
out[2] /= len;
|
||
}
|
||
|
||
// Height of the ray above the terrain at distance t.
|
||
float above_ground(const Geo & g, const float p[3], const float dir[3], float t)
|
||
{
|
||
return p[1] + dir[1] * t - height_at(g, p[0] + dir[0] * t, p[2] + dir[2] * t);
|
||
}
|
||
|
||
// Terrain crossing of the ray within [t0, t1], marched at half the heightmap spacing and bisected.
|
||
bool terrain_in(const Geo & g, const float p[3], const float dir[3], float t0, float t1, RayHit & best)
|
||
{
|
||
const float step = g.hmCell * 0.5f;
|
||
float prevT = t0;
|
||
if (above_ground(g, p, dir, t0) <= 0.0f) {
|
||
best.dist = t0;
|
||
} else {
|
||
bool crossed = false;
|
||
for (float t = std::min(t0 + step, t1); prevT < t1; t = std::min(t + step, t1)) {
|
||
if (above_ground(g, p, dir, t) <= 0.0f) {
|
||
float lo = prevT;
|
||
float hi = t;
|
||
for (int i = 0; i < 8; i++) {
|
||
const float mid = 0.5f * (lo + hi);
|
||
if (above_ground(g, p, dir, mid) > 0.0f)
|
||
lo = mid;
|
||
else
|
||
hi = mid;
|
||
}
|
||
best.dist = hi;
|
||
crossed = true;
|
||
break;
|
||
}
|
||
prevT = t;
|
||
}
|
||
if (!crossed)
|
||
return false;
|
||
}
|
||
best.kind = 0;
|
||
terrain_normal(g, p[0] + dir[0] * best.dist, p[2] + dir[2] * best.dist, best.normal);
|
||
return true;
|
||
}
|
||
|
||
// Walks the grid cells under the ray (2D DDA in XZ). In each cell the terrain is marched only when
|
||
// the ray dips below the cell's highest ground, the objects tested only when it dips below the
|
||
// cell's highest triangle. Stops at the first cell that holds a hit.
|
||
void ray_walk(Geo & g, const float p[3], const float dir[3], float len, int mask, RayHit & best)
|
||
{
|
||
if (++g.rayId == 0) {
|
||
std::fill(g.stamp.begin(), g.stamp.end(), 0);
|
||
g.rayId = 1;
|
||
}
|
||
const bool terrain = (mask & RAY_TERRAIN) != 0;
|
||
const int objects = mask & ~RAY_TERRAIN;
|
||
const float gx = (p[0] - g.gridOrigin[0]) / CELL_M;
|
||
const float gz = (p[2] - g.gridOrigin[1]) / CELL_M;
|
||
int c = int(std::floor(gx));
|
||
int r = int(std::floor(gz));
|
||
const int stepC = dir[0] > 0.0f ? 1 : -1;
|
||
const int stepR = dir[2] > 0.0f ? 1 : -1;
|
||
const float big = 1.0e30f;
|
||
const float dtC = std::fabs(dir[0]) > 1.0e-7f ? CELL_M / std::fabs(dir[0]) : big;
|
||
const float dtR = std::fabs(dir[2]) > 1.0e-7f ? CELL_M / std::fabs(dir[2]) : big;
|
||
float nextC = std::fabs(dir[0]) > 1.0e-7f ? ((stepC > 0 ? float(c + 1) - gx : gx - float(c)) * dtC) : big;
|
||
float nextR = std::fabs(dir[2]) > 1.0e-7f ? ((stepR > 0 ? float(r + 1) - gz : gz - float(r)) * dtR) : big;
|
||
float t0 = 0.0f;
|
||
while (t0 <= len) {
|
||
const float t1 = std::min(len, std::min(nextC, nextR));
|
||
const bool inside = c >= 0 && r >= 0 && c < g.gridCols && r < g.gridRows;
|
||
const size_t cell = inside ? size_t(r) * g.gridCols + c : 0;
|
||
const float lowY = std::min(p[1] + dir[1] * t0, p[1] + dir[1] * t1);
|
||
if (objects != 0 && inside && lowY <= g.cellTop[cell])
|
||
test_cell(g, cell, p, dir, len, objects, best);
|
||
const float groundEnd = best.dist >= 0.0f ? std::min(t1, best.dist) : t1;
|
||
if (terrain && (!inside || lowY <= g.cellGround[cell]) && terrain_in(g, p, dir, t0, groundEnd, best))
|
||
return;
|
||
if (best.dist >= 0.0f && best.dist <= t1)
|
||
return;
|
||
if (t1 >= len)
|
||
return;
|
||
if (nextC < nextR) {
|
||
c += stepC;
|
||
t0 = nextC;
|
||
nextC += dtC;
|
||
} else {
|
||
r += stepR;
|
||
t0 = nextR;
|
||
nextR += dtR;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
bool geo_load(const std::string & path, std::string & error)
|
||
{
|
||
Geo g;
|
||
std::ifstream in(path, std::ios::binary);
|
||
GeoHeader h;
|
||
if (!in || !read_bytes(in, &h, sizeof(h))) {
|
||
error = "cannot read " + path;
|
||
return false;
|
||
}
|
||
if (std::memcmp(h.magic, GEO_MAGIC, sizeof(GEO_MAGIC)) != 0 || h.version < 1 || h.version > GEO_MAX_VERSION) {
|
||
error = "not a .wtogeo file of a known version";
|
||
return false;
|
||
}
|
||
g.water = h.waterLevel;
|
||
g.hmW = h.hmWidth;
|
||
g.hmH = h.hmHeight;
|
||
g.hmCell = h.hmCellSize > 0.0f ? h.hmCellSize : 1.0f;
|
||
g.hmOrigin[0] = h.hmOrigin[0];
|
||
g.hmOrigin[1] = h.hmOrigin[1];
|
||
g.heights.resize(size_t(h.hmWidth) * h.hmHeight);
|
||
std::vector<float> verts(size_t(h.obsVertCount) * 3);
|
||
std::vector<uint8_t> raw(size_t(h.obsTriCount) * 13);
|
||
if (!read_bytes(in, g.heights.data(), g.heights.size() * sizeof(float)) ||
|
||
!read_bytes(in, verts.data(), verts.size() * sizeof(float)) || !read_bytes(in, raw.data(), raw.size())) {
|
||
error = "file is truncated";
|
||
return false;
|
||
}
|
||
keep_triangles(g, verts, raw, h.obsTriCount);
|
||
build_grid(g, h.boundsMin, h.boundsMax);
|
||
build_ground_tops(g);
|
||
g.loaded = true;
|
||
g_geo = std::move(g);
|
||
return true;
|
||
}
|
||
|
||
bool geo_loaded()
|
||
{
|
||
return g_geo.loaded;
|
||
}
|
||
|
||
float geo_height(float x, float z)
|
||
{
|
||
return height_at(g_geo, x, z);
|
||
}
|
||
|
||
float geo_water_level()
|
||
{
|
||
return g_geo.water;
|
||
}
|
||
|
||
RayHit geo_ray(const float p[3], const float dir[3], float len, int mask)
|
||
{
|
||
RayHit best;
|
||
if (!g_geo.loaded || !(len > 0.0f))
|
||
return best;
|
||
ray_walk(g_geo, p, dir, len, mask, best);
|
||
return best;
|
||
}
|
||
|
||
uint32_t geo_triangle_count()
|
||
{
|
||
return uint32_t(g_geo.tris.size());
|
||
}
|
||
|
||
}
|