#include "ofApp.h"
void ofApp::setup(){
ofEnableSmoothing();
ofSetVerticalSync(true);
video.setup(320, 240);
finder.setup("haarcascade_frontalface_default.xml");
usePreview = false;
previewCamera.setDistance(3.0f);
previewCamera.setNearClip(0.01f);
previewCamera.setFarClip(500.0f);
previewCamera.setPosition(0.4f, 0.2f, 0.8f);
previewCamera.lookAt(glm::vec3(0.0f, 0.0f, 0.0f));
headTrackedCamera.setNearClip(0.01f);
headTrackedCamera.setFarClip(1000.0f);
windowWidth = 0.3f;
windowHeight = 0.2f;
windowTopLeft = glm::vec3(-windowWidth / 2.0f,
+windowHeight / 2.0f,
0.0f);
windowBottomLeft = glm::vec3(-windowWidth / 2.0f,
- windowHeight / 2.0f,
0.0f);
windowBottomRight = glm::vec3(+windowWidth / 2.0f,
-windowHeight / 2.0f,
0.0f);
viewerDistance = 0.4f;
}
void ofApp::update(){
video.update();
finder.findHaarObjects(video.getPixels());
glm::vec3 headPosition(0,0,viewerDistance);
if (finder.blobs.size() > 0) {
const ofxCvBlob & blob = finder.blobs.front();
float cameraHeadX = blob.centroid.x;
float cameraHeadY = blob.centroid.y;
float worldHeadX = ofMap(cameraHeadX, 0, video.getWidth(), windowBottomRight.x, windowBottomLeft.x);
float worldHeadY = ofMap(cameraHeadY, 0, video.getHeight(), windowTopLeft.y, windowBottomLeft.y);
headPosition = glm::vec3(worldHeadX, worldHeadY, viewerDistance);
} else {
if (!video.isInitialized()) {
headPosition = glm::vec3(0.5f * windowWidth * sin(ofGetElapsedTimef()), 0.5f * windowHeight * cos(ofGetElapsedTimef()), viewerDistance);
}
}
headPositionHistory.push_back(headPosition);
while (headPositionHistory.size() > 50.0f){
headPositionHistory.pop_front();
}
headTrackedCamera.setPosition(headPosition);
headTrackedCamera.setupOffAxisViewPortal(windowTopLeft, windowBottomLeft, windowBottomRight);
}
void ofApp::drawScene(bool isPreview){
ofEnableDepthTest();
if (isPreview) {
ofPushStyle();
ofSetColor(150, 100, 100);
ofDrawGrid(1.0f, 5.0f, true);
ofSetColor(255);
headTrackedCamera.transformGL();
ofPushMatrix();
ofScale(0.002f, 0.002f, 0.002f);
ofNode().draw();
ofPopMatrix();
ofMultMatrix(glm::inverse(headTrackedCamera.getProjectionMatrix()));
ofNoFill();
ofDrawBox(2.0f);
headTrackedCamera.restoreTransformGL();
window.clear();
window.addVertex(windowTopLeft);
window.addVertex(windowBottomLeft);
window.addVertex(windowBottomRight);
window.setMode(OF_PRIMITIVE_LINE_STRIP);
window.draw();
glPointSize(3.0f);
window.drawVertices();
ofPopStyle();
}
ofPushStyle();
ofNoFill();
ofColor col(200,100,100);
for (float z = 0.0f; z > -40.0f; z-= 0.1f){
col.setHue(int(-z * 100.0f + ofGetElapsedTimef() * 10.0f) % 360);
ofSetColor(col);
ofDrawRectangle(-windowWidth / 2.0f, -windowHeight / 2.0f, z, windowWidth, windowHeight);
}
ofPopStyle();
ofPushStyle();
ofEnableSmoothing();
ofSetColor(255);
ofSetLineWidth(5.0f);
ofBeginShape();
for (unsigned int i=0; i<headPositionHistory.size(); i++) {
glm::vec3 vertex(headPositionHistory[i].x, headPositionHistory[i].y, -float( headPositionHistory.size() - i ) * 0.05f);
ofCurveVertex(vertex);
}
ofEndShape(false);
ofPopStyle();
ofDisableDepthTest();
}
void ofApp::draw(){
ofBackgroundGradient(ofColor(50), ofColor(0));
if (usePreview){
previewCamera.begin();
}
else{
headTrackedCamera.begin();
}
drawScene(usePreview);
if (usePreview){
previewCamera.end();
}
else{
headTrackedCamera.end();
}
video.draw(0, 0);
ofPushStyle();
ofNoFill();
for(unsigned int i = 0; i < finder.blobs.size(); i++) {
ofRectangle cur = finder.blobs[i].boundingRect;
ofDrawRectangle(cur.x, cur.y, cur.width, cur.height);
}
ofPopStyle();
stringstream message;
message << "[SPACE] = User preview camera [" << (usePreview ? 'x' : ' ') << "]";
ofDrawBitmapString(message.str(), video.getWidth() + 10, 20);
if (usePreview){
ofRectangle bottomLeft(0, ofGetHeight() - 200.0f, 300.0f, 200.0f);
ofPushStyle();
ofSetColor(0);
ofDrawRectangle(bottomLeft);
ofPopStyle();
headTrackedCamera.begin(bottomLeft);
drawScene(false);
headTrackedCamera.end();
}
}
void ofApp::keyPressed(int key){
if (key == ' ')
usePreview = !usePreview;
}
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){
}
Comments