Skip to main content
Participant
October 31, 2011
Question

Problem with Context3D Sample

  • October 31, 2011
  • 1 reply
  • 1673 views

I was trying to make my own version of the Context3D Sample, but it renders nothing. I am sure that there must be something in the logic, but I need help on finding this bug. Thanks for all the help I can get on this. My code is as follows:

package

{

  import com.adobe.utils.AGALMiniAssembler;

          import com.adobe.utils.PerspectiveMatrix3D;

          import flash.display.Sprite;

          import flash.display.Stage3D;

          import flash.display.StageAlign;

          import flash.display.StageScaleMode;

          import flash.display3D.Context3D;

          import flash.display3D.Context3DRenderMode;

          import flash.display3D.Context3DProgramType;

          import flash.display3D.Context3DTriangleFace;

          import flash.display3D.Context3DVertexBufferFormat;

          import flash.display3D.IndexBuffer3D;

          import flash.display3D.Program3D;

          import flash.display3D.VertexBuffer3D;

          import flash.events.ErrorEvent;

          import flash.events.Event;

          import flash.geom.Matrix3D;

          import flash.geom.Rectangle;

          import flash.geom.Matrix;

          import flash.geom.Vector3D;

 

          public class S3DTest extends Sprite

          {

                    // basic Stage3D objects

                    private var myS3D:Stage3D;

                    private var myC3D:Context3D;

                    // necessary buffers for polygonal object description

                    private var myVBuffer:VertexBuffer3D;

                    private var myIBuffer:IndexBuffer3D;

                    // object data

                    private var vertexes:Vector.<Number>=Vector.<Number>([0,0,0,1,0,0,

                                                                                                                                                      0,1,0,1,0,0,

                                                                                                                                                      1,1,0,1,0,0,

                                                                                                                                                      1,0,0,1,0,0,

                                                                                                                                                      0,0,0,0,1,0,

                                                                                                                                                      1,0,0,0,1,0,

                                                                                                                                                      1,0,1,0,1,0,

                                                                                                                                                      0,0,1,0,1,0,

                                                                                                                                                      0,0,1,1,0,0,

                                                                                                                                                      1,0,1,1,0,0,

                                                                                                                                                      1,1,1,1,0,0,

                                                                                                                                                      0,1,1,1,0,0,

                                                                                                                                                      0,1,1,0,1,0,

                                                                                                                                                      1,1,1,0,1,0,

                                                                                                                                                      1,1,0,0,1,0,

                                                                                                                                                      0,1,0,0,1,0,

                                                                                                                                                      0,1,1,0,0,1,

                                                                                                                                                      0,1,0,0,0,1,

                                                                                                                                                      0,0,0,0,0,1,

                                                                                                                                                      0,0,1,0,0,1,

                                                                                                                                                      1,1,0,0,0,1,

                                                                                                                                                      1,1,1,0,0,1,

                                                                                                                                                      1,0,1,0,0,1,

                                                                                                                                                      1,0,0,0,0,1]);

                    private var indexes:Vector.<uint>=Vector.<uint>([2,1,0,

                                                                                                                                                      3,2,0,

                                                                                                                                                      4,7,5,

                                                                                                                                                      7,6,5,

                                                                                                                                                      8,11,9,

                                                                                                                                                      9,11,10,

                                                                                                                                                      12,15,13,

                                                                                                                                                      13,15,14,

                                                                                                                                                      16,19,17,

                                                                                                                                                      17,19,18,

                                                                                                                                                      20,23,21,

                                                                                                                                                      21,23,22]);

                    // shader data

                    private var myVertexAssembler:AGALMiniAssembler=new AGALMiniAssembler();

                    private var myFragmentAssembler:AGALMiniAssembler=new AGALMiniAssembler();

                    private var myProgram:Program3D;

                    // camera related data

                    private var viewWidth:Number=640;

                    private var viewHeight:Number=480;

                    private var camZNear:Number=1;

                    private var camZFar:Number=500;

                    private var camFOV:Number=45;

                    private var projection:PerspectiveMatrix3D=new PerspectiveMatrix3D();

                    private var model:Matrix3D=new Matrix3D();

                    private var view:Matrix3D=new Matrix3D();

                    private var transformMatrix:Matrix3D=new Matrix3D();

                    private var pivot:Vector3D=new Vector3D();

 

                    public function S3DTest():void

                    {

                              // setup initial state of things in Flash

                              stage.scaleMode=StageScaleMode.NO_SCALE;

                              stage.align=StageAlign.TOP_LEFT;

                              // setup 3D

                              myS3D=stage.stage3Ds[0];

                              myS3D.x=10;

                              myS3D.y=10;

                              myS3D.addEventListener(Event.CONTEXT3D_CREATE,contextCreated);

                              myS3D.addEventListener(ErrorEvent.ERROR,contextError);

                              myS3D.requestContext3D(Context3DRenderMode.AUTO);

                    }

 

                    private function contextCreated(e:Event):void{

                              // setup 3D context

                              myC3D=Stage3D(e.target).context3D;

                              trace("3D driver: "+myC3D.driverInfo);

                              myC3D.enableErrorChecking=true; // slows render, but useful when testing

                              myC3D.configureBackBuffer(viewWidth,viewHeight,2,false);

                              myC3D.setCulling(Context3DTriangleFace.BACK);

                              initPolys(); // after getting our 3D context we initialize our objects

                              initShaders(); // we then proceed to the render step

                    }

 

                    private function initPolys():void{

                              // load 3D objects

                              myVBuffer=myC3D.createVertexBuffer(vertexes.length/6,6);

                              myVBuffer.uploadFromVector(vertexes,0,vertexes.length/6);

                              myIBuffer=myC3D.createIndexBuffer(indexes.length);

                              myIBuffer.uploadFromVector(indexes,0,indexes.length);

                              // assign vertex data inputs for the shader code

                              myC3D.setVertexBufferAt(0,myVBuffer,0,Context3DVertexBufferFormat.FLOAT_3); // coordinates

                              myC3D.setVertexBufferAt(1,myVBuffer,3,Context3DVertexBufferFormat.FLOAT_3); // colors

                    }

 

                    private function initShaders():void{

                              // vertex shaders

                              myVertexAssembler.assemble(Context3DProgramType.VERTEX,"m44 op, va0, vc0 \n"+"mov v0, va1",false);

                              myFragmentAssembler.assemble(Context3DProgramType.FRAGMENT,"mov oc, v0",false);

                              // create shader program and upload vertex and fragment codes

                              myProgram=myC3D.createProgram();

                              myProgram.upload(myVertexAssembler.agalcode,myFragmentAssembler.agalcode);

                              myC3D.setProgram(myProgram);

                              // set camera related data

                              projection.perspectiveFieldOfViewLH(camFOV,viewWidth/viewHeight,camZNear,camZFar);

                              view.appendTranslation(0,0,-2); // move camera back in Z axis

                              model.appendTranslation(-0.5,-0.5,-0.5); // translate object to center

                              // set the render loop

                              stage.addEventListener(Event.ENTER_FRAME,renderPolys); // for your 3D animation

                    }

 

                    private function renderPolys(e:Event):void{

                              // rotate the object each frame. Note that the rotation order is important

                              model.appendRotation(0.5,Vector3D.Z_AXIS,pivot);

                              model.appendRotation(0.5,Vector3D.Y_AXIS,pivot);

                              model.appendRotation(0.5,Vector3D.X_AXIS,pivot);

                              // add camera and object transformations

                              transformMatrix.identity();

                              transformMatrix.append(model);

                              transformMatrix.append(view);

                              transformMatrix.append(projection);

                              // apply transformations

                              myC3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX,0,transformMatrix,true);

                              // clear render area and render

                              myC3D.clear(0.2,0.6,0.8);

                              myC3D.drawTriangles(myIBuffer,0,12);

                              myC3D.present();

                    }

 

                    private function contextError(e:ErrorEvent):void{

                              trace(e.errorID+": "+e.text);

                    }

          }

}

This topic has been closed for replies.

1 reply

Participating Frequently
February 10, 2014

This was posted in 2011. I'm having the same problem with the example in 2014. It would be really helpful if some of this documentation were kept up-to-date in the AS3 Developer's guide. Very frustrating to try and learn.

Participant
February 10, 2014

It is an old post, so I do not exactly remember if I made this very same code work. But I can tell you that I started a totally new sample coded by hand and it worked.

You can see my article and code in: http://sdjournal.org/alternativa-3d-december-issue-of-flash-and-flex-developers-magazine/ which is a free issue of the magazine. My article is the one about Proscenium 3D.

I hope the text may be of any help to you.

Participating Frequently
February 11, 2014

I will definitely take a look at it. Thank you so much!