| 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.text.TextField; |
|---|
| 30 |
import flash.events.Event; |
|---|
| 31 |
import flash.utils.getTimer; |
|---|
| 32 |
|
|---|
| 33 |
public class Demo3 extends Sprite |
|---|
| 34 |
{ |
|---|
| 35 |
private var render:Render3; |
|---|
| 36 |
private var worldMatrix:Matrix4; |
|---|
| 37 |
private var vertices:Array; |
|---|
| 38 |
private var indices:Array; |
|---|
| 39 |
private var len:uint; |
|---|
| 40 |
private var fps:uint; |
|---|
| 41 |
private var fpsField:TextField; |
|---|
| 42 |
private var rx:Number; |
|---|
| 43 |
private var ry:Number; |
|---|
| 44 |
private var rz:Number; |
|---|
| 45 |
private var textureSource:Loader; |
|---|
| 46 |
private var texture:BitmapData; |
|---|
| 47 |
|
|---|
| 48 |
public function Demo3() |
|---|
| 49 |
{ |
|---|
| 50 |
stage.scaleMode = 'noScale'; |
|---|
| 51 |
|
|---|
| 52 |
render = new Render3(); |
|---|
| 53 |
|
|---|
| 54 |
initializeTexture(render); |
|---|
| 55 |
initializeMatrices(render); |
|---|
| 56 |
initializeLight(render); |
|---|
| 57 |
initializeModel(); |
|---|
| 58 |
|
|---|
| 59 |
render.setRenderTarget(graphics); |
|---|
| 60 |
render.setViewport(new Viewport(0, 0, 320, 240)); |
|---|
| 61 |
|
|---|
| 62 |
initializeFpsField(); |
|---|
| 63 |
|
|---|
| 64 |
rx = ry = rz = 0; |
|---|
| 65 |
|
|---|
| 66 |
textureSource = new ProtectedLoader(441); |
|---|
| 67 |
textureSource.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); |
|---|
| 68 |
textureSource.load(new URLRequest('texture.swf')); |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
private function completeHandler(event:Event):void |
|---|
| 72 |
{ |
|---|
| 73 |
textureSource.removeEventListener(Event.COMPLETE, completeHandler); |
|---|
| 74 |
textureSource.content.scaleX = 128 / textureSource.contentLoaderInfo.width; |
|---|
| 75 |
textureSource.content.scaleY = 128 / textureSource.contentLoaderInfo.height; |
|---|
| 76 |
|
|---|
| 77 |
addEventListener(Event.ENTER_FRAME, enterFrameHandler); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
private function enterFrameHandler(event:Event):void |
|---|
| 81 |
{ |
|---|
| 82 |
var m:Matrix4 = worldMatrix; |
|---|
| 83 |
var r:Render3 = render; |
|---|
| 84 |
|
|---|
| 85 |
m.identity(); |
|---|
| 86 |
m.rotateX(rx += 0.01); |
|---|
| 87 |
m.rotateY(ry += 0.02); |
|---|
| 88 |
m.rotateZ(rz += 0.05); |
|---|
| 89 |
|
|---|
| 90 |
texture.draw(textureSource); |
|---|
| 91 |
|
|---|
| 92 |
r.updateMatrix(); |
|---|
| 93 |
r.clear(); |
|---|
| 94 |
r.beginScene(); |
|---|
| 95 |
r.drawIndexedPrimitive(indices, vertices, len); |
|---|
| 96 |
var poly:uint = r.endScene(); |
|---|
| 97 |
r.present(); |
|---|
| 98 |
|
|---|
| 99 |
fpsField.text = Math.floor(1000 / (getTimer() - fps)) + 'fps; ' + poly + 'polygons'; |
|---|
| 100 |
fps = getTimer(); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
private function initializeFpsField():void |
|---|
| 104 |
{ |
|---|
| 105 |
fpsField = new TextField(); |
|---|
| 106 |
fpsField.x = 240; |
|---|
| 107 |
fpsField.y = 0; |
|---|
| 108 |
addChild(fpsField); |
|---|
| 109 |
|
|---|
| 110 |
fps = getTimer(); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
private function initializeTexture(render:Render3):void |
|---|
| 114 |
{ |
|---|
| 115 |
texture = new BitmapData(128, 128, false); |
|---|
| 116 |
render.setTexture(texture); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
private function initializeMatrices(render:Render3):void |
|---|
| 120 |
{ |
|---|
| 121 |
worldMatrix = new Matrix4(); |
|---|
| 122 |
render.setWorldMatrix(worldMatrix); |
|---|
| 123 |
|
|---|
| 124 |
var view:Matrix4 = new Matrix4(); |
|---|
| 125 |
var vCam:Vector3D = new Vector3D(0, 0, 222); |
|---|
| 126 |
var vCamEye:Vector3D = new Vector3D(0, 0, 0); |
|---|
| 127 |
var vCamTop:Vector3D = new Vector3D(0, 1, 0); |
|---|
| 128 |
var camDiv:Number = 0.1; |
|---|
| 129 |
MatrixUtil.lookAt(view, vCam, vCamEye, vCamTop); |
|---|
| 130 |
render.setViewMatrix(view); |
|---|
| 131 |
|
|---|
| 132 |
var projection:Matrix4 = new Matrix4(); |
|---|
| 133 |
MatrixUtil.perspective(projection, 60 * Math.PI / 180, 3 / 4, 0.01, 100); |
|---|
| 134 |
render.setProjectionMatrix(projection); |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
private function initializeLight(render:Render3):void |
|---|
| 138 |
{ |
|---|
| 139 |
var d:Vector3D = new Vector3D(0, -5, -5); |
|---|
| 140 |
d.normalize(); |
|---|
| 141 |
render.setLight(new Light(d, new ColorValue(0.3, 0.3, 0.3), new ColorValue(0.7, 0.7, 0.7))); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
private function initializeModel():void |
|---|
| 145 |
{ |
|---|
| 146 |
vertices = new Array(54); |
|---|
| 147 |
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)); |
|---|
| 148 |
createPlane(vertices, 9, 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)); |
|---|
| 149 |
createPlane(vertices, 18, 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)); |
|---|
| 150 |
createPlane(vertices, 27, 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)); |
|---|
| 151 |
createPlane(vertices, 36, 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)); |
|---|
| 152 |
createPlane(vertices, 45, 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)); |
|---|
| 153 |
indices = new Array(144); |
|---|
| 154 |
createPlaneIndex(indices, 0, 0); |
|---|
| 155 |
createPlaneIndex(indices, 24, 9); |
|---|
| 156 |
createPlaneIndex(indices, 48, 18); |
|---|
| 157 |
createPlaneIndex(indices, 72, 27); |
|---|
| 158 |
createPlaneIndex(indices, 96, 36); |
|---|
| 159 |
createPlaneIndex(indices, 120, 45); |
|---|
| 160 |
len = 48; |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
private function createPlane(v:Array, i:uint, a:Vertex, b:Vertex, c:Vertex, d:Vertex):void |
|---|
| 164 |
{ |
|---|
| 165 |
v[i] = a; |
|---|
| 166 |
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); |
|---|
| 167 |
v[i + 2] = b; |
|---|
| 168 |
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); |
|---|
| 169 |
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); |
|---|
| 170 |
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); |
|---|
| 171 |
v[i + 6] = c; |
|---|
| 172 |
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); |
|---|
| 173 |
v[i + 8] = d; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
private function createPlaneIndex(v:Array, i:uint, j:uint):void |
|---|
| 177 |
{ |
|---|
| 178 |
createPolygonIndex(v, i , j , j + 1, j + 3, j + 4); |
|---|
| 179 |
createPolygonIndex(v, i + 6, j + 1, j + 2, j + 4, j + 5); |
|---|
| 180 |
createPolygonIndex(v, i + 12, j + 3, j + 4, j + 6, j + 7); |
|---|
| 181 |
createPolygonIndex(v, i + 18, j + 4, j + 5, j + 7, j + 8); |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
private function createPolygonIndex(v:Array, i:uint, a:uint, b:uint, c:uint, d:uint):void |
|---|
| 185 |
{ |
|---|
| 186 |
v[i] = a; |
|---|
| 187 |
v[i + 1] = b; |
|---|
| 188 |
v[i + 2] = c; |
|---|
| 189 |
v[i + 3] = c; |
|---|
| 190 |
v[i + 4] = b; |
|---|
| 191 |
v[i + 5] = d; |
|---|
| 192 |
} |
|---|
| 193 |
} |
|---|
| 194 |
} |
|---|