diff options
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/utilities.js')
-rwxr-xr-x | js/helper-classes/RDGE/src/core/script/utilities.js | 282 |
1 files changed, 139 insertions, 143 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/utilities.js b/js/helper-classes/RDGE/src/core/script/utilities.js index e19c727f..dcc61d45 100755 --- a/js/helper-classes/RDGE/src/core/script/utilities.js +++ b/js/helper-classes/RDGE/src/core/script/utilities.js | |||
@@ -4,125 +4,126 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | 4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. |
5 | </copyright> */ | 5 | </copyright> */ |
6 | 6 | ||
7 | function getRandColor() | 7 | // RDGE namespaces |
8 | { | 8 | var RDGE = RDGE || {}; |
9 | var r = Math.random(); | 9 | |
10 | var g = Math.random(); | 10 | |
11 | var b =Math.random(); | 11 | /* |
12 | 12 | // Methods not currently used in Ninja | |
13 | return [r, g, b, 1.0]; | 13 | RDGE.getRandColor = function () { |
14 | } | 14 | var r = Math.random(); |
15 | 15 | var g = Math.random(); | |
16 | 16 | var b = Math.random(); | |
17 | function unProject( winx, winy, winz, modelMatrix, projMatrix, viewport) | 17 | |
18 | { | 18 | return [r, g, b, 1.0]; |
19 | var inVal = [0,0,0,0]; | 19 | }; |
20 | 20 | ||
21 | var finalMatrix = mat4.mul( modelMatrix, projMatrix ); | 21 | RDGE.unProject = function (winx, winy, winz, modelMatrix, projMatrix, viewport) { |
22 | 22 | var inVal = [0, 0, 0, 0]; | |
23 | finalMatrix = mat4.inverse(finalMatrix); | 23 | |
24 | if(!finalMatrix) | 24 | var finalMatrix = RDGE.mat4.mul(modelMatrix, projMatrix); |
25 | return null; | 25 | |
26 | 26 | finalMatrix = RDGE.mat4.inverse(finalMatrix); | |
27 | inVal[0]=winx; | 27 | if (!finalMatrix) |
28 | inVal[1]=winy; | 28 | return null; |
29 | inVal[2]=winz; | 29 | |
30 | inVal[3]=1.0; | 30 | inVal[0] = winx; |
31 | 31 | inVal[1] = winy; | |
32 | /* Map x and y from window coordinates */ | 32 | inVal[2] = winz; |
33 | inVal[3] = 1.0; | ||
34 | |||
35 | // Map x and y from window coordinates | ||
33 | inVal[0] = (inVal[0] - viewport[0]) / viewport[2]; | 36 | inVal[0] = (inVal[0] - viewport[0]) / viewport[2]; |
34 | inVal[1] = (inVal[1] - viewport[1]) / viewport[3]; | 37 | inVal[1] = (inVal[1] - viewport[1]) / viewport[3]; |
35 | 38 | ||
36 | /* Map to range -1 to 1 */ | 39 | // Map to range -1 to 1 |
37 | inVal[0] = inVal[0] * 2 - 1; | 40 | inVal[0] = inVal[0] * 2 - 1; |
38 | inVal[1] = inVal[1] * 2 - 1; | 41 | inVal[1] = inVal[1] * 2 - 1; |
39 | inVal[2] = inVal[2] * 2 - 1; | 42 | inVal[2] = inVal[2] * 2 - 1; |
40 | 43 | ||
41 | var v4Out = mat4.transformPoint( finalMatrix, inVal ); | 44 | var v4Out = RDGE.mat4.transformPoint(finalMatrix, inVal); |
42 | 45 | ||
43 | if (v4Out[3] <= 0.0001) | 46 | if (v4Out[3] <= 0.0001) |
44 | return null; | 47 | return null; |
45 | 48 | ||
46 | v4Out[0] /= v4Out[3]; | 49 | v4Out[0] /= v4Out[3]; |
47 | v4Out[1] /= v4Out[3]; | 50 | v4Out[1] /= v4Out[3]; |
48 | v4Out[2] /= v4Out[3]; | 51 | v4Out[2] /= v4Out[3]; |
49 | 52 | ||
50 | return [ v4Out[0], v4Out[1], v4Out[2] ]; | 53 | return [v4Out[0], v4Out[1], v4Out[2]]; |
51 | } | 54 | }; |
52 | 55 | ||
53 | function AABB2LineSegment(box, startPoint, endPoint ) | 56 | RDGE.AABB2LineSegment = function (box, startPoint, endPoint) { |
54 | { | 57 | c = RDGE.vec3.scale(RDGE.vec3.add(box.min, box.max), 0.5); |
55 | c = vec3.scale( vec3.add(box.min, box.max), 0.5 ); | 58 | e = RDGE.vec3.sub(box.max, box.min); |
56 | e = vec3.sub(box.max, box.min); | 59 | d = RDGE.vec3.sub(endPoint, startPoint); |
57 | d = vec3.sub(endPoint, startPoint); | 60 | m = RDGE.vec3.sub(startPoint, endPoint); |
58 | m = vec3.sub(startPoint, endPoint); | 61 | m = RDGE.vec3.sub(m, box.min), |
59 | m = vec3.sub(m, box.min), | 62 | m = RDGE.vec3.sub(m, box.max); |
60 | m = vec3.sub(m, box.max); | 63 | |
61 | 64 | var adx = Math.abs(d[0]); | |
62 | var adx = Math.abs(d[0]); | 65 | if (Math.abs(m[0]) > e[0] + adx) return false; |
63 | if( Math.abs(m[0]) > e[0] + adx ) return false; | 66 | |
64 | 67 | var ady = Math.abs(d[1]); | |
65 | var ady = Math.abs(d[1]); | 68 | if (Math.abs(m[1]) > e[1] + ady) return false; |
66 | if( Math.abs(m[1]) > e[1] + ady ) return false; | 69 | |
67 | 70 | var adz = Math.abs(d[2]); | |
68 | var adz = Math.abs(d[2]); | 71 | if (Math.abs(m[2]) > e[2] + adz) return false; |
69 | if( Math.abs(m[2]) > e[2] + adz ) return false; | 72 | |
70 | 73 | adx += 1.192092896e-07; | |
71 | adx += 1.192092896e-07; | 74 | ady += 1.192092896e-07; |
72 | ady += 1.192092896e-07; | 75 | adz += 1.192092896e-07; |
73 | adz += 1.192092896e-07; | 76 | |
74 | 77 | if (Math.abs(m[1] * d[2] - m[2] * d[1]) > e[1] * adz + e[2] * ady) return false; | |
75 | if( Math.abs(m[1] * d[2] - m[2] * d[1]) > e[1] * adz + e[2] * ady ) return false; | 78 | if (Math.abs(m[2] * d[0] - m[0] * d[2]) > e[0] * adz + e[2] * adx) return false; |
76 | if( Math.abs(m[2] * d[0] - m[0] * d[2]) > e[0] * adz + e[2] * adx ) return false; | 79 | if (Math.abs(m[0] * d[1] - m[1] * d[0]) > e[0] * ady + e[1] * adx) return false; |
77 | if( Math.abs(m[0] * d[1] - m[1] * d[0]) > e[0] * ady + e[1] * adx ) return false; | 80 | |
78 | 81 | return true; | |
79 | return true; | 82 | }; |
80 | } | 83 | |
81 | 84 | RDGE.hitTest = function (mesh, near, far) { | |
82 | function hitTest(mesh, near, far) | 85 | // holds distance to the nearest BV |
83 | { | 86 | var dist = null; |
84 | // holds distance to the nearst BV | 87 | var BV = null; |
85 | var dist = null; | 88 | |
86 | var BV = null; | 89 | for (var index = 0; index < mesh.BVL.length; index++) { |
87 | 90 | if (AABB2LineSegment(mesh.BVL[index], near, far)) { | |
88 | for(var index = 0; index < mesh.BVL.length; index++) | 91 | var center = RDGE.vec3.scale(RDGE.vec3.add(mesh.BVL[index].min, mesh.BVL[index].max), 0.5); |
89 | { | 92 | var newDist = RDGE.vec3.dot(RDGE.mat4.row(RDGE.globals.cam.world, 2), center); |
90 | if(AABB2LineSegment(mesh.BVL[index], near, far)) | 93 | if (newDist < dist || dist == null) { |
91 | { | 94 | dist = newDist; |
92 | var center = vec3.scale( vec3.add(mesh.BVL[index].min, mesh.BVL[index].max), 0.5 ); | 95 | BV = mesh.BVL[index]; |
93 | var newDist = vec3.dot( mat4.row( g_cam.world, 2 ), center); | 96 | } |
94 | if(newDist < dist || dist == null) | 97 | } |
95 | { | ||
96 | dist = newDist; | ||
97 | BV = mesh.BVL[index]; | ||
98 | } | ||
99 | } | 98 | } |
100 | } | 99 | return BV; |
101 | return BV; | 100 | }; |
102 | } | 101 | |
102 | |||
103 | |||
104 | */ | ||
105 | |||
106 | |||
107 | |||
108 | |||
109 | |||
103 | 110 | ||
104 | 111 | ||
105 | 112 | ||
106 | // | ||
107 | // loadShader | 113 | // loadShader |
108 | // | 114 | // 'shaderId' is the id of a <script> element containing the shader source string. |
109 | // 'shaderId' is the id of a <script> element containing the shader source string. | ||
110 | // Load this shader and return the WebGLShader object corresponding to it. | 115 | // Load this shader and return the WebGLShader object corresponding to it. |
111 | // | 116 | RDGE.loadShader = function (ctx, shaderType, shaderStr) { |
112 | function loadShader(ctx, shaderType, shaderStr) | 117 | // pre-pend preprocessor settings |
113 | { | 118 | var preProcessor = "#define PC\n" |
114 | 119 | preProcessor += shaderStr; | |
115 | // pre-pend preprocessor settings | 120 | shaderStr = preProcessor; |
116 | |||
117 | var preProcessor = "#define PC\n" | ||
118 | preProcessor += shaderStr; | ||
119 | shaderStr = preProcessor; | ||
120 | 121 | ||
121 | // Create the shader object | 122 | // Create the shader object |
122 | var shader = ctx.createShader(shaderType); | 123 | var shader = ctx.createShader(shaderType); |
123 | if (shader == null) { | 124 | if (shader == null) { |
124 | 125 | ||
125 | ctx.console.log("*** Error: unable to create shader '"+shaderType+"'"); | 126 | ctx.console.log("*** Error: unable to create shader '" + shaderType + "'"); |
126 | 127 | ||
127 | return null; | 128 | return null; |
128 | } | 129 | } |
@@ -139,43 +140,40 @@ function loadShader(ctx, shaderType, shaderStr) | |||
139 | // Something went wrong during compilation; get the error | 140 | // Something went wrong during compilation; get the error |
140 | var error = ctx.getShaderInfoLog(shader); | 141 | var error = ctx.getShaderInfoLog(shader); |
141 | 142 | ||
142 | ctx.console.log("*** Error compiling shader '"+shaderType+"':"+error); | 143 | ctx.console.log("*** Error compiling shader '" + shaderType + "':" + error); |
143 | 144 | ||
144 | ctx.deleteShader(shader); | 145 | ctx.deleteShader(shader); |
145 | return null; | 146 | return null; |
146 | } | 147 | } |
147 | 148 | ||
148 | return shader; | 149 | return shader; |
149 | } | 150 | }; |
150 | 151 | ||