Getting Started with Augmented Reality / FLARToolKit
Create Your First Project with Augmented Reality
Setup a View Class
The FLARManager comes with a Simple Cubes example that allows you to use multiple markers and display a different object on each cube, we are going to take a bit of a different approach. You can do this anywhere you like but I am just going to create a views folder in the root of the src and then a class in there named HelloFlarView.as .
Now we are going to copy most of what was in the Simple Cubes Example with a couple small changes/modifications. Open up the class you just created and past the following into it.
-
// flash
-
import flash.display.Sprite;
-
import flash.events.Event;
-
import flash.utils.Dictionary;
-
// flartoolkit
-
import org.libspark.flartoolkit.core.param.FLARParam;
-
import org.libspark.flartoolkit.pv3d.FLARCamera3D;
-
// pv3d
-
import org.papervision3d.lights.PointLight3D;
-
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
-
import org.papervision3d.materials.utils.MaterialsList;
-
import org.papervision3d.objects.DisplayObject3D;
-
import org.papervision3d.objects.primitives.Cube;
-
import org.papervision3d.render.LazyRenderEngine;
-
import org.papervision3d.scenes.Scene3D;
-
import org.papervision3d.view.stats.StatsView;
-
import org.papervision3d.view.Viewport3D;
-
// flarmanager
-
import com.transmote.utils.geom.FLARPVGeomUtils;
-
import com.transmote.flar.FLARMarker;
-
-
/**
-
* …
-
* @author Marc Pelland
-
*/
-
public class HelloFlarView extends Sprite {
-
-
private static const CUBE_SIZE:Number = 40;
-
private var viewport3D:Viewport3D;
-
private var camera3D:FLARCamera3D;
-
private var scene3D:Scene3D;
-
private var renderEngine:LazyRenderEngine;
-
private var pointLight3D:PointLight3D;
-
private var statsView:StatsView;
-
private var stats:Boolean = true; // display the stats view
-
private var marker:FLARMarker;
-
private var container:DisplayObject3D;
-
-
/**
-
* create your view
-
* @param cameraParams
-
* @param viewportWidth
-
* @param viewportHeight
-
*/
-
public function HelloFlarView(cameraParams:FLARParam, viewportWidth:Number, viewportHeight:Number) {
-
init3D(cameraParams, viewportWidth, viewportHeight);
-
}
-
-
/**
-
* initialize the 3d scene
-
* @param cameraParams
-
* @param viewportWidth
-
* @param viewportHeight
-
*/
-
private function init3D(cameraParams:FLARParam, viewportWidth:Number, viewportHeight:Number) :void {
-
// instantiate the container for the FLAR based scene
-
container = new DisplayObject3D();
-
scene3D = new Scene3D();
-
camera3D = new FLARCamera3D(cameraParams);
-
viewport3D = new Viewport3D(viewportWidth, viewportHeight);
-
addChild(viewport3D);
-
// start the render engine
-
renderEngine = new LazyRenderEngine(scene3D, camera3D, viewport3D);
-
// create the global light for the scene
-
pointLight3D = new PointLight3D();
-
pointLight3D.x = 1000;
-
pointLight3D.y = 1000;
-
pointLight3D.z = -1000;
-
// create the stats view
-
statsView = new StatsView(renderEngine);
-
addChild(statsView);
-
// add an enterframe listener to update teh scene and renderer
-
addEventListener(Event.ENTER_FRAME, onEnterFrame);
-
}
-
-
/**
-
* show and hide the stats view
-
*/
-
public function toggleStatsView():void {
-
if (stats) {
-
removeChild(statsView);
-
stats = false;
-
} else {
-
addChild(statsView);
-
stats = true;
-
}
-
}
-
-
/**
-
* take the marker and build the scene on it
-
* @param ARG_marker
-
*/
-
public function addMarker (ARG_marker:FLARMarker) :void {
-
// store marker
-
marker = ARG_marker
-
// build the 3d scene within the container
-
buildScene();
-
// add the container to the scene
-
scene3D.addChild(container);
-
}
-
-
/**
-
* build the 3d scene and add it to the container
-
*/
-
private function buildScene():void {
-
// create a new Cube, and place it inside a container (DisplayObject3D) for manipulation
-
var materialsList:MaterialsList = new MaterialsList({all: new FlatShadeMaterial(pointLight3D, 0xffffff, 0×000000)});
-
var cube:Cube = new Cube(materialsList, CUBE_SIZE, CUBE_SIZE, CUBE_SIZE);
-
cube.z = 20;
-
// add the cube to the container
-
container.addChild(cube);
-
}
-
-
/**
-
* clear the marker and remove all the content
-
* @param marker
-
*/
-
public function removeMarker (marker:FLARMarker) :void {
-
// find and remove marker
-
marker = null;
-
// find and remove corresponding container
-
for (var i:String in container.children) {
-
trace(container.children[i]);
-
container.removeChild(container.children[i]);
-
}
-
// remove the container
-
scene3D.removeChild(container);
-
}
-
-
private function onEnterFrame (evt:Event) :void {
-
// update the scene
-
updateScene();
-
// tell the renderer to render
-
renderEngine.render();
-
}
-
-
private function updateScene () :void {
-
// update all Cube containers according to the transformation matrix in their associated FLARMarkers
-
if (marker != null) container.transform = FLARPVGeomUtils.translateFLARMatrixToPVMatrix(marker.transformMatrix);
-
}
-
}
-
}
Read the documentation with that file to understand just what I did. It is really close to the SimpleCubes demo, but I removed a couple things that I didn’t really think were needed. I also removed all the references for multiple markers which we are not going to need.
If you would like to customize the 3d scene in any way, check out the buildScene function in the view.
Connect the Controller to Your Class
The controller class that comes with FLARManager is ideal if you are looking to have multiple patterns in the scene, but we just removed that functionality from our view so we will now remove it from the Manager. Paste the following code over your controller:
-
import views.HelloFlarView;
-
import flash.events.KeyboardEvent;
-
import com.transmote.flar.FLARCameraSource;
-
import com.transmote.flar.FLARLoaderSource;
-
import com.transmote.flar.FLARManager;
-
import com.transmote.flar.FLARMarkerEvent;
-
import com.transmote.flar.FLARPattern;
-
import flash.display.Sprite;
-
import flash.events.Event;
-
/**
-
* standard FLARToolkit Papervision3D example, with our friends the Cubes.
-
* code is borrowed heavily from Saqoosha, Mikko Haapoja, and Squidder.
-
* http://saqoosha.net/en/flartoolkit/start-up-guide/
-
* http://www.mikkoh.com/blog/?p=182
-
* http://www.squidder.com/2009/03/06/flar-how-to-multiple-instances-of-multiple-markers/#more-285
-
*
-
* @author Eric Socolofsky
-
* @url http://transmote.com/flar
-
* @see updated by marc pelland (www.marcpelland.com)
-
*/
-
[SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]
-
public class FLARManagerTest_PV3D extends Sprite {
-
-
private static const CAMERA_PARAMS_PATH:String = "../resources/flar/FLARparams.dat";
-
private static const PATTERN_PATH:String = "../resources/flar/patterns/";
-
private static const PATTERN_RESOLUTION:uint = 16;
-
-
private var pattern:FLARPattern;
-
private var flarManager:FLARManager;
-
private var flarLoader:FLARLoaderSource;
-
private var helloFlarView:HelloFlarView;
-
-
public function FLARManagerTest_PV3D () {
-
init();
-
}
-
-
private function init () :void {
-
// build list of FLARPatterns for FLARToolkit to detect
-
var patterns:Vector.<FLARPattern> = new Vector.<FLARPattern>();
-
patterns.push(new FLARPattern(PATTERN_PATH+"pattern.patt", PATTERN_RESOLUTION));
-
pattern = patterns[0];
-
// use Camera (default)
-
flarManager = new FLARManager(CAMERA_PARAMS_PATH, patterns);
-
addChild(FLARCameraSource(flarManager.flarSource));
-
// begin listening for FLARMarkerEvents
-
flarManager.addEventListener(FLARMarkerEvent.MARKER_ADDED, onMarkerAdded);
-
flarManager.addEventListener(FLARMarkerEvent.MARKER_REMOVED, onMarkerRemoved);
-
// the flar manager fires an init when it is ready
-
flarManager.addEventListener(Event.INIT, onFlarManagerInited);
-
}
-
-
/**
-
* key listener
-
* @param ARG_evt
-
*/
-
private function onKeyDown(ARG_evt:KeyboardEvent):void {
-
// if the user hit the S key
-
if (ARG_evt.keyCode == 83) {
-
helloFlarView.toggleStatsView();
-
}
-
}
-
-
/**
-
* when the flar manager has been initiated, initiate your custom view
-
* @param evt
-
*/
-
private function onFlarManagerInited (evt:Event) :void {
-
helloFlarView = new HelloFlarView(flarManager.cameraParams, stage.stageWidth, stage.stageHeight);
-
addChild(helloFlarView);
-
}
-
-
/**
-
* function triggered when a marker has been discovered
-
* @param evt
-
*/
-
private function onMarkerAdded (evt:FLARMarkerEvent) :void {
-
helloFlarView.addMarker(evt.marker);
-
}
-
-
/**
-
* triggered when the marker is no longer being discovered in the camera view
-
* @param evt
-
*/
-
private function onMarkerRemoved (evt:FLARMarkerEvent) :void {
-
helloFlarView.removeMarker(evt.marker);
-
}
-
}
-
}
** there is a line : var patterns:Vector.<FLARPattern> = new Vector.<FLARPattern>(); that isn’t displaying properly in the code block and i can’t figure it out… replace it with this previous line.
If you followed along in the documentation you will notice that I am doing a couple basic things, creating the pattern detection and listening for when that is found and lost. I have also added a key listener to add and remove a stats view from you Papervision scene to get a little more detail for debugging.
Now you are ready to test out the scene. I get pretty good results from such a simple setup, when it gets a little bit more complex you will notice a steady decline in frame rate. On the next page we will have a look at optimization.











February 7th, 2009 at 2:07 am
I bet many of PV3D developers are waiting for something like that. It looks very interesting, it’s a nice work even if its on an early state, i wonder if someday it will turn into a desktop app (maybe it could be added as an option).
April 29th, 2009 at 6:04 pm
Hi Marc
This tutorial its so awesome, its really answers me a lot of doubts about FLAR and how to handle it
I got a question.
How can I detect the angle of the container where all the items were placed, i mean, if I rotate the pattern over any angle in diferent axis(x,y,z), how can i get the currents angle(x,y,z) were the container is rotates
Best regards,
David Andres Niño
May 5th, 2009 at 10:37 pm
A error!!, please you can’t send project source files?
Spanish
Me ocurren errores cuando quiero ejecutar los archivos, me dice que las clases tienen errores, me podrias enviar el proyecto completo?
Gracias
May 6th, 2009 at 9:03 am
Hello !!
Thanks for great explainations on a difficult subject
(at least for me)
I followed all your step but I still get an error :
Error #1063: Argument count mismatch on HelloFlarView$iinit(). 3 expected, 0 found.
(translated from french sorry)
Any idea how to fix that ?
Thanks a lot !!!
May 13th, 2009 at 8:34 pm
It’s great, but when i rotate my face, it can’t tracking
July 16th, 2009 at 7:37 pm
I have two major problems following this tutorial:
1. if i chose flash 10 nothing will compile with the error Error: unknown configuration variable ‘target-player’
10.0.0
2. if i chose flash 9 i get the error FLARManagerTest_PV3D.as(44): col: 24 Error: Syntaxfehler: identifier vor lessthan erforderlich.
var patterns:Vector. = new Vector.();
^
Build halted with errors (mxmlc).
Done (1)
i guess it’s because i use newer versions, any idea how to fix this would be appreciated
August 14th, 2009 at 6:21 pm
Thanks for the tutorial. So far, so good.
August 19th, 2009 at 3:06 am
Hey. I believe that all government is evil, and that trying to improve it is largely a waste of time. Help me! Please help find sites for: Deep conditioners recipe. I found only this - best central air conditioners. Applications the sag fighter is a specific type of power conditioner used for protection against deep voltage sags deep voltage drops. In recent years, I purchased a “prize” yellow labrador puppy and began searching the market for a shampoo and conditioner that would serve that vivid memory. Thanks for the help :rolleyes:, Giva from Hungary.
August 29th, 2009 at 4:33 am
Excuse me. Computer Science is no more about computers than astronomy is about telescopes. Help me! Could you help me find sites on the: Stock to pick. I found only this - louis navellier stock picks. Barron’s agreed the shape that madoff’s tricks were most even such to basic making his player’s ivory coefficients, future stock picks. Best regards :mad:, Kachine from Cambodia.
October 19th, 2009 at 5:54 am
hello, i am tommy ,i am a student,i want to create some flash game use the face detection, but i dont know how can do it.i try serach it information in internet but i cant search the particular description to tell the face detection by flash.
could you tell me have any website have particular to tell the flash face detection ,thank you very much.
January 11th, 2010 at 7:08 pm
Would you be so kind as to share this code?
January 15th, 2010 at 1:12 pm
if you want to check out what you can do with this library I leave my projects page here http://facepong.blogspot.com/
January 24th, 2010 at 7:03 am
Hi there,
I am trying to follow your example but i get a black window (no camera input). Any idea how to fix this??
btw: for those having the lessthan error. Make sure you target Flash player 10 and also have the latest flex sdk
March 2nd, 2010 at 1:18 am
I have an error.
C:\Users\Carlos\HelloFlarToolkit\src\views\HelloFlarView.as(103): col: 108 Error: Syntax error.
var materialsList:MaterialsList = new MaterialsList({all: new FlatShadeMaterial(pointLight3D, 0xffffff, 0×000000)});
Anyone can help me?
March 2nd, 2010 at 10:10 pm
Alex, I have the same problem. I only see a black window. Did you fix the problem?
March 2nd, 2010 at 11:11 pm
looks like you are using a special character in the colour definition (0×000000), that isn’t an x that you are using. that could be the reason for that error