diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/helper-classes/RDGE/src/core/script/utilities.js | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/utilities.js')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/utilities.js | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/utilities.js b/js/helper-classes/RDGE/src/core/script/utilities.js new file mode 100644 index 00000000..e19c727f --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/utilities.js | |||
@@ -0,0 +1,245 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | function getRandColor() | ||
8 | { | ||
9 | var r = Math.random(); | ||
10 | var g = Math.random(); | ||
11 | var b =Math.random(); | ||
12 | |||
13 | return [r, g, b, 1.0]; | ||
14 | } | ||
15 | |||
16 | |||
17 | function unProject( winx, winy, winz, modelMatrix, projMatrix, viewport) | ||
18 | { | ||
19 | var inVal = [0,0,0,0]; | ||
20 | |||
21 | var finalMatrix = mat4.mul( modelMatrix, projMatrix ); | ||
22 | |||
23 | finalMatrix = mat4.inverse(finalMatrix); | ||
24 | if(!finalMatrix) | ||
25 | return null; | ||
26 | |||
27 | inVal[0]=winx; | ||
28 | inVal[1]=winy; | ||
29 | inVal[2]=winz; | ||
30 | inVal[3]=1.0; | ||
31 | |||
32 | /* Map x and y from window coordinates */ | ||
33 | inVal[0] = (inVal[0] - viewport[0]) / viewport[2]; | ||
34 | inVal[1] = (inVal[1] - viewport[1]) / viewport[3]; | ||
35 | |||
36 | /* Map to range -1 to 1 */ | ||
37 | inVal[0] = inVal[0] * 2 - 1; | ||
38 | inVal[1] = inVal[1] * 2 - 1; | ||
39 | inVal[2] = inVal[2] * 2 - 1; | ||
40 | |||
41 | var v4Out = mat4.transformPoint( finalMatrix, inVal ); | ||
42 | |||
43 | if (v4Out[3] <= 0.0001) | ||
44 | return null; | ||
45 | |||
46 | v4Out[0] /= v4Out[3]; | ||
47 | v4Out[1] /= v4Out[3]; | ||
48 | v4Out[2] /= v4Out[3]; | ||
49 | |||
50 | return [ v4Out[0], v4Out[1], v4Out[2] ]; | ||
51 | } | ||
52 | |||
53 | function AABB2LineSegment(box, startPoint, endPoint ) | ||
54 | { | ||
55 | c = vec3.scale( vec3.add(box.min, box.max), 0.5 ); | ||
56 | e = vec3.sub(box.max, box.min); | ||
57 | d = vec3.sub(endPoint, startPoint); | ||
58 | m = vec3.sub(startPoint, endPoint); | ||
59 | m = vec3.sub(m, box.min), | ||
60 | m = vec3.sub(m, box.max); | ||
61 | |||
62 | var adx = Math.abs(d[0]); | ||
63 | if( Math.abs(m[0]) > e[0] + adx ) return false; | ||
64 | |||
65 | var ady = Math.abs(d[1]); | ||
66 | if( Math.abs(m[1]) > e[1] + ady ) return false; | ||
67 | |||
68 | var adz = Math.abs(d[2]); | ||
69 | if( Math.abs(m[2]) > e[2] + adz ) return false; | ||
70 | |||
71 | adx += 1.192092896e-07; | ||
72 | ady += 1.192092896e-07; | ||
73 | adz += 1.192092896e-07; | ||
74 | |||
75 | if( Math.abs(m[1] * d[2] - m[2] * d[1]) > e[1] * adz + e[2] * ady ) return false; | ||
76 | if( Math.abs(m[2] * d[0] - m[0] * d[2]) > e[0] * adz + e[2] * adx ) return false; | ||
77 | if( Math.abs(m[0] * d[1] - m[1] * d[0]) > e[0] * ady + e[1] * adx ) return false; | ||
78 | |||
79 | return true; | ||
80 | } | ||
81 | |||
82 | function hitTest(mesh, near, far) | ||
83 | { | ||
84 | // holds distance to the nearst BV | ||
85 | var dist = null; | ||
86 | var BV = null; | ||
87 | |||
88 | for(var index = 0; index < mesh.BVL.length; index++) | ||
89 | { | ||
90 | if(AABB2LineSegment(mesh.BVL[index], near, far)) | ||
91 | { | ||
92 | var center = vec3.scale( vec3.add(mesh.BVL[index].min, mesh.BVL[index].max), 0.5 ); | ||
93 | var newDist = vec3.dot( mat4.row( g_cam.world, 2 ), center); | ||
94 | if(newDist < dist || dist == null) | ||
95 | { | ||
96 | dist = newDist; | ||
97 | BV = mesh.BVL[index]; | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | return BV; | ||
102 | } | ||
103 | |||
104 | |||
105 | |||
106 | // | ||
107 | // loadShader | ||
108 | // | ||
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. | ||
111 | // | ||
112 | function loadShader(ctx, shaderType, shaderStr) | ||
113 | { | ||
114 | |||
115 | // pre-pend preprocessor settings | ||
116 | |||
117 | var preProcessor = "#define PC\n" | ||
118 | preProcessor += shaderStr; | ||
119 | shaderStr = preProcessor; | ||
120 | |||
121 | // Create the shader object | ||
122 | var shader = ctx.createShader(shaderType); | ||
123 | if (shader == null) { | ||
124 | |||
125 | ctx.console.log("*** Error: unable to create shader '"+shaderType+"'"); | ||
126 | |||
127 | return null; | ||
128 | } | ||
129 | |||
130 | // Load the shader source | ||
131 | ctx.shaderSource(shader, shaderStr); | ||
132 | |||
133 | // Compile the shader | ||
134 | ctx.compileShader(shader); | ||
135 | |||
136 | // Check the compile status | ||
137 | var compiled = ctx.getShaderParameter(shader, ctx.COMPILE_STATUS); | ||
138 | if (!compiled) { | ||
139 | // Something went wrong during compilation; get the error | ||
140 | var error = ctx.getShaderInfoLog(shader); | ||
141 | |||
142 | ctx.console.log("*** Error compiling shader '"+shaderType+"':"+error); | ||
143 | |||
144 | ctx.deleteShader(shader); | ||
145 | return null; | ||
146 | } | ||
147 | |||
148 | return shader; | ||
149 | } | ||
150 | |||
151 | // creates id for shader | ||
152 | g_shaderCounter = 0; | ||
153 | function createShader(ctx, strVertShaderName, strFragShaderName, attribs) | ||
154 | { | ||
155 | var vShader = '', fShader = ''; | ||
156 | |||
157 | if (strVertShaderName.indexOf('{') != -1) { | ||
158 | vShader = strVertShaderName; | ||
159 | } else | ||
160 | { | ||
161 | var vshaderRequest = new XMLHttpRequest(); | ||
162 | vshaderRequest.open("GET", 'assets/shaders/' + strVertShaderName + '.glsl', false); | ||
163 | vshaderRequest.send(null); | ||
164 | vShader = vshaderRequest.responseText; | ||
165 | } | ||
166 | |||
167 | if (strFragShaderName.indexOf('{') != -1) { | ||
168 | fShader = strFragShaderName; | ||
169 | } else | ||
170 | { | ||
171 | var fshaderRequest = new XMLHttpRequest(); | ||
172 | fshaderRequest.open("GET", 'assets/shaders/' + strFragShaderName + '.glsl', false); | ||
173 | fshaderRequest.send(null); | ||
174 | fShader = fshaderRequest.responseText; | ||
175 | } | ||
176 | |||
177 | ctx.useProgram(null); | ||
178 | // create our shaders | ||
179 | var vertexShader = loadShader(ctx, ctx.VERTEX_SHADER, vShader); | ||
180 | var fragmentShader = loadShader(ctx, ctx.FRAGMENT_SHADER, fShader); | ||
181 | |||
182 | if (!vertexShader || !fragmentShader) | ||
183 | return null; | ||
184 | |||
185 | // Create the program object | ||
186 | var program = ctx.createProgram(); | ||
187 | |||
188 | if (! program) | ||
189 | return null; | ||
190 | |||
191 | // Attach our two shaders to the program | ||
192 | ctx.attachShader (program, vertexShader); | ||
193 | ctx.attachShader (program, fragmentShader); | ||
194 | |||
195 | // Bind attributes | ||
196 | for (var i in attribs) | ||
197 | ctx.bindAttribLocation (program, i, attribs[i]); | ||
198 | |||
199 | // Link the program | ||
200 | ctx.linkProgram(program); | ||
201 | |||
202 | // Check the link status | ||
203 | var linked = ctx.getProgramParameter( program, ctx.LINK_STATUS); | ||
204 | if (!linked) { | ||
205 | // something went wrong with the link | ||
206 | var error = ctx.getProgramInfoLog (program); | ||
207 | |||
208 | ctx.console.log("Error in program linking:"+error); | ||
209 | |||
210 | |||
211 | ctx.deleteProgram(program); | ||
212 | ctx.deleteProgram(fragmentShader); | ||
213 | ctx.deleteProgram(vertexShader); | ||
214 | |||
215 | return null; | ||
216 | } | ||
217 | |||
218 | program.shaderID = "Shader" + g_shaderCounter++; | ||