Repository navigation
Expand file tree
/
Copy pathObject.cpp
More file actions
453 lines (409 loc) · 19.6 KB
/
Copy pathObject.cpp
File metadata and controls
453 lines (409 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "Object.hpp"
#include "Light.hpp"
#include "TrigLUT.hpp"
#include <cmath>
#include <cstdlib>
#include <new>
#include <algorithm>
#if defined(ESP_PLATFORM)
#include "esp_heap_caps.h"
#endif
namespace Renderer
{
Object::Object()
{
}
#if JET_MESH_INSTANCING
Object::InstanceTransform Object::InstanceTransform::rotated(const Vector3& degrees, const Vector3& position) {
// Poses can be authored before the first Scene initializes the tables.
if (sin_table[90] == 0) initializeTrigTables();
constexpr float unit = 1.0f / FIXED_POINT_SCALE;
const float cx=lookupCosI(degrees.x)*unit, sx=lookupSinI(degrees.x)*unit;
const float cy=lookupCosI(degrees.y)*unit, sy=lookupSinI(degrees.y)*unit;
const float cz=lookupCosI(degrees.z)*unit, sz=lookupSinI(degrees.z)*unit;
InstanceTransform t;
const float m[9] = {cz*cy, cz*sy*sx-sz*cx, cz*sy*cx+sz*sx,
sz*cy, sz*sy*sx+cz*cx, sz*sy*cx-cz*sx,
-sy, cy*sx, cy*cx};
std::copy(m,m+9,t.basis); t.position=position;
return t;
}
Object::SharedMesh Object::freezeMesh(Object&& authored) {
if (!authored.instances.empty()) return {}; // Flat prototypes avoid cycles/recursive draws.
authored.calculateBoundingBox();
authored.vertices.shrink_to_fit(); authored.triangles.shrink_to_fit();
authored.cachePositions();
return std::make_shared<const Object>(std::move(authored));
}
bool Object::addInstance(SharedMesh mesh, const InstanceTransform& transform, Material* materialOverride,
TriangleMaterials triangleMaterials) {
if (!mesh || mesh.get()==this || !mesh->instances.empty()) return false;
if (triangleMaterials && triangleMaterials->size()!=mesh->triangles.size()) return false;
instances.push_back({std::move(mesh),transform,materialOverride,std::move(triangleMaterials)});
return true;
}
size_t Object::vertexCount() const {
size_t n=vertices.size();
for (const auto& instance:instances) if (instance.mesh) n+=instance.mesh->vertices.size();
return n;
}
size_t Object::triangleCount() const {
size_t n=triangles.size();
for (const auto& instance:instances) if (instance.mesh) n+=instance.mesh->triangles.size();
return n;
}
#endif // JET_MESH_INSTANCING
bool Object::cachePositions() {
static_assert(std::is_trivially_destructible<Vector3>::value,
"Position cache allocation requires trivial destruction");
invalidatePositions();
if (vertices.empty()) return true;
if (vertices.size() > SIZE_MAX / sizeof(Vector3)) return false;
const size_t bytes = vertices.size() * sizeof(Vector3);
#if defined(ESP_PLATFORM) && defined(CONFIG_SPIRAM)
void* storage = heap_caps_malloc(bytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
#else
void* storage = std::malloc(bytes);
#endif
if (!storage) return false;
auto* positions = static_cast<Vector3*>(storage);
for (size_t i = 0; i < vertices.size(); ++i)
new (positions + i) Vector3(vertices[i].position);
// Vector3 is trivially destructible; release the raw allocation once
// the last Object sharing this immutable stream lets it go.
positionCache = std::shared_ptr<const Vector3>(positions,
[](const Vector3* p) { std::free(const_cast<Vector3*>(p)); });
positionCacheSize = vertices.size();
// Preserve the public one-position-per-vertex stream. A small map
// points duplicates at their earliest vertex, whose projection is
// already available when Scene reaches them. Do not merge normals/UVs.
const size_t count = vertices.size();
if (count >= 8 && count <= size_t(UINT16_MAX) + 1) {
auto allocateMap = [count]() -> uint16_t* {
#if defined(ESP_PLATFORM) && defined(CONFIG_SPIRAM)
return static_cast<uint16_t*>(heap_caps_malloc(count * sizeof(uint16_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
#else
return static_cast<uint16_t*>(std::malloc(count * sizeof(uint16_t)));
#endif
};
uint16_t* order = allocateMap();
uint16_t* sources = allocateMap();
if (!order || !sources) { std::free(order); std::free(sources); return true; }
for (size_t i = 0; i < count; ++i) order[i] = (uint16_t)i;
std::sort(order, order + count, [positions](uint16_t a, uint16_t b) {
const Vector3 &pa = positions[a], &pb = positions[b];
if (pa.x != pb.x) return pa.x < pb.x;
if (pa.y != pb.y) return pa.y < pb.y;
if (pa.z != pb.z) return pa.z < pb.z;
return a < b;
});
size_t duplicates = 0;
uint16_t first = order[0];
for (size_t i = 0; i < count; ++i) {
const uint16_t index = order[i];
const Vector3 &p = positions[index], &q = positions[first];
if (p.x != q.x || p.y != q.y || p.z != q.z) first = index;
sources[index] = first;
duplicates += first != index;
}
std::free(order);
// Avoid an extra stream for meshes with little position reuse.
if (duplicates >= count / 4)
positionSources = std::shared_ptr<const uint16_t>(sources,
[](const uint16_t* p) { std::free(const_cast<uint16_t*>(p)); });
else std::free(sources);
}
return true;
}
void Object::calculateBoundingBox() {
invalidatePositions();
#if JET_MESH_INSTANCING
bool first=true;
auto include = [&](const Vector3& p) {
if (first) { boundingBoxMin=boundingBoxMax=p; first=false; }
else {
boundingBoxMin.x=std::min(boundingBoxMin.x,p.x); boundingBoxMax.x=std::max(boundingBoxMax.x,p.x);
boundingBoxMin.y=std::min(boundingBoxMin.y,p.y); boundingBoxMax.y=std::max(boundingBoxMax.y,p.y);
boundingBoxMin.z=std::min(boundingBoxMin.z,p.z); boundingBoxMax.z=std::max(boundingBoxMax.z,p.z);
}
};
for (const auto& vertex:vertices) include(vertex.position);
for (const auto& instance:instances) {
if (!instance.mesh || instance.mesh->vertices.empty()) continue;
const auto& lo=instance.mesh->boundingBoxMin; const auto& hi=instance.mesh->boundingBoxMax;
const auto& t=instance.transform; const float* m=t.basis;
for (int i=0;i<8;++i) {
const float x=(float)((i&1)?hi.x:lo.x), y=(float)((i&2)?hi.y:lo.y), z=(float)((i&4)?hi.z:lo.z);
const float px=m[0]*x+m[1]*y+m[2]*z+t.position.x;
const float py=m[3]*x+m[4]*y+m[5]*z+t.position.y;
const float pz=m[6]*x+m[7]*y+m[8]*z+t.position.z;
include({(int32_t)std::floor(px),(int32_t)std::floor(py),(int32_t)std::floor(pz)});
include({(int32_t)std::ceil(px),(int32_t)std::ceil(py),(int32_t)std::ceil(pz)});
}
#else
if (vertices.empty()) {
boundingBoxMin = {0, 0, 0};
boundingBoxMax = {0, 0, 0};
return;
#endif // JET_MESH_INSTANCING
}
#if JET_MESH_INSTANCING
if (first) boundingBoxMin=boundingBoxMax={0,0,0};
#else
boundingBoxMin.assign(vertices[0].position);
boundingBoxMax.assign(vertices[0].position);
for (const auto& vertex : vertices) {
if (vertex.position.x < boundingBoxMin.x) boundingBoxMin.x = vertex.position.x;
if (vertex.position.y < boundingBoxMin.y) boundingBoxMin.y = vertex.position.y;
if (vertex.position.z < boundingBoxMin.z) boundingBoxMin.z = vertex.position.z;
if (vertex.position.x > boundingBoxMax.x) boundingBoxMax.x = vertex.position.x;
if (vertex.position.y > boundingBoxMax.y) boundingBoxMax.y = vertex.position.y;
if (vertex.position.z > boundingBoxMax.z) boundingBoxMax.z = vertex.position.z;
}
#endif // JET_MESH_INSTANCING
centreVolume = (boundingBoxMin + boundingBoxMax).divide(2);
}
void Object::bakeRotation()
{
// No-op fast path: nothing to bake.
if (rotation.x == 0 && rotation.y == 0 && rotation.z == 0) return;
// Match the per-vertex rotation order used by Scene::renderObject
// exactly (X then Y then Z) so the baked result is bit-identical
// to what the renderer would have produced at the previous
// rotation. After the bake we zero rotation and recompute the
// bounding box.
const int32_t cosX = lookupCosI(rotation.x);
const int32_t sinX = lookupSinI(rotation.x);
const int32_t cosY = lookupCosI(rotation.y);
const int32_t sinY = lookupSinI(rotation.y);
const int32_t cosZ = lookupCosI(rotation.z);
const int32_t sinZ = lookupSinI(rotation.z);
auto rotXYZ = [&](Vector3& v) {
// X-axis
int32_t y1 = (v.y * cosX - v.z * sinX) / FIXED_POINT_SCALE;
int32_t z1 = (v.y * sinX + v.z * cosX) / FIXED_POINT_SCALE;
v.y = y1; v.z = z1;
// Y-axis
int32_t x2 = ( v.x * cosY + v.z * sinY) / FIXED_POINT_SCALE;
int32_t z2 = (-v.x * sinY + v.z * cosY) / FIXED_POINT_SCALE;
v.x = x2; v.z = z2;
// Z-axis
int32_t x3 = (v.x * cosZ - v.y * sinZ) / FIXED_POINT_SCALE;
int32_t y3 = (v.x * sinZ + v.y * cosZ) / FIXED_POINT_SCALE;
v.x = x3; v.y = y3;
};
for (auto& vert : vertices) {
rotXYZ(vert.position);
rotXYZ(vert.normal);
}
#if JET_MESH_INSTANCING
if (!instances.empty()) {
const auto r=InstanceTransform::rotated(rotation);
for (auto& instance:instances) {
const auto old=instance.transform;
for (int row=0;row<3;++row) for(int col=0;col<3;++col)
instance.transform.basis[row*3+col]=r.basis[row*3]*old.basis[col]+r.basis[row*3+1]*old.basis[3+col]+r.basis[row*3+2]*old.basis[6+col];
rotXYZ(instance.transform.position);
}
}
#endif // JET_MESH_INSTANCING
rotation.assign(0, 0, 0);
calculateBoundingBox();
}
void Object::bakeScale(int32_t numerator, int32_t denominator)
{
if (denominator == 0) return; // guard against divide-by-zero
if (numerator == denominator) return; // identity scale: nothing to do
for (auto& vert : vertices) {
// 64-bit intermediates: a position component near INT32_MAX/8
// (~2.7e8 — i.e. several worlds wide) times a numerator of
// ~256 still fits in int64.
vert.position.x = (int32_t)(((int64_t)vert.position.x * numerator) / denominator);
vert.position.y = (int32_t)(((int64_t)vert.position.y * numerator) / denominator);
vert.position.z = (int32_t)(((int64_t)vert.position.z * numerator) / denominator);
// Normals are unit-direction vectors and must not be scaled.
}
#if JET_MESH_INSTANCING
std::vector<std::pair<const Object*, SharedMesh>> scaled;
for (auto& instance:instances) {
if (!instance.mesh) continue;
const Object* key=instance.mesh.get();
auto found=std::find_if(scaled.begin(),scaled.end(),[key](const auto& p){return p.first==key;});
if (found==scaled.end()) {
Object copy=*instance.mesh; copy.bakeScale(numerator,denominator);
scaled.emplace_back(key,freezeMesh(std::move(copy))); found=scaled.end()-1;
}
instance.mesh=found->second;
auto& p=instance.transform.position;
p.x=(int32_t)((int64_t)p.x*numerator/denominator);
p.y=(int32_t)((int64_t)p.y*numerator/denominator);
p.z=(int32_t)((int64_t)p.z*numerator/denominator);
}
#endif // JET_MESH_INSTANCING
calculateBoundingBox();
}
void Object::addVertex(const Vertex &vertex)
{
invalidatePositions();
vertices.push_back(vertex);
}
void Object::addTriangle(uint16_t idx1, uint16_t idx2, uint16_t idx3, Material *material)
{
triangles.push_back({idx1, idx2, idx3, material});
}
void Object::addFace(uint16_t v1, uint16_t v2, uint16_t v3, uint16_t v4, Material *material)
{
// Add two triangles to form a face
addTriangle(v1, v2, v3, material); // First triangle
addTriangle(v3, v4, v1, material); // Second triangle
}
void Object::computeFlatNormals()
{
// Per-triangle face normal via cross product on int64 to survive
// any reasonable design-unit coordinate range (positions can be
// thousands of units pre-WORLD_SCALE; products would overflow
// int32 on big scenery). The length is computed from those int64
// intermediates and we then rescale to FIXED_POINT_SCALE so the
// resulting vector matches the convention the renderer expects.
for (const auto& tri : triangles) {
Vertex& a = vertices[tri.v1];
Vertex& b = vertices[tri.v2];
Vertex& c = vertices[tri.v3];
const int64_t ux = (int64_t)b.position.x - a.position.x;
const int64_t uy = (int64_t)b.position.y - a.position.y;
const int64_t uz = (int64_t)b.position.z - a.position.z;
const int64_t vx = (int64_t)c.position.x - a.position.x;
const int64_t vy = (int64_t)c.position.y - a.position.y;
const int64_t vz = (int64_t)c.position.z - a.position.z;
const int64_t nx = uy * vz - uz * vy;
const int64_t ny = uz * vx - ux * vz;
const int64_t nz = ux * vy - uy * vx;
// Integer sqrt of nx² + ny² + nz². Use double here — this is a
// one-shot setup cost per triangle at mesh-build time, not a
// per-frame hot path, so the floating-point detour is fine.
const double mag = std::sqrt((double)nx*nx + (double)ny*ny + (double)nz*nz);
if (mag <= 0.0) continue;
const double s = (double)FIXED_POINT_SCALE / mag;
const int32_t fx = (int32_t)(nx * s);
const int32_t fy = (int32_t)(ny * s);
const int32_t fz = (int32_t)(nz * s);
a.normal.x = fx; a.normal.y = fy; a.normal.z = fz;
b.normal.x = fx; b.normal.y = fy; b.normal.z = fz;
c.normal.x = fx; c.normal.y = fy; c.normal.z = fz;
}
}
void Object::setPosition(int32_t x, int32_t y, int32_t z)
{
position.x = x;
position.y = y;
position.z = z;
}
void Object::setPosition(const Vector3 &pos)
{
position.assign(pos);
}
void Object::setRotation(int32_t rotX, int32_t rotY, int32_t rotZ)
{
rotation.x = rotX % ANGLE_MAX;
rotation.y = rotY % ANGLE_MAX;
rotation.z = rotZ % ANGLE_MAX;
}
void Object::setRotation(const Vector3 &rot)
{
rotation.assign(rot);
}
void Object::rotate(int32_t angleX, int32_t angleY, int32_t angleZ)
{
rotation.x = (rotation.x + angleX + ANGLE_MAX) % ANGLE_MAX;
rotation.y = (rotation.y + angleY + ANGLE_MAX) % ANGLE_MAX;
rotation.z = (rotation.z + angleZ + ANGLE_MAX) % ANGLE_MAX;
}
void Object::rotate(const Vector3 &rot)
{
rotation.add(rot);
rotation.x %= ANGLE_MAX;
rotation.y %= ANGLE_MAX;
rotation.z %= ANGLE_MAX;
}
void Object::translate(int32_t dx, int32_t dy, int32_t dz)
{
position.x += dx;
position.y += dy;
position.z += dz;
}
void Object::lookAt(const Vector3& target)
{
// Calculate the direction vector from the camera to the target
Vector3 direction = target - position;
// Calculate the rotation angles to look at the target in 3D space
rotation.assign(
-static_cast<int32_t>(std::atan2(direction.y, std::sqrt(direction.x * direction.x + direction.z * direction.z)) * 180 / M_PI),
static_cast<int32_t>(std::atan2(direction.x, direction.z) * 180 / M_PI),
0
);
}
void Object::lookAt(const Object* targetObject)
{
lookAt(targetObject->position);
}
void Object::bakeFlatLighting(const DirectionalLight* directionalLight,
const AmbientLight* ambientLight)
{
// Ambient components (0 if no ambient light).
const uint32_t ambR = ambientLight ? ambientLight->color.r : 0;
const uint32_t ambG = ambientLight ? ambientLight->color.g : 0;
const uint32_t ambB = ambientLight ? ambientLight->color.b : 0;
for (auto& tri : triangles) {
tri.colorBaked = false;
if (!tri.material) continue;
if (tri.material->diffuseMap) continue; // textured — skip
const ShadingMode mode = tri.material->shadingMode;
if (mode != ShadingMode::FLAT &&
mode != ShadingMode::GOURAUD &&
mode != ShadingMode::UNLIT) continue;
if (mode == ShadingMode::UNLIT ||
(!directionalLight && !ambientLight)) {
// Already emissive / no lights — just stamp the raw colour.
tri.bakedColor = tri.material->color;
tri.colorBaked = true;
continue;
}
// Compute brightness from the face normal (v1 for FLAT/GOURAUD).
// Use the same squared-falloff Lambert as jetShadeBrightness.
uint32_t brightness = 0;
if (directionalLight) {
const Vector3& N = vertices[tri.v1].normal;
const Vector3& L = directionalLight->worldLightDir;
int64_t dot = (int64_t)N.x * L.x + (int64_t)N.y * L.y + (int64_t)N.z * L.z;
if (dot > 0) {
uint32_t lambert = (uint32_t)(dot >> 12);
if (lambert > 255) lambert = 255;
lambert = (lambert * lambert + 128) >> 8; // squared falloff
lambert = (lambert * (uint32_t)directionalLight->intensity) >> 8;
brightness = (lambert * (uint32_t)tri.material->diffuse) >> 8;
const uint32_t maxBrightness = 255u + tri.material->specular;
if (brightness > maxBrightness) brightness = maxBrightness;
}
}
// Per-channel modulation (mirrors jetModulateRGB565 in Renderer.cpp).
const uint16_t base = tri.material->color;
const uint32_t maxBrightness = 255u + tri.material->specular;
const uint32_t tR = std::min(brightness + ambR, maxBrightness);
const uint32_t tG = std::min(brightness + ambG, maxBrightness);
const uint32_t tB = std::min(brightness + ambB, maxBrightness);
auto ch5 = [](uint32_t base5, uint32_t t) -> uint32_t {
if (t > 255) { const uint32_t blow = t-255; return base5 + ((31u-base5)*blow)/256u; }
const uint32_t v = base5 * t; return (v + 128u + (v>>8)) >> 8;
};
auto ch6 = [](uint32_t base6, uint32_t t) -> uint32_t {
if (t > 255) { const uint32_t blow = t-255; return base6 + ((63u-base6)*blow)/256u; }
const uint32_t v = base6 * t; return (v + 128u + (v>>8)) >> 8;
};
const uint32_t r = ch5((base >> 11) & 0x1Fu, tR);
const uint32_t g = ch6((base >> 5) & 0x3Fu, tG);
const uint32_t b = ch5( base & 0x1Fu, tB);
tri.bakedColor = (uint16_t)((r << 11) | (g << 5) | b);
tri.colorBaked = true;
}
}
} // namespace Renderer