Skip to content

Commit

Permalink
Scale line points to viewport height
Browse files Browse the repository at this point in the history
  • Loading branch information
MEEPofFaith committed Nov 19, 2024
1 parent 74c0c36 commit faf69db
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/progressed/graphics/draw3d/Draw3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
23 changes: 19 additions & 4 deletions src/progressed/graphics/draw3d/Lines3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -62,21 +62,36 @@ 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];
float px = (x2 - x1) / (pointCount - 1);
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;
Expand Down
12 changes: 11 additions & 1 deletion src/progressed/graphics/draw3d/Perspective.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit faf69db

Please sign in to comment.