package com.squidder.flar {
	import flash.events.Event;
	
	import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
	import org.libspark.flartoolkit.pv3d.FLARBaseNode;
	import org.libspark.flartoolkit.pv3d.FLARCamera3D;
	import org.papervision3d.core.math.Matrix3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;	

	/**
	 * @author Jon Reiling
	 */
	public class PVFLARBaseApplication extends FLARBaseApplication {

		public var scene : Scene3D;
		public var viewport : Viewport3D;
		public var renderer : BasicRenderEngine;

		protected var _camera : FLARCamera3D;
		protected var _height : Number;
		protected var _width : Number;
		protected var _baseNode : FLARBaseNode;
		protected var _resultMat : FLARTransMatResult;
		protected var _resultsArray : Array;
		
		public function PVFLARBaseApplication() {
			super( );
		}

		override protected function _init( event : Event ) : void {
			
			super._init( event );	
			
			scene = new Scene3D( );

			viewport = new Viewport3D( stage.width , stage.height , false , false );
			addChild( viewport );

			renderer = new BasicRenderEngine( );		
			
			_baseNode = new FLARBaseNode();			
			scene.addChild( _baseNode );
			
			_resultMat = new FLARTransMatResult();
			_camera = new FLARCamera3D( _flarParam );
			startRendering();
		}

		public function startRendering() : void {
			
			addEventListener( Event.ENTER_FRAME , _onRenderTick );
		}

		
		protected function _detectMarkers() : void {
			
			_resultsArray = _flarDetector.updateMarkerPosition( _flarRaster , 80 , .5 );
				
			
		}

		
		protected function _onRenderTick( event : Event = null ) : void {

			_flarBitmap.bitmapData.draw( _video );
			_detectMarkers();			
			renderer.renderScene( scene , _camera , viewport );
		}
		

		protected function transformMatrix( target : DisplayObject3D , r:FLARTransMatResult):void {
			var m:Matrix3D = target.transform;
			
			m.n11 =  r.m01; m.n12 =  r.m00; m.n13 =  r.m02; m.n14 =  r.m03;
			m.n21 = -r.m11; m.n22 = -r.m10; m.n23 = -r.m12; m.n24 = -r.m13;
			m.n31 =  r.m21; m.n32 =  r.m20; m.n33 =  r.m22; m.n34 =  r.m23;
			
		}	
		
	}
}
