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 | 110 |
1 files changed, 53 insertions, 57 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/utilities.js b/js/helper-classes/RDGE/src/core/script/utilities.js index e55f2b33..d3e49398 100755 --- a/js/helper-classes/RDGE/src/core/script/utilities.js +++ b/js/helper-classes/RDGE/src/core/script/utilities.js | |||
@@ -4,23 +4,26 @@ 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 | |||
10 | |||
11 | /* | ||
12 | // Methods not currently used in Ninja | ||
13 | RDGE.getRandColor = function () { | ||
9 | var r = Math.random(); | 14 | var r = Math.random(); |
10 | var g = Math.random(); | 15 | var g = Math.random(); |
11 | var b =Math.random(); | 16 | var b =Math.random(); |
12 | 17 | ||
13 | return [r, g, b, 1.0]; | 18 | return [r, g, b, 1.0]; |
14 | } | 19 | }; |
15 | 20 | ||
16 | 21 | RDGE.unProject = function (winx, winy, winz, modelMatrix, projMatrix, viewport) { | |
17 | function unProject( winx, winy, winz, modelMatrix, projMatrix, viewport) | ||
18 | { | ||
19 | var inVal = [0,0,0,0]; | 22 | var inVal = [0,0,0,0]; |
20 | 23 | ||
21 | var finalMatrix = mat4.mul( modelMatrix, projMatrix ); | 24 | var finalMatrix = RDGE.mat4.mul(modelMatrix, projMatrix); |
22 | 25 | ||
23 | finalMatrix = mat4.inverse(finalMatrix); | 26 | finalMatrix = RDGE.mat4.inverse(finalMatrix); |
24 | if(!finalMatrix) | 27 | if(!finalMatrix) |
25 | return null; | 28 | return null; |
26 | 29 | ||
@@ -29,16 +32,16 @@ function unProject( winx, winy, winz, modelMatrix, projMatrix, viewport) | |||
29 | inVal[2]=winz; | 32 | inVal[2]=winz; |
30 | inVal[3]=1.0; | 33 | inVal[3]=1.0; |
31 | 34 | ||
32 | /* Map x and y from window coordinates */ | 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; |
@@ -48,16 +51,15 @@ function unProject( winx, winy, winz, modelMatrix, projMatrix, viewport) | |||
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); | ||
61 | 63 | ||
62 | var adx = Math.abs(d[0]); | 64 | var adx = Math.abs(d[0]); |
63 | if( Math.abs(m[0]) > e[0] + adx ) return false; | 65 | if( Math.abs(m[0]) > e[0] + adx ) return false; |
@@ -77,43 +79,42 @@ function AABB2LineSegment(box, startPoint, endPoint ) | |||
77 | if( Math.abs(m[0] * d[1] - m[1] * d[0]) > e[0] * ady + e[1] * adx ) return false; | 79 | if( Math.abs(m[0] * d[1] - m[1] * d[0]) > e[0] * ady + e[1] * adx ) return false; |
78 | 80 | ||
79 | return true; | 81 | return true; |
80 | } | 82 | }; |
81 | 83 | ||
82 | function hitTest(mesh, near, far) | 84 | RDGE.hitTest = function (mesh, near, far) { |
83 | { | 85 | // holds distance to the nearest BV |
84 | // holds distance to the nearst BV | ||
85 | var dist = null; | 86 | var dist = null; |
86 | var BV = null; | 87 | var BV = null; |
87 | 88 | ||
88 | for(var index = 0; index < mesh.BVL.length; index++) | 89 | for (var index = 0; index < mesh.BVL.length; index++) { |
89 | { | 90 | if (AABB2LineSegment(mesh.BVL[index], near, far)) { |
90 | if(AABB2LineSegment(mesh.BVL[index], near, far)) | 91 | var center = RDGE.vec3.scale(RDGE.vec3.add(mesh.BVL[index].min, mesh.BVL[index].max), 0.5); |
91 | { | 92 | var newDist = RDGE.vec3.dot(RDGE.mat4.row(RDGE.globals.cam.world, 2), center); |
92 | var center = vec3.scale( vec3.add(mesh.BVL[index].min, mesh.BVL[index].max), 0.5 ); | 93 | if (newDist < dist || dist == null) { |
93 | var newDist = vec3.dot( mat4.row( g_cam.world, 2 ), center); | ||
94 | if(newDist < dist || dist == null) | ||
95 | { | ||
96 | dist = newDist; | 94 | dist = newDist; |
97 | BV = mesh.BVL[index]; | 95 | BV = mesh.BVL[index]; |
98 | } | 96 | } |
99 | } | 97 | } |
100 | } | 98 | } |
101 | return BV; | 99 | return BV; |
102 | } | 100 | }; |
101 | |||
102 | |||
103 | |||
104 | */ | ||
105 | |||
106 | |||
107 | |||
108 | |||
109 | |||
103 | 110 | ||
104 | 111 | ||
105 | 112 | ||
106 | // | ||
107 | // loadShader | 113 | // loadShader |
108 | // | ||
109 | // 'shaderId' is the id of a <script> element containing the shader source string. | 114 | // '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) | ||
113 | { | ||
114 | |||
115 | // pre-pend preprocessor settings | 117 | // pre-pend preprocessor settings |
116 | |||
117 | var preProcessor = "#define PC\n" | 118 | var preProcessor = "#define PC\n" |
118 | preProcessor += shaderStr; | 119 | preProcessor += shaderStr; |
119 | shaderStr = preProcessor; | 120 | shaderStr = preProcessor; |
@@ -146,30 +147,27 @@ function loadShader(ctx, shaderType, shaderStr) | |||
146 | } | 147 | } |
147 | 148 | ||
148 | return shader; | 149 | return shader; |
149 | } | 150 | }; |
150 | 151 | ||
151 | // creates id for shader | 152 | // creates id for shader |
152 | g_shaderCounter = 0; | 153 | RDGE.g_shaderCounter = 0; |
153 | function createShader(ctx, strVertShaderName, strFragShaderName, attribs) | 154 | RDGE.createShader = function (ctx, strVertShaderName, strFragShaderName, attribs) { |
154 | { | ||
155 | var vShader = '', fShader = ''; | 155 | var vShader = '', fShader = ''; |
156 | 156 | ||
157 | if (strVertShaderName.indexOf('{') != -1) { | 157 | if (strVertShaderName.indexOf('{') != -1) { |
158 | vShader = strVertShaderName; | 158 | vShader = strVertShaderName; |
159 | } else | 159 | } else { |
160 | { | ||
161 | var vshaderRequest = new XMLHttpRequest(); | 160 | var vshaderRequest = new XMLHttpRequest(); |
162 | vshaderRequest.open("GET", g_Engine._assetPath+'shaders/' + strVertShaderName + '.glsl', false); | 161 | vshaderRequest.open("GET", RDGE.globals.engine._assetPath+'shaders/' + strVertShaderName + '.glsl', false); |
163 | vshaderRequest.send(null); | 162 | vshaderRequest.send(null); |
164 | vShader = vshaderRequest.responseText; | 163 | vShader = vshaderRequest.responseText; |
165 | } | 164 | } |
166 | 165 | ||
167 | if (strFragShaderName.indexOf('{') != -1) { | 166 | if (strFragShaderName.indexOf('{') != -1) { |
168 | fShader = strFragShaderName; | 167 | fShader = strFragShaderName; |
169 | } else | 168 | } else { |
170 | { | ||
171 | var fshaderRequest = new XMLHttpRequest(); | 169 | var fshaderRequest = new XMLHttpRequest(); |
172 | fshaderRequest.open("GET", g_Engine._assetPath+'shaders/' + strFragShaderName + '.glsl', false); | 170 | fshaderRequest.open("GET", RDGE.globals.engine._assetPath+'shaders/' + strFragShaderName + '.glsl', false); |
173 | fshaderRequest.send(null); | 171 | fshaderRequest.send(null); |
174 | fShader = fshaderRequest.responseText; | 172 | fShader = fshaderRequest.responseText; |
175 | } | 173 | } |
@@ -215,18 +213,17 @@ function createShader(ctx, strVertShaderName, strFragShaderName, attribs) | |||
215 | return null; | 213 | return null; |
216 | } | 214 | } |
217 | 215 | ||
218 | program.shaderID = "Shader" + g_shaderCounter++; | 216 | program.shaderID = "Shader" + RDGE.g_shaderCounter++; |
219 | program.vname = strVertShaderName; | 217 | program.vname = strVertShaderName; |
220 | program.RDGEUniform = new RDGEUniformInit(); | 218 | program.RDGEUniform = new RDGE.RDGEUniformInit(); |
221 | 219 | ||
222 | return program; | 220 | return program; |
223 | } |