qtrack
package {
import alternativ7.engine3d.alternativa3d;
import alternativ7.engine3d.core.Camera3D;
import alternativ7.engine3d.core.Object3DContainer;
import alternativ7.engine3d.core.Sorting;
import alternativ7.engine3d.core.View;
import alternativ7.engine3d.loaders.Parser3DS;
import alternativ7.engine3d.materials.TextureMaterial;
import alternativ7.engine3d.objects.Mesh;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.system.SecurityDomain;
/**
* Is it possible to run arbitrary code at wonderfl?
* I guess it is. Let's try to run qtrack Alternativa3D example.
* @author makc
*/
public class qtrackDemo extends Sprite {
private var camera:Camera3D;
private var model:Mesh;
private var tracker:*;
public function qtrackDemo () {
// load qtrack zip
var loader:URLLoader = new URLLoader;
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener (Event.COMPLETE, onZipDownloadComplete);
loader.load (new URLRequest ("http://ideaskill.com/qtrack/qtrack.1.1.zip"));
}
private function onZipDownloadComplete (e:Event):void {
// did you know that nochump is bundled with pv3d?
FileUtil.AddZip (URLLoader (e.target).data);
// unpack swc from zip
FileUtil.AddZip (FileUtil.GetFile ("qtrack.swc"));
// unpack swf from swc
var loader:Loader = new Loader;
loader.contentLoaderInfo.addEventListener (Event.COMPLETE, onLibraryLoaded);
loader.loadBytes (FileUtil.GetFile ("library.swf"),
new LoaderContext (false, ApplicationDomain.currentDomain)
);
}
private var libraryDomain:ApplicationDomain;
private function onLibraryLoaded (e:Event):void {
var info:LoaderInfo = LoaderInfo (e.target);
libraryDomain = info.applicationDomain;
// load duck texture in background
duckTexture = new BitmapData (300, 250, true, 0);
var duckTextureLoader:Loader = new Loader;
duckTextureLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, onDuckTextureLoaded);
duckTextureLoader.loadBytes (FileUtil.GetFile ("duck/duck.jpg"),
new LoaderContext (false, ApplicationDomain.currentDomain)
);
// load marker image
markerImage = new BitmapData (600, 600, true, 0);
var markerImageLoader:Loader = new Loader;
markerImageLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, onMarkerImageLoaded);
markerImageLoader.loadBytes (FileUtil.GetFile ("duck/duck.gif"),
new LoaderContext (false, ApplicationDomain.currentDomain)
);
}
private var duckTexture:BitmapData;
private function onDuckTextureLoaded (e:Event):void {
var info:LoaderInfo = LoaderInfo (e.target);
duckTexture.draw (info.content);
}
private var markerImage:BitmapData;
private function onMarkerImageLoaded (e:Event):void {
var info:LoaderInfo = LoaderInfo (e.target);
markerImage.draw (info.content);
// ok, we got enough to start the demo
camera = new Camera3D;
camera.view = new View (465, 465);
(new Object3DContainer).addChild (camera);
var material:TextureMaterial = new TextureMaterial (duckTexture);
var parser:Parser3DS = new Parser3DS;
parser.parse (FileUtil.GetFile ("duck/duck.3ds"), "", 0.1);
model = Mesh (parser.objects [0]);
model.sorting = Sorting.AVERAGE_Z;
model.setMaterialToAllFaces (material);
// update Camera3D internal values (focal length)
camera.render ();
tracker = new (libraryDomain.getDefinition ("qtrack.QuadTracker") as Class) (
new (libraryDomain.getDefinition ("qtrack.QuadMarker") as Class) (markerImage),
0, 465, 465, camera.alternativa3d::focalLength
);
tracker.addEventListener ("markerFound", onMarkerFound);
tracker.addEventListener ("markerLost", onMarkerLost);
tracker.addEventListener ("markerMove", onMarkerMove);
addChild (tracker);
addChild (camera.view);
tracker.start ();
}
private function onMarkerFound (e:Event):void {
camera.parent.addChild (model);
}
private function onMarkerLost (e:Event):void {
camera.parent.removeChild (model);
camera.render ();
}
private function onMarkerMove (e:Event):void {
model.matrix = tracker.getPose3D ();
camera.render ();
}
}
}
import flash.utils.*;
import nochump.util.zip.*;
class FileUtil {
public static var Zips:Dictionary = new Dictionary;
public static function AddZip (data:ByteArray):void {
var zip:ZipFile = new ZipFile (data);
for (var i:int = 0; i < zip.entries.length; i++) {
var entry:ZipEntry = zip.entries [i];
var finfo:FileInfo = new FileInfo;
finfo.zip = zip;
finfo.entry = entry;
Zips [entry.name] = finfo;
}
}
public static function GetFile (name:String):ByteArray {
var finfo:FileInfo = FileInfo (Zips [name]);
if (finfo == null) return null;
return finfo.zip.getInput (finfo.entry);
}
}
import nochump.util.zip.*;
class FileInfo {
public var zip:ZipFile;
public var entry:ZipEntry;
}