AR

In my last post on Augmented Reality 101, i had shown a sample of how the FLARToolkit outputs using Flash. Here i would like to share the code i had written for that sample so that u know what are all the modules that u need to import from the various packages, (from FLAR and Papervision) and what are all the classes that need to be defined to get this to work. You will obviously need to replace the model file and the marker file to a custom one, rest of it is a good skeleton code to build something for AR.

Requirements:

FLEX SDK 3.0 Library - Download from HERE.

package {

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.PixelSnapping;
    import flash.display.Sprite;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.events.Event;
    import flash.utils.ByteArray;

    //FLAR IMPORT
    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.detector.FLARSingleMarkerDetector;

    //PAPERVISION IMPORT SCENE GLOBALS
    import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
    import org.libspark.flartoolkit.support.pv3d.FLARBaseNode;
    import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
    import org.papervision3d.render.LazyRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;

    //PAPERVISION IMPORT OBJECTS GLOBALS
    import org.papervision3d.objects.parsers.DAE;
    import org.papervision3d.lights.PointLight3D;
    import org.papervision3d.materials.WireframeMaterial;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.materials.ColorMaterial;
    import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
    import org.papervision3d.materials.shadematerials.PhongMaterial;
    import org.papervision3d.events.FileLoadEvent;

    //INITIALIZE SWF PARAMS
    [SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]

    public class ArApp extends Sprite
    {
        //EMBED MARKER FILE (NEEDS FLEX SDK 3)
        [Embed(source="Data/crunkMarker16.pat", mimeType="application/octet-stream")]
        private var pattern:Class;

        //EMBED CAMERA PARAMS FILE (NEEDS FLEX SDK 3)
        [Embed(source="Data/camera_para.dat", mimeType="application/octet-stream")]
        private var params:Class;

        //FLAR VARS
        protected var Param:FLARParam;
        protected var Code:FLARCode;
        protected var raster:FLARRgbRaster_BitmapData;
        protected var detector:FLARSingleMarkerDetector;
        protected var resultMat:FLARTransMatResult = new FLARTransMatResult();

        //WEBCAM DATA VARS
        protected var webcamBox:Camera;
        protected var video:Video;
        protected var capture:BitmapData;

        //PAPERVISION VARS
        protected var viewport:Viewport3D;
        protected var camera3d:FLARCamera3D;
        protected var scene:Scene3D;
        protected var renderer:LazyRenderEngine;
        protected var markerNode:FLARBaseNode;

        //EXTERNAL MODEL VARS
        private var ext_model:DAE;

        public function ArApp() {

            doArApp();
            addEventListener(Event.ENTER_FRAME, loop);
        }

        private function doArApp():void
        {

        // FLAR CONSTRUCTOR
        Param = new FLARParam();
        Param.loadARParam(new params() as ByteArray);
        Code = new FLARCode(16, 16);
        Code.loadARPatt(new pattern());

        //WEBCAM DATA
        webcamBox = Camera.getCamera();
            if (!webcamBox) {
                trace ('No webcam!!!!');
                throw new Error('No webcam!!!!');

            }

        webcamBox.setMode(640, 480, 30);
        video = new Video(640, 480);
        video.attachCamera(webcamBox);
        addChild(video);

        //BITMAP DATA
        capture = new BitmapData(640, 480);
        capture.draw(video);
        raster = new FLARRgbRaster_BitmapData(capture);
        detector = new FLARSingleMarkerDetector(Param, Code, 80);

        //PV3D DATA
        camera3d = new FLARCamera3D(Param);
        scene = new Scene3D();
        markerNode = scene.addChild(new FLARBaseNode()) as FLARBaseNode;
        viewport = new Viewport3D();
        renderer = new LazyRenderEngine(scene, camera3d, viewport);

        //OBJECT BASED VARS - LIGHTS, MATERIALS, MODEL.
        var light:PointLight3D = new PointLight3D();
        light.x = 0;
        light.y = 1000;
        light.z = -1000;

        var ar_materials:MaterialsList = new MaterialsList({all: new PhongMaterial(light,0xffffff,0x000000,0.3)});

        ext_model = new DAE();
        ext_model.load('model/dastardly2.dae',ar_materials);

        //CUSTOM OBJECT TRANSFORM
        ext_model.scale = 10;
        ext_model.rotationX = 90;
        markerNode.addChild(ext_model);
        addChild(viewport);

        }

        private function loop(e:Event):void {
            capture.draw(video);

            var detected:Boolean = false;
            try {
                detected = detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.5;
            } catch (e:Error) {}

            if (detected) {
                detector.getTransformMatrix(resultMat); //GET
                markerNode.setTransformMatrix(resultMat); //SET
                markerNode.visible = true; //GO
            } else {
                markerNode.visible = false; //REMOVE IF MARKER NO IN FRAME
            }
            renderer.render(); //RENDER THE SCENE
        }

    }
}