-
Notifications
You must be signed in to change notification settings - Fork 11
/
Cam.pde
132 lines (109 loc) · 2.65 KB
/
Cam.pde
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
// https://processing.org/reference/camera_.html
class Cam {
PVector pos = new PVector(0,0,0);
PVector poi = new PVector(0,0,0);
PVector up = new PVector(0,0,0);
PVector mouse = new PVector(0,0,0);
PGraphics3D p3d;
PMatrix3D proj, cam, modvw, modvwInv, screen2Model;
String displayText = "";
PFont font;
int fontSize = 12;
void init() {
p3d = (PGraphics3D) g;
//proj = new PMatrix3D();
cam = new PMatrix3D();
//modvw = new PMatrix3D();
modvwInv = new PMatrix3D();
screen2Model = new PMatrix3D();
font = createFont("Arial", fontSize);
}
PVector screenToWorldCoords(PVector p) {
//proj = p3d.projection.get();
cam = p3d.modelview.get(); //camera.get();
//modvw = p3d.modelview.get();
modvwInv = p3d.modelviewInv.get();
screen2Model = modvwInv;
screen2Model.apply(cam);
float screen[] = { p.x, p.y, p.z };
float model[] = { 0, 0, 0 };
model = screen2Model.mult(screen, model);
return new PVector(model[0] + (poi.x - width/2), model[1] + (poi.y - height/2), model[2]);
}
void screenToWorldMouse() {
mouse = screenToWorldCoords(new PVector(mouseX, mouseY, poi.z));
}
Cam() {
defaultPos();
defaultPoi();
defaultUp();
init();
}
Cam(PVector _pos) {
pos = _pos;
defaultPoi();
defaultUp();
init();
}
Cam(PVector _pos, PVector _poi) {
pos = _pos;
poi = _poi;
defaultUp();
init();
}
Cam(PVector _pos, PVector _poi, PVector _up) {
pos = _pos;
poi = _poi;
up = _up;
init();
}
void update() {
screenToWorldMouse();
}
void draw() {
camera(pos.x, pos.y, pos.z, poi.x, poi.y, poi.z, up.x, up.y, up.z);
drawText();
}
void run() {
update();
draw();
}
void move(float x, float y, float z) {
PVector p = new PVector(x,y,z);
pos = pos.add(p);
poi = poi.add(p);
}
void defaultPos() {
pos.x = width/2.0;
pos.y = height/2.0;
pos.z = (height/2.0) / tan(PI*30.0 / 180.0);
}
void defaultPoi() {
poi.x = width/2.0;
poi.y = height/2.0;
poi.z = 0;
}
void defaultUp() {
up.x = 0;
up.y = 1;
up.z = 0;
}
void reset() {
defaultPos();
defaultPoi();
defaultUp();
}
void drawText() {
if (!displayText.equals("")) {
pushMatrix();
translate((pos.x - (width/2)) + (fontSize/2), (pos.y - (height/2)) + fontSize, poi.z);
textFont(font, fontSize);
text(displayText, 0, 0);
popMatrix();
}
}
}
// TODO
// https://processing.org/reference/frustum_.html
// https://processing.org/reference/beginCamera_.html
// https://processing.org/reference/endCamera_.html