08. ARToolkit example

This example shows how to track augmented reality markers in OpenFrameworks with ARToolkit using the ofxARToolkitPlus addon.

testApp.h

/*
 *  ofxARToolkitPlus example
 *  openFrameworks wrapper addon for ARToolkitPlus
 * 
 *  Created by Rick Companje on 02-11-10.
 *
 */

#ifndef _TEST_APP
#define _TEST_APP

#include "ofMain.h"
#include "ofxOpenCv.h"
#include "ofxARToolkitPlus.h"

class testApp : public ofBaseApp {
public:

    void setup();
    void update();
    void draw();

    void keyPressed  (int key);
    
    ofVideoGrabber mov;
    ofxCvColorImage img;
    ofxCvGrayscaleImage gray;
        
    ofxARToolkitPlus tracker;
};

#endif

testApp.cpp

/*
 *  ofxARToolkitPlus example
 *  openFrameworks wrapper addon for ARToolkitPlus
 *  
 *  Created by Rick Companje on 02-11-10.
 *
 */

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup() {
    ofBackground(0,0,0);
    
    ofEnableAlphaBlending();

    mov.initGrabber(ofGetWidth(), ofGetHeight());
    img.allocate(ofGetWidth(), ofGetHeight());
    gray.allocate(ofGetWidth(), ofGetHeight());
    tracker.setup(ofGetWidth(), ofGetHeight());
}

//--------------------------------------------------------------
void testApp::update(){
        
    mov.update();
    
    if (mov.isFrameNew()) {
        img.setFromPixels(mov.getPixels(), ofGetWidth(), ofGetHeight());
        gray = img;
        tracker.setFromPixels(gray.getPixels());
    }
}

//--------------------------------------------------------------
void testApp::draw(){
    ofSetColor(0xffffff);
    mov.draw(0, 0);
    
    for (int i=0; i<tracker.markers.size(); i++) {
        ARMarkerInfo &m = tracker.markers[i];
        tracker.loadMarkerModelViewMatrix(m);
        ofSetColor(255, 255, 0, 100);
        ofCircle(0,0,25);
        ofSetColor(0);
        ofDrawBitmapString(ofToString(m.id),0,0);
    }
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
    if (key == 's') mov.videoSettings();
}

ċ
ARToolkitExample.zip
(378k)
Rick Companje,
31 jan. 2011 04:30
Comments