package {
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Camera;
import flash.media.Video;
import flash.utils.ByteArray;
import org.libspark.flartoolkit.core.FLARCode;
import org.libspark.flartoolkit.core.param.FLARParam;
import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
import org.libspark.flartoolkit.pv3d.FLARBaseNode;
import org.libspark.flartoolkit.pv3d.FLARCamera3D;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.parsers.DAE;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
//set the properties of our movie using he SWF tag, set the width
//and height and make the frame rate 30 like below.
[SWF(width=”640″, height=”480″, framerate=”30″, backgroundColor=”#FFFFFF”)]
public class Flar extends Sprite
{
//Add the embed tags to include the material2.jpg, pat1.pat and camera_para.dat file
// INclude the mimeType for the pat and dat , basically we used the mimetype inorder
// to represent the binary coded files
[Embed(source=”dpPic_Gray.jpg”)]
private var Matt:Class;
[Embed(source=”FLARPattern.pat”, mimeType=”application/octet-stream”)]
private var pattern:Class;
[Embed(source=”camera_para.dat”, mimeType=”application/octet-stream”)]
private var params:Class;
//the fparams will control the webcam to work with Augmented Reality and
//The mpattern variable will be used alongside the pattern
//you imported to match up the pattern on the webcam
private var fparams:FLARParam;
private var mpattern:FLARCode;
private var vid:Video;
private var cam:Camera;
// Mapping of the pattern to the webcam
//Bitmapdata object takes a snapshot of the webcam and
//then passes the BitmapData to the FlarToolkit and t
//the FlarToolkit will detect the pattern in the Bitmap Data
private var bmd:BitmapData;
private var raster:FLARRgbRaster_BitmapData;
private var detector:FLARSingleMarkerDetector;
//we’re creating a basic rending engine and a ‘trans’
//variable which will hold the new transformation information once the object’s moved.
private var scene:Scene3D;
private var camera:FLARCamera3D;
private var container:FLARBaseNode;
private var vp:Viewport3D;
private var bre:BasicRenderEngine;
private var trans:FLARTransMatResult;
public function Flar()
{
setupFLAR();
setupCamera();
setupBitmap();
setupPV3D();
//which will call a loop function, the loop function is important
//because it draws the new 3D shape on each frame
addEventListener(Event.ENTER_FRAME, loop);
}
// here we are load the dat and pat file
private function setupFLAR():void
{
fparams = new FLARParam();
fparams.loadARParam(new params() as ByteArray);
mpattern = new FLARCode(16, 16);
mpattern.loadARPatt(new pattern());
}
// set the camera and vide size
private function setupCamera():void
{
vid = new Video(640, 480);
cam = Camera.getCamera();
cam.setMode(640, 480, 15);
vid.attachCamera(cam);
addChild(vid);
}
// capture the video object as bmd , then pass in to the bmd
//Then pass the ‘bmd’ into the raster variable you created.
//Lastly create a detector which maps the camera to the pattern
private function setupBitmap():void
{
bmd = new BitmapData(640, 480);
bmd.draw(vid);
raster = new FLARRgbRaster_BitmapData(bmd);
detector = new FLARSingleMarkerDetector(fparams, mpattern, 80);
}
private function setupPV3D():void
{
scene = new Scene3D();
camera = new FLARCamera3D(fparams);
container = new FLARBaseNode();
scene.addChild(container);
var pl:PointLight3D = new PointLight3D();
pl.x = pl.y = 1000;
pl.z = -1000;
/* var matt:BitmapMaterial = new BitmapMaterial(((new Matt()) as Bitmap).bitmapData);
var ml:MaterialsList = new MaterialsList({all:matt});
var cube:Cube = new Cube(ml,100,100,100);
cube.z = 1;
container.addChild(cube); */
// Adding a 3D model
var model:DAE = new DAE();
model.load(“model/3d/manhead.dae”);
model.scale= 1000;
model.z = -301;
model.y = -200;
model.x = 1;
model.rotationY =180;
/* model.rotationZ =90; */
var modelHolder:DisplayObject3D = new DisplayObject3D();
modelHolder.addChild(model);
container.addChild(modelHolder)
bre = new BasicRenderEngine();
trans = new FLARTransMatResult();
vp = new Viewport3D();
addChild(vp);
}
private function loop(evt:Event):void
{
bmd.draw(vid);
// check to see if match the pattern
try
{
if(detector.detectMarkerLite(raster, 80) && detector.getConfidence()> 0.5)
{
// get transfor matrix
detector.getTransformMatrix(trans);
// set transformation matrix
container.setTransformMatrix(trans);
// render 3D
bre.renderScene(scene, camera, vp);
}
} catch(e:Error){}
}
}
}