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 | 468 |
1 files changed, 234 insertions, 234 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/camera.js b/js/helper-classes/RDGE/src/core/script/camera.js index caff232b..12490211 100755 --- a/js/helper-classes/RDGE/src/core/script/camera.js +++ b/js/helper-classes/RDGE/src/core/script/camera.js | |||
@@ -6,193 +6,193 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
6 | /* | 6 | /* |
7 | * camera class | 7 | * camera class |
8 | */ | 8 | */ |
9 | camera = function() { | 9 | var RDGE = RDGE || {}; |
10 | this.proj = mat4.identity(); | 10 | |
11 | this.view = mat4.identity(); | 11 | RDGE.camera = function () { |
12 | this.world = mat4.identity(); | 12 | this.proj = RDGE.mat4.identity(); |
13 | this.viewProj = mat4.identity(); | 13 | this.view = RDGE.mat4.identity(); |
14 | this.invViewProj = mat4.identity(); | 14 | this.world = RDGE.mat4.identity(); |
15 | this.frustum = []; | 15 | this.viewProj = RDGE.mat4.identity(); |
16 | this.frustumPts = []; | 16 | this.invViewProj = RDGE.mat4.identity(); |
17 | this.controller = null; | 17 | this.frustum = []; |
18 | 18 | this.frustumPts = []; | |
19 | this.setPerspective = function(fov, aratio, near, far) | 19 | this.controller = null; |
20 | { | 20 | |
21 | this.ortho = null; | 21 | this.setPerspective = function (fov, aratio, near, far) { |
22 | this.persp = {}; | 22 | this.ortho = null; |
23 | this.persp.fov = fov; | 23 | this.persp = {}; |
24 | this.persp.aratio = aratio; | 24 | this.persp.fov = fov; |
25 | this.persp.near = near; | 25 | this.persp.aratio = aratio; |
26 | this.persp.far = far; | 26 | this.persp.near = near; |
27 | this.proj = mat4.perspective(fov, aratio, near, far); | 27 | this.persp.far = far; |
28 | this.recalc(); | 28 | this.proj = RDGE.mat4.perspective(fov, aratio, near, far); |
29 | } | 29 | this.recalc(); |
30 | 30 | }; | |
31 | this.reset = function() { | 31 | |
32 | this.world = mat4.identity(); | 32 | this.reset = function () { |
33 | this.recalc(); | 33 | this.world = RDGE.mat4.identity(); |
34 | } | 34 | this.recalc(); |
35 | 35 | }; | |
36 | this.copy = function(cam) { | 36 | |
37 | mat4.inplace_copy(this.view, cam.view); | 37 | this.copy = function (cam) { |
38 | mat4.inplace_copy(this.world, cam.world); | 38 | RDGE.mat4.inplace_copy(this.view, cam.view); |
39 | mat4.inplace_copy(this.proj, cam.proj); | 39 | RDGE.mat4.inplace_copy(this.world, cam.world); |
40 | mat4.inplace_copy(this.viewProj, cam.viewProj); | 40 | RDGE.mat4.inplace_copy(this.proj, cam.proj); |
41 | mat4.inplace_copy(this.invViewProj, cam.invViewProj); | 41 | RDGE.mat4.inplace_copy(this.viewProj, cam.viewProj); |
42 | this.frustum = cam.frustum.slice(); | 42 | RDGE.mat4.inplace_copy(this.invViewProj, cam.invViewProj); |
43 | this.frustumPts = cam.frustumPts.slice(); | 43 | this.frustum = cam.frustum.slice(); |
44 | } | 44 | this.frustumPts = cam.frustumPts.slice(); |
45 | 45 | }; | |
46 | this.recalc = function() { | 46 | |
47 | // update frustum planes | 47 | this.recalc = function () { |
48 | this.frustum = []; | 48 | // update frustum planes |
49 | var vp = this.viewProj; | 49 | this.frustum = []; |
50 | 50 | var vp = this.viewProj; | |
51 | normalizePlane = function( p ) { | 51 | |
52 | var len = vec3.length( p ); | 52 | normalizePlane = function (p) { |
53 | if( Math.abs( 1.0 - len ) > 0.001 ) { | 53 | var len = RDGE.vec3.length(p); |
54 | p[0] /= len; | 54 | if (Math.abs(1.0 - len) > 0.001) { |
55 | p[1] /= len; | 55 | p[0] /= len; |
56 | p[2] /= len; | 56 | p[1] /= len; |
57 | p[3] /= len; | 57 | p[2] /= len; |
58 | } | 58 | p[3] /= len; |
59 | return p; | 59 | } |
60 | } | 60 | return p; |
61 | 61 | }; | |
62 | /* This is the old way | 62 | |
63 | var t = this.persp.near * Math.tan(0.017453292519943295769236 * this.persp.fov * 0.5); | 63 | /* This is the old way |
64 | var r = t * this.persp.aratio; | 64 | var t = this.persp.near * Math.tan(0.017453292519943295769236 * this.persp.fov * 0.5); |
65 | var u = t; | 65 | var r = t * this.persp.aratio; |
66 | var l = -r; | 66 | var u = t; |
67 | var b = -u; | 67 | var l = -r; |
68 | 68 | var b = -u; | |
69 | tview = mat4.transpose(this.view); | 69 | |
70 | this.frustum.push( normalizePlane( mat4.transformPoint(tview, [this.persp.near, 0.0, l, 0.0] ) ) ); // left | 70 | tview = RDGE.mat4.transpose(this.view); |
71 | this.frustum.push( normalizePlane( mat4.transformPoint(tview, [-this.persp.near, 0.0, -r, 0.0] ) ) ); // right | 71 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [this.persp.near, 0.0, l, 0.0] ) ) ); // left |
72 | this.frustum.push( normalizePlane( mat4.transformPoint(tview, [0.0, this.persp.near, b, 0.0] ) ) ); // bottom | 72 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [-this.persp.near, 0.0, -r, 0.0] ) ) ); // right |
73 | this.frustum.push( normalizePlane( mat4.transformPoint(tview, [0.0, -this.persp.near, -u, 0.0] ) ) ); // top | 73 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, this.persp.near, b, 0.0] ) ) ); // bottom |
74 | this.frustum.push( normalizePlane( mat4.transformPoint(tview, [0.0, 0.0, -1.0, -this.persp.near] ) ) ); // near | 74 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, -this.persp.near, -u, 0.0] ) ) ); // top |
75 | this.frustum.push( normalizePlane( mat4.transformPoint(tview, [0.0, 0.0, 1.0, this.persp.far] ) ) ); // far | 75 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, 0.0, -1.0, -this.persp.near] ) ) ); // near |
76 | */ | 76 | this.frustum.push( normalizePlane( RDGE.mat4.transformPoint(tview, [0.0, 0.0, 1.0, this.persp.far] ) ) ); // far |
77 | var l = normalizePlane( [vp[3] + vp[0], vp[7] + vp[4], vp[11] + vp[8], vp[15]+vp[12]] ); | 77 | */ |
78 | var r = normalizePlane( [vp[3] - vp[0], vp[7] - vp[4], vp[11] - vp[8], vp[15]-vp[12]] ); | 78 | var l = normalizePlane([vp[3] + vp[0], vp[7] + vp[4], vp[11] + vp[8], vp[15] + vp[12]]); |
79 | var t = normalizePlane( [vp[3] - vp[1], vp[7] - vp[5], vp[11] - vp[9], vp[15]-vp[13]] ); | 79 | var r = normalizePlane([vp[3] - vp[0], vp[7] - vp[4], vp[11] - vp[8], vp[15] - vp[12]]); |
80 | var b = normalizePlane( [vp[3] + vp[1], vp[7] + vp[5], vp[11] + vp[9], vp[15]+vp[13]] ); | 80 | var t = normalizePlane([vp[3] - vp[1], vp[7] - vp[5], vp[11] - vp[9], vp[15] - vp[13]]); |
81 | var n = normalizePlane( [vp[3] + vp[2], vp[7] + vp[6], vp[11] + vp[10], vp[15]+vp[14]] ); | 81 | var b = normalizePlane([vp[3] + vp[1], vp[7] + vp[5], vp[11] + vp[9], vp[15] + vp[13]]); |
82 | var f = normalizePlane( [vp[3] - vp[2], vp[7] - vp[6], vp[11] - vp[10], vp[15]-vp[14]] ); | 82 | var n = normalizePlane([vp[3] + vp[2], vp[7] + vp[6], vp[11] + vp[10], vp[15] + vp[14]]); |
83 | 83 | var f = normalizePlane([vp[3] - vp[2], vp[7] - vp[6], vp[11] - vp[10], vp[15] - vp[14]]); | |
84 | this.frustum.push(l); | 84 | |
85 | this.frustum.push(r); | 85 | this.frustum.push(l); |
86 | this.frustum.push(t); | 86 | this.frustum.push(r); |
87 | this.frustum.push(b); | 87 | this.frustum.push(t); |
88 | this.frustum.push(n); | 88 | this.frustum.push(b); |
89 | this.frustum.push(f); | 89 | this.frustum.push(n); |
90 | 90 | this.frustum.push(f); | |
91 | // update frustum points | 91 | |
92 | this.frustumPts = []; | 92 | // update frustum points |
93 | var invvp = this.viewProj; | 93 | this.frustumPts = []; |
94 | this.frustumPts.push( mat4.transformPoint( invvp, [-1,-1,-1] ) ); | 94 | var invvp = this.viewProj; |
95 | this.frustumPts.push( mat4.transformPoint( invvp, [-1,1,-1] ) ); | 95 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, -1, -1])); |
96 | this.frustumPts.push( mat4.transformPoint( invvp, [1,1,-1] ) ); | 96 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, 1, -1])); |
97 | this.frustumPts.push( mat4.transformPoint( invvp, [1,-1,-1] ) ); | 97 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, 1, -1])); |
98 | this.frustumPts.push( mat4.transformPoint( invvp, [-1,-1,1] ) ); | 98 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, -1, -1])); |
99 | this.frustumPts.push( mat4.transformPoint( invvp, [-1,1,1] ) ); | 99 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, -1, 1])); |
100 | this.frustumPts.push( mat4.transformPoint( invvp, [1,1,1] ) ); | 100 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [-1, 1, 1])); |
101 | this.frustumPts.push( mat4.transformPoint( invvp, [1,-1,1] ) ); | 101 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, 1, 1])); |
102 | }; | 102 | this.frustumPts.push(RDGE.mat4.transformPoint(invvp, [1, -1, 1])); |
103 | 103 | }; | |
104 | this.setWorld = function(m) { | 104 | |
105 | this.world = m; | 105 | this.setWorld = function (m) { |
106 | this.view = mat4.inverse(m); | 106 | this.world = m; |
107 | this.viewProj = mat4.mul( this.view, this.proj ); | 107 | this.view = RDGE.mat4.inverse(m); |
108 | this.invViewProj = mat4.inverse(this.viewProj); | 108 | this.viewProj = RDGE.mat4.mul(this.view, this.proj); |
109 | this.recalc(); | 109 | this.invViewProj = RDGE.mat4.inverse(this.viewProj); |
110 | } | 110 | this.recalc(); |
111 | 111 | }; | |
112 | this.setView = function(m) { | 112 | |
113 | this.view = m; | 113 | this.setView = function (m) { |
114 | this.world = mat4.inverse(m); | 114 | this.view = m; |
115 | this.viewProj = mat4.mul( this.view, this.proj ); | 115 | this.world = RDGE.mat4.inverse(m); |
116 | this.invViewProj = mat4.inverse(this.viewProj); | 116 | this.viewProj = RDGE.mat4.mul(this.view, this.proj); |
117 | this.recalc(); | 117 | this.invViewProj = RDGE.mat4.inverse(this.viewProj); |
118 | } | 118 | this.recalc(); |
119 | 119 | }; | |
120 | this.setLookAt = function(eyePos, targetPos, upVec) { | 120 | |
121 | this.setWorld( mat4.lookAt(eyePos, targetPos, upVec) ); | 121 | this.setLookAt = function (eyePos, targetPos, upVec) { |
122 | //this.recalc(); | 122 | this.setWorld(RDGE.mat4.lookAt(eyePos, targetPos, upVec)); |
123 | }; | 123 | //this.recalc(); |
124 | 124 | }; | |
125 | this.setPerspective = function(fov, aratio, near, far) { | 125 | |
126 | this.ortho = null; | 126 | this.setPerspective = function (fov, aratio, near, far) { |
127 | this.persp = {}; | 127 | this.ortho = null; |
128 | this.persp.fov = fov; | 128 | this.persp = {}; |
129 | this.persp.aratio = aratio; | 129 | this.persp.fov = fov; |
130 | this.persp.near = near; | 130 | this.persp.aratio = aratio; |
131 | this.persp.far = far; | 131 | this.persp.near = near; |
132 | this.proj = mat4.perspective(fov, aratio, near, far); | 132 | this.persp.far = far; |
133 | this.recalc(); | 133 | this.proj = RDGE.mat4.perspective(fov, aratio, near, far); |
134 | } | 134 | this.recalc(); |
135 | 135 | }; | |
136 | this.setOrthographic = function( l, r, t, b, n, f ) { | 136 | |
137 | this.persp = null; | 137 | this.setOrthographic = function (l, r, t, b, n, f) { |
138 |