package
{
import com.adobe.pixelBender3D.AGALProgramPair;
import com.adobe.pixelBender3D.PBASMCompiler;
import com.adobe.pixelBender3D.PBASMProgram;
import com.adobe.pixelBender3D.RegisterMap;
import com.adobe.pixelBender3D.utils.VertexBufferHelper;
import flash.display.Sprite;
import flash.display.Stage3D;
import flash.display3D.Context3D;
import flash.display3D.Context3DCompareMode;
import flash.display3D.Context3DTriangleFace;
import flash.display3D.IndexBuffer3D;
import flash.display3D.Program3D;
import flash.display3D.VertexBuffer3D;
import flash.display3D.Context3DRenderMode;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.getTimer;
import flash.utils.Timer;
import ExampleUtils;
public class Main extends Sprite
{
[Embed (source="./vp_triangle.pbasm", mimeType="application/octet-stream")]
protected static const VertexPositionProgram : Class;
[Embed (source="./mv_triangle.pbasm", mimeType="application/octet-stream")]
protected static const VertexMaterialProgram : Class;
[Embed (source="./mf_triangle.pbasm", mimeType="application/octet-stream")]
protected static const FragmentMaterialProgram : Class;
// molehill-related variables
private var context3D_ : Context3D;
private var indexBuffer_ : IndexBuffer3D;
private var rendertimer_ : Timer;
private var shaderProgram_ : Program3D;
private var vertexBuffer_ : VertexBuffer3D;
private var _stage3D:Stage3D;
// PB3D-related variables
private var vertexRegisterMap_ : RegisterMap;
private var fragmentRegisterMap_ : RegisterMap;
private var vertexBufferMap_ : VertexBufferHelper;
public function Main()
{
_stage3D = stage.stage3Ds[0];
_stage3D.addEventListener( Event.CONTEXT3D_CREATE, Context3DCreateHandler );
_stage3D.requestContext3D( Context3DRenderMode.AUTO );
}
private function Context3DCreateHandler(e:Event) : void
{
context3D_ = _stage3D.context3D;
init(context3D_);
}
public function init(context : Context3D):void
{
context3D_ = context;
setupBuffer();
setupShadingPrograms();
setupGeometry();
vertexBufferMap_ = new VertexBufferHelper(context3D_,
vertexRegisterMap_.inputVertexRegisters,
vertexBuffer_);
// at this point, our Triangle object has sufficient information to do
// its job when the render calls start coming in, so let's get them
// going
rendertimer_ = new Timer(1);
rendertimer_.addEventListener(TimerEvent.TIMER, frameEventHandler);
rendertimer_.start();
}
private function frameEventHandler(event : Event) : void
{
render(getTimer());
}
public function render(time : Number) : void
{
context3D_.clear(0, 0, 0, 0);
context3D_.setProgram(shaderProgram_);
vertexBufferMap_.setVertexBuffers();
context3D_.drawTriangles(indexBuffer_, 0, 1);
context3D_.present();
}
private function setupBuffer() : void
{
context3D_.configureBackBuffer(400, 400, 0, true);
context3D_.setDepthTest(true, Context3DCompareMode.LESS);
context3D_.setCulling(Context3DTriangleFace.NONE);
}
private function setupGeometry() : void
{
indexBuffer_ = context3D_.createIndexBuffer(3);
indexBuffer_.uploadFromVector(Vector.([0, 1, 2]), 0, 3);
vertexBuffer_ = context3D_.createVertexBuffer(3, 8); // 3 vertices, XYZW, RGBA
vertexBuffer_.uploadFromVector(Vector.([-1, -1, 0, 1, 1, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 1,
1, -1, 0, 1, 0, 0, 1, 1]), 0, 3);
}
private function setupShadingPrograms() : void
{
var vertexPositionProgram : PBASMProgram;
var vertexMaterialProgram : PBASMProgram;
var fragmentMaterialProgram : PBASMProgram;
vertexPositionProgram = ExampleUtils.resourceToPBASMProgram(VertexPositionProgram);
vertexMaterialProgram = ExampleUtils.resourceToPBASMProgram(VertexMaterialProgram);
fragmentMaterialProgram = ExampleUtils.resourceToPBASMProgram(FragmentMaterialProgram);
var translatedPrograms : AGALProgramPair;
translatedPrograms = PBASMCompiler.compile(vertexPositionProgram,
vertexMaterialProgram,
fragmentMaterialProgram);
// we'll need these in order to set variables without resorting to low
// level molehill calls. A later example will show PB3D use
// without use of RegisterMap objects.
vertexRegisterMap_ = translatedPrograms.vertexProgram.registers;
fragmentRegisterMap_ = translatedPrograms.fragmentProgram.registers;
// we've now translated everything to AGAL, the binary format that
// molehill accepts, so let's create a Molehill shader program and upload
// our binaries
shaderProgram_ = context3D_.createProgram();
shaderProgram_.upload(translatedPrograms.vertexProgram.byteCode,
translatedPrograms.fragmentProgram.byteCode);
}
}
}