| 1 |
/* |
|---|
| 2 |
* Copyright(c) 2006 the Spark project. |
|---|
| 3 |
* |
|---|
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 5 |
* you may not use this file except in compliance with the License. |
|---|
| 6 |
* You may obtain a copy of the License at |
|---|
| 7 |
* |
|---|
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 9 |
* |
|---|
| 10 |
* Unless required by applicable law or agreed to in writing, software |
|---|
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
|---|
| 13 |
* either express or implied. See the License for the specific language |
|---|
| 14 |
* governing permissions and limitations under the License. |
|---|
| 15 |
*/ |
|---|
| 16 |
|
|---|
| 17 |
package |
|---|
| 18 |
{ |
|---|
| 19 |
import org.libspark.geom.Matrix4; |
|---|
| 20 |
import org.libspark.geom.Vector3D; |
|---|
| 21 |
import org.libspark.geom.Vector4D; |
|---|
| 22 |
import org.libspark.thunder.*; |
|---|
| 23 |
import org.libspark.protect.ProtectedLoader; |
|---|
| 24 |
import flash.display.Sprite; |
|---|
| 25 |
import flash.display.BitmapData; |
|---|
| 26 |
import flash.display.Bitmap; |
|---|
| 27 |
import flash.display.Loader; |
|---|
| 28 |
import flash.net.URLRequest; |
|---|
| 29 |
import flash.events.Event; |
|---|
| 30 |
import flash.events.KeyboardEvent; |
|---|
| 31 |
import flash.events.MouseEvent; |
|---|
| 32 |
import flash.ui.Keyboard; |
|---|
| 33 |
import flash.utils.getTimer; |
|---|
| 34 |
|
|---|
| 35 |
public class Demo4 extends Sprite |
|---|
| 36 |
{ |
|---|
| 37 |
private var render:Render3; |
|---|
| 38 |
private var worldMatrix:Matrix4; |
|---|
| 39 |
private var vertices:Array; |
|---|
| 40 |
private var indices:Array; |
|---|
| 41 |
private var len:uint; |
|---|
| 42 |
private var ry:Number; |
|---|
| 43 |
private var rdy:Number; |
|---|
| 44 |
private var isShiftDowning:Boolean; |
|---|
| 45 |
|
|---|
| 46 |
private var loadedTextures:uint; |
|---|
| 47 |
private var textureSourceR:Loader; |
|---|
| 48 |
private var textureR:BitmapData; |
|---|
| 49 |
private var textureSourceL:Loader; |
|---|
| 50 |
private var textureL:BitmapData; |
|---|
| 51 |
|
|---|
| 52 |
public function Demo4() |
|---|
| 53 |
{ |
|---|
| 54 |
stage.scaleMode = 'noScale'; |
|---|
| 55 |
|
|---|
| 56 |
render = new Render3(); |
|---|
| 57 |
|
|---|
| 58 |
initializeMatrices(render); |
|---|
| 59 |
initializeLight(render); |
|---|
| 60 |
initializeModel(); |
|---|
| 61 |
|
|---|
| 62 |
render.setRenderTarget(graphics); |
|---|
| 63 |
render.setViewport(new Viewport(0, 0, 256, 256)); |
|---|
| 64 |
|
|---|
| 65 |
ry = rdy = 0; |
|---|
| 66 |
isShiftDowning = false; |
|---|
| 67 |
|
|---|
| 68 |
loadedTextures = 0; |
|---|
| 69 |
textureR = new BitmapData(256, 256, false); |
|---|
| 70 |
textureL = new BitmapData(256, 256, false); |
|---|
| 71 |
textureSourceR = new ProtectedLoader(441); |
|---|
| 72 |
textureSourceL = new ProtectedLoader(441); |
|---|
| 73 |
loadTexture(textureSourceR, 'texture.swf'); |
|---|
| 74 |
loadTexture(textureSourceL, 'texture.2.swf'); |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
private function loadTexture(loader:Loader, url:String):void |
|---|
| 78 |
{ |
|---|
| 79 |
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); |
|---|
| 80 |
loader.load(new URLRequest(url)); |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
private function completeHandler(event:Event):void |
|---|
| 84 |
{ |
|---|
| 85 |
if (++loadedTextures >= 2) { |
|---|
| 86 |
textureSourceR.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler); |
|---|
| 87 |
textureSourceR.content.scaleX = 256 / textureSourceR.contentLoaderInfo.width; |
|---|
| 88 |
textureSourceR.content.scaleY = 256 / textureSourceR.contentLoaderInfo.height; |
|---|
| 89 |
|
|---|
| 90 |
textureSourceL.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler); |
|---|
| 91 |
textureSourceL.content.scaleX = 256 / textureSourceL.contentLoaderInfo.width; |
|---|
| 92 |
textureSourceL.content.scaleY = 256 / textureSourceL.contentLoaderInfo.height; |
|---|
| 93 |
|
|---|
| 94 |
stage.addEventListener(MouseEvent.CLICK, clickHandler); |
|---|
| 95 |
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); |
|---|
| 96 |
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); |
|---|
| 97 |
addEventListener(Event.ENTER_FRAME, enterFrameHandler); |
|---|
| 98 |
} |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
private function enterFrameHandler(event:Event):void |
|---|
| 102 |
{ |
|---|
| 103 |
var m:Matrix4 = worldMatrix; |
|---|
| 104 |
var r:Render3 = render; |
|---|
| 105 |
|
|---|
| 106 |
if (rdy != 0) { |
|---|
| 107 |
ry += rdy; |
|---|
| 108 |
|
|---|
| 109 |
if (rdy < 0 && ry <= 0) { |
|---|
| 110 |
rdy = 0; |
|---|
| 111 |
ry = 0; |
|---|
| 112 |
} |
|---|
| 113 |
if (rdy > 0 && ry >= (Math.PI / 2)) { |
|---|
| 114 |
rdy = 0; |
|---|
| 115 |
ry = Math.PI / 2; |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
m.identity(); |
|---|
| 120 |
|
|---|
| 121 |
textureR.draw(textureSourceR); |
|---|
| 122 |
textureL.draw(textureSourceL); |
|---|
| 123 |
|
|---|
| 124 |
r.clear(); |
|---|
| 125 |
r.beginScene(); |
|---|
| 126 |
{ |
|---|
| 127 |
if (isShiftDowning) { |
|---|
| 128 |
m.rotateY((mouseX / 256) * (Math.PI / 2)); |
|---|
| 129 |
} |
|---|
| 130 |
else { |
|---|
| 131 |
m.rotateY(ry); |
|---|
| 132 |
} |
|---|
| 133 |
r.updateMatrix(); |
|---|
| 134 |
r.setTexture(textureR); |
|---|
| 135 |
r.drawIndexedPrimitive(indices, vertices, len); |
|---|
| 136 |
|
|---|
| 137 |
m.rotateY(- Math.PI / 2); |
|---|
| 138 |
r.updateMatrix(); |
|---|
| 139 |
r.setTexture(textureL); |
|---|
| 140 |
r.drawIndexedPrimitive(indices, vertices, len); |
|---|
| 141 |
} |
|---|
| 142 |
var poly:uint = r.endScene(); |
|---|
| 143 |
r.present(); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
private function keyDownHandler(event:KeyboardEvent):void |
|---|
| 147 |
{ |
|---|
| 148 |
isShiftDowning = (event.keyCode == Keyboard.SHIFT); |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
private function keyUpHandler(event:KeyboardEvent):void |
|---|
| 152 |
{ |
|---|
| 153 |
isShiftDowning = false; |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
private function clickHandler(event:Event):void |
|---|
| 157 |
{ |
|---|
| 158 |
if (!isShiftDowning) { |
|---|
| 159 |
if (ry == 0) { |
|---|
| 160 |
rdy = 0.3; |
|---|
| 161 |
} |
|---|
| 162 |
if (ry == (Math.PI / 2)) { |
|---|
| 163 |
rdy = -0.3; |
|---|
| 164 |
} |
|---|
| 165 |
} |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
private function initializeMatrices(render:Render3):void |
|---|
| 169 |
{ |
|---|
| 170 |
worldMatrix = new Matrix4(); |
|---|
| 171 |
render.setWorldMatrix(worldMatrix); |
|---|
| 172 |
|
|---|
| 173 |
var view:Matrix4 = new Matrix4(); |
|---|
| 174 |
var vCam:Vector3D = new Vector3D(0, 0, 172); |
|---|
| 175 |
var vCamEye:Vector3D = new Vector3D(0, 0, 0); |
|---|
| 176 |
var vCamTop:Vector3D = new Vector3D(0, 1, 0); |
|---|
| 177 |
var camDiv:Number = 0.1; |
|---|
| 178 |
MatrixUtil.lookAt(view, vCam, vCamEye, vCamTop); |
|---|
| 179 |
render.setViewMatrix(view); |
|---|
| 180 |
|
|---|
| 181 |
var projection:Matrix4 = new Matrix4(); |
|---|
| 182 |
MatrixUtil.perspective(projection, 60 * Math.PI / 180, 1, 0.01, 100); |
|---|
| 183 |
render.setProjectionMatrix(projection); |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
private function initializeLight(render:Render3):void |
|---|
| 187 |
{ |
|---|
| 188 |
var d:Vector3D = new Vector3D(0, -5, -5); |
|---|
| 189 |
d.normalize(); |
|---|
| 190 |
render.setLight(new Light(d, new ColorValue(0.3, 0.3, 0.3), new ColorValue(0.7, 0.7, 0.7))); |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
private function initializeModel():void |
|---|
| 194 |
{ |
|---|
| 195 |
vertices = new Array(9); |
|---|
| 196 |
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)); |
|---|
| 197 |
indices = new Array(24); |
|---|
| 198 |
createPlaneIndex(indices, 0, 0); |
|---|
| 199 |
len = 8; |
|---|
| 200 |
} |
|---|
| 201 |
|
|---|
| 202 |
private function createPlane(v:Array, i:uint, a:Vertex, b:Vertex, c:Vertex, d:Vertex):void |
|---|
| 203 |
{ |
|---|
| 204 |
v[i] = a; |
|---|
| 205 |
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); |
|---|
| 206 |
v[i + 2] = b; |
|---|
| 207 |
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); |
|---|
| 208 |
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); |
|---|
| 209 |
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); |
|---|
| 210 |
v[i + 6] = c; |
|---|
| 211 |
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); |
|---|
| 212 |
v[i + 8] = d; |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
private function createPlaneIndex(v:Array, i:uint, j:uint):void |
|---|
| 216 |
{ |
|---|
| 217 |
createPolygonIndex(v, i , j , j + 1, j + 3, j + 4); |
|---|
| 218 |
createPolygonIndex(v, i + 6, j + 1, j + 2, j + 4, j + 5); |
|---|
| 219 |
createPolygonIndex(v, i + 12, j + 3, j + 4, j + 6, j + 7); |
|---|
| 220 |
createPolygonIndex(v, i + 18, j + 4, j + 5, j + 7, j + 8); |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
private function createPolygonIndex(v:Array, i:uint, a:uint, b:uint, c:uint, d:uint):void |
|---|
| 224 |
{ |
|---|
| 225 |
v[i] = a; |
|---|
| 226 |
v[i + 1] = b; |
|---|
| 227 |
v[i + 2] = c; |
|---|
| 228 |
v[i + 3] = c; |
|---|
| 229 |
v[i + 4] = b; |
|---|
| 230 |
v[i + 5] = d; |
|---|
| 231 |
} |
|---|
| 232 |
} |
|---|
| 233 |
} |
|---|