diff --git a/src/progressed/graphics/draw3d/Draw3D.java b/src/progressed/graphics/draw3d/Draw3D.java index d98ccc3f..0c8dde0d 100644 --- a/src/progressed/graphics/draw3d/Draw3D.java +++ b/src/progressed/graphics/draw3d/Draw3D.java @@ -126,7 +126,7 @@ public static void drawDiskDebug(float x1, float y1, float x2, float y2, float z public static void drawLineDebug(float x1, float y1, float z1, float x2, float y2, float z2){ Lines3D.line(x1, y1, z1, x2, y2, z2); - int pointCount = Lines3D.linePointCounts(x1, y1, z1, x2, y2, z2); + int pointCount = Lines3D.linePointCount(x1, y1, z1, x2, y2, z2); float[] points = Lines3D.linePoints(x1, y1, z1, x2, y2, z2, pointCount); for(int i = 0; i < points.length; i += 3){ float x = points[i], diff --git a/src/progressed/graphics/draw3d/Lines3D.java b/src/progressed/graphics/draw3d/Lines3D.java index 21197070..ddcc0ab9 100644 --- a/src/progressed/graphics/draw3d/Lines3D.java +++ b/src/progressed/graphics/draw3d/Lines3D.java @@ -50,7 +50,7 @@ public static void line(float x1, float y1, float z1, float x2, float y2, float } public static void line(float x1, float y1, float z1, float x2, float y2, float z2, boolean scale){ - line(x1, y1, z1, x2, y2, z2, linePointCounts(x1, y1, z1, x2, y2, z2), scale); + line(x1, y1, z1, x2, y2, z2, linePointCount(x1, y1, z1, x2, y2, z2), scale); } public static void line(float x1, float y1, float z1, float x2, float y2, float z2){ @@ -62,13 +62,28 @@ public static void lineAngleBase(float x, float y, float z, float length, float line(x, y, z, x + Tmp.v31.x, y + Tmp.v31.y, z + Tmp.v31.z); } - public static int linePointCounts(float x1, float y1, float z1, float x2, float y2, float z2){ + public static int linePointCount(float x1, float y1, float z1, float x2, float y2, float z2){ return (int)(Math3D.dst(x1, y1, z1, x2, y2, z2) / tilesize / tilesize); } public static float[] linePoints(float x1, float y1, float z1, float x2, float y2, float z2, int pointCount){ if(z1 > z2){ //Always return from bottom to top - return linePoints(x2, y2, z2, x1, y1, z1, pointCount); + float tx = x1, ty = y1, tz = z1; + x1 = x2; + y1 = y2; + z1 = z2; + x2 = tx; + y2 = ty; + z2 = tz; + } + + float vz = Perspective.viewportZ(); + if(z2 > vz){ //If line goes above viewport height, scale to viewport height. + float scl = vz / (z2 - z1); + x2 = x1 + (x2 - x1) * scl; + y2 = y1 + (y2 - y1) * scl; + z2 = vz; + pointCount = Mathf.ceil(pointCount * scl); } float[] points = new float[pointCount * 3]; @@ -76,7 +91,7 @@ public static float[] linePoints(float x1, float y1, float z1, float x2, float y float py = (y2 - y1) / (pointCount - 1); float pz = (z2 - z1) / (pointCount - 1); - for(int i = 0; i < pointCount; i++){ //TODO check if goes above viewport z. If so, limit to viewport and cut short. + for(int i = 0; i < pointCount; i++){ points[i * 3] = x1 + px * i; points[i * 3 + 1] = y1 + py * i; points[i * 3 + 2] = z1 + pz * i; diff --git a/src/progressed/graphics/draw3d/Perspective.java b/src/progressed/graphics/draw3d/Perspective.java index 0d5b056d..bc523c92 100644 --- a/src/progressed/graphics/draw3d/Perspective.java +++ b/src/progressed/graphics/draw3d/Perspective.java @@ -83,13 +83,23 @@ public static float alpha(float x, float y, float z){ } } - /** Calculates the camera height based on FOV and the size of the vanilla camera. */ + /** + * Calculates the camera z coordinate based on FOV and the size of the vanilla camera. + * @return camera z coordinate + * */ public static float cameraZ(){ float width = Math.max(camera.width, camera.height) / 2f; //TOA return (float)(width / Math.tan(fov / 2f * Mathf.degRad)); } + /** + * @return viewport z coordinate + */ + public static float viewportZ(){ + return cameraZ() - viewportOffset; + } + /** Calculates the size of the viewport. */ public static Vec2 viewportSize(){ float v1 = (float)(Math.tan(fov / 2f * Mathf.degRad) * viewportOffset * 2f);