import flash.utils.Timer; import flash.events.TimerEvent; import flash.events.Event; import flash.geom.Vector3D; import flash.geom.Point; import flash.display.Sprite; import flash.text.TextField; class Vec { public var x : Float; public var y : Float; public var z : Float; public function new(x:Float, y:Float, z:Float) { this.x = x; this.y = y; this.z = z; } public function normalize() { var mag:Float = Math.sqrt(x * x + y * y + z * z); if (mag == 0.0) return; x /= mag; y /= mag; z /= mag; } public function matrixMultiply(m:Array) { var tmp:Vec = new Vec(0.0, 0.0, 0.0); tmp.x += m[0] * x; tmp.x += m[4] * y; tmp.x += m[8] * z; tmp.x += m[12]; tmp.y += m[1] * x; tmp.y += m[5] * y; tmp.y += m[9] * z; tmp.y += m[13]; tmp.z += m[2] * x; tmp.z += m[6] * y; tmp.z += m[10] * z; tmp.z += m[14]; return tmp; } public function rotateAroundAxis(axis:Vec, theta:Float) { var x:Float = axis.x; var y:Float = axis.y; var z:Float = axis.z; var s:Float = Math.sin(theta); var c:Float = Math.cos(theta); var t:Float = 1 - c; var x2:Float = x * x; var y2:Float = y * y; var z2:Float = z * z; var tx2:Float = t * x2; var ty2:Float = t * y2; var tz2:Float = t * z2; var sx:Float = s * x; var sy:Float = s * y; var sz:Float = s * z; var txy:Float = t * x * y; var txz:Float = t * x * z; var tyz:Float = t * y * z; var m : Array = [ tx2 + c, txy + sz, txz - sy, 0.0, txy -sz, ty2 + c, tyz + sx, 0.0, txy + sy, tyz - sx, tz2 + c, 0.0, 0.0, 0.0, 0.0, 1.0]; return this.matrixMultiply(m); } } class SpinningCube extends Sprite { public var canvas : flash.display.Shape; public var vs : Array; public var tvs : Array; public var t : Float; public var vertexLabels : Array; public var debugLabel:TextField; public function new() { super(); this.canvas = new flash.display.Shape(); flash.Lib.current.addChild(this); this.addChild(this.canvas); flash.Lib.current.addChild(this.canvas); this.vs = [ new Vec(-1, -1, -1), new Vec(-1, -1, 1), new Vec(-1, 1, -1), new Vec(-1, 1, 1), new Vec(1, -1, -1), new Vec(1, -1, 1), new Vec(1, 1, -1), new Vec(1, 1, 1) ]; this.t = 0.0; this.tvs = new Array(); this.vertexLabels = new Array(); var i = 0; for (i in 0...this.vs.length) { // this.vs[i] = this.vs[i].rotateAroundAxis(new Vec(1, 0, 0), 0.5); var vect:Vec = this.vs[i]; this.tvs.push(new Point(0, 0)); var textField = new TextField(); textField.htmlText = "" + i + ""; textField.selectable = false; textField.height = 20; this.addChild(textField); this.vertexLabels.push(textField); this.debugLabel = new TextField(); this.debugLabel.selectable = false; this.debugLabel.x = 0; this.debugLabel.y = 0; this.addChild(this.debugLabel); } var timer:Timer = new Timer(25, 0); timer.addEventListener(TimerEvent.TIMER, drawScene); timer.start(); } public function drawScene(e:Event) { this.canvas.graphics.clear(); this.canvas.graphics.lineStyle(1, 0xFF0000); this.canvas.graphics.beginFill(0xFF0000, 0.25); this.canvas.graphics.moveTo(0, 0); this.canvas.graphics.lineTo(200, 200); this.canvas.graphics.endFill(); t += 0.01; var cameraZ : Float = 5.0; var fov : Float = 300.0; var cx : Int = Std.int(stage.stageWidth / 2); var cy : Int = Std.int(stage.stageHeight / 2); var rotAxis : Vec = new Vec(0, 1, 0); rotAxis.normalize(); this.canvas.graphics.clear(); this.canvas.graphics.lineStyle(1, 0xFF0000); for (i in 0...this.vs.length) { var tv:Vec = this.vs[i].rotateAroundAxis(rotAxis, t); tvs[i].x = cx + fov * tv.x / (cameraZ - tv.z); tvs[i].y = cy + fov * tv.y / (cameraZ - tv.z); this.canvas.graphics.drawCircle(tvs[i].x, tvs[i].y, 4); this.vertexLabels[i].x = tvs[i].x; this.vertexLabels[i].y = tvs[i].y; } this.drawQuad(1, 0, 2, 3); this.drawQuad(1, 0, 4, 5); this.drawQuad(7, 6, 2, 3); this.drawQuad(7, 6, 4, 5); this.drawQuad(1, 5, 7, 3); this.drawQuad(4, 0, 2, 6); this.debugLabel.htmlText = "Theta: " + this.t + ""; } public function drawQuad(v1:Int, v2:Int, v3:Int, v4:Int) { this.canvas.graphics.beginFill(0xFF0000, 0.25); this.canvas.graphics.moveTo(tvs[v1].x, tvs[v1].y); this.canvas.graphics.lineTo(tvs[v2].x, tvs[v2].y); this.canvas.graphics.lineTo(tvs[v3].x, tvs[v3].y); this.canvas.graphics.lineTo(tvs[v4].x, tvs[v4].y); this.canvas.graphics.endFill(); } } class Main { static function main() { var application = new SpinningCube(); } } // vim: set sts=2 sw=2 expandtab syntax=javascript: