BasicFLAR.as
- FLARToolKit made easy
- FLARToolKit made easy
A while ago i posted my first fidlings with FLARToolkit, and promised to show more. Well since I was going to do a presentation at FUGN, I though I might as well write is down here.
FLARToolKit for me has been made possible from three sources. Saqoosha is the guy that ported the ARToolKit to ActionScript 3, and has a number of examples of his creative work at his site. Mikko Haapoja’s blog has been a fantastic resource, and Lee Brimelow’s tutorial was what kick-started me.
Here is an example of BasicFLAR.as in use, by yours truly:
The prerequisites for using my BasicFLAR class can be met by following the step called “Step 1: Download” written down by Mikko Haapoja here. Much of this post will be summarizing alot of what Mikko writes in his post, so if you have the time, you should read up on his post before continuing. Anyways, let’s get started.
The Marker
First of all, what we need to do is make a marker, for the FLAR to detect. You can either download mine here and print it out on thick paper, or build one yourself. To build it yourself you need to need to draw it first, using the guidelines Mikko Haapoja points out in his post, and print it out. When this is done you need to create a pattern file that we will later load into the FLARToolKit library. Saqoosha made an air app that creates this file for us. Simply download and install it from here. Start the app, hold your marker infront of the camera, and click “save pattern” when you see the read box forming round your pattern. Make sure you add the “.pat” extension when you save it.
The Camera Parameters
There is one more ting the FLAR is dependent on, and that is a camera parameters file. This file is a binary file that contains information that corrects lens distortion of your webcam. Mikko Explains this more in detail in his post. Just use the “camera_para.dat” file that comes with FLARToolKit, and you should be sorted.
The BasicFLAR.as
Now on to the code. Using my BasicFLAR class lets you just start doing the 3D stuff and let the FLAR stuff be taken care of by the class. Simply extend the class, and start coding, like this:
// Import the class
import com.perkstoveland.flar.BasicFLAR;
public class FLARExample extends BasicFLAR
{
/**
* Constructor.
**/
public function FLARExample()
{
// Feed the two files that FLARToolKit requires to work
super( "camera_para.dat", "perkmarker.pat" );
}
}
Now the first time you compile FLARToolKit and Papervision3D, you most likely will get an error. Mikko Haapoja, again, explains this very well here. Since the issue can result in a head-scratcher, i’ve quoted what he says about the issue:
The very first time you try to compile FlarToolkit after getting Papervision even the examples will not compile and you will get this error message:
Error: Attempted access of inaccessible property _projection through a reference with static type org.libspark.flartoolkit.pv3d:FLARCamera3D.
To fix this just go into the class-
org.papervision3d.cameras.Camera3D
And change the line-
private var _projection:Matrix3D;
to
protected var _projection:Matrix3D;
This will not break Papervision however it will just allow FlarToolkit’s FlarCamera3D to use the projection matrix of the Camera3d class.
Ok, so we go the FLARToolKit going, what we need to do now is start adding 3D objects to the stage. Since we need to load the files before BasicFLAR is ready to receive 3D objects, in our example here we override the BasicFLAR’s onInit() function, and feed the Papervision3D container with objects. Let’s create a cube that sits on the marker:
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
// We store the cube as a class variable,
// so we can manipulate it later.
private var _cube:Cube;
// Next we override the onInit function, note that
// we do not need to call the super's onInit() method
// as that method doesn't really do anything. It's
// there for you to override and implement.
override protected function onInit():void
{
// Create the cube materials
var mat:ColorMaterial = new ColorMaterial();
var matList:MaterialsList = new MaterialsList( { all: mat } );
// Create the cube.
_cube = new Cube( matList, 30, 30, 30 );
// Since the cube's center point will be in the middle
// of the marker, we just want to lift it up half its
// height, so it is "standing" on the marker.
_cube.z = 15;
// Finally we add it to BasicFLAR's container property.
container.addChild( _cube );
}
Finally, if we want, we can manipulate and animate the 3D objects that we just added. Again this is done by overriding an existing method in BasicFLAR, namely onFrameTick(). But this time we need to be sure to call the superclass’s method we are overriding. This method takes care of the 3D rendering and marker detecting. This is the way to do it:
override protected function onFrameTick( $e:Event ):void
{
// Roll the cube by 2 degrees each frame.
_cube.roll( 2 );
// Call the super's method, and pass the event to it.
super.onFrameTick( $e );
}
Thats is, you should now be seeing an animated cube following the moves you make on the marker through you web cam. Below are downloads and source code for you to dig into. I’d love to see what you do with it. Enjoy!
The Downloads
– This is the class i explain in this post, authored by me.
– There is loads of documentation on papervision out there, just google it
– You can get a start-up guide here, as well as the FLARToolKit library.
– The AIR app built by saqoosha, generates a marker file.
Popularity: 27% [?]

