Skip to content

Commit faf69db

Browse files
committed
Scale line points to viewport height
1 parent 74c0c36 commit faf69db

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/progressed/graphics/draw3d/Draw3D.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static void drawDiskDebug(float x1, float y1, float x2, float y2, float z
126126
public static void drawLineDebug(float x1, float y1, float z1, float x2, float y2, float z2){
127127
Lines3D.line(x1, y1, z1, x2, y2, z2);
128128

129-
int pointCount = Lines3D.linePointCounts(x1, y1, z1, x2, y2, z2);
129+
int pointCount = Lines3D.linePointCount(x1, y1, z1, x2, y2, z2);
130130
float[] points = Lines3D.linePoints(x1, y1, z1, x2, y2, z2, pointCount);
131131
for(int i = 0; i < points.length; i += 3){
132132
float x = points[i],

src/progressed/graphics/draw3d/Lines3D.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void line(float x1, float y1, float z1, float x2, float y2, float
5050
}
5151

5252
public static void line(float x1, float y1, float z1, float x2, float y2, float z2, boolean scale){
53-
line(x1, y1, z1, x2, y2, z2, linePointCounts(x1, y1, z1, x2, y2, z2), scale);
53+
line(x1, y1, z1, x2, y2, z2, linePointCount(x1, y1, z1, x2, y2, z2), scale);
5454
}
5555

5656
public static void line(float x1, float y1, float z1, float x2, float y2, float z2){
@@ -62,21 +62,36 @@ public static void lineAngleBase(float x, float y, float z, float length, float
6262
line(x, y, z, x + Tmp.v31.x, y + Tmp.v31.y, z + Tmp.v31.z);
6363
}
6464

65-
public static int linePointCounts(float x1, float y1, float z1, float x2, float y2, float z2){
65+
public static int linePointCount(float x1, float y1, float z1, float x2, float y2, float z2){
6666
return (int)(Math3D.dst(x1, y1, z1, x2, y2, z2) / tilesize / tilesize);
6767
}
6868

6969
public static float[] linePoints(float x1, float y1, float z1, float x2, float y2, float z2, int pointCount){
7070
if(z1 > z2){ //Always return from bottom to top
71-
return linePoints(x2, y2, z2, x1, y1, z1, pointCount);
71+
float tx = x1, ty = y1, tz = z1;
72+
x1 = x2;
73+
y1 = y2;
74+
z1 = z2;
75+
x2 = tx;
76+
y2 = ty;
77+
z2 = tz;
78+
}
79+
80+
float vz = Perspective.viewportZ();
81+
if(z2 > vz){ //If line goes above viewport height, scale to viewport height.
82+
float scl = vz / (z2 - z1);
83+
x2 = x1 + (x2 - x1) * scl;
84+
y2 = y1 + (y2 - y1) * scl;
85+
z2 = vz;
86+
pointCount = Mathf.ceil(pointCount * scl);
7287
}
7388

7489
float[] points = new float[pointCount * 3];
7590
float px = (x2 - x1) / (pointCount - 1);
7691
float py = (y2 - y1) / (pointCount - 1);
7792
float pz = (z2 - z1) / (pointCount - 1);
7893

79-
for(int i = 0; i < pointCount; i++){ //TODO check if goes above viewport z. If so, limit to viewport and cut short.
94+
for(int i = 0; i < pointCount; i++){
8095
points[i * 3] = x1 + px * i;
8196
points[i * 3 + 1] = y1 + py * i;
8297
points[i * 3 + 2] = z1 + pz * i;

src/progressed/graphics/draw3d/Perspective.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,23 @@ public static float alpha(float x, float y, float z){
8383
}
8484
}
8585

86-
/** Calculates the camera height based on FOV and the size of the vanilla camera. */
86+
/**
87+
* Calculates the camera z coordinate based on FOV and the size of the vanilla camera.
88+
* @return camera z coordinate
89+
* */
8790
public static float cameraZ(){
8891
float width = Math.max(camera.width, camera.height) / 2f;
8992
//TOA
9093
return (float)(width / Math.tan(fov / 2f * Mathf.degRad));
9194
}
9295

96+
/**
97+
* @return viewport z coordinate
98+
*/
99+
public static float viewportZ(){
100+
return cameraZ() - viewportOffset;
101+
}
102+
93103
/** Calculates the size of the viewport. */
94104
public static Vec2 viewportSize(){
95105
float v1 = (float)(Math.tan(fov / 2f * Mathf.degRad) * viewportOffset * 2f);

0 commit comments

Comments
 (0)