-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObject.cpp
More file actions
215 lines (181 loc) · 7.77 KB
/
Object.cpp
File metadata and controls
215 lines (181 loc) · 7.77 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
#include "Object.hpp"
#include "TrigLUT.hpp"
#include <cmath>
namespace Renderer
{
Object::Object()
{
}
void Object::calculateBoundingBox() {
if (vertices.empty()) {
boundingBoxMin = {0, 0, 0};
boundingBoxMax = {0, 0, 0};
return;
}
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;
}
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);
}
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.
}
calculateBoundingBox();
}
void Object::addVertex(const Vertex &vertex)
{
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);
}
} // namespace Renderer