| 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 flash.display.Sprite; |
|---|
| 24 |
import flash.text.TextField; |
|---|
| 25 |
import flash.events.Event; |
|---|
| 26 |
import flash.utils.getTimer; |
|---|
| 27 |
|
|---|
| 28 |
public class Demo extends Sprite |
|---|
| 29 |
{ |
|---|
| 30 |
private var render:Render; |
|---|
| 31 |
private var worldMatrix:Matrix4; |
|---|
| 32 |
private var vertices:Array; |
|---|
| 33 |
private var indices:Array; |
|---|
| 34 |
private var len:uint; |
|---|
| 35 |
private var fps:uint; |
|---|
| 36 |
private var fpsField:TextField; |
|---|
| 37 |
private var rx:Number; |
|---|
| 38 |
private var ry:Number; |
|---|
| 39 |
private var rz:Number; |
|---|
| 40 |
|
|---|
| 41 |
public function Demo() |
|---|
| 42 |
{ |
|---|
| 43 |
stage.scaleMode = 'noScale'; |
|---|
| 44 |
|
|---|
| 45 |
render = new Render(); |
|---|
| 46 |
render.setRenderTarget(graphics); |
|---|
| 47 |
|
|---|
| 48 |
worldMatrix = new Matrix4(); |
|---|
| 49 |
render.setWorldMatrix(worldMatrix); |
|---|
| 50 |
|
|---|
| 51 |
var view:Matrix4 = new Matrix4(); |
|---|
| 52 |
var vCam:Vector3D = new Vector3D(0, 0, 3); |
|---|
| 53 |
var vCamEye:Vector3D = new Vector3D(0, 0, 0); |
|---|
| 54 |
var vCamTop:Vector3D = new Vector3D(0, 1, 0); |
|---|
| 55 |
var camDiv:Number = 0.1; |
|---|
| 56 |
MatrixUtil.lookAt(view, vCam, vCamEye, vCamTop); |
|---|
| 57 |
render.setViewMatrix(view); |
|---|
| 58 |
|
|---|
| 59 |
var projection:Matrix4 = new Matrix4(); |
|---|
| 60 |
MatrixUtil.perspective(projection, 60 * Math.PI / 180, 3 / 4, 0.01, 100); |
|---|
| 61 |
render.setProjectionMatrix(projection); |
|---|
| 62 |
|
|---|
| 63 |
var d:Vector3D = new Vector3D(0, -5, -5); |
|---|
| 64 |
d.normalize(); |
|---|
| 65 |
render.setLight(new Light(d, new ColorValue(0.3, 0.3, 0.3), new ColorValue(0.7, 0.7, 0.7))); |
|---|
| 66 |
render.setViewport(new Viewport(0, 0, 320, 240)); |
|---|
| 67 |
|
|---|
| 68 |
var shape:Object = SimpleShape.createDonuts(23); |
|---|
| 69 |
vertices = shape.vertices; |
|---|
| 70 |
indices = shape.indices; |
|---|
| 71 |
len = indices.length / 3; |
|---|
| 72 |
|
|---|
| 73 |
fpsField = new TextField(); |
|---|
| 74 |
fpsField.x = 240; |
|---|
| 75 |
fpsField.y = 0; |
|---|
| 76 |
addChild(fpsField); |
|---|
| 77 |
|
|---|
| 78 |
fps = getTimer(); |
|---|
| 79 |
|
|---|
| 80 |
rx = ry = rz = 0; |
|---|
| 81 |
|
|---|
| 82 |
addEventListener(Event.ENTER_FRAME, enterFrameHandler); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
private function enterFrameHandler(event:Event):void |
|---|
| 86 |
{ |
|---|
| 87 |
var m:Matrix4 = worldMatrix; |
|---|
| 88 |
var r:Render = render; |
|---|
| 89 |
|
|---|
| 90 |
m.identity(); |
|---|
| 91 |
m.rotateX(rx += 0.01); |
|---|
| 92 |
m.rotateY(ry += 0.02); |
|---|
| 93 |
m.rotateZ(rz += 0.05); |
|---|
| 94 |
|
|---|
| 95 |
r.updateMatrix(); |
|---|
| 96 |
r.clear(); |
|---|
| 97 |
r.beginScene(); |
|---|
| 98 |
r.drawIndexedPrimitive(indices, vertices, len); |
|---|
| 99 |
var poly:uint = r.endScene(); |
|---|
| 100 |
r.present(); |
|---|
| 101 |
|
|---|
| 102 |
fpsField.text = Math.floor(1000 / (getTimer() - fps)) + 'fps; ' + poly + 'polygons'; |
|---|
| 103 |
fps = getTimer(); |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|