transmote speaks…

design + art + code
  • portfolio
  • rss
  • Home
  • About
  • FLARManager: Augmented Reality in Flash
    • FLARManager intro
    • FLARManager documentation
    • Inside FLARManager
      • Inside FLARManager: Getting Started
      • Inside FLARManager: 2D Marker Tracking
      • Inside FLARManager: Basic Augmented Reality
      • Inside FLARManager: Loading Collada Models
      • Inside FLARManager: Customization
      • Inside FLARManager: FLARManager Miscellany
  • Contact

Inside FLARManager: Basic Augmented Reality

<– 2D Marker Tracking | Loading Collada Models –>
 
So, you want to create augmented reality in a browser. You saw the GE ad, you tried stuffing your cat into a USPS box, and now you want a piece.

This tutorial will run you through the basics of getting an augmented reality application up and running. You might want to try the 2D tutorial first, to get a handle on a Papervision-less FLARManager application. Or, you might just want to dive right in.

 

FLARManager Tutorial: 3D and Augmented Reality

Download source for this tutorial here:
FLARManagerTutorial_3D
and place it in the root of the /src/ FLARManager folder.
Print out any of the ‘pattNNN.png’ markers in this folder to use with the tutorial.
 
 
This tutorial will demonstrate how to draw a cube wherever FLARToolkit sees a marker. We’ll start with just one cube for now; things get more complex when you want to support multiple cubes tied to multiple markers.

First, we create a FLARManager instance, passing in the path to an external xml configuration file. For now, we’ll use flarConfig.xml (located in the /resources/ folder in the FLARManager download).
this.flarManager = new FLARManager("../resources/flar/flarConfig.xml");

Once you’re comfortable with the basics of FLARManager, you can edit this config file, or create your own, as you see fit. More information on configuration options lives here.

We want to see the video capture, so let’s add it to the stage. FLARManager creates a default video source all ready to go, though the source can also be modified via the configuration file.
this.addChild(Sprite(this.flarManager.flarSource));

FLARManager uses an event model to notify any interested parties of newly-detected markers, changes in already-detected markers, and marker removal. We add FLARMarkerEvent handlers to respond to these changes.

this.flarManager.addEventListener(FLARMarkerEvent.MARKER_ADDED, this.onMarkerAdded);
this.flarManager.addEventListener(FLARMarkerEvent.MARKER_UPDATED, this.onMarkerUpdated);
this.flarManager.addEventListener(FLARMarkerEvent.MARKER_REMOVED, this.onMarkerRemoved);

We’ll write our event handlers in just a a bit. First, we wait for FLARManager to initialize before setting up the Papervision3D environment.

this.flarManager.addEventListener(Event.INIT, this.onFlarManagerInited);

 

Setting up Papervision3D

once FLARManager has finished initializing, we can set up the Papervision3D environment:

private function onFlarManagerInited (evt:Event) :void {
    this.flarManager.removeEventListener(Event.INIT, this.onFlarManagerInited);
 
    this.scene3D = new Scene3D();
 
    // initialize FLARCamera3D with parsed camera parameters.
    this.camera3D = new FLARCamera3D(this.flarManager.cameraParams);
 
    this.viewport3D = new Viewport3D(this.stage.stageWidth, this.stage.stageHeight);
    this.addChild(this.viewport3D);
 
    // flip display to match mirrored source
    this.viewport3D.x = this.stage.stageWidth;
    this.viewport3D.scaleX = -1;
 
    this.renderEngine = new LazyRenderEngine(this.scene3D, this.camera3D, this.viewport3D);
 
    this.pointLight3D = new PointLight3D();
    this.pointLight3D.x = 1000;
    this.pointLight3D.y = 1000;
    this.pointLight3D.z = -1000;
 
    this.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);

Papervision has to re-render the scene every frame, so we add an ENTER_FRAME event handler in which we’ll do that. (We’ll get to that part in a sec.)

 

FLARToolkit camera parameters

This section is extra credit, but thought you might want to know about that FLARCamera3D line…

FLARToolkit compensates for distortion caused by the camera lens by referring to an external camera parameters file. In the FLARToolkit download, this is named ‘camera_para.dat’; in FLARManager, the file is named ‘FLARCameraParams.dat’. FLARToolkit creates a special FLARCamera3D object (that subclasses Papervision3D’s Camera3D) that incorporates this correction into Papervision3D’s display.

FLARCamera3D requires the information from the camera parameters file, but FLARManager cannot provide this until it has loaded and parsed this file. Therefore, we wait to initialize the Papervision3D environment until after FLARManager has initialized. Once there, we pass the parsed camera parameters data (stored in a FLARParam instance) to the new FLARCamera3D.

 

Create the Cube

Let’s set up a single Cube instance that we’ll map to the detected marker:

    var cubeMaterial:FlatShadeMaterial = new FlatShadeMaterial(this.pointLight3D, 0xFF1919, 0x730000);
    var materialsList:MaterialsList = new MaterialsList({all: cubeMaterial});
    var cube:Cube = new Cube(materialsList, 40, 40, 40);
    cube.z += 20;
 
    // create a container for the cube, that will accept matrix transformations.
    this.cubeContainer = new DisplayObject3D();
    this.cubeContainer.addChild(cube);
    this.scene3D.addChild(this.cubeContainer);

We place the Cube inside of a DisplayObject3D so that we can lift the Cube 20 pixels (in the z-axis) above the center of the marker, so it appears to be sitting on the marker instead of embedded in it. If we moved the Cube up 20 pixels and then applied the transformation matrix directly to it, the 20px z-coordinate would get overwritten, and the Cube would appear stuck in the middle of the marker.

 

Responding to FLARMarkerEvents

Now that FLARManager and Papervision3D are both set up, we can make the two work together by drawing Papervision3D objects when handling FLARMarkerEvents coming from FLARManager.

private function onMarkerAdded (evt:FLARMarkerEvent) :void {
    this.cubeContainer.visible = true;
    this.activeMarker = evt.marker;
}
 
private function onMarkerRemoved (evt:FLARMarkerEvent) :void {
    this.cubeContainer.visible = false;
    this.activeMarker = null;
}

For the purposes of this tutorial, we’re simply toggling the visibility of the Cube as a marker is added and removed from the camera’s view. We’re also keeping track of the active FLARMarker, from which we’ll extract and apply the transformation matrix that makes the Cube appear to be tethered to the marker.

For more on FLARMarkerEvents, see the 2D tutorial.

 

Updating the Cube

As mentioned earlier, Papervision has to re-render the scene every frame. We’ll take advantage of this opportunity to apply the latest transformation matrix to the Cube, to make it look like it’s Augmenting our Reality.

private function onEnterFrame (evt:Event) :void {
    this.cube.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(this.activeMarker.transformMatrix);
    this.renderEngine.render();
}

Note that the FLARMarker instance we’re tracking as this.activeMarker will be updating itself continuously; FLARManager handles this for us. We could listen for changes as MARKER_UPDATED events, but since we have to render the Papervision scene every frame, we can just grab the updated information from this.activeMarker while we’re at it.

You might also notice that long, ugly method call, FLARPVGeomUtils.convertFLARMatrixToPVMatrix. (Unfortunately at times like this, I’m a firm believer in self-commenting code.) FLARToolkit uses a different coordinate system than Papervision (and Flash 3D, and Away3D, and Sandy, and Alternativa), so we have to convert FLARToolkit’s transformation matrix into numbers that make sense to Papervision before applying the matrix to the Cube. Coincidentally, FLARPVGeomUtils has just the method for us!

That should take care of it — you should now see a colored cube perched gently on the marker in your hand!

 
To handle multiple cubes and multiple markers, we have to be smarter about managing all the DisplayObject3D instances and active FLARMarker instances. One way to do this is laid out in this example:
FLARManagerExample_PV3D

Also, FLARManager supports arbitrary aspect ratios. No need to stick with 4:3! Here’s a 16:9 example, if you want to get all cinematic:
FLARManagerExample_PV3D_Wide
 
 
<– 2D Marker Tracking | Loading Collada Models –>



Comments rss
Comments rss
Trackback
Trackback

16 responses

I got everything working finally after a fresh install and

Chauncey Frend | 2009/06/30 | 2:49 pm

I got everything working finally after a fresh install and update of Flex 3 and Flash player. I have a question about runtime errors in the console.

My console looks like this

[8] added
Error
Error
Error
Error

The red cube is drawn and is tracked normally, but error as you can see is in console. I can ignore it for now.

hi chauncey. that error is coming from within FLARToolkit.

ericsoco | 2009/06/30 | 4:50 pm

hi chauncey. that error is coming from within FLARToolkit. i’m guessing it’s the same error described in this thread. try turning on more lights in your room.

sorry i can’t give you a better answer than that. unfortunately, FLARToolkit is not well documented at all, and full of undescriptive errors like this one.

Thanks Eric, the lights did help & it seems infrequent

Chauncey Frend | 2009/07/01 | 11:47 am

Thanks Eric, the lights did help & it seems infrequent as far as duplicating it from day to day so it must be FLARToolKit not liking the lights.

This is so fantastic. I am new to FlarManager and looking

rufi | 2009/07/13 | 5:16 am

This is so fantastic.

I am new to FlarManager and looking for a simple tutorial to associate different markers with different Objects. I have read the whole tutorial but can’t get the way around to specify specific marker to specific object. Any help please?

Looking for a simple code.

Cheers.

hi rufi. basically, you just need to associate your

ericsoco | 2009/07/13 | 3:43 pm

hi rufi. basically, you just need to associate your objects with pattern ids.
you can access the pattern id of a detected marker from the FLARMarkerEvent associated with an added, updated, or removed event via FLARMarkerEvent.marker.patternId.

you could then, for example, create an array of objects, and toggle their visibility based on patternId. for example (this is just pseudocode):


var objects:Array = [obj0, obj1, obj2]; // store all 3D objects for display, by patternId
var markers:Array = new Array(); // store active FLARMarker instances, by patternId
// ...
private function onMarkerAdded (evt:FLARMarkerEvent) :void {
    objects[evt.marker.patternId].visible = true;
    markers[evt.marker.patternId] = evt.marker; // need to access transformation matrix later...
}
 
private function onMarkerRemoved (evt:FLARMarkerEvent) :void {
    objects[evt.marker.patternId].visible = false;
    markers[evt.marker.patternId] = null;
}
 
private function onEnterFrame (evt:Event) :void {
    for (var i:int=0; i<objects.length; i++) {
        if (objects[i].visible) {
            objects[i].transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(markers[i].transformMatrix);
        }
    }
}

note that your patterns will receive ids based on the order in which you list them in your flarConfig.xml file, starting with 0 and moving up from there.

note that FLARManagerExample_PV3D demonstrates this already, but now that i look at it, i think the code i wrote here is probably easier to understand. i may rewrite the example to be more like this; let me know if this code makes sense to you.

[...] Inside FLARManager: Basic Augmented Reality [...]

transmote speaks… » FLARManager v0.5 (for FLARToolkit) | 2009/07/18 | 6:30 pm

[...] Inside FLARManager: Basic Augmented Reality [...]

Hi, just so you know, there are a few links

ChicoPacoPancho | 2009/08/05 | 8:23 am

Hi, just so you know, there are a few links to tutorials and examples giving 404 errors:

FLARManagerTutorial_3D
FLARManagerExample_PV3D
FLARManagerExample_PV3D_Wide

crapola. moved the example files around recently, didn't notice

ericsoco | 2009/08/05 | 3:40 pm

crapola. moved the example files around recently, didn’t notice they would break these links. thx for catching that, @cpp. fixed.

Thanks for the great tool you gave us Eric. I

Tolis | 2009/08/25 | 1:30 pm

Thanks for the great tool you gave us Eric.

I have a problem though. The example above works fine with cube. My problem is that i cant display a simple Plane. I make a new DisplayObject3D, I add my plane object in it and then i add my DisplayObject3D to scene3D.

Then in the EnterFrame function i write

this.plane.transform = FLARPVGeomUtils.convertFLARMatrixToPVMatrix(this.activeMarker.transformMatrix);

@ Tollis -- did you try replacing the cube with

ericsoco | 2009/08/25 | 1:46 pm

@ Tollis — did you try replacing the cube with a plane, without modifying anything else? so you still don’t apply the transform to the plane, you apply it to the plane’s parent object (in the example, cubeContainer). also, planes are a little trickier because materials are one-sided; either set your material.doublesided = true, or set your plane.rotationX or rotationY to 180 to flip the plane around so it’s facing the camera. (note that rotation gets overwritten when you apply a transform matrix, which is why i put the cube into a container and apply the transform matrix to the container.)

I've made a planeContainer = new DisplayObject3D() and i applied

Tolis | 2009/08/25 | 3:36 pm

I’ve made a planeContainer = new DisplayObject3D() and i applied the transform to it.

I’ll try to rotate it and make it doublesided too. Thanks for the advices :)

Compiled wth FLash CS4 makes me tones of error messages: 1044:

Ico | 2009/11/15 | 6:06 pm

Compiled wth FLash CS4 makes me tones of error messages:

1044: Interface method labeling in namespace org.libspark.flartoolkit.core.labeling:IFLARLabeling not implemented by class org.libspark.flartoolkit.core.labeling:FLARLabeling_BitmapData.

Thanks for the tutorials, they are great. I have a question

James | 2009/12/20 | 10:13 am

Thanks for the tutorials, they are great.
I have a question though, I’m trying to change the size of the cubes depending on the pattern id, like it does with the colours, but I not sure what to replace in the code. Could you put me in the right direction?
Cheers.

it works fine, but i have a problem with the

Andy | 2010/01/14 | 7:47 am

it works fine, but i have a problem with the transformation. using the tutorial source except those lines:

this.viewport3D.x = this.stage.stageWidth;
this.viewport3D.scaleX = -1;

…and everything set to 320×240 (swf, flarmanager-xml). but whenever i create something at the origin (0,0,0) it is moved 10-20 pixels in y-direction. i created a plane with 80×80, it is exactly as big as the marker but the coordinates are wrong…

also, i created a plane with 80x80 that should be

Andy | 2010/01/15 | 4:00 am

also, i created a plane with 80×80 that should be exactly at the position of the marker, but it isn´t and it´s higher than the marker, but same width…

var wMat:WireframeMaterial = new WireframeMaterial(0xFFFFFF, 1, 1);
var plane:Plane = new Plane(wMat, 80, 80);
this.mainContainer.addChild(plane);

Hi Eric, thanks for the tutorial. I'm newbie. I've download flarmanager. I

Maya | 2010/01/27 | 5:45 pm

Hi Eric,

thanks for the tutorial. I’m newbie.

I’ve download flarmanager. I tried to compile FLARManagerTutorial_3D.as, but I get some error, I guess it comes from

import flash.display.Sprite;
import flash.events.Event;

because of the source that I downloaded did not have the flash package. where I can get the package? how to solve this problem?

btw, I also did not find

package “transmote.utils.time.FramerateDisplay”, which this package is used in some examples

thanks

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

Recent Posts

  • elastotron: visage @ YBCA/TVOT weds mar 3
  • FLARManager @ FITC Toronto 2010
  • true fullscreen in AIR on OSX
  • FLAR presentation @ ARDevCamp 2009.dec.05
  • FLARManager v0.61 (augmented reality in Flash)

Tags

3d actionscript Add new tag AR ARDevCamp as3 augmented reality camera Capabilities computer vision connections culture digital elastic event handler exhibit exploratorium fiducial flar flarmanager flartoolkit flash flash player forum garbage collection generative grid interactive Keyboard marker memory mesh mirror notes papervision presentation reflection slides sputnik theory to see tracking video video mirror webcam


rss Comments rss design by jide