FLAR Toolkit integration

From Flare3D Wiki
Jump to: navigation, search

This example shows how to integrate FLAR Toolkit into you Flare3D application. You can find the full project files here:



To try the example print this marker



FTScene3D Class

package  
{
	import flare.basic.*;
	import flare.core.*;
	import flare.materials.*;
	import flare.materials.filters.*;
	import flare.primitives.*;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.media.*;
	import flash.utils.ByteArray;
	import org.libspark.flartoolkit.core.*;
	import org.libspark.flartoolkit.core.param.*;
	import org.libspark.flartoolkit.core.raster.rgb.*;
	import org.libspark.flartoolkit.core.transmat.*;
	import org.libspark.flartoolkit.detector.*;
 
	/**
	 * ...
	 * @author Ivan Vodopiviz
	 */
	public class FTScene3D extends Scene3D 
	{
		private var _container:DisplayObjectContainer;
		private var _stage:Stage;
 
		private var _texture:Texture3D;
		private var _displayPlane:Plane;
 
		private var _canvasWidth:int;
		private var _canvasHeight:int;
		private var _cameraWidth:int;
		private var _cameraHeight:int;
		private var _cameraFrameRate:int;
		private var _mirrorDisplay:Boolean;
 
		private var _downSampleRatio:Number;
		private var _threshold:int;
		private var _makerSize:int;
		private var _makerResolution:int;
		private var _makerConfidence:Number;
 
		private var _cameraParam:FLARParam;
		private var _markerPatternCode:FLARCode;
		private var _resultMat:FLARTransMatResult = new FLARTransMatResult();
 
		private var _webCam:Camera;
		private var _video:Video;
		private var _capture:Bitmap;
		private var _viewCapture:Bitmap;
 
		private var _matrix:Matrix;
		private var _raster:FLARRgbRaster_BitmapData;
		private var _detector:FLARSingleMarkerDetector;
 
		private var _rawData:Vector.<Number> = new Vector.<Number>(16, true);
 
		public function FTScene3D(container:DisplayObjectContainer, file:String="") 
		{
			_container = container;
			_stage = container.stage;
			super(container, file);
 
			_canvasWidth = 640;
			_canvasHeight = 480;
			_cameraWidth = 640;
			_cameraHeight = 480;
			_cameraFrameRate = 120;
			_mirrorDisplay = false;
			_downSampleRatio = 0.25;
			_threshold = 110;
			_makerSize = 80;
			_makerResolution = 8;
			_makerConfidence = 0.5;
 
			_viewCapture = new Bitmap( new BitmapData( _cameraWidth, _cameraHeight, false, 0 ) );
			_capture = new Bitmap( new BitmapData( _cameraWidth * _downSampleRatio, _cameraHeight * _downSampleRatio, false, 0 ), PixelSnapping.AUTO, true );
			_texture = new Texture3D(_viewCapture.bitmapData, true);
			this.initAR3DRendering();
		}
 
		private function initAR3DRendering(fov:Number = 0):void
		{
			// embed camera parameters and pattern file
			[Embed(source = "embed/camera_para.dat", mimeType="application/octet-stream")] var cameraPara:Class;
			_cameraParam = new FLARParam();
			_cameraParam.loadARParam(new cameraPara() as ByteArray);
			_cameraParam.changeScreenSize( _cameraWidth * _downSampleRatio, _cameraHeight * _downSampleRatio );
 
			[Embed(source = "embed/patt001.pat", mimeType="application/octet-stream")] var logoPat:Class;
			_markerPatternCode = new FLARCode(_makerResolution, _makerResolution);
			_markerPatternCode.loadARPatt(new logoPat);
 
			_raster = new FLARRgbRaster_BitmapData(_capture.bitmapData);
 
			// setup single marker detector
			_detector = new FLARSingleMarkerDetector(_cameraParam, _markerPatternCode, _makerSize);
			_detector.setContinueMode( true );
 
			// set up our camera and start capturing video
			_webCam = Camera.getCamera();
			if ( !_webCam ) throw new Error("No webcam detected!");
			_webCam.setMode(_cameraWidth, _cameraHeight, _cameraFrameRate);
 
			_video = new Video(_cameraWidth, _cameraHeight);
			_video.attachCamera(_webCam);
			_video.width = _canvasWidth;
			_video.height = _canvasHeight;
 
			_matrix = new Matrix(_downSampleRatio, 0, 0, _downSampleRatio);
 
			// set up our 3d camera and our "display plane"
			// the plane is used to render the video
			this.camera = new Camera3D("FTCamera");
			this.camera.fieldOfView = 45;
			this.camera.parent = this;
 
			var shader:Shader3D = new Shader3D();
			shader.filters.push(new TextureMapFilter(_texture));
			shader.build();
 
			// create a plane to display our video stream
			_displayPlane = new Plane("DisplayPlane", _stage.stageWidth * 2, _stage.stageHeight * 2);
			_displayPlane.z = 0.5 / this.camera.zoom * _stage.stageWidth * 2;
			_displayPlane.setMaterial(shader);
			this.camera.addChild(_displayPlane);
 
			_video.addEventListener(Event.ENTER_FRAME, this.updateVideoEvent);
		}
 
		private function updateVideoEvent(e:Event):void 
		{
			// update our capture bitmaps, viewcapture is full resolution and
			// capture is scaled down to ease detection
			_viewCapture.bitmapData.draw(_video);
			_capture.bitmapData.draw(_video, _matrix);
			if(_texture.scene)
				_texture.uploadTexture();
 
			var detected:Boolean = false;
			detected = _detector.detectMarkerLite(_raster, 80) && _detector.getConfidence() > _makerConfidence;
 
			if (detected) 
			{
				_detector.getTransformMatrix(_resultMat);
 
				_rawData[ 0 ] = _resultMat.m00;
				_rawData[ 1 ] = -_resultMat.m10;
				_rawData[ 2 ] = _resultMat.m20;
				_rawData[ 3 ] = 0;
				_rawData[ 4 ] = _resultMat.m01;
				_rawData[ 5 ] = -_resultMat.m11;
				_rawData[ 6 ] = _resultMat.m21;
				_rawData[ 7 ] = 0;
				_rawData[ 8 ] = _resultMat.m02;
				_rawData[ 9 ] = -_resultMat.m12;
				_rawData[ 10 ] = _resultMat.m22;
				_rawData[ 11 ] = 0;
				_rawData[ 12 ] = _resultMat.m03;
				_rawData[ 13 ] = -_resultMat.m13;
				_rawData[ 14 ] = _resultMat.m23;
				_rawData[ 15 ] = 1;
 
				this.camera.transform.rawData = _rawData;
				this.camera.transform.prependScale(1, -1, 1);
				this.camera.transform.prependRotation(90, Vector3D.X_AXIS);
				this.camera.transform.invert();
				this.camera.updateTransforms(true);
			} 
			else 
			{
				_threshold = (_threshold) / 2;
			}
		}
	}
}

How to use it

package 
{
	import flare.basic.*;
	import flare.core.*;
	import flare.loaders.*;
	import flare.system.*;
	import flare.utils.Pivot3DUtils;
	import flash.display.*;
	import flash.events.*;
 
	[SWF(frameRate = 30, width = 640, height = 480, backgroundColor = 0x000000)]
 
	public class Main extends Sprite
	{
		private var _scene:Scene3D;
 
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
 
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
 
			// set up flare
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
 
			_scene = new FTScene3D(this);
 
			// set up our 3d model
			var model:Pivot3D = _scene.addChildFromFile("bird.f3d");
			model.setScale(100, 100, 100);
		}
	}
}
blog comments powered by Disqus