aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/olacraft/render/jogl/PlaneRenderer.java
blob: 754a093cedc7965377cefd5e72f22f844bfcf8d2 (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
package ru.olamedia.olacraft.render.jogl;

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;

import ru.olamedia.math.Plane;

public class PlaneRenderer {
	private static float getZ(Plane p, float x, float y) {
		return -(p.n.x * x + p.n.y * y + p.d) / p.n.z;
	}

	private static float getY(Plane p, float x, float z) {
		return -(p.n.x * x + p.n.z * z + p.d) / p.n.y;
	}

	private static float getX(Plane p, float y, float z) {
		return -(p.n.y * y + p.n.z * z + p.d) / p.n.x;
	}

	public static void render(Plane p, GLAutoDrawable drawable) {
		GL2 gl = drawable.getGL().getGL2();
		float size = 100;
		float step = size / 5;
		for (float x = -size; x <= size; x += step) {
			for (float y = -size; y <= size; y += step) {
				// nx * x + ny * y + nz * z + d = 0
				// (z = nx * x + ny * y + d) / nz
				float z = getZ(p, x, y);
				float x2 = x + step;
				float y2 = y;
				float z2 = getZ(p, x2, y2);
				float x3 = x + step;
				float y3 = y + step;
				float z3 = getZ(p, x3, y3);
				float x4 = x;
				float y4 = y + step;
				float z4 = getZ(p, x4, y4);
				gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
				gl.glBegin(GL2.GL_QUADS);
				{
					gl.glVertex3f(x, y, z);
					gl.glVertex3f(x2, y2, z2);
					gl.glVertex3f(x3, y3, z3);
					gl.glVertex3f(x4, y4, z4);
				}
				gl.glEnd();
			}
		}
		for (float x = -size; x <= size; x += step) {
			for (float z = -size; z <= size; z += step) {
				// nx * x + ny * y + nz * z + d = 0
				// (z = nx * x + ny * y + d) / nz
				float y = getY(p, x, z);
				float x2 = x + step;
				float z2 = z;
				float y2 = getY(p, x2, z2);
				float x3 = x + step;
				float z3 = z + step;
				float y3 = getY(p, x3, z3);
				float x4 = x;
				float z4 = z + step;
				float y4 = getY(p, x4, z4);
				gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
				gl.glBegin(GL2.GL_QUADS);
				{
					gl.glVertex3f(x, y, z);
					gl.glVertex3f(x2, y2, z2);
					gl.glVertex3f(x3, y3, z3);
					gl.glVertex3f(x4, y4, z4);
				}
				gl.glEnd();
			}
		}

		for (float y = -size; y <= size; y += step) {
			for (float z = -size; z <= size; z += step) {
				// nx * x + ny * y + nz * z + d = 0
				// (z = nx * x + ny * y + d) / nz
				float x = getX(p, y, z);
				float y2 = y + step;
				float z2 = z;
				float x2 = getX(p, y2, z2);
				float y3 = y + step;
				float z3 = z + step;
				float x3 = getX(p, y3, z3);
				float y4 = y;
				float z4 = z + step;
				float x4 = getX(p, y4, z4);
				gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
				gl.glBegin(GL2.GL_QUADS);
				{
					gl.glVertex3f(x, y, z);
					gl.glVertex3f(x2, y2, z2);
					gl.glVertex3f(x3, y3, z3);
					gl.glVertex3f(x4, y4, z4);
				}
				gl.glEnd();
			}
		}
	}
}