/* * Copyright(c) 2006 the Spark project. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package { import org.libspark.geom.Matrix4; import org.libspark.geom.Vector3D; import org.libspark.geom.Vector4D; import org.libspark.thunder.*; import org.libspark.protect.ProtectedLoader; import flash.display.Sprite; import flash.display.BitmapData; import flash.display.Bitmap; import flash.display.Loader; import flash.net.URLRequest; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.ui.Keyboard; import flash.utils.getTimer; public class Demo4 extends Sprite { private var render:Render3; private var worldMatrix:Matrix4; private var vertices:Array; private var indices:Array; private var len:uint; private var ry:Number; private var rdy:Number; private var isShiftDowning:Boolean; private var loadedTextures:uint; private var textureSourceR:Loader; private var textureR:BitmapData; private var textureSourceL:Loader; private var textureL:BitmapData; public function Demo4() { stage.scaleMode = 'noScale'; render = new Render3(); initializeMatrices(render); initializeLight(render); initializeModel(); render.setRenderTarget(graphics); render.setViewport(new Viewport(0, 0, 256, 256)); ry = rdy = 0; isShiftDowning = false; loadedTextures = 0; textureR = new BitmapData(256, 256, false); textureL = new BitmapData(256, 256, false); textureSourceR = new ProtectedLoader(441); textureSourceL = new ProtectedLoader(441); loadTexture(textureSourceR, 'texture.swf'); loadTexture(textureSourceL, 'texture.2.swf'); } private function loadTexture(loader:Loader, url:String):void { loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); loader.load(new URLRequest(url)); } private function completeHandler(event:Event):void { if (++loadedTextures >= 2) { textureSourceR.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler); textureSourceR.content.scaleX = 256 / textureSourceR.contentLoaderInfo.width; textureSourceR.content.scaleY = 256 / textureSourceR.contentLoaderInfo.height; textureSourceL.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler); textureSourceL.content.scaleX = 256 / textureSourceL.contentLoaderInfo.width; textureSourceL.content.scaleY = 256 / textureSourceL.contentLoaderInfo.height; stage.addEventListener(MouseEvent.CLICK, clickHandler); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); addEventListener(Event.ENTER_FRAME, enterFrameHandler); } } private function enterFrameHandler(event:Event):void { var m:Matrix4 = worldMatrix; var r:Render3 = render; if (rdy != 0) { ry += rdy; if (rdy < 0 && ry <= 0) { rdy = 0; ry = 0; } if (rdy > 0 && ry >= (Math.PI / 2)) { rdy = 0; ry = Math.PI / 2; } } m.identity(); textureR.draw(textureSourceR); textureL.draw(textureSourceL); r.clear(); r.beginScene(); { if (isShiftDowning) { m.rotateY((mouseX / 256) * (Math.PI / 2)); } else { m.rotateY(ry); } r.updateMatrix(); r.setTexture(textureR); r.drawIndexedPrimitive(indices, vertices, len); m.rotateY(- Math.PI / 2); r.updateMatrix(); r.setTexture(textureL); r.drawIndexedPrimitive(indices, vertices, len); } var poly:uint = r.endScene(); r.present(); } private function keyDownHandler(event:KeyboardEvent):void { isShiftDowning = (event.keyCode == Keyboard.SHIFT); } private function keyUpHandler(event:KeyboardEvent):void { isShiftDowning = false; } private function clickHandler(event:Event):void { if (!isShiftDowning) { if (ry == 0) { rdy = 0.3; } if (ry == (Math.PI / 2)) { rdy = -0.3; } } } private function initializeMatrices(render:Render3):void { worldMatrix = new Matrix4(); render.setWorldMatrix(worldMatrix); var view:Matrix4 = new Matrix4(); var vCam:Vector3D = new Vector3D(0, 0, 172); var vCamEye:Vector3D = new Vector3D(0, 0, 0); var vCamTop:Vector3D = new Vector3D(0, 1, 0); var camDiv:Number = 0.1; MatrixUtil.lookAt(view, vCam, vCamEye, vCamTop); render.setViewMatrix(view); var projection:Matrix4 = new Matrix4(); MatrixUtil.perspective(projection, 60 * Math.PI / 180, 1, 0.01, 100); render.setProjectionMatrix(projection); } private function initializeLight(render:Render3):void { var d:Vector3D = new Vector3D(0, -5, -5); d.normalize(); render.setLight(new Light(d, new ColorValue(0.3, 0.3, 0.3), new ColorValue(0.7, 0.7, 0.7))); } private function initializeModel():void { vertices = new Array(9); createPlane(vertices, 0, new Vertex(-64, 64, 64, 0, 0), new Vertex(-64, -64, 64, 0, 1), new Vertex(64, 64, 64, 1, 0), new Vertex(64, -64, 64, 1, 1)); indices = new Array(24); createPlaneIndex(indices, 0, 0); len = 8; } private function createPlane(v:Array, i:uint, a:Vertex, b:Vertex, c:Vertex, d:Vertex):void { v[i] = a; v[i + 1] = new Vertex((a.vec.x + b.vec.x) / 2, (a.vec.y + b.vec.y) / 2, (a.vec.z + b.vec.z) / 2, (a.u + b.u) / 2, (a.v + b.v) / 2); v[i + 2] = b; v[i + 3] = new Vertex((a.vec.x + c.vec.x) / 2, (a.vec.y + c.vec.y) / 2, (a.vec.z + c.vec.z) / 2, (a.u + c.u) / 2, (a.v + c.v) / 2); v[i + 4] = new Vertex((b.vec.x + c.vec.x) / 2, (b.vec.y + c.vec.y) / 2, (b.vec.z + c.vec.z) / 2, (b.u + c.u) / 2, (b.v + c.v) / 2); v[i + 5] = new Vertex((b.vec.x + d.vec.x) / 2, (b.vec.y + d.vec.y) / 2, (b.vec.z + d.vec.z) / 2, (b.u + d.u) / 2, (b.v + d.v) / 2); v[i + 6] = c; v[i + 7] = new Vertex((c.vec.x + d.vec.x) / 2, (c.vec.y + d.vec.y) / 2, (c.vec.z + d.vec.z) / 2, (c.u + d.u) / 2, (c.v + d.v) / 2); v[i + 8] = d; } private function createPlaneIndex(v:Array, i:uint, j:uint):void { createPolygonIndex(v, i , j , j + 1, j + 3, j + 4); createPolygonIndex(v, i + 6, j + 1, j + 2, j + 4, j + 5); createPolygonIndex(v, i + 12, j + 3, j + 4, j + 6, j + 7); createPolygonIndex(v, i + 18, j + 4, j + 5, j + 7, j + 8); } private function createPolygonIndex(v:Array, i:uint, a:uint, b:uint, c:uint, d:uint):void { v[i] = a; v[i + 1] = b; v[i + 2] = c; v[i + 3] = c; v[i + 4] = b; v[i + 5] = d; } } }