package
{
    import flash.display.*;
    import flash.events.*;

    import org.papervision3d.scenes.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.materials.*;


    public class AirEarth3D extends Sprite
    {
        private var container :Sprite;
        private var scene     :Scene3D;
        private var camera    :Camera3D;
        private var sphere    :Ase;

        function AirEarth3D()
        {
            init3D();

            this.addEventListener( Event.ENTER_FRAME, loop3D );
        }

        private function init3D():void
        {
            container = new Sprite();
            addChild( container );
            container.x = 400/2;
            container.y = 320/2;

            scene = new Scene3D( container );

            camera = new Camera3D();

            addEarth();
            addSpace();
        }


        private function addEarth():void
        {
            var materialEarth :BitmapFileMaterial = new BitmapFileMaterial("EarthMap.jpg");

            sphere = new Ase( materialEarth, "world.ase", 0.5 );

            // Position sphere
            sphere.rotationX = 45;
            sphere.yaw( -30 );

            // Add to scene
            scene.addChild( sphere );
        }

        private function addSpace():void
        {
            var materialSpace :BitmapFileMaterial = new BitmapFileMaterial("space.jpg");

            var plane:DisplayObject3D = new Plane( materialSpace, 6400, 4800, 8, 8 );

            // Position plane
            plane.z = 500;

            // Add to scene
            scene.addChild( plane );
        }

        private function loop3D(event:Event):void
        {
            camera.x = -container.mouseX/4;
            camera.y = container.mouseY/3;

            sphere.yaw( 0.2 );

            scene.renderCamera( camera );
        }
    }
}