aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/math/Frustum.java
blob: 7fb18279fc6ae850a6bca08ed9c65ff945be9e3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package ru.olamedia.math;

import com.jogamp.opengl.math.geom.AABBox;

public class Frustum {
	public Plane topPlane = new Plane();
	public Plane bottomPlane = new Plane();
	public Plane leftPlane = new Plane();
	public Plane rightPlane = new Plane();
	public Plane nearPlane = new Plane();
	public Plane farPlane = new Plane();
	private Plane[] planes = getPlanes();

	private Plane[] getPlanes() {
		return new Plane[] { leftPlane, rightPlane, topPlane, bottomPlane };// ,
																			// topPlane,,
																			// topPlane
		// bottomPlane,
		// leftPlane,
		// rightPlane, farPlane };
	}

	public Vector3f[] getVertices() {
		Vector3f[] v = new Vector3f[8];
		v[0] = topPlane.cross(leftPlane, nearPlane);
		v[1] = topPlane.cross(leftPlane, farPlane);
		v[2] = topPlane.cross(rightPlane, nearPlane);
		v[3] = topPlane.cross(rightPlane, farPlane);
		v[4] = bottomPlane.cross(leftPlane, nearPlane);
		v[5] = bottomPlane.cross(leftPlane, farPlane);
		v[6] = bottomPlane.cross(rightPlane, nearPlane);
		v[7] = bottomPlane.cross(rightPlane, farPlane);
		return v;
	}

	private static final boolean quickClassify(Plane p, AABBox box) {
		final float[] low = box.getLow();
		final float[] high = box.getHigh();
		if (p.distance(low[0], low[1], low[2]) > 0.0f)
			return (true);
		if (p.distance(high[0], low[1], low[2]) > 0.0f)
			return (true);
		if (p.distance(low[0], high[1], low[2]) > 0.0f)
			return (true);
		if (p.distance(high[0], high[1], low[2]) > 0.0f)
			return (true);
		if (p.distance(low[0], low[1], high[2]) > 0.0f)
			return (true);
		if (p.distance(high[0], low[1], high[2]) > 0.0f)
			return (true);
		if (p.distance(low[0], high[1], high[2]) > 0.0f)
			return (true);
		if (p.distance(high[0], high[1], high[2]) > 0.0f)
			return (true);

		return (false);
	}

	/**
	 * Quick check to see if an orthogonal bounding box is inside the frustum
	 */
	public final boolean isOutside(AABBox box) {
		if (!quickClassify(leftPlane, box))
			return true;
//		if (!quickClassify(rightPlane, box))
//			return true;
//		if (!quickClassify(topPlane, box))
//			return true;
//		if (!quickClassify(bottomPlane, box))
//			return true;
//		if (!quickClassify(nearPlane, box))
//			return true;
//		if (!quickClassify(farPlane, box))
//			return true;

		// We make no attempt to determine whether it's fully inside or not.
		return false;
	}

	private static final boolean isPointInside(Plane plane, Vector3f p) {
		return (plane.distance(p) <= 0.0f);
	}

	@SuppressWarnings("unused")
	private final boolean isPointInside(Vector3f p) {
		return isPointInside(topPlane, p) && isPointInside(bottomPlane, p) && isPointInside(leftPlane, p)
				&& isPointInside(rightPlane, p) && isPointInside(nearPlane, p);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Frustum[\r\n" + //
				"Top " + topPlane + "\r\n" + //
				"Bottom " + bottomPlane + "\r\n" + //
				"Left " + leftPlane + "\r\n" + //
				"Right " + rightPlane + "\r\n" + //
				"Near " + nearPlane + "\r\n" + //
				"Far " + farPlane + "\r\n" + //
				"]";
	}

}