12bool pointInTriangle2D(
const glm::vec2&
p,
const glm::vec2&
a,
const glm::vec2&
b,
const glm::vec2&
c,
float& w0,
13 float& w1,
float& w2) {
14 const glm::vec2 v0 =
b -
a;
15 const glm::vec2 v1 =
c -
a;
16 const glm::vec2 v2 =
p -
a;
17 const float den = v0.x * v1.y - v1.x * v0.y;
18 if (std::abs(den) < 1e-12f)
return false;
19 const float inv = 1.f / den;
20 w1 = (v2.x * v1.y - v1.x * v2.y) * inv;
21 w2 = (v0.x * v2.y - v2.x * v0.y) * inv;
23 return w0 >= -1e-4f && w1 >= -1e-4f && w2 >= -1e-4f;
29 if (field.height.empty() || field.resolution < 2)
return;
30 const int res = field.resolution;
31 const float cell = field.worldSize /
static_cast<float>(res - 1);
32 field.normals.assign(field.height.
size(), glm::vec3(0.f, 1.f, 0.f));
33 for (
int z = 0;
z < res; ++
z) {
34 for (
int x = 0;
x < res; ++
x) {
35 const int x0 = std::max(
x - 1, 0);
36 const int x1 = std::min(
x + 1, res - 1);
37 const int z0 = std::max(
z - 1, 0);
38 const int z1 = std::min(
z + 1, res - 1);
39 const float hx0 = field.height[
static_cast<size_t>(x0 + res *
z)];
40 const float hx1 = field.height[
static_cast<size_t>(x1 + res *
z)];
41 const float hz0 = field.height[
static_cast<size_t>(
x + res * z0)];
42 const float hz1 = field.height[
static_cast<size_t>(
x + res * z1)];
43 const glm::vec3
dx(2.f *
cell, hx1 - hx0, 0.f);
44 const glm::vec3
dz(0.f, hz1 - hz0, 2.f *
cell);
45 glm::vec3
n = glm::cross(
dz,
dx);
46 const float len = glm::length(
n);
50 n = glm::vec3(0.f, 1.f, 0.f);
51 field.normals[
static_cast<size_t>(
x + res *
z)] =
n;
57 std::span<const std::uint32_t>
indices,
const glm::vec3& originMin,
58 float worldSize,
int resolution,
float baseHeight) {
63 if (!(worldSize > 0.f)) {
65 "surface capture worldSize must be positive",
"worldSize", {},
70 "triangle index count must be a multiple of 3",
"indices", {},
76 "triangle index out of range",
"indices", {},
"gpuagents"));
80 out.initFlat(originMin, worldSize, resolution, baseHeight);
81 const float cell = worldSize /
static_cast<float>(resolution - 1);
83 for (
size_t t = 0;
t + 2 <
indices.size();
t += 3) {
87 const glm::vec2
a(A.x, A.z),
b(B.x, B.z),
c(C.x, C.z);
88 const float minX = std::min({
a.x,
b.x,
c.x});
89 const float maxX = std::max({
a.x,
b.x,
c.x});
90 const float minZ = std::min({
a.y,
b.y,
c.y});
91 const float maxZ = std::max({
a.y,
b.y,
c.y});
92 const int x0 = std::clamp(
static_cast<int>(std::floor((minX - originMin.x) /
cell)), 0, resolution - 1);
93 const int x1 = std::clamp(
static_cast<int>(std::ceil((maxX - originMin.x) /
cell)), 0, resolution - 1);
94 const int z0 = std::clamp(
static_cast<int>(std::floor((minZ - originMin.z) /
cell)), 0, resolution - 1);
95 const int z1 = std::clamp(
static_cast<int>(std::ceil((maxZ - originMin.z) /
cell)), 0, resolution - 1);
97 for (
int z = z0;
z <= z1; ++
z) {
98 for (
int x = x0;
x <= x1; ++
x) {
99 const glm::vec2
p(originMin.x +
cell *
static_cast<float>(
x),
100 originMin.z +
cell *
static_cast<float>(
z));
101 float w0 = 0.f, w1 = 0.f, w2 = 0.f;
102 if (!pointInTriangle2D(
p,
a,
b,
c, w0, w1, w2))
continue;
103 const float h = w0 * A.y + w1 * B.y + w2 * C.y;
104 auto& dest =
out.height[
static_cast<size_t>(
x + resolution *
z)];
105 dest = std::max(dest,
h);
125 const size_t n =
static_cast<size_t>(res) *
static_cast<size_t>(res);
127 normals.assign(
n, glm::vec3(0.f, 1.f, 0.f));
135int SurfaceField::index(
int x,
int z)
const {
return x +
resolution *
z; }
140 if (
height.empty())
return 0.f;
142 const float fx =
uv.x *
static_cast<float>(
resolution - 1);
143 const float fz =
uv.y *
static_cast<float>(
resolution - 1);
144 const int x0 = std::clamp(
static_cast<int>(std::floor(fx)), 0,
resolution - 1);
145 const int z0 = std::clamp(
static_cast<int>(std::floor(fz)), 0,
resolution - 1);
146 const int x1 = std::min(x0 + 1,
resolution - 1);
147 const int z1 = std::min(z0 + 1,
resolution - 1);
148 const float tx = fx -
static_cast<float>(x0);
149 const float tz = fz -
static_cast<float>(z0);
150 const float h00 =
height[
static_cast<size_t>(
index(x0, z0))];
151 const float h10 =
height[
static_cast<size_t>(
index(x1, z0))];
152 const float h01 =
height[
static_cast<size_t>(
index(x0, z1))];
153 const float h11 =
height[
static_cast<size_t>(
index(x1, z1))];
154 return glm::mix(glm::mix(h00, h10, tx), glm::mix(h01, h11, tx), tz);
158 if (
normals.empty())
return glm::vec3(0.f, 1.f, 0.f);
165glm::vec4 SurfaceField::sampleLifeTexel(
float u,
float v)
const {
166 const float fx =
u *
static_cast<float>(
resolution - 1);
167 const float fz =
v *
static_cast<float>(
resolution - 1);
168 const int x0 = std::clamp(
static_cast<int>(std::floor(fx)), 0,
resolution - 1);
169 const int z0 = std::clamp(
static_cast<int>(std::floor(fz)), 0,
resolution - 1);
170 const int x1 = std::min(x0 + 1,
resolution - 1);
171 const int z1 = std::min(z0 + 1,
resolution - 1);
172 const float tx = fx -
static_cast<float>(x0);
173 const float tz = fz -
static_cast<float>(z0);
175 return glm::mix(glm::mix(
at(x0, z0),
at(x1, z0), tx), glm::mix(
at(x0, z1),
at(x1, z1), tx), tz);
179 if (
lifeField.empty())
return glm::vec4(0.f);
181 return sampleLifeTexel(std::clamp(
uv.x, 0.f, 1.f), std::clamp(
uv.y, 0.f, 1.f));
187 const std::vector<glm::vec3>& deposits,
float depositStrength) {
190 const float decay = std::exp(-decayRate *
dt);
191 const float freshDecay = halfLife > 1e-4f ? std::exp(-std::log(2.f) *
dt / halfLife) : 0.f;
195 for (
int z = 0;
z < res; ++
z) {
196 for (
int x = 0;
x < res; ++
x) {
197 const size_t i =
static_cast<size_t>(
index(
x,
z));
201 if (diffusion > 0.f) {
204 for (
int dz = -1;
dz <= 1; ++
dz) {
205 for (
int dx = -1;
dx <= 1; ++
dx) {
206 if (
dx == 0 &&
dz == 0)
continue;
207 const int nx =
x +
dx;
208 const int nz =
z +
dz;
209 if (!inBounds(
nx,
nz))
continue;
215 const glm::vec4 avg = sum /
static_cast<float>(
count);
216 c.r = glm::mix(
c.r, avg.r, diffusion);
217 c.g = glm::mix(
c.g, avg.g, diffusion * 0.5f);
218 c.b = glm::mix(
c.b, avg.b, diffusion * 0.25f);
225 for (
const glm::vec3&
p : deposits) {
227 if (
uv.x < 0.f ||
uv.y < 0.f ||
uv.x >= 1.f ||
uv.y >= 1.f)
continue;
228 const int x = std::clamp(
static_cast<int>(
uv.x * res), 0, res - 1);
229 const int z = std::clamp(
static_cast<int>(
uv.y * res), 0, res - 1);
230 auto&
c = next[
static_cast<size_t>(
index(
x,
z))];
231 c.r = std::min(
c.r + depositStrength, 1.f);
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Stable, structured diagnostics shared by engine modules.
std::vector< std::uint32_t > indices
std::vector< float > positions
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
static Result< void > captureFromTriangles(SurfaceField &out, std::span< const glm::vec3 > positions, std::span< const std::uint32_t > indices, const glm::vec3 &originMin, float worldSize, int resolution, float baseHeight=0.f)
Bake triangle positions into out over the given XZ domain.
static void rebuildNormals(SurfaceField &field)
Rebuild normals from the current height grid (central differences).
2.5D surface + life-field textures for Life Network.
std::vector< glm::vec4 > lifeField
Life field RGBA: R=trail, G=nutrient, B=danger, A=freshness.
glm::vec3 sampleNormal(float x, float z) const
Bilinear sample of surface normal.
glm::vec3 project(const glm::vec3 &p) const
Project a world point onto the surface (Y from height).
void stepLife(float dt, float decayRate, float halfLife, float diffusion, const std::vector< glm::vec3 > &deposits, float depositStrength)
Advance trail deposit / decay / diffusion / danger for one fixed step.
float sampleHeight(float x, float z) const
Bilinear sample of height at world XZ.
std::vector< glm::vec3 > normals
Packed normals as vec3 per texel.
std::vector< float > height
Height samples, resolution^2, row-major X then Z.
void initFlat(const glm::vec3 &originMin, float size, int res, float planeY=0.f)
Allocate grids and fill a flat plane at y = planeY.
glm::vec2 worldToUv(const glm::vec3 &p) const
World XZ → texel UV in [0,1].
glm::vec4 sampleLife(float x, float z) const
Bilinear sample of life RGBA.
size_t size() const
Returns the size of .