Home > Uncategorized > Source for Basic3D

Source for Basic3D

package
{
import flash.display.TriangleCulling;
import flash.display.Sprite;
import flash.events.Event;
import net.richardlord.coral.Matrix3d;
import net.richardlord.coral.Point3d;
import net.richardlord.coral.Vector3d;

/**
* …
* @author Shiu
*/
public class Proj_3D2 extends Sprite
{
private var modelSpace:Vector.<Vector3d>
private var worldSpace:Vector.<Vector3d>
private var local:Vector3d;

private var projected:Vector.<Number>
private var dex:Vector.<int>;

public function Proj_3D2()
{
modelSpace = new Vector.<Vector3d>;
modelSpace.push(new Vector3d( -50, -50, -50)); //A
modelSpace.push(new Vector3d( 50, -50, -50)); //B
modelSpace.push(new Vector3d( 50, 50, -50)); //C
modelSpace.push(new Vector3d( -50, 50, -50)); //D

modelSpace.push(new Vector3d( -50, -50, 50)); //E
modelSpace.push(new Vector3d( 50, -50, 50)); //F
modelSpace.push(new Vector3d( 50, 50, 50)); //G
modelSpace.push(new Vector3d( -50, 50, 50)); //H

worldSpace = new Vector.<Vector3d>;
worldSpace = modelSpace;

local = new Vector3d(stage.stageWidth*0.2, stage.stageHeight >> 1, 100);
addEventListener(Event.ENTER_FRAME, refresh);
}

private function refresh(e:Event):void
{
//spin(10);
convert2World();
prepareData();
//perspective();
draw();
}

private function perspective():void
{

}

private function spin(angle:Number):void
{
var m3:Matrix3d = new Matrix3d();
m3.appendRotation(angle / 180 * Math.PI, Vector3d.Y_AXIS);

modelSpace = m3.transformVectors(modelSpace);
}

private function convert2World():void {
worldSpace = new Vector.<Vector3d>;
worldSpace.push(local.add(modelSpace[0])); //A
worldSpace.push(local.add(modelSpace[1])); //B
worldSpace.push(local.add(modelSpace[2])); //C
worldSpace.push(local.add(modelSpace[3])); //D

worldSpace.push(local.add(modelSpace[4])); //E
worldSpace.push(local.add(modelSpace[5])); //F
worldSpace.push(local.add(modelSpace[6])); //G
worldSpace.push(local.add(modelSpace[7])); //H
}

private function prepareData():void {
//verts
projected = new Vector.<Number>;
projected.push(
worldSpace[0].x, worldSpace[0].y,
worldSpace[1].x, worldSpace[1].y,
worldSpace[2].x, worldSpace[2].y,
worldSpace[3].x, worldSpace[3].y);

dex = new Vector.<int>;
dex.push(3, 0, 1,
3, 1, 2);
}

private function draw():void {
graphics.clear();
graphics.lineStyle(0);
graphics.beginFill(0xFF8000);
//graphics.drawTriangles(projected, dex, null,TriangleCulling.POSITIVE);
graphics.drawTriangles(projected, dex, null);
graphics.endFill();
}

}

}

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: