-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNodeAPI2.java
More file actions
386 lines (352 loc) · 9.68 KB
/
NodeAPI2.java
File metadata and controls
386 lines (352 loc) · 9.68 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* Node API.
* by Jean Pierre Charalambos.
* <p>
* This example illustrates the powerful Node API used to convert points and
* vectors along a node hierarchy. The following node hierarchy is implemented:
* <p>
* world
* ^
* |\
* | \
* f1 eye
* ^ ^
* |\ \
* | \ \
* f2 f3 f5
* ^
* |
* |
* f4
* <p>
* Press the space bar to browse the different conversion methods shown here.
*/
import nub.core.Node;
import nub.primitives.Quaternion;
import nub.primitives.Vector;
import nub.processing.Scene;
import processing.core.PApplet;
import processing.core.PFont;
import processing.event.MouseEvent;
/**
* Thorough node api test (2)
*/
public class NodeAPI2 extends PApplet {
Scene scene;
InteractiveNode f1, f2, f3, f4, f5;
Vector pnt = new Vector(40, 30, 20);
Vector vec = new Vector(50, 50, 50);
PFont font16, font13;
Mode mode;
int wColor = color(255, 255, 255);
int f1Color = color(255, 0, 0);
int f2Color = color(0, 255, 0);
int f3Color = color(0, 0, 255);
int f4Color = color(255, 0, 255);
int f5Color = color(255, 255, 0);
public enum Mode {
m1, m2, m3, m4, m5, m6
}
//Choose FX2D, JAVA2D, P2D or P3D
String renderer = P3D;
public void settings() {
size(900, 900, renderer);
}
public void setup() {
scene = new Scene(this, 200);
mode = Mode.m1;
scene.fit(1000);
f1 = new InteractiveNode(scene, f1Color);
f1.translate(-50, -20, 30);
f1.scale(1.3f);
f2 = new InteractiveNode(scene, f1, f2Color);
f2.translate(60, -40, -30);
f2.scale(1.2f);
f3 = new InteractiveNode(scene, f1, f3Color);
f3.translate(60, 55, -30);
f3.rotate(new Quaternion(new Vector(0, 1, 0), -HALF_PI));
f3.scale(1.1f);
f4 = new InteractiveNode(scene, f2, f4Color);
f4.translate(60, -55, 30);
f4.rotate(new Quaternion(new Vector(0, 1, 0), QUARTER_PI));
f4.scale(0.9f);
f5 = new InteractiveNode(scene, scene.eye(), f5Color);
f5.translate(-100, 0, -250);
font16 = loadFont("FreeSans-16.vlw");
font13 = loadFont("FreeSans-13.vlw");
}
public void draw() {
background(0);
//world:
scene.drawAxes();
pushStyle();
stroke(wColor);
strokeWeight(10);
point(pnt.x(), pnt.y(), pnt.z());
popStyle();
scene.render();
drawMode();
displayText();
}
void drawMode() {
// points
pushStyle();
noStroke();
fill(0, 255, 255);
switch (mode) {
///*
case m1: // f2 -> world
drawArrowConnectingPoints(f2.worldLocation(pnt));
break;
case m2: // f2 -> f1
drawArrowConnectingPoints(f1, f1.location(pnt, f2));
break;
case m3: // f1 -> f2
drawArrowConnectingPoints(f2, f2.location(pnt, f1));
break;
case m4: // f3 -> f4
drawArrowConnectingPoints(f4, f4.location(pnt, f3));
break;
case m5: // f4 -> f3
drawArrowConnectingPoints(f3, f3.location(pnt, f4));
break;
case m6: // f5 -> f4
drawArrowConnectingPoints(f4, f4.location(pnt, f5));
break;
//*/
/*
case m1: // f2 -> world
drawArrowConnectingPoints(f2.worldLocation(pnt));
break;
case m2: // f2 -> f1
drawArrowConnectingPoints(f1, f1.location(f2.worldLocation(pnt)));
break;
case m3: // f1 -> f2
drawArrowConnectingPoints(f2, f2.location(f1.worldLocation(pnt)));
break;
case m4: // f3 -> f4
drawArrowConnectingPoints(f4, f4.location(f3.worldLocation(pnt)));
break;
case m5: // f4 -> f3
drawArrowConnectingPoints(f3, f3.location(f4.worldLocation(pnt)));
break;
case m6: // f5 -> f4
drawArrowConnectingPoints(f4, f4.location(f5.worldLocation(pnt)));
break;
// */
}
popStyle();
// vectors
pushStyle();
noStroke();
fill(125);
switch (mode) {
///*
case m1: // f2 -> world
drawVector(f2, vec);
drawVector(f2.worldDisplacement(vec));
break;
case m2: // f2 -> f1
drawVector(f2, vec);
drawVector(f1, f1.displacement(vec, f2));
break;
case m3: // f1 -> f2
drawVector(f1, vec);
drawVector(f2, f2.displacement(vec, f1));
break;
case m4: // f3 -> f4
drawVector(f3, vec);
drawVector(f4, f4.displacement(vec, f3));
break;
case m5: // f4 -> f3
drawVector(f4, vec);
drawVector(f3, f3.displacement(vec, f4));
break;
case m6: // f5 -> f4
drawVector(f5, vec);
drawVector(f4, f4.displacement(vec, f5));
break;
//*/
/*
case m1: // f2 -> world
drawVector(f2, vec);
drawVector(f2.worldDisplacement(vec));
break;
case m2: // f2 -> f1
drawVector(f2, vec);
drawVector(f1, f1.displacement(f2.worldDisplacement(vec)));
break;
case m3: // f1 -> f2
drawVector(f1, vec);
drawVector(f2, f2.displacement(f1.worldDisplacement(vec)));
break;
case m4: // f3 -> f4
drawVector(f3, vec);
drawVector(f4, f4.displacement(f3.worldDisplacement(vec)));
break;
case m5: // f4 -> f3
drawVector(f4, vec);
drawVector(f3, f3.displacement(f4.worldDisplacement(vec)));
break;
case m6: // f5 -> f4
drawVector(f5, vec);
drawVector(f4, f4.displacement(f5.worldDisplacement(vec)));
break;
//*/
}
popStyle();
}
void displayText() {
pushStyle();
Vector pos;
scene.beginHUD();
textFont(font13);
fill(f1Color);
pos = scene.screenLocation(f1.worldPosition());
text("Node 1", pos.x(), pos.y());
fill(f2Color);
pos = scene.screenLocation(f2.worldPosition());
text("Node 2", pos.x(), pos.y());
fill(f3Color);
pos = scene.screenLocation(f3.worldPosition());
text("Node 3", pos.x(), pos.y());
fill(f4Color);
pos = scene.screenLocation(f4.worldPosition());
text("Node 4", pos.x(), pos.y());
fill(f5Color);
pos = scene.screenLocation(f5.worldPosition());
text("Node 5", pos.x(), pos.y());
fill(wColor);
textFont(font16);
text("Press the space bar to change mode", 5, 15);
switch (mode) {
case m1: // f2 -> world
text("Converts vectors (grey arrows) and points (see the cyan arrow) from node 2 to world", 5, 35);
break;
case m2: // f2 -> f1
text("Converts vectors (grey arrows) and points (see the cyan arrow) from node 2 to node 1", 5, 35);
break;
case m3: // f1 -> f2
text("Converts vectors (grey arrows) and points (see the cyan arrow) from node 1 to node 2", 5, 35);
break;
case m4: // f3 -> f4
text("Converts vectors (grey arrows) and points (see the cyan arrow) from node 3 to node 4", 5, 35);
break;
case m5: // f4 -> f3
text("Converts vectors (grey arrows) and points (see the cyan arrow) from node 4 to node 3", 5, 35);
break;
case m6: // f5 -> f4
text("Converts vectors (grey arrows) and points (see the cyan arrow) from node 5 to node 4", 5, 35);
break;
}
scene.endHUD();
popStyle();
}
void drawArrowConnectingPoints(Vector to) {
drawArrow(null, pnt, to);
}
void drawArrowConnectingPoints(Node node, Vector to) {
drawArrow(node, pnt, to);
}
void drawVector(Vector to) {
drawArrow(null, new Vector(), to);
}
void drawVector(Node node, Vector to) {
drawArrow(node, new Vector(), to);
}
void drawArrow(Node node, Vector from, Vector to) {
if (node != null) {
pushMatrix();
// TODO fix me!
//scene.applyWorldTransformation(node);
scene.drawArrow(from, to, 1);
popMatrix();
} else
scene.drawArrow(from, to, 1);
}
public void keyPressed() {
if (key == ' ')
switch (mode) {
case m1:
mode = Mode.m2;
break;
case m2:
mode = Mode.m3;
break;
case m3:
mode = Mode.m4;
break;
case m4:
mode = Mode.m5;
break;
case m5:
mode = Mode.m6;
break;
case m6:
mode = Mode.m1;
break;
}
if (key == 'v' || key == 'V')
Scene.leftHanded = !Scene.leftHanded;
if (key == '+')
scene.eye().setMagnitude(scene.eye().magnitude() * 1.1f);
if (key == '-')
scene.eye().setMagnitude(scene.eye().magnitude() / 1.1f);
}
@Override
public void mouseMoved(MouseEvent event) {
if (event.isControlDown())
return;
if (event.isShiftDown())
scene.shift();
else if (scene.node() == null)
scene.lookAround();
else
scene.spin();
}
@Override
public void mouseDragged() {
if (mouseButton == LEFT)
scene.spin();
else if (mouseButton == RIGHT)
scene.shift();
else
scene.moveForward(mouseX - pmouseX);
}
@Override
public void mouseWheel(MouseEvent event) {
scene.zoom(event.getCount() * 20);
}
@Override
public void mouseClicked(MouseEvent event) {
if (event.getCount() == 1)
scene.updateTag();
else if (event.getCount() == 2)
if (event.getButton() == LEFT)
scene.focus();
else
scene.align();
}
public class InteractiveNode extends Node {
int _c;
Vector pnt;
Scene scene;
public InteractiveNode(Scene graph, int color) {
super();
enableHint(Node.BULLSEYE | Node.AXES);
scene = graph;
_c = color;
pnt = new Vector(40, 30, 20);
}
public InteractiveNode(Scene graph, Node node, int color) {
super(node);
enableHint(Node.BULLSEYE | Node.AXES);
scene = graph;
_c = color;
pnt = new Vector(40, 30, 20);
}
}
public static void main(String[] args) {
PApplet.main(new String[]{"NodeAPI2"});
}
}