diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/camera.js')
-rwxr-xr-x | js/helper-classes/RDGE/src/core/script/camera.js | 551 |
1 files changed, 276 insertions, 275 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/camera.js b/js/helper-classes/RDGE/src/core/script/camera.js index da9aff3d..72d24d08 100755 --- a/js/helper-classes/RDGE/src/core/script/camera.js +++ b/js/helper-classes/RDGE/src/core/script/camera.js | |||
@@ -1,275 +1,276 @@ | |||
1 | /* <copyright> | 1 | /* <copyright> |
2 | Copyright (c) 2012, Motorola Mobility, Inc | 2 | Copyright (c) 2012, Motorola Mobility LLC. |
3 | All Rights Reserved. | 3 | All Rights Reserved. |
4 | BSD License. | 4 | |
5 | 5 | Redistribution and use in source and binary forms, with or without | |
6 | Redistribution and use in source and binary forms, with or without | 6 | modification, are permitted provided that the following conditions are met: |
7 | modification, are permitted provided that the following conditions are met: | 7 | |
8 | 8 | * Redistributions of source code must retain the above copyright notice, | |
9 | - Redistributions of source code must retain the above copyright notice, | 9 | this list of conditions and the following disclaimer. |
10 | this list of conditions and the following disclaimer. | 10 | |
11 | - Redistributions in binary form must reproduce the above copyright | 11 | * Redistributions in binary form must reproduce the above copyright notice, |
12 | notice, this list of conditions and the following disclaimer in the | 12 | this list of conditions and the following disclaimer in the documentation |
13 | documentation and/or other materials provided with the distribution. | 13 | and/or other materials provided with the distribution. |
14 | - Neither the name of Motorola Mobility nor the names of its contributors | 14 | |
15 | may be used to endorse or promote products derived from this software | 15 | * Neither the name of Motorola Mobility LLC nor the names of its |
16 | without specific prior written permission. | 16 | contributors may be used to endorse or promote products derived from this |
17 | 17 | software without specific prior written permission. | |
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 18 | |
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
23 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 23 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 26 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | POSSIBILITY OF SUCH DAMAGE. | 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | </copyright> */ | 29 | POSSIBILITY OF SUCH DAMAGE. |
30 | /* | 30 | </copyright> */ |
31 | * camera class | 31 | /* |
32 | */ | 32 | * camera class |
33 | var RDGE = RDGE || {}; | 33 | */ |
34 | 34 | var RDGE = RDGE || {}; | |
35 | RDGE.camera = function () { | 35 | |
36 | this.proj = RDGE.mat4.identity(); | 36 | RDGE.camera = function () { |
37 | this.view = RDGE.mat4.identity(); | 37 | this.proj = RDGE.mat4.identity(); |
38 | this.world = RDGE.mat4.identity(); | 38 | this.view = RDGE.mat4.identity(); |
39 | this.viewProj = RDGE.mat4.identity(); | 39 | this.world = RDGE.mat4.identity(); |
40 | this.invViewProj = RDGE.mat4.identity(); | 40 | this.viewProj = RDGE.mat4.identity(); |
41 | this.frustum = []; | 41 | this.invViewProj = RDGE.mat4.identity(); |
42 | this.frustumPts = []; | 42 | this.frustum = []; |
43 | this.controller = null; | 43 | this.frustumPts = []; |
44 | 44 | this.controller = null; | |
45 | this.setPerspective = function (fov, aratio, near, far) { | 45 | |
46 | this.ortho = null; | 46 | this.setPerspective = function (fov, aratio, near, far) { |
47 | this.persp = {}; | 47 | this.ortho = null; |
48 | this.persp.fov = fov; | 48 | this.persp = {}; |
49 | this.persp.aratio = aratio; | 49 | this.persp.fov = fov; |
50 | this.persp.near = near; | 50 | this.persp.aratio = aratio; |
51 | this.persp.far = far; | 51 | this.persp.near = near; |
52 | this.proj = RDGE.mat4.perspective(fov, aratio, near, far); | 52 | this.persp.far = far; |
53 | this.recalc(); | 53 | this.proj = RDGE.mat4.perspective(fov, aratio, near, far); |
54 | }; | 54 | this.recalc(); |
55 | 55 | }; | |
56 | this.reset = function () { | 56 | |
57 | this.world = RDGE.mat4.identity(); | 57 | this.reset = function () { |
58 | this.recalc(); | 58 | this.world = RDGE.mat4.identity(); |
59 | }; | 59 | this.recalc(); |
60 | 60 | }; | |
61 | this.copy = function (cam) { | 61 | |
62 | RDGE.mat4.inplace_copy(this.view, cam.view); | 62 | this.copy = function (cam) { |
63 | RDGE.mat4.inplace_copy(this.world, cam.world); | 63 | RDGE.mat4.inplace_copy(this.view, cam.view); |
64 | RDGE.mat4.inplace_copy(this.proj, cam.proj); | 64 | RDGE.mat4.inplace_copy(this.world, cam.world); |
65 | RDGE.mat4.inplace_copy(this.viewProj, cam.viewProj); | 65 | RDGE.mat4.inplace_copy(this.proj, cam.proj); |
66 | RDGE.mat4.inplace_copy(this.invViewProj, cam.invViewProj); | 66 | RDGE.mat4.inplace_copy(this.viewProj, cam.viewProj); |
67 | this.frustum = cam.frustum.slice(); | 67 | RDGE.mat4.inplace_copy(this.invViewProj, cam.invViewProj); |
68 | this.frustumPts = cam.frustumPts.slice(); | 68 | this.frustum = cam.frustum.slice(); |
69 | }; | 69 | this.frustumPts = cam.frustumPts.slice(); |
70 | 70 | }; | |
71 | this.recalc = function () { | 71 | |
72 | // update frustum planes | 72 | this.recalc = function () { |
73 | this.frustum = []; | 73 | // update frustum planes |
74 | var vp = this.viewProj; | 74 | this.frustum = []; |
75 | 75 | var vp = this.viewProj; | |
76 | normalizePlane = function (p) { | 76 | |
77 | var len = RDGE.vec3.length(p); | 77 | normalizePlane = function (p) { |
78 | if (Math.abs(1.0 - len) > 0.001) { | 78 | var len = RDGE.vec3.length(p); |
79 | p[0] /= len; | 79 | if (Math.abs(1.0 - len) > 0.001) { |
80 | p[1] /= len; | 80 | p[0] /= len; |
81 | p[2] /= len; | 81 | p[1] /= len; |
82 | p[3] /= len; | 82 | p[2] /= len; |
83 | } | 83 | p[3] /= len; |
84 | return p; | 84 | } |
85 | }; | 85 | return p; |
86 | 86 | }; | |
87 | /* This is the old way | 87 | |
88 | var t = this.persp.near * Math.tan(0.017453292519943295769236 * this.persp.fov * 0.5); | 88 | /* This is the old way |
89 | var r = t * this.persp.aratio; | 89 | var t = this.persp.near * Math.tan(0.017453292519943295769236 * this.persp.fov * 0.5); |
90 | var u = t; | 90 | var r = t * this.persp.aratio; |
91 | var l = -r; | 91 | var u = t; |
92 | var b = -u; | 92 | var l = -r; |
93 | 93 | var b = -u; | |
94 | tview = RDGE.mat4.transpose(this.view); | 94 | |
95 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [this.persp.near, 0.0, l, 0.0] ) ) ); // left | 95 | tview = RDGE.mat4.transpose(this.view); |
96 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [-this.persp.near, 0.0, -r, 0.0] ) ) ); // right | 96 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [this.persp.near, 0.0, l, 0.0] ) ) ); // left |
97 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, this.persp.near, b, 0.0] ) ) ); // bottom | 97 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [-this.persp.near, 0.0, -r, 0.0] ) ) ); // right |
98 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, -this.persp.near, -u, 0.0] ) ) ); // top | 98 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, this.persp.near, b, 0.0] ) ) ); // bottom |
99 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, 0.0, -1.0, -this.persp.near] ) ) ); // near | 99 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, -this.persp.near, -u, 0.0] ) ) ); // top |
100 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, 0.0, 1.0, this.persp.far] ) ) ); // far | 100 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, 0.0, -1.0, -this.persp.near] ) ) ); // near |
101 | */ | 101 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, 0.0, 1.0, this.persp.far] ) ) ); // far |
102 | var l = normalizePlane([vp[3] + vp[0], vp[7] + vp[4], vp[11] + vp[8], vp[15] + vp[12]]); | 102 | */ |
103 | var r = normalizePlane([vp[3] - vp[0], vp[7] - vp[4], vp[11] - vp[8], vp[15] - vp[12]]); | 103 | var l = normalizePlane([vp[3] + vp[0], vp[7] + vp[4], vp[11] + vp[8], vp[15] + vp[12]]); |
104 | var t = normalizePlane([vp[3] - vp[1], vp[7] - vp[5], vp[11] - vp[9], vp[15] - vp[13]]); | 104 | var r = normalizePlane([vp[3] - vp[0], vp[7] - vp[4], vp[11] - vp[8], vp[15] - vp[12]]); |
105 | var b = normalizePlane([vp[3] + vp[1], vp[7] + vp[5], vp[11] + vp[9], vp[15] + vp[13]]); | 105 | var t = normalizePlane([vp[3] - vp[1], vp[7] - vp[5], vp[11] - vp[9], vp[15] - vp[13]]); |
106 | var n = normalizePlane([vp[3] + vp[2], vp[7] + vp[6], vp[11] + vp[10], vp[15] + vp[14]]); | 106 | var b = normalizePlane([vp[3] + vp[1], vp[7] + vp[5], vp[11] + vp[9], vp[15] + vp[13]]); |
107 | var f = normalizePlane([vp[3] - vp[2], vp[7] - vp[6], vp[11] - vp[10], vp[15] - vp[14]]); | 107 | var n = normalizePlane([vp[3] + vp[2], vp[7] + vp[6], vp[11] + vp[10], vp[15] + vp[14]]); |
108 | 108 | var f = normalizePlane([vp[3] - vp[2], vp[7] - vp[6], vp[11] - vp[10], vp[15] - vp[14]]); | |
109 | this.frustum.push(l); | 109 | |
110 | this.frustum.push(r); | 110 | this.frustum.push(l); |
111 | this.frustum.push(t); | 111 | this.frustum.push(r); |
112 | this.frustum.push(b); | 112 | this.frustum.push(t); |
113 | this.frustum.push(n); | 113 | this.frustum.push(b); |
114 | this.frustum.push(f); | 114 | this.frustum.push(n); |
115 | 115 | this.frustum.push(f); | |
116 | // update frustum points | 116 | |
117 | this.frustumPts = []; | 117 | // update frustum points |
118 | var invvp = this.viewProj; | 118 | this.frustumPts = []; |
119 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, -1, -1])); | 119 | var invvp = this.viewProj; |
120 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, 1, -1])); | 120 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, -1, -1])); |
121 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, 1, -1])); | 121 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, 1, -1])); |
122 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, -1, -1])); | 122 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, 1, -1])); |
123 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, -1, 1])); | 123 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, -1, -1])); |
124 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, 1, 1])); | 124 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, -1, 1])); |
125 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, 1, 1])); | 125 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, 1, 1])); |
126 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, -1, 1])); | 126 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, 1, 1])); |
127 | }; | 127 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, -1, 1])); |
128 | 128 | }; | |
129 | this.setWorld = function (m) { | 129 | |
130 | this.world = m; | 130 | this.setWorld = function (m) { |
131 | this.view = RDGE.mat4.inverse(m); | 131 | this.world = m; |
132 | this.viewProj = RDGE.mat4.mul(this.view, this.proj); | 132 | this.view = RDGE.mat4.inverse(m); |
133 |