-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathOrthogonalViewer.java
More file actions
130 lines (117 loc) · 3.2 KB
/
OrthogonalViewer.java
File metadata and controls
130 lines (117 loc) · 3.2 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
import nub.core.Node;
import nub.primitives.Vector;
import nub.processing.Scene;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PShape;
import processing.event.MouseEvent;
/**
* Setting up a viewer orthogonal to the current view helps
* out to appreciate current objects depth within a scene
*/
public class OrthogonalViewer extends PApplet {
Scene scene, visualHint, focus;
int w = 500, h = 500, atX, atY;
Node[] models;
Vector orig = new Vector();
Vector dir = new Vector();
Vector end = new Vector();
Vector pup;
@Override
public void settings() {
size(1800, 1400, P3D);
}
public void setup() {
scene = new Scene(this, 1000);
scene.fit(1000);
models = new Node[100];
for (int i = 0; i < models.length; i++) {
models[i] = new Node(boxShape());
scene.randomize(models[i]);
}
visualHint = new Scene(createGraphics(w, h, P3D), 300);
}
public void draw() {
focus = pup != null ? visualHint.hasFocus() ? visualHint : scene : scene;
background(0);
drawRay();
scene.drawAxes();
scene.render();
if (pup != null) {
visualHint.openContext();
visualHint.context().background(125);
visualHint.render();
visualHint.closeContext();
visualHint.image(atX, atY);
}
}
void drawRay() {
PGraphics pg = scene.context();
if (pup != null) {
pg.pushStyle();
pg.strokeWeight(20);
pg.stroke(255, 255, 0);
pg.point(pup.x(), pup.y(), pup.z());
pg.strokeWeight(8);
pg.stroke(0, 0, 255);
pg.line(orig.x(), orig.y(), orig.z(), end.x(), end.y(), end.z());
pg.popStyle();
}
}
public void mouseClicked(MouseEvent event) {
if (event.getCount() == 1)
scene.focus();
else
scene.align();
}
PShape boxShape() {
PShape box = createShape(BOX, 60);
box.setFill(color(random(0, 255), random(0, 255), random(0, 255)));
return box;
}
@Override
public void mouseMoved(MouseEvent event) {
if (event.isControlDown()) {
pup = scene.location();
if (pup != null) {
visualHint.setCenter(pup);
visualHint.setRadius(300);
visualHint.eye().setWorldPosition(pup);
//hint.setViewDirection(scene.displacement(Vector.plusJ));
visualHint.setViewDirection(scene.displacement(new Vector(0, 1, 0)));
visualHint.setUpVector(scene.displacement(new Vector(0, 0, -1)));
visualHint.fit();
atX = mouseX - w / 2;
atY = mouseY - h;
// debug
scene.pixelToLine(orig, dir);
end = Vector.add(orig, Vector.multiply(dir, 4000.0f));
}
} else {
focus.tag();
}
}
@Override
public void mouseDragged() {
if (mouseButton == LEFT)
focus.spin();
else if (mouseButton == RIGHT)
focus.shift();
else
focus.moveForward(focus.mouseDX());
}
@Override
public void mouseWheel(MouseEvent event) {
focus.zoom(event.getCount() * 20);
}
@Override
public void keyPressed() {
if (key == ' ')
scene.togglePerspective();
if (key == 'f')
Scene.leftHanded = !Scene.leftHanded;
}
public static void main(String[] args) {
PApplet.main(new String[]{"OrthogonalViewer"});
}
}