forked from: forked from: code on 2008-12-24
// forked from jasu's forked from: code on 2008-12-24
// forked from jasu's code on 2008-12-24
// write as3 code here..
package{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class CubeRotationExample extends Sprite {
private static const EDGE :Number = 40; //edge length
private var mtxWorld :Matrix3D; //world transform matrix
private var vCube :Vector.<Number>; //cube points
private var vFlatCube :Vector.<Number>; //projected cube points
private var vUVT :Vector.<Number>; //uvt data
private var vIndicies :Vector.<int>; //triangle indicies
private var sprCube :Sprite; //canvas object
public function CubeRotationExample(){
// setup and initialize
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.BEST;
mtxWorld = new Matrix3D();
vCube = Vector.<Number>([ EDGE,EDGE,-EDGE,
EDGE,-EDGE,-EDGE,
-EDGE,-EDGE,-EDGE,
-EDGE,EDGE,-EDGE,
EDGE,EDGE,EDGE,
EDGE,-EDGE,EDGE,
-EDGE,-EDGE,EDGE,
-EDGE,EDGE,EDGE
]);
vFlatCube = new Vector.<Number>(16);
vUVT = new Vector.<Number>(24);
vIndicies = Vector.<int>([0,1,2, 2,3,0, 4,7,6, 6,5,4, 0,4,5, 5,1,0, 1,5,6, 6,2,1, 2,6,7, 7,3,2, 4,0,3, 3,7,4]);
sprCube = new Sprite();
addChild(sprCube);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(eo:Event):void {
// center the cube
sprCube.x = stage.stageWidth/2;
sprCube.y = stage.stageHeight/2;
// rotate the transform matrix
mtxWorld.appendRotation(2, new Vector3D(0,1,0));
mtxWorld.appendRotation(2, new Vector3D(0,0,1));
mtxWorld.appendRotation(2, new Vector3D(1,0,0));
// project 3D data to 2D plane
Utils3D.projectVectors(mtxWorld, vCube, vFlatCube, vUVT);
// draw the cube using cube data
sprCube.graphics.clear();
sprCube.graphics.beginFill(0xFF00FF);
sprCube.graphics.lineStyle(1,0xFF0000,1);
sprCube.graphics.drawTriangles(vFlatCube, vIndicies, null, TriangleCulling.POSITIVE);
sprCube.graphics.endFill();
}
}
}