-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjection.cpp
More file actions
47 lines (41 loc) · 1.12 KB
/
Projection.cpp
File metadata and controls
47 lines (41 loc) · 1.12 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
#include "Projection.h"
float* PerspectiveProject(float tesseractVertices[64], float cameraPos[4], float fovScale, float scale[2], int offset[2]) {
float* projectedPoints = new float[32];
float xc, yc, zc, wc, x, y, z, w, temp, x3d, y3d, z3d, x2d, y2d;
xc = cameraPos[0];
yc = cameraPos[1];
zc = cameraPos[2];
wc = cameraPos[3];
for (int i = 0; i < 64; i += 4) {
x = tesseractVertices[i];
y = tesseractVertices[i + 1];
z = tesseractVertices[i + 2];
w = tesseractVertices[i + 3];
// 4D to 3D perspective projection
// Ensure w != wc to avoid division by zero
if (w != wc) {
temp = w - wc;
x3d = (x - xc) / temp;
y3d = (y - yc) / temp;
z3d = (z - zc) / temp;
}
else {
x3d = x - xc;
y3d = y - yc;
z3d = z - zc;
}
// 3D to 2D perspective projection
if (z3d + fovScale) {
x2d = (x3d * fovScale) / (z3d + fovScale);
y2d = (y3d * fovScale) / (z3d + fovScale);
}
else {
x2d = x3d;
y2d = y3d;
}
int projectIndex = i / 2;
projectedPoints[projectIndex] = x2d * scale[0] + offset[0];
projectedPoints[projectIndex + 1] = y2d * scale[1] + offset[1];
}
return projectedPoints;
}