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