ofDocsexamples 3d cameraParentingExample src ofApp.cpp
#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){ 
	
}