Perspective: Add modelToCanvas

This has significantly less overhead than repeatedly calling
localToCanvas, and may be vectorized in the future by a smarter jit
This commit is contained in:
Max Weber
2019-09-09 10:33:49 -06:00
parent 293b9cdf7c
commit b77461a3e0

View File

@@ -148,7 +148,74 @@ public class Perspective
}
return null;
}
/**
* Translates a model's vertices into 2d space
*/
public static void modelToCanvas(Client client, int end, int x3dCenter, int y3dCenter, int z3dCenter, int rotate, int[] x3d, int[] y3d, int[] z3d, int[] x2d, int[] y2d)
{
final int
cameraPitch = client.getCameraPitch(),
cameraYaw = client.getCameraYaw(),
pitchSin = SINE[cameraPitch],
pitchCos = COSINE[cameraPitch],
yawSin = SINE[cameraYaw],
yawCos = COSINE[cameraYaw],
rotateSin = SINE[rotate],
rotateCos = COSINE[rotate],
cx = x3dCenter - client.getCameraX(),
cy = y3dCenter - client.getCameraY(),
cz = z3dCenter - client.getCameraZ(),
viewportXMiddle = client.getViewportWidth() / 2,
viewportYMiddle = client.getViewportHeight() / 2,
viewportXOffset = client.getViewportXOffset(),
viewportYOffset = client.getViewportYOffset(),
zoom3d = client.getScale();
for (int i = 0; i < end; i++)
{
int x = x3d[i];
int y = y3d[i];
int z = z3d[i];
if (rotate != 0)
{
int x0 = x;
x = x0 * rotateCos + y * rotateSin >> 16;
y = y * rotateCos - x0 * rotateSin >> 16;
}
x += cx;
y += cy;
z += cz;
final int
x1 = x * yawCos + y * yawSin >> 16,
y1 = y * yawCos - x * yawSin >> 16,
y2 = z * pitchCos - y1 * pitchSin >> 16,
z1 = y1 * pitchCos + z * pitchSin >> 16;
int viewX, viewY;
if (z1 < 50)
{
viewX = Integer.MIN_VALUE;
viewY = Integer.MIN_VALUE;
}
else
{
viewX = (viewportXMiddle + x1 * zoom3d / z1) + viewportXOffset;
viewY = (viewportYMiddle + y2 * zoom3d / z1) + viewportYOffset;
}
x2d[i] = viewX;
y2d[i] = viewY;
}
}
/**