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

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

    public class Pv3dWindow extends NativeWindow
    {
        private var container : Sprite;
        private var scene     : Scene3D;
        private var camera    : Camera3D;

        private var planeObj  : DisplayObject3D;
        private var planeSize : int = 300;      ///< Planeオブジェクト1辺の長さ
        private var segment   : int = 1;        ///< 面の分割数
        private var material  : BitmapMaterial;
        private var bmpData   : BitmapData;

        private var valx      : Number = 0;
        private var valy      : Number = 0;

        static public const
            TEXTURE_W:int = 240,    ///< テクスチャの幅
            TEXTURE_H:int = 240;    ///< テクスチャの高さ

        /// コンストラクタ
        public function Pv3dWindow(initOptions:NativeWindowInitOptions){
            super(initOptions);

            stage.quality   = "MEDIUM";
            stage.scaleMode = "noScale";
            stage.align = StageAlign.TOP_LEFT;
            this.stage.addEventListener(Event.RESIZE, onStageResize);

            init3D();
        }

        // _______________________________________________________________________
        //                                                                  Init3D
        private function init3D():void
        {
            //コンテナ生成
            this.container = new Sprite();
            stage.addChild(this.container);
            this.container.x = this.stage.stageWidth  / 2;
            this.container.y = this.stage.stageHeight / 2;

            //シーン生成
            scene = new Scene3D( container );

            //カメラ設定
            camera = new Camera3D();
            camera.z = -planeSize;
            camera.focus = 500;
            camera.zoom = 1;

            //BitmapDataの作成。Bitmapに角丸矩形を描く
            bmpData = new BitmapData(TEXTURE_W,TEXTURE_H,true,0x00000000);
            var rrect:Shape = new Shape();
            rrect.graphics.lineStyle(0,0x47FF8A);
            rrect.graphics.beginFill(0x47FF8A);
            rrect.graphics.drawRoundRect(0,0,TEXTURE_W,TEXTURE_H,50);
            rrect.graphics.endFill();
            bmpData.draw(rrect);

            //マテリアル設定
            material = new BitmapMaterial(bmpData);
            material.oneSide = false;

            //Planeオブジェクト生成
            planeObj = scene.addChild( new Plane( material, planeSize, planeSize, segment, segment) );
        }

        // _______________________________________________________________________
        //                                                                    Loop
        public function loop3D( event:Event ):void
        {
            trace("loop3d");
            valx += 1;
            valy += 1;
            planeObj.rotationY = valx;
            planeObj.rotationX = valy;

            //レンダリング
            this.scene.renderCamera( camera );
        }

        // _______________________________________________________________________
        //                                                           onStageResize
        private function onStageResize(event:Event):void
        {
            this.container.x = this.stage.stageWidth  / 2;
            this.container.y = this.stage.stageHeight / 2;
        }
    }
}