|
12 | 12 |
|
13 | 13 | public class ObjectSelectionRender { |
14 | 14 |
|
15 | | - private ColorGenerator generator; |
16 | | - private int width; |
17 | | - private int height; |
18 | | - private PApplet p; |
19 | | - private PGraphics3D g3d; |
20 | | - private HashMap<Integer, String> colorToObjectName; |
21 | | - private HashMap<String, Integer> nameToColor; |
22 | | - |
23 | | - public ObjectSelectionRender(PApplet p) { |
24 | | - colorToObjectName = new HashMap<Integer, String>(); |
25 | | - nameToColor = new HashMap<String, Integer>(); |
26 | | - generator = new ColorGenerator(); |
27 | | - this.p = p; |
28 | | - g3d = (PGraphics3D) p.createGraphics(p.width, p.height, PApplet.P3D); |
29 | | - p.registerMethod("pre", this); |
30 | | - } |
31 | | - |
32 | | - public void pre() { |
33 | | - if (p.width != this.width || p.height != this.height) { |
34 | | - this.width = p.width; |
35 | | - this.height = p.height; |
36 | | - g3d = (PGraphics3D) p.createGraphics(p.width, p.height, PApplet.P3D); |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - public String getObject(int x, int y) { |
41 | | - int color = getColor(x, y); |
42 | | - return colorToObjectName.get(color); |
43 | | - } |
44 | | - |
45 | | - public int getColor(int x, int y) { |
46 | | - int color = g3d.get(x, y); |
47 | | - return color; |
48 | | - } |
49 | | - |
50 | | - public void draw(List<SceneObject> sceneObjects) { |
51 | | - g3d.beginDraw(); |
52 | | - g3d.setMatrix(p.getMatrix()); |
53 | | - g3d.noLights(); |
54 | | - g3d.background(0, 0, 0, 0); |
55 | | - g3d.noStroke(); |
56 | | - for (SceneObject sceneObject : sceneObjects) |
57 | | - drawBuffer(sceneObject); |
58 | | - g3d.endDraw(); |
59 | | - } |
60 | | - |
61 | | - public void drawColorBuffer() { |
62 | | - if (g3d == null) |
63 | | - return; |
64 | | - p.image(g3d, 0, 0, p.width, p.height); |
65 | | - } |
66 | | - |
67 | | - private void drawBuffer(SceneObject sceneObject) { |
68 | | - PGraphics3D context = g3d; |
69 | | - Mesh3D mesh = sceneObject.getMesh(); |
70 | | - |
71 | | - int c = -1; |
72 | | - if (nameToColor.containsKey(sceneObject.getName())) { |
73 | | - c = nameToColor.get(sceneObject.getName()); |
74 | | - } else { |
75 | | - c = generator.next().getRGBA(); |
76 | | - nameToColor.put(sceneObject.getName(), c); |
77 | | - } |
78 | | - |
79 | | - this.colorToObjectName.put(c, sceneObject.getName()); |
80 | | - |
81 | | - context.fill(c); |
82 | | - |
83 | | - context.pushMatrix(); |
84 | | - |
85 | | - for (int i = 0; i < mesh.faces.size(); i++) { |
86 | | - Face3D f = mesh.getFaceAt(i); |
87 | | - Vector3f v; |
88 | | - |
89 | | - if (f.indices.length == 3) { |
90 | | - context.beginShape(PApplet.TRIANGLES); |
91 | | - } |
92 | | - |
93 | | - if (f.indices.length == 4) { |
94 | | - context.beginShape(PApplet.QUADS); |
95 | | - } |
96 | | - |
97 | | - if (f.indices.length > 4) { |
98 | | - context.beginShape(); |
99 | | - } |
100 | | - |
101 | | - for (int j = 0; j < f.indices.length; j++) { |
102 | | - v = mesh.vertices.get(f.indices[j]); |
103 | | - context.vertex(v.getX(), v.getY(), v.getZ()); |
104 | | - } |
105 | | - |
106 | | - if (f.indices.length > 4) { |
107 | | - context.endShape(PApplet.CLOSE); |
108 | | - } else { |
109 | | - context.endShape(); |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - context.popMatrix(); |
114 | | - } |
| 15 | + private ColorGenerator generator; |
| 16 | + |
| 17 | + private int width; |
| 18 | + |
| 19 | + private int height; |
| 20 | + |
| 21 | + private PApplet p; |
| 22 | + |
| 23 | + private PGraphics3D g3d; |
| 24 | + |
| 25 | + private HashMap<Integer, String> colorToObjectName; |
| 26 | + |
| 27 | + private HashMap<String, Integer> nameToColor; |
| 28 | + |
| 29 | + public ObjectSelectionRender(PApplet p) { |
| 30 | + colorToObjectName = new HashMap<Integer, String>(); |
| 31 | + nameToColor = new HashMap<String, Integer>(); |
| 32 | + generator = new ColorGenerator(); |
| 33 | + this.p = p; |
| 34 | + g3d = (PGraphics3D) p.createGraphics(p.width, p.height, PApplet.P3D); |
| 35 | + p.registerMethod("pre", this); |
| 36 | + } |
| 37 | + |
| 38 | + public void pre() { |
| 39 | + if (p.width != this.width || p.height != this.height) { |
| 40 | + this.width = p.width; |
| 41 | + this.height = p.height; |
| 42 | + g3d = (PGraphics3D) p |
| 43 | + .createGraphics(p.width, p.height, PApplet.P3D); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public String getObject(int x, int y) { |
| 48 | + int color = getColor(x, y); |
| 49 | + return colorToObjectName.get(color); |
| 50 | + } |
| 51 | + |
| 52 | + public int getColor(int x, int y) { |
| 53 | + int color = g3d.get(x, y); |
| 54 | + return color; |
| 55 | + } |
| 56 | + |
| 57 | + public void draw(List<SceneObject> sceneObjects) { |
| 58 | + g3d.beginDraw(); |
| 59 | + g3d.setMatrix(p.getMatrix()); |
| 60 | + g3d.noLights(); |
| 61 | + g3d.background(0, 0, 0, 0); |
| 62 | + g3d.noStroke(); |
| 63 | + for (SceneObject sceneObject : sceneObjects) |
| 64 | + drawBuffer(sceneObject); |
| 65 | + g3d.endDraw(); |
| 66 | + } |
| 67 | + |
| 68 | + public void drawColorBuffer() { |
| 69 | + if (g3d == null) |
| 70 | + return; |
| 71 | + p.image(g3d, 0, 0, p.width, p.height); |
| 72 | + } |
| 73 | + |
| 74 | + private void drawBuffer(SceneObject sceneObject) { |
| 75 | + PGraphics3D context = g3d; |
| 76 | + Mesh3D mesh = sceneObject.getMesh(); |
| 77 | + |
| 78 | + int c = -1; |
| 79 | + if (nameToColor.containsKey(sceneObject.getName())) { |
| 80 | + c = nameToColor.get(sceneObject.getName()); |
| 81 | + } else { |
| 82 | + c = generator.next().getRGBA(); |
| 83 | + nameToColor.put(sceneObject.getName(), c); |
| 84 | + } |
| 85 | + |
| 86 | + this.colorToObjectName.put(c, sceneObject.getName()); |
| 87 | + |
| 88 | + context.fill(c); |
| 89 | + |
| 90 | + context.pushMatrix(); |
| 91 | + |
| 92 | + for (int i = 0; i < mesh.faces.size(); i++) { |
| 93 | + Face3D f = mesh.getFaceAt(i); |
| 94 | + Vector3f v; |
| 95 | + |
| 96 | + if (f.indices.length == 3) { |
| 97 | + context.beginShape(PApplet.TRIANGLES); |
| 98 | + } |
| 99 | + |
| 100 | + if (f.indices.length == 4) { |
| 101 | + context.beginShape(PApplet.QUADS); |
| 102 | + } |
| 103 | + |
| 104 | + if (f.indices.length > 4) { |
| 105 | + context.beginShape(); |
| 106 | + } |
| 107 | + |
| 108 | + for (int j = 0; j < f.indices.length; j++) { |
| 109 | + v = mesh.vertices.get(f.indices[j]); |
| 110 | + context.vertex(v.getX(), v.getY(), v.getZ()); |
| 111 | + } |
| 112 | + |
| 113 | + if (f.indices.length > 4) { |
| 114 | + context.endShape(PApplet.CLOSE); |
| 115 | + } else { |
| 116 | + context.endShape(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + context.popMatrix(); |
| 121 | + } |
115 | 122 |
|
116 | 123 | } |
0 commit comments