Erain 3D
-->

Level: Beginner Version : 2.0 Author : Thomas PFEIFFER Date : OCtober 08th 2007
Reference : Starting with Sandy 2.0


Create a Box mapped with external bitmap

First, read this first tutorial: Starting with Sandy 2.0

Ok, I think people need a bit of example to know how to start with this beta version.

So let's go, and display a simple box, with an external picture as texture.

Code example

import com.bourre.commands.Delegate;
import com.bourre.data.libs.GraphicLib;
import com.bourre.data.libs.GraphicLibLocator;
import com.bourre.data.libs.LibEvent;
import com.bourre.data.libs.LibStack;
import com.bourre.log.Logger;
import com.bourre.utils.LuminicTracer;
 
import flash.display.BitmapData;
 
import sandy.core.scenegraph.Camera3D;
import sandy.core.scenegraph.Group;
import sandy.core.World3D;
import sandy.materials.Appearance;
import sandy.materials.BitmapMaterial;
import sandy.primitive.Box;
import sandy.util.BitmapUtil;
 
/**
 * @author thomas pfeiffer
 */
class Test
{	
	private var _mc:MovieClip;
	private var _world:World3D;
	private var box:Box;
	public static function main( mc:MovieClip ):Test
	{
		var t:Test = new Test(mc);
		return t;
	}
 
	public function Test( p_oMc:MovieClip )
	{
		// In case you need a debugger console. Here we use the Luminic one.
		//Logger.getInstance().addLogListener(LuminicTracer.getInstance());
 
		Stage.align = "TL";
		Stage.scaleMode = "noscale";
 
		this._mc = p_oMc;
		this._world = World3D.getInstance();
		this._world.container = p_oMc;
 
                // creation of the stack to load our external files.
                // Here we only load a picture, but you can add as many files as you want
		var lStack:LibStack = new LibStack();
		var lContainer:MovieClip = _mc.createEmptyMovieClip("texture",_mc.getNextHighestDepth());
		var gl:GraphicLib = new GraphicLib( lContainer, 0, false );
                // We consider to load a picture nammed : "texture.jpg" at the same position as the swf file.
		lStack.enqueue( gl, "TEXTURE", "texture.jpg" );
		lStack.addEventListener( LibStack.onTimeOutEVENT, this, _onTimeout);
		lStack.addEventListener( LibStack.onLoadCompleteEVENT, this, _onReady);
		lStack.load();
	}	
 
	private function _onTimeout( pEvt:LibEvent ):Void
	{
		Logger.LOG("timeout");
	}
 
	private function _onReady( pEvt:LibEvent ):Void
	{
		this._init();
	}
 
	// Initiate the world
	private function _init( Void ):Void
	{
		this._world.root = _createScene();
 
		var oCam:Camera3D = new Camera3D(500,500);
		this._world.camera = oCam;
		this._world.root.addChild( _world.camera );		
                // we need to call ourself now the render method.
                // Here we use the enterframe event, using a Delegate command.
		this._mc.onEnterFrame = Delegate.create( this, _onRender );
	}
 
	// Create the object tree
	private function _createScene( Void ):Group
	{
		var g:Group = new Group("root");
 
		this.box = new Box("myBox", 100, 100, 100, "tri", 2);
		this.box.x = 0;
		this.box.y = 0;
		this.box.z = 300;
		// we retrieve the file, and convert it as a bitmapdata object
		var gl:GraphicLib = GraphicLibLocator.getInstance().getGraphicLib("TEXTURE");
		var lBmd:BitmapData = BitmapUtil.movieToBitmap( gl.getContent(), true );
		// we apply the bitmap appearance
		this.box.appearance = new Appearance( new BitmapMaterial( lBmd ) );
 
		g.addChild( this.box );
 
		return g;
	}
 
        // we render the world
	private function _onRender( Void ):Void
	{
		_world.render();
	}
}