Normand Briere
2018-07-01 89c1ad67bc65d24ceadfa9e95f8c5515283f1e97
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
108
109
110
111
112
113
114
115
116
117
118
119
package mocap.scene;
 
import java.awt.Font;
import java.util.ArrayList;
 
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3d;
 
import com.sun.j3d.utils.geometry.Text2D;
 
/**
 * The floor is a green grid, with a small red square
 * at the (0,0) position on the (X,Z) plane, and with numbers along
 * the X- and Z- axes.
 */
public class Floor
{
 
    private final static int FLOOR_LEN = 30;  // should be even
    private final static Color3f CENTER_MARK_RED = new Color3f(0.7f, 0f, 0f);
    private final static Color3f NUMBERS_COLOR = new Color3f(.3f, .3f, .3f);
    private final static Color3f GRID_COLOR = new Color3f(.2f, .2f, .2f);
    private BranchGroup _floorBG;
 
    /**
     * create tiles, add origin marker, then the axes labels
     */
    public Floor()
    {
        ArrayList blueCoords = new ArrayList();
        _floorBG = new BranchGroup();
        for (int z = -FLOOR_LEN / 2; z <= (FLOOR_LEN / 2) - 1; z++) {
            for (int x = -FLOOR_LEN / 2; x <= (FLOOR_LEN / 2) - 1; x++) {
                createCoords(x, z, blueCoords);
            }
        }
        _floorBG.addChild(new Tiles(blueCoords, GRID_COLOR));
        addOriginMarker();
        labelAxes();
    }
 
    /**
     * Coors for a single square.
     * Its left hand corner at (x,0,z)
     */
    private void createCoords(int x, int z, ArrayList coords)
    {
        // points created in counter-clockwise order
        Point3f p1 = new Point3f(x, 0.0f, z + 1.0f);
        Point3f p2 = new Point3f(x + 1.0f, 0.0f, z + 1.0f);
        Point3f p3 = new Point3f(x + 1.0f, 0.0f, z);
        Point3f p4 = new Point3f(x, 0.0f, z);
        coords.add(p1);
        coords.add(p2);
        coords.add(p3);
        coords.add(p4);
    }
 
    private void addOriginMarker() // A red square centered at (0,0,0), of length 1
    {  // points created counter-clockwise, a bit above the floor
        Point3f p1 = new Point3f(-0.5f, 0.01f, 0.5f);
        Point3f p2 = new Point3f(0.5f, 0.01f, 0.5f);
        Point3f p3 = new Point3f(0.5f, 0.01f, -0.5f);
        Point3f p4 = new Point3f(-0.5f, 0.01f, -0.5f);
 
        ArrayList oCoords = new ArrayList();
        oCoords.add(p1);
        oCoords.add(p2);
        oCoords.add(p3);
        oCoords.add(p4);
 
        _floorBG.addChild(new Tiles(oCoords, CENTER_MARK_RED));
    }
 
    private void labelAxes() // Place numbers along the X- and Z-axes at the integer positions
    {
        Vector3d pt = new Vector3d();
        for (int i = -FLOOR_LEN / 2; i <= FLOOR_LEN / 2; i++) {
            pt.x = i;
            _floorBG.addChild(makeText(pt, "" + i));   // along x-axis
        }
 
        pt.x = 0;
        for (int i = -FLOOR_LEN / 2; i <= FLOOR_LEN / 2; i++) {
            pt.z = i;
            _floorBG.addChild(makeText(pt, "" + i));   // along z-axis
        }
    }
 
    private TransformGroup makeText(Vector3d vertex, String text) // Create a Text2D object at the specified vertex
    {
        Text2D message = new Text2D(text, NUMBERS_COLOR, "SansSerif", 24, Font.BOLD);
        // 36 point bold Sans Serif
 
        Appearance app = message.getAppearance();
        PolygonAttributes polyAttrib = new PolygonAttributes();
        polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
        polyAttrib.setBackFaceNormalFlip(true);
        app.setPolygonAttributes(polyAttrib);
 
        TransformGroup tg = new TransformGroup();
        Transform3D t3d = new Transform3D();
        t3d.setTranslation(vertex);
        tg.setTransform(t3d);
        tg.addChild(message);
        return tg;
    }
 
    public BranchGroup getBG()
    {
        return _floorBG;
    }
}