Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class FrustumPlanes {
- public static class Plane {
- public final Point3D point;
- public final Vector3D normal;
- public Plane(Point3D point, Vector3D normal) {
- this.point = point;
- this.normal = normal;
- }
- }
- public static class Point3D {
- public final double x, y, z;
- public Point3D(double x, double y, double z) {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- }
- public static class Vector3D {
- public final double x, y, z;
- public Vector3D(double x, double y, double z) {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- }
- public static Plane[] calculateFrustumPlanes(Point3D cameraPosition, double zNear, double zFar, Vector3D cameraDirection, double fovAngle) {
- // Calculate the half-height and half-width of the near plane
- double nearHeight = 2.0 * Math.tan(Math.toRadians(fovAngle / 2.0)) * zNear;
- double nearWidth = nearHeight;
- // Calculate the center and normal of the near plane
- Point3D nearCenter = new Point3D(
- cameraPosition.x + cameraDirection.x * zNear,
- cameraPosition.y + cameraDirection.y * zNear,
- cameraPosition.z + cameraDirection.z * zNear
- );
- Vector3D nearNormal = new Vector3D(
- cameraDirection.x,
- cameraDirection.y,
- cameraDirection.z
- );
- // Calculate the center and normal of the far plane
- Point3D farCenter = new Point3D(
- cameraPosition.x + cameraDirection.x * zFar,
- cameraPosition.y + cameraDirection.y * zFar,
- cameraPosition.z + cameraDirection.z * zFar
- );
- Vector3D farNormal = new Vector3D(
- -cameraDirection.x,
- -cameraDirection.y,
- -cameraDirection.z
- );
- // Calculate the corners of the near plane
- Point3D nearTopLeft = new Point3D(
- nearCenter.x - cameraDirection.y * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
- nearCenter.y + cameraDirection.x * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
- nearCenter.z - cameraDirection.x * nearWidth / 2.0 - cameraDirection.y * nearHeight / 2.0
- );
- Point3D nearTopRight = new Point3D(
- nearCenter.x + cameraDirection.y * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
- nearCenter.y - cameraDirection.x * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
- nearCenter.z - cameraDirection.x * nearWidth / 2.0 - cameraDirection.y * nearHeight / 2.0
- );
- Point3D nearBottomLeft = new Point3D(
- nearCenter.x - cameraDirection.y * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
- nearCenter.y + cameraDirection.x * nearHeight / 2.0 + cameraDirection.z * nearWidth/ 2.0,
- nearCenter.z - cameraDirection.x * nearWidth / 2.0 + cameraDirection.y * nearHeight / 2.0
- );
- Point3D nearBottomRight = new Point3D(
- nearCenter.x + cameraDirection.y * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
- nearCenter.y - cameraDirection.x * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
- nearCenter.z - cameraDirection.x * nearWidth / 2.0 + cameraDirection.y * nearHeight / 2.0
- );
- // Calculate the corners of the far plane
- Point3D farTopLeft = new Point3D(
- farCenter.x - cameraDirection.y * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
- farCenter.y + cameraDirection.x * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
- farCenter.z - cameraDirection.x * nearWidth / 2.0 - cameraDirection.y * nearHeight / 2.0
- );
- Point3D farTopRight = new Point3D(
- farCenter.x + cameraDirection.y * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
- farCenter.y - cameraDirection.x * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
- farCenter.z - cameraDirection.x * nearWidth / 2.0 - cameraDirection.y * nearHeight / 2.0
- );
- Point3D farBottomLeft = new Point3D(
- farCenter.x - cameraDirection.y * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
- farCenter.y + cameraDirection.x * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
- farCenter.z - cameraDirection.x * nearWidth / 2.0 + cameraDirection.y * nearHeight / 2.0
- );
- Point3D farBottomRight = new Point3D(
- farCenter.x + cameraDirection.y * nearHeight / 2.0 - cameraDirection.z * nearWidth / 2.0,
- farCenter.y - cameraDirection.x * nearHeight / 2.0 + cameraDirection.z * nearWidth / 2.0,
- farCenter.z - cameraDirection.x * nearWidth / 2.0 + cameraDirection.y * nearHeight / 2.0
- );
- // Create the planes using the corner points and normals
- Plane[] planes = new Plane[6];
- planes[0] = new Plane(nearTopLeft, calculatePlaneNormal(nearTopLeft, nearTopRight, nearBottomRight)); // Near plane
- planes[1] = new Plane(farTopRight, calculatePlaneNormal(farTopRight, farTopLeft, farBottomLeft)); // Far plane
- planes[2] = new Plane(nearTopRight, calculatePlaneNormal(nearTopRight, farTopRight, farBottomRight)); // Right plane
- planes[3] = new Plane(nearTopLeft, calculatePlaneNormal(nearTopLeft, farTopLeft, farBottomLeft)); // Left plane
- planes[4] = new Plane(nearTopLeft, calculatePlaneNormal(nearTopLeft, nearTopRight, farTopRight)); // Top plane
- planes[5] = new Plane(nearBottomRight, calculatePlaneNormal(nearBottomRight, nearBottomLeft, farBottomLeft)); // Bottom plane
- return planes;
- }
- private static Vector3D calculatePlaneNormal(Point3D p1, Point3D p2, Point3D p3) {
- // Calculate the vectors for two sides of the plane
- double v1x = p2.x - p1.x;
- double v1y = p2.y - p1.y;
- double v1z = p2.z - p1.z;
- double v2x = p3.x - p1.x;
- double v2y = p3.y - p1.y;
- double v2z = p3.z - p1.z;
- // Calculate the cross product of the two vectors
- double nx = v1y * v2z - v1z * v2y;
- double ny = v1z * v2x - v1x * v2z;
- double nz = v1x * v2y - v1y * v2x;
- // Normalize the cross product vector
- double magnitude = Math.sqrt(nx * nx + ny * ny + nz * nz);
- nx /= magnitude;
- ny /= magnitude;
- nz /= magnitude;
- return new Vector3D(nx, ny, nz);
- }
- public static void main(String[] args) {
- // Example usage
- Point3D cameraPosition = new Point3D(0, 0, 0);
- double zNear = 1.0;
- double zFar = 10.0;
- Vector3D cameraDirection = new Vector3D(0, 0, -1);
- double fovAngle = 60.0;
- Plane[] frustumPlanes = calculateFrustumPlanes(cameraPosition, zNear, zFar, cameraDirection, fovAngle);
- for (Plane plane : frustumPlanes) {
- System.out.println("Point: (" + plane.point.x + ", " + plane.point.y + ", " + plane.point.z + ")");
- System.out.println("Normal: (" + plane.normal.x + ", " + plane.normal.y + ", " + plane.normal.z + ")");
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement