/
ofApp.cpp
309 lines (238 loc) · 7.92 KB
/
ofApp.cpp
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
#include "ofApp.h"
#define kMoveInc 10
#define kRotInc 5
// reset all transformations and options to defaults
//--------------------------------------------------------------
void ofApp::reset() {
camToView = 0;
camToConfigure = 1;
orbitRadius = 200;
for(int i=0; i<kNumCameras; i++) {
cam[i].resetTransform();
cam[i].setFov(60);
cam[i].clearParent();
lookatIndex[i] = -1; // don't lookat at any node
parentIndex[i] = -1; // don't parent to any node
doMouseOrbit[i] = false;
}
cam[0].setPosition(40, 40, 190);
doMouseOrbit[0] = true;
cam[1].setPosition(80, 40, 30);
lookatIndex[1] = kNumTestNodes-1; // look at smallest node
}
//--------------------------------------------------------------
void ofApp::setup(){
ofEnableDepthTest();
ofSetVerticalSync(true);
ofEnableLighting();
for(int i = 0; i < kNumLights; i++) {
light[i].enable();
}
reset();
// link all testNodes (parent to each other)
for(int i=0; i<kNumTestNodes; i++) {
if(i>0) testNodes[i].setParent(testNodes[i-1]);
}
}
//--------------------------------------------------------------
void ofApp::update(){
float freqMult = 1;
float amp = 30;
float scale = 1;
// all testNodes move in simple circles
// but because they are parented to each other, complex paths emerge
for(int i=0; i<kNumTestNodes; i++) {
testNodes[i].setPosition(glm::vec3(sin(ofGetElapsedTimef() * freqMult) * amp, cos(ofGetElapsedTimef() * freqMult) * amp, sin(ofGetElapsedTimef() * freqMult * 0.7) * amp));
testNodes[i].setOrientation(glm::vec3(sin(ofGetElapsedTimef() * freqMult * 0.2) * amp * 5, cos(ofGetElapsedTimef() * freqMult * 0.2) * amp * 5, sin(ofGetElapsedTimef() * freqMult * 0.2 * 0.7) * amp * 5));
testNodes[i].setScale(scale);
freqMult *= 3;
amp *= 0.8;
scale *= 0.8;
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofBackground(0);
string s = string("") +
"\n" +
"Purple boxes (4 of them) are generic nodes with simple circular motion, linked in a hierarchy (with ofNode::setParent).\n" +
"Yellow boxes (2 of them) are cameras. You are looking through one of them so can only see one box on screen.\n" +
"\n" +
"KEYS:\n" +
"\n" +
"z reset transforms\n" +
"\n" +
"v switch camera to view: " + ofToString(camToView) + "\n" +
"\n" +
"o toggle mouse orbit for cam\n" +
"\n" +
"c switch camera to configure: " + ofToString(camToConfigure) + "\n" +
" t cycle lookat\n" +
" p cycle parent\n" +
" LEFT pan left\n" +
" RIGHT pan right\n" +
" UP tilt up\n" +
" DOWN tilt down\n" +
" , roll left\n" +
" . roll right\n" +
" a truck left\n" +
" d truck right\n" +
" w dolly forward\n" +
" s dolly backward\n" +
" r boom up\n" +
" f boom down\n";
glDisable(GL_CULL_FACE);
ofSetColor(255);
ofDisableLighting();
ofDrawBitmapString(s, 20, 20);
glEnable(GL_CULL_FACE);
ofEnableLighting();
// update camera transforms
for(int i=0; i<kNumCameras; i++) {
// lookat node if it has one
if(lookatIndex[i] >= 0) cam[i].lookAt(testNodes[lookatIndex[i]], {0.f,1.f,0.f});
// mouse orbit camera
if(doMouseOrbit[i] && ofGetMousePressed(0)) {
static float lon = 0;
static float lat = 0;
lon = ofClamp(lon + mouseX - ofGetPreviousMouseX(), -180, 180);
lat = ofClamp(lat + mouseY - ofGetPreviousMouseY(), -90, 90);
if(lookatIndex[i] < 0) {
cam[i].orbitDeg(lon, lat, orbitRadius);
} else {
cam[i].orbitDeg(lon, lat, orbitRadius, testNodes[lookatIndex[1]]);
}
}
}
// activate camera
cam[camToView].begin();
// draw world axis
ofDrawAxis(100);
// draw testNodes
for(int i=0; i<kNumTestNodes; i++) {
ofSetColor(255, 128, 255);
testNodes[i].draw();
}
// draw cameras
for(int i=0; i<kNumCameras; i++) {
ofSetColor(255, 255, 0);
cam[i].draw();
// draw line from cam to its lookat
if(lookatIndex[i] >= 0) {
ofSetColor(0, 255, 255);
glm::vec3 v1 = cam[i].getGlobalPosition();
glm::vec3 v2 = testNodes[lookatIndex[i]].getGlobalPosition();
ofDrawLine(v1,v2);
}
// draw line from cam to its parent
if(parentIndex[i] >= 0) {
ofSetColor(255, 255, 0);
glm::vec3 v1 = cam[i].getGlobalPosition();
glm::vec3 v2 = testNodes[parentIndex[i]].getGlobalPosition();
ofDrawLine(v1,v2);
}
}
// restore view to previous state (default openFrameworks view)
cam[camToView].end();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
ofNode *n = &cam[camToConfigure];
switch(key) {
case 'c':
camToConfigure = 1 - camToConfigure;
cout << "\n\n** MOVING CAMERA " + ofToString(camToConfigure) + "**\n";
break;
case 'v':
camToView = 1 - camToView;
cout << "\n\n** SHOWING CAMERA " + ofToString(camToView) + "**\n";
break;
case OF_KEY_LEFT: n->panDeg(kRotInc); break;
case OF_KEY_RIGHT: n->panDeg(-kRotInc); break;
case OF_KEY_UP: n->tiltDeg(-kRotInc); break;
case OF_KEY_DOWN: n->tiltDeg(kRotInc); break;
case ',': n->rollDeg(kRotInc); break;
case '.': n->rollDeg(-kRotInc); break;
case 'a': n->truck(-kMoveInc); break;
case 'd': n->truck(kMoveInc); break;
case 'w': n->dolly(-kMoveInc); break;
case 's': n->dolly(kMoveInc); break;
case 'r': n->boom(kMoveInc); break;
case 'f': n->boom(-kMoveInc); break;
case 'o':
doMouseOrbit[camToConfigure] ^= true;
break;
// case 'O':
// doMouseOrbit = false;
// doAutoOrbit ^= true;
// break;
//
// // if(cam[camToView].getOrtho()) {
// // cam[camToView].disableOrtho();
// // } else {
// // cam[camToView].enableOrtho();
// // }
// break;
//
case 'z':
reset();
break;
case 't':
lookatIndex[camToConfigure]++ ;
if(lookatIndex[camToConfigure]>=kNumTestNodes) {
lookatIndex[camToConfigure] = -1;
// cam[camToConfigure].disableTarget();
// } else {
// cam[camToConfigure].setTarget(testNodes[parentIndex[camToConfigure]]);
}
break;
case 'p':
parentIndex[camToConfigure]++ ;
glm::vec3 oldP = cam[camToConfigure].getGlobalPosition();
glm::quat oldQ = cam[camToConfigure].getGlobalOrientation();
if(parentIndex[camToConfigure]>=kNumTestNodes) {
parentIndex[camToConfigure] = -1;
cam[camToConfigure].clearParent();
} else {
cam[camToConfigure].setParent(testNodes[parentIndex[camToConfigure]]);
}
cam[camToConfigure].setGlobalPosition(oldP);
cam[camToConfigure].setGlobalOrientation(oldQ);
break;
}
cout << "\n** MOVING CAMERA " << ofToString(camToConfigure) << " **\n";
cout << "POSITION: "+ ofToString(n->getX()) + " " + ofToString(n->getY()) + " " + ofToString(n->getZ()) + "\n";
cout << "X-AXIS: " + ofToString(n->getXAxis().x) + " " + ofToString(n->getXAxis().y) + " " + ofToString(n->getXAxis().z) + "\n";
cout << "Y-AXIS: " + ofToString(n->getYAxis().x) + " " + ofToString(n->getYAxis().y) + " " + ofToString(n->getYAxis().z) + "\n";
cout << "Z-AXIS: " + ofToString(n->getZAxis().x) + " " + ofToString(n->getZAxis().y) + " " + ofToString(n->getZAxis().z) + "\n";
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}