Advertisement
OrangoMango

Frustum

May 21st, 2023
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.44 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class FrustumPlanes {
  4.     public static class Plane {
  5.         public final Point3D point;
  6.         public final Vector3D normal;
  7.  
  8.         public Plane(Point3D point, Vector3D normal) {
  9.             this.point = point;
  10.             this.normal = normal;
  11.         }
  12.     }
  13.  
  14.     public static class Point3D {
  15.         public final double x, y, z;
  16.  
  17.         public Point3D(double x, double y, double z) {
  18.             this.x = x;
  19.             this.y = y;
  20.             this.z = z;
  21.         }
  22.     }
  23.  
  24.     public static class Vector3D {
  25.         public final double x, y, z;
  26.  
  27.         public Vector3D(double x, double y, double z) {
  28.             this.x = x;
  29.             this.y = y;
  30.             this.z = z;
  31.         }
  32.     }
  33.  
  34.     public static Plane[] calculateFrustumPlanes(Point3D cameraPosition, double zNear, double zFar, Vector3D cameraDirection, double fovAngle) {
  35.         // Calculate the half-height and half-width of the near plane
  36.         double nearHeight = 2.0 * Math.tan(Math.toRadians(fovAngle / 2.0)) * zNear;
  37.         double nearWidth = nearHeight;
  38.  
  39.         // Calculate the center and normal of the near plane
  40.         Point3D nearCenter = new Point3D(
  41.                 cameraPosition.x + cameraDirection.x * zNear,
  42.                 cameraPosition.y + cameraDirection.y * zNear,
  43.                 cameraPosition.z + cameraDirection.z * zNear
  44.         );
  45.         Vector3D nearNormal = new Vector3D(
  46.                 cameraDirection.x,
  47.                 cameraDirection.y,
  48.                 cameraDirection.z
  49.         );
  50.  
  51.         // Calculate the center and normal of the far plane
  52.         Point3D farCenter = new Point3D(
  53.                 cameraPosition.x + cameraDirection.x * zFar,
  54.                 cameraPosition.y + cameraDirection.y * zFar,
  55.                 cameraPosition.z + cameraDirection.z * zFar
  56.         );
  57.         Vector3D farNormal = new Vector3D(
  58.                 -cameraDirection.x,
  59.                 -cameraDirection.y,
  60.                 -cameraDirection.z
  61.         );
  62.  
  63.         // Calculate the corners of the near plane
  64.         Point3D nearTopLeft = new Point3D(
  65.                 nearCenter.x - cameraDirection.y * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
  66.                 nearCenter.y + cameraDirection.x * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
  67.                 nearCenter.z - cameraDirection.x * nearWidth / 2.0 - cameraDirection.y * nearHeight / 2.0
  68.         );
  69.         Point3D nearTopRight = new Point3D(
  70.                 nearCenter.x + cameraDirection.y * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
  71.                 nearCenter.y - cameraDirection.x * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
  72.                 nearCenter.z - cameraDirection.x * nearWidth / 2.0 - cameraDirection.y * nearHeight / 2.0
  73.         );
  74.         Point3D nearBottomLeft = new Point3D(
  75.                 nearCenter.x - cameraDirection.y * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
  76.                 nearCenter.y + cameraDirection.x * nearHeight / 2.0 + cameraDirection.z * nearWidth/ 2.0,
  77.                 nearCenter.z - cameraDirection.x * nearWidth / 2.0 + cameraDirection.y * nearHeight / 2.0
  78.         );
  79.         Point3D nearBottomRight = new Point3D(
  80.                 nearCenter.x + cameraDirection.y * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
  81.                 nearCenter.y - cameraDirection.x * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
  82.                 nearCenter.z - cameraDirection.x * nearWidth / 2.0 + cameraDirection.y * nearHeight / 2.0
  83.         );
  84.  
  85.         // Calculate the corners of the far plane
  86.         Point3D farTopLeft = new Point3D(
  87.                 farCenter.x - cameraDirection.y * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
  88.                 farCenter.y + cameraDirection.x * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
  89.                 farCenter.z - cameraDirection.x * nearWidth / 2.0 - cameraDirection.y * nearHeight / 2.0
  90.         );
  91.         Point3D farTopRight = new Point3D(
  92.                 farCenter.x + cameraDirection.y * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
  93.                 farCenter.y - cameraDirection.x * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
  94.                 farCenter.z - cameraDirection.x * nearWidth / 2.0 - cameraDirection.y * nearHeight / 2.0
  95.         );
  96.         Point3D farBottomLeft = new Point3D(
  97.                 farCenter.x - cameraDirection.y * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
  98.                 farCenter.y + cameraDirection.x * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
  99.                 farCenter.z - cameraDirection.x * nearWidth / 2.0 + cameraDirection.y * nearHeight / 2.0
  100.         );
  101.         Point3D farBottomRight = new Point3D(
  102.                 farCenter.x + cameraDirection.y * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
  103.                 farCenter.y - cameraDirection.x * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
  104.                 farCenter.z - cameraDirection.x * nearWidth / 2.0 + cameraDirection.y * nearHeight / 2.0
  105.         );
  106.  
  107.         // Create the planes using the corner points and normals
  108.         Plane[] planes = new Plane[6];
  109.         planes[0] = new Plane(nearTopLeft, calculatePlaneNormal(nearTopLeft, nearTopRight, nearBottomRight)); // Near plane
  110.         planes[1] = new Plane(farTopRight, calculatePlaneNormal(farTopRight, farTopLeft, farBottomLeft)); // Far plane
  111.         planes[2] = new Plane(nearTopRight, calculatePlaneNormal(nearTopRight, farTopRight, farBottomRight)); // Right plane
  112.         planes[3] = new Plane(nearTopLeft, calculatePlaneNormal(nearTopLeft, farTopLeft, farBottomLeft)); // Left plane
  113.         planes[4] = new Plane(nearTopLeft, calculatePlaneNormal(nearTopLeft, nearTopRight, farTopRight)); // Top plane
  114.         planes[5] = new Plane(nearBottomRight, calculatePlaneNormal(nearBottomRight, nearBottomLeft, farBottomLeft)); // Bottom plane
  115.  
  116.         return planes;
  117.     }
  118.  
  119.     private static Vector3D calculatePlaneNormal(Point3D p1, Point3D p2, Point3D p3) {
  120.         // Calculate the vectors for two sides of the plane
  121.         double v1x = p2.x - p1.x;
  122.         double v1y = p2.y - p1.y;
  123.         double v1z = p2.z - p1.z;
  124.         double v2x = p3.x - p1.x;
  125.         double v2y = p3.y - p1.y;
  126.         double v2z = p3.z - p1.z;
  127.  
  128.         // Calculate the cross product of the two vectors
  129.         double nx = v1y * v2z - v1z * v2y;
  130.         double ny = v1z * v2x - v1x * v2z;
  131.         double nz = v1x * v2y - v1y * v2x;
  132.  
  133.         // Normalize the cross product vector
  134.         double magnitude = Math.sqrt(nx * nx + ny * ny + nz * nz);
  135.         nx /= magnitude;
  136.         ny /= magnitude;
  137.         nz /= magnitude;
  138.  
  139.         return new Vector3D(nx, ny, nz);
  140.     }
  141.  
  142.     public static void main(String[] args) {
  143.         // Example usage
  144.         Point3D cameraPosition = new Point3D(0, 0, 0);
  145.         double zNear = 1.0;
  146.         double zFar = 10.0;
  147.         Vector3D cameraDirection = new Vector3D(0, 0, -1);
  148.         double fovAngle = 60.0;
  149.  
  150.         Plane[] frustumPlanes = calculateFrustumPlanes(cameraPosition, zNear, zFar, cameraDirection, fovAngle);
  151.  
  152.         for (Plane plane : frustumPlanes) {
  153.             System.out.println("Point: (" + plane.point.x + ", " + plane.point.y + ", " + plane.point.z + ")");
  154.             System.out.println("Normal: (" + plane.normal.x + ", " + plane.normal.y + ", " + plane.normal.z + ")");
  155.             System.out.println();
  156.         }
  157.     }
  158. }
  159.  
  160.  
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement