topical media & game development

talk show tell print

student-papervision-samples-PaperBase.ax

student-papervision-samples-PaperBase.ax (swf ) [ flash ] flex


  package  {
          // These lines make differant 'pieces' available in your code.
          import flash.display.Sprite; // To extend this class
          import flash.events.Event; // To work out when a frame is entered.
          import org.papervision3d.core.proto.CameraObject3D;
          import org.papervision3d.view.Viewport3D; // We need a viewport
          import org.papervision3d.cameras.*; // Import all types of camera
          import org.papervision3d.scenes.Scene3D; // We'll need at least one scene
          import org.papervision3d.render.BasicRenderEngine; // And we need a renderer
          
          public class @ax-student-papervision-samples-PaperBase extends Sprite { //Must be "extends Sprite"
                  
                  public var viewport:Viewport3D; // The Viewport
                  public var renderer:BasicRenderEngine; // Rendering engine
                  
                  public var current_scene:Scene3D;
                  public var current_camera:CameraObject3D;
                  public var current_viewport:Viewport3D;
                  // Scenes //
                  public var default_scene:Scene3D; // A Scene
                  // Cameras //
                  public var default_camera:Camera3D; // A Camera
                  
                  public function init(vpWidth:Number = 800, vpHeight:Number = 600):void {
                          initPapervision(vpWidth, vpHeight); // Initialise papervision
                          init3d(); // Initialise the 3d stuff..
                          init2d(); // Initialise the interface..
                          initEvents(); // Set up any event listeners..
                  }
                  
                  protected function initPapervision(vpWidth:Number, vpHeight:Number):void {
                          // Here is where we initialise everything we need to
                          // render a papervision scene.
                          viewport = new Viewport3D(vpWidth, vpHeight);
                          // The viewport is the object added to the flash scene.
                          // You 'look at' the papervision scene through the viewport
                          // window, which is placed on the flash stage.
                          addChild(viewport); // Add the viewport to the stage.
                          // Initialise the rendering engine.
                          renderer = new BasicRenderEngine();
                          // Initialise the Scenes  //
                          default_scene = new Scene3D();
                          // Initialise the Cameras  //
                          default_camera = new Camera3D();
                          
                          current_camera = default_camera;
                          current_scene = default_scene;
                          current_viewport = viewport;
                          
                  }
                  
                  protected function init3d():void {
                          // This function should hold all of the stages needed
                          // to initialise everything used for papervision.
                          // Models, materials, cameras etc.
                  }
                  
                  protected function init2d():void {
                          // This function should create all of the 2d items
                          // that will be overlayed on your papervision project.
                          // User interfaces, Heads up displays etc.
                  }
                  
                  protected function initEvents():void {
                          // This function makes the onFrame function get called for
                          // every frame.
                          addEventListener(Event.ENTER_FRAME, onEnterFrame);
                          // This line of code makes the onEnterFrame function get
                          // called when every frame is entered.
                  }
                  
                  protected function processFrame():void {
                          // Process any movement or animation here.
                  }
                  
                  protected function onEnterFrame( ThisEvent:Event ):void {
                          //We need to render the scene and update anything here.
                          processFrame();
                          renderer.renderScene(current_scene, current_camera, current_viewport);
                  }
                  
          }
          
  }


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.