aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/math/FrustumUtil.java
blob: 80b01fbd9b8c789d00eec67270a2999e6d0c1f00 (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
package ru.olamedia.math;

public class FrustumUtil {
	public static Plane[] extractPlanes(javax.vecmath.Matrix4f m, boolean normalize) {

		Plane[] planes = new Plane[6];
		for (int j = 0; j < 6; j++) {
			planes[j] = new Plane();
		}
		// Left: [30+00, 31+01, 32+02, 33+03]

		planes[0].n.x = m.m30 + m.m00;
		planes[0].n.y = m.m31 + m.m01;
		planes[0].n.z = m.m32 + m.m02;
		planes[0].d = m.m33 + m.m03;

		// Right: [30-00, 31-01, 32-02, 33-03]

		planes[1].n.x = m.m30 - m.m00;
		planes[1].n.y = m.m31 - m.m01;
		planes[1].n.z = m.m32 - m.m02;
		planes[1].d = m.m33 - m.m03;

		// Bottom: [30+10, 31+11, 32+12, 33+13]

		planes[2].n.x = m.m30 + m.m10;
		planes[2].n.y = m.m31 + m.m11;
		planes[2].n.z = m.m32 + m.m12;
		planes[2].d = m.m33 + m.m13;

		// Top: [30-10, 31-11, 32-12, 33-13]

		planes[3].n.x = m.m30 - m.m10;
		planes[3].n.y = m.m31 - m.m11;
		planes[3].n.z = m.m32 - m.m12;
		planes[3].d = m.m33 - m.m13;

		// Near: [30+20, 31+21, 32+22, 33+23]

		planes[4].n.x = m.m30 + m.m20;
		planes[4].n.y = m.m31 + m.m21;
		planes[4].n.z = m.m32 + m.m22;
		planes[4].d = m.m33 + m.m23;

		// Far: [30-20, 31-21, 32-22, 33-23]

		planes[5].n.x = m.m30 - m.m20;
		planes[5].n.y = m.m31 - m.m21;
		planes[5].n.z = m.m32 - m.m22;
		planes[5].d = m.m33 - m.m23;

		// Normalize
		if (normalize) {
			for (int i = 0; i < 6; ++i) {
				planes[i].normalize();
			}
		}
		return planes;
	}

	public static Frustum extractFrustum(javax.vecmath.Matrix4f m) {
		Frustum f = new Frustum();
		Plane[] planes = extractPlanes(m, true);
		f.leftPlane.set(planes[0]);
		f.rightPlane.set(planes[1]);
		f.bottomPlane.set(planes[2]);
		f.topPlane.set(planes[3]);
		f.nearPlane.set(planes[4]);
		f.farPlane.set(planes[5]);
		return f;
	}
}