Augmented Reality - The Code

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.

1package {
2
3 import flash.display.Bitmap;
4 import flash.display.BitmapData;
5 import flash.display.PixelSnapping;
6 import flash.display.Sprite;
7 import flash.media.Camera;
8 import flash.media.Video;
9 import flash.events.Event;
10 import flash.utils.ByteArray;
11
12 //FLAR IMPORT
13 import org.libspark.flartoolkit.core.FLARCode;
14 import org.libspark.flartoolkit.core.param.FLARParam;
15 import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
16 import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
17
18 //PAPERVISION IMPORT SCENE GLOBALS
19 import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
20 import org.libspark.flartoolkit.support.pv3d.FLARBaseNode;
21 import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
22 import org.papervision3d.render.LazyRenderEngine;
23 import org.papervision3d.scenes.Scene3D;
24 import org.papervision3d.view.Viewport3D;
25
26 //PAPERVISION IMPORT OBJECTS GLOBALS
27 import org.papervision3d.objects.parsers.DAE;
28 import org.papervision3d.lights.PointLight3D;
29 import org.papervision3d.materials.WireframeMaterial;
30 import org.papervision3d.materials.utils.MaterialsList;
31 import org.papervision3d.materials.ColorMaterial;
32 import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
33 import org.papervision3d.materials.shadematerials.PhongMaterial;
34 import org.papervision3d.events.FileLoadEvent;
35
36 //INITIALIZE SWF PARAMS
37 [SWF(width="640", height="480", frameRate="30", backgroundColor="#FFFFFF")]
38
39 public class ArApp extends Sprite
40 {
41 //EMBED MARKER FILE (NEEDS FLEX SDK 3)
42 [Embed(source="Data/crunkMarker16.pat", mimeType="application/octet-stream")]
43 private var pattern:Class;
44
45 //EMBED CAMERA PARAMS FILE (NEEDS FLEX SDK 3)
46 [Embed(source="Data/camera_para.dat", mimeType="application/octet-stream")]
47 private var params:Class;
48
49 //FLAR VARS
50 protected var Param:FLARParam;
51 protected var Code:FLARCode;
52 protected var raster:FLARRgbRaster_BitmapData;
53 protected var detector:FLARSingleMarkerDetector;
54 protected var resultMat:FLARTransMatResult = new FLARTransMatResult();
55
56 //WEBCAM DATA VARS
57 protected var webcamBox:Camera;
58 protected var video:Video;
59 protected var capture:BitmapData;
60
61 //PAPERVISION VARS
62 protected var viewport:Viewport3D;
63 protected var camera3d:FLARCamera3D;
64 protected var scene:Scene3D;
65 protected var renderer:LazyRenderEngine;
66 protected var markerNode:FLARBaseNode;
67
68 //EXTERNAL MODEL VARS
69 private var ext_model:DAE;
70
71 public function ArApp() {
72
73 doArApp();
74 addEventListener(Event.ENTER_FRAME, loop);
75 }
76
77 private function doArApp():void
78 {
79
80 // FLAR CONSTRUCTOR
81 Param = new FLARParam();
82 Param.loadARParam(new params() as ByteArray);
83 Code = new FLARCode(16, 16);
84 Code.loadARPatt(new pattern());
85
86 //WEBCAM DATA
87 webcamBox = Camera.getCamera();
88 if (!webcamBox) {
89 trace ('No webcam!!!!');
90 throw new Error('No webcam!!!!');
91
92 }
93
94 webcamBox.setMode(640, 480, 30);
95 video = new Video(640, 480);
96 video.attachCamera(webcamBox);
97 addChild(video);
98
99 //BITMAP DATA
100 capture = new BitmapData(640, 480);
101 capture.draw(video);
102 raster = new FLARRgbRaster_BitmapData(capture);
103 detector = new FLARSingleMarkerDetector(Param, Code, 80);
104
105 //PV3D DATA
106 camera3d = new FLARCamera3D(Param);
107 scene = new Scene3D();
108 markerNode = scene.addChild(new FLARBaseNode()) as FLARBaseNode;
109 viewport = new Viewport3D();
110 renderer = new LazyRenderEngine(scene, camera3d, viewport);
111
112 //OBJECT BASED VARS - LIGHTS, MATERIALS, MODEL.
113 var light:PointLight3D = new PointLight3D();
114 light.x = 0;
115 light.y = 1000;
116 light.z = -1000;
117
118 var ar_materials:MaterialsList = new MaterialsList({all: new PhongMaterial(light,0xffffff,0x000000,0.3)});
119
120 ext_model = new DAE();
121 ext_model.load('model/dastardly2.dae',ar_materials);
122
123 //CUSTOM OBJECT TRANSFORM
124 ext_model.scale = 10;
125 ext_model.rotationX = 90;
126 markerNode.addChild(ext_model);
127 addChild(viewport);
128
129 }
130
131 private function loop(e:Event):void {
132 capture.draw(video);
133
134 var detected:Boolean = false;
135 try {
136 detected = detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.5;
137 } catch (e:Error) {}
138
139 if (detected) {
140 detector.getTransformMatrix(resultMat); //GET
141 markerNode.setTransformMatrix(resultMat); //SET
142 markerNode.visible = true; //GO
143 } else {
144 markerNode.visible = false; //REMOVE IF MARKER NO IN FRAME
145 }
146 renderer.render(); //RENDER THE SCENE
147 }
148
149 }
150}
Comments