gl-matrix-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +28 -0
- data/Rakefile +18 -0
- data/gl-matrix-rails.gemspec +24 -0
- data/lib/gl-matrix-rails.rb +1 -0
- data/lib/gl-matrix.rb +6 -0
- data/lib/gl-matrix/engine.rb +6 -0
- data/lib/gl-matrix/version.rb +8 -0
- data/spec/assets/request_asset_spec.rb +5 -0
- data/spec/spec_helper.rb +10 -0
- data/vendor/assets/javascripts/gl-matrix.js +3071 -0
- metadata +93 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Colin MacKenzie IV
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# gl-matrix-rails
|
2
|
+
|
3
|
+
A gem to automate using [gl-matrix](http://glmatrix.net/) with Rails 3.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'gl-matrix-rails'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Just add the following to your Rails asset manifest in
|
18
|
+
`app/assets/javascripts/application.js`:
|
19
|
+
|
20
|
+
//= require gl-matrix
|
21
|
+
|
22
|
+
## Contributing
|
23
|
+
|
24
|
+
1. Fork it
|
25
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
26
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
27
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
28
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
desc "update gl-matrix"
|
5
|
+
task :update do
|
6
|
+
cwd = File.dirname(__FILE__)
|
7
|
+
raise "failed to fetch latest" unless system *%w(npm install gl-matrix)
|
8
|
+
mv File.expand_path("node_modules/gl-matrix/dist/gl-matrix.js", cwd),
|
9
|
+
File.expand_path("vendor/assets/javascripts/gl-matrix.js", cwd)
|
10
|
+
rm_rf File.expand_path("node_modules", cwd)
|
11
|
+
puts
|
12
|
+
puts "Done."
|
13
|
+
puts
|
14
|
+
end
|
15
|
+
|
16
|
+
RSpec::Core::RakeTask.new
|
17
|
+
|
18
|
+
task :default => :spec
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gl-matrix/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "gl-matrix-rails"
|
8
|
+
gem.version = GLMatrix::VERSION
|
9
|
+
gem.authors = ["Colin MacKenzie IV"]
|
10
|
+
gem.email = ["sinisterchipmunk@gmail.com"]
|
11
|
+
gem.description = %q{A gem to automate using gl-matrix with Rails 3}
|
12
|
+
gem.summary = %q{A gem to automate using gl-matrix with Rails 3}
|
13
|
+
gem.homepage = "http://glmatrix.net/"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "railties", ">= 3.2.0"
|
21
|
+
|
22
|
+
gem.add_development_dependency 'rspec'
|
23
|
+
gem.add_development_dependency 'tzinfo'
|
24
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'gl-matrix'
|
data/lib/gl-matrix.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,3071 @@
|
|
1
|
+
/**
|
2
|
+
* @fileoverview gl-matrix - High performance matrix and vector operations
|
3
|
+
* @author Brandon Jones
|
4
|
+
* @author Colin MacKenzie IV
|
5
|
+
* @version 2.0.0
|
6
|
+
*/
|
7
|
+
|
8
|
+
/* Copyright (c) 2012, Brandon Jones, Colin MacKenzie IV. All rights reserved.
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without modification,
|
11
|
+
are permitted provided that the following conditions are met:
|
12
|
+
|
13
|
+
* Redistributions of source code must retain the above copyright notice, this
|
14
|
+
list of conditions and the following disclaimer.
|
15
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
16
|
+
this list of conditions and the following disclaimer in the documentation
|
17
|
+
and/or other materials provided with the distribution.
|
18
|
+
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
20
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
21
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
23
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
24
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
25
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
26
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
27
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
28
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
29
|
+
|
30
|
+
|
31
|
+
(function() {
|
32
|
+
"use strict";
|
33
|
+
|
34
|
+
var shim = {};
|
35
|
+
if (typeof(exports) === 'undefined') {
|
36
|
+
if(typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
|
37
|
+
shim.exports = {};
|
38
|
+
define(function() {
|
39
|
+
return shim.exports;
|
40
|
+
});
|
41
|
+
} else {
|
42
|
+
// gl-matrix lives in a browser, define its namespaces in global
|
43
|
+
shim.exports = window;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
else {
|
47
|
+
// gl-matrix lives in commonjs, define its namespaces in exports
|
48
|
+
shim.exports = exports;
|
49
|
+
}
|
50
|
+
|
51
|
+
(function(exports) {
|
52
|
+
/* Copyright (c) 2012, Brandon Jones, Colin MacKenzie IV. All rights reserved.
|
53
|
+
|
54
|
+
Redistribution and use in source and binary forms, with or without modification,
|
55
|
+
are permitted provided that the following conditions are met:
|
56
|
+
|
57
|
+
* Redistributions of source code must retain the above copyright notice, this
|
58
|
+
list of conditions and the following disclaimer.
|
59
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
60
|
+
this list of conditions and the following disclaimer in the documentation
|
61
|
+
and/or other materials provided with the distribution.
|
62
|
+
|
63
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
64
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
65
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
66
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
67
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
68
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
69
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
70
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
71
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
72
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
73
|
+
|
74
|
+
/**
|
75
|
+
* @class 2 Dimensional Vector
|
76
|
+
* @name vec2
|
77
|
+
*/
|
78
|
+
|
79
|
+
var vec2 = {};
|
80
|
+
|
81
|
+
if(!GLMAT_EPSILON) {
|
82
|
+
var GLMAT_EPSILON = 0.000001;
|
83
|
+
}
|
84
|
+
|
85
|
+
/**
|
86
|
+
* Creates a new, empty vec2
|
87
|
+
*
|
88
|
+
* @returns {vec2} a new 2D vector
|
89
|
+
*/
|
90
|
+
vec2.create = function() {
|
91
|
+
return new Float32Array(2);
|
92
|
+
};
|
93
|
+
|
94
|
+
/**
|
95
|
+
* Creates a new vec2 initialized with values from an existing vector
|
96
|
+
*
|
97
|
+
* @param {vec2} a vector to clone
|
98
|
+
* @returns {vec2} a new 2D vector
|
99
|
+
*/
|
100
|
+
vec2.clone = function(a) {
|
101
|
+
var out = new Float32Array(2);
|
102
|
+
out[0] = a[0];
|
103
|
+
out[1] = a[1];
|
104
|
+
return out;
|
105
|
+
};
|
106
|
+
|
107
|
+
/**
|
108
|
+
* Creates a new vec2 initialized with the given values
|
109
|
+
*
|
110
|
+
* @param {Number} x X component
|
111
|
+
* @param {Number} y Y component
|
112
|
+
* @returns {vec2} a new 2D vector
|
113
|
+
*/
|
114
|
+
vec2.fromValues = function(x, y) {
|
115
|
+
var out = new Float32Array(2);
|
116
|
+
out[0] = x;
|
117
|
+
out[1] = y;
|
118
|
+
return out;
|
119
|
+
};
|
120
|
+
|
121
|
+
/**
|
122
|
+
* Copy the values from one vec2 to another
|
123
|
+
*
|
124
|
+
* @param {vec2} out the receiving vector
|
125
|
+
* @param {vec2} a the source vector
|
126
|
+
* @returns {vec2} out
|
127
|
+
*/
|
128
|
+
vec2.copy = function(out, a) {
|
129
|
+
out[0] = a[0];
|
130
|
+
out[1] = a[1];
|
131
|
+
return out;
|
132
|
+
};
|
133
|
+
|
134
|
+
/**
|
135
|
+
* Set the components of a vec2 to the given values
|
136
|
+
*
|
137
|
+
* @param {vec2} out the receiving vector
|
138
|
+
* @param {Number} x X component
|
139
|
+
* @param {Number} y Y component
|
140
|
+
* @returns {vec2} out
|
141
|
+
*/
|
142
|
+
vec2.set = function(out, x, y) {
|
143
|
+
out[0] = x;
|
144
|
+
out[1] = y;
|
145
|
+
return out;
|
146
|
+
};
|
147
|
+
|
148
|
+
/**
|
149
|
+
* Adds two vec2's
|
150
|
+
*
|
151
|
+
* @param {vec2} out the receiving vector
|
152
|
+
* @param {vec2} a the first operand
|
153
|
+
* @param {vec2} b the second operand
|
154
|
+
* @returns {vec2} out
|
155
|
+
*/
|
156
|
+
vec2.add = function(out, a, b) {
|
157
|
+
out[0] = a[0] + b[0];
|
158
|
+
out[1] = a[1] + b[1];
|
159
|
+
return out;
|
160
|
+
};
|
161
|
+
|
162
|
+
/**
|
163
|
+
* Subtracts two vec2's
|
164
|
+
*
|
165
|
+
* @param {vec2} out the receiving vector
|
166
|
+
* @param {vec2} a the first operand
|
167
|
+
* @param {vec2} b the second operand
|
168
|
+
* @returns {vec2} out
|
169
|
+
*/
|
170
|
+
vec2.sub = vec2.subtract = function(out, a, b) {
|
171
|
+
out[0] = a[0] - b[0];
|
172
|
+
out[1] = a[1] - b[1];
|
173
|
+
return out;
|
174
|
+
};
|
175
|
+
|
176
|
+
/**
|
177
|
+
* Multiplies two vec2's
|
178
|
+
*
|
179
|
+
* @param {vec2} out the receiving vector
|
180
|
+
* @param {vec2} a the first operand
|
181
|
+
* @param {vec2} b the second operand
|
182
|
+
* @returns {vec2} out
|
183
|
+
*/
|
184
|
+
vec2.mul = vec2.multiply = function(out, a, b) {
|
185
|
+
out[0] = a[0] * b[0];
|
186
|
+
out[1] = a[1] * b[1];
|
187
|
+
return out;
|
188
|
+
};
|
189
|
+
|
190
|
+
/**
|
191
|
+
* Divides two vec2's
|
192
|
+
*
|
193
|
+
* @param {vec2} out the receiving vector
|
194
|
+
* @param {vec2} a the first operand
|
195
|
+
* @param {vec2} b the second operand
|
196
|
+
* @returns {vec2} out
|
197
|
+
*/
|
198
|
+
vec2.div = vec2.divide = function(out, a, b) {
|
199
|
+
out[0] = a[0] / b[0];
|
200
|
+
out[1] = a[1] / b[1];
|
201
|
+
return out;
|
202
|
+
};
|
203
|
+
|
204
|
+
/**
|
205
|
+
* Returns the minimum of two vec2's
|
206
|
+
*
|
207
|
+
* @param {vec2} out the receiving vector
|
208
|
+
* @param {vec2} a the first operand
|
209
|
+
* @param {vec2} b the second operand
|
210
|
+
* @returns {vec2} out
|
211
|
+
*/
|
212
|
+
vec2.min = function(out, a, b) {
|
213
|
+
out[0] = Math.min(a[0], b[0]);
|
214
|
+
out[1] = Math.min(a[1], b[1]);
|
215
|
+
return out;
|
216
|
+
};
|
217
|
+
|
218
|
+
/**
|
219
|
+
* Returns the maximum of two vec2's
|
220
|
+
*
|
221
|
+
* @param {vec2} out the receiving vector
|
222
|
+
* @param {vec2} a the first operand
|
223
|
+
* @param {vec2} b the second operand
|
224
|
+
* @returns {vec2} out
|
225
|
+
*/
|
226
|
+
vec2.max = function(out, a, b) {
|
227
|
+
out[0] = Math.max(a[0], b[0]);
|
228
|
+
out[1] = Math.max(a[1], b[1]);
|
229
|
+
return out;
|
230
|
+
};
|
231
|
+
|
232
|
+
/**
|
233
|
+
* Scales a vec2 by a scalar number
|
234
|
+
*
|
235
|
+
* @param {vec2} out the receiving vector
|
236
|
+
* @param {vec2} a the vector to scale
|
237
|
+
* @param {vec2} b amount to scale the vector by
|
238
|
+
* @returns {vec2} out
|
239
|
+
*/
|
240
|
+
vec2.scale = function(out, a, b) {
|
241
|
+
out[0] = a[0] * b;
|
242
|
+
out[1] = a[1] * b;
|
243
|
+
return out;
|
244
|
+
};
|
245
|
+
|
246
|
+
/**
|
247
|
+
* Calculates the euclidian distance between two vec2's
|
248
|
+
*
|
249
|
+
* @param {vec2} a the first operand
|
250
|
+
* @param {vec2} b the second operand
|
251
|
+
* @returns {Number} distance between a and b
|
252
|
+
*/
|
253
|
+
vec2.dist = vec2.distance = function(a, b) {
|
254
|
+
var x = b[0] - a[0],
|
255
|
+
y = b[1] - a[1];
|
256
|
+
return Math.sqrt(x*x + y*y);
|
257
|
+
};
|
258
|
+
|
259
|
+
/**
|
260
|
+
* Calculates the squared euclidian distance between two vec2's
|
261
|
+
*
|
262
|
+
* @param {vec2} a the first operand
|
263
|
+
* @param {vec2} b the second operand
|
264
|
+
* @returns {Number} squared distance between a and b
|
265
|
+
*/
|
266
|
+
vec2.sqrDist = vec2.squaredDistance = function(a, b) {
|
267
|
+
var x = b[0] - a[0],
|
268
|
+
y = b[1] - a[1];
|
269
|
+
return x*x + y*y;
|
270
|
+
};
|
271
|
+
|
272
|
+
/**
|
273
|
+
* Caclulates the length of a vec2
|
274
|
+
*
|
275
|
+
* @param {vec2} a vector to calculate length of
|
276
|
+
* @returns {Number} length of a
|
277
|
+
*/
|
278
|
+
vec2.len = vec2.length = function (a) {
|
279
|
+
var x = a[0],
|
280
|
+
y = a[1];
|
281
|
+
return Math.sqrt(x*x + y*y);
|
282
|
+
};
|
283
|
+
|
284
|
+
/**
|
285
|
+
* Caclulates the squared length of a vec2
|
286
|
+
*
|
287
|
+
* @param {vec2} a vector to calculate squared length of
|
288
|
+
* @returns {Number} squared length of a
|
289
|
+
*/
|
290
|
+
vec2.sqrLen = vec2.squaredLength = function (a) {
|
291
|
+
var x = a[0],
|
292
|
+
y = a[1];
|
293
|
+
return x*x + y*y;
|
294
|
+
};
|
295
|
+
|
296
|
+
/**
|
297
|
+
* Negates the components of a vec2
|
298
|
+
*
|
299
|
+
* @param {vec2} out the receiving vector
|
300
|
+
* @param {vec2} a vector to negate
|
301
|
+
* @returns {vec2} out
|
302
|
+
*/
|
303
|
+
vec2.negate = function(out, a) {
|
304
|
+
out[0] = -a[0];
|
305
|
+
out[1] = -a[1];
|
306
|
+
return out;
|
307
|
+
};
|
308
|
+
|
309
|
+
/**
|
310
|
+
* Normalize a vec2
|
311
|
+
*
|
312
|
+
* @param {vec2} out the receiving vector
|
313
|
+
* @param {vec2} a vector to normalize
|
314
|
+
* @returns {vec2} out
|
315
|
+
*/
|
316
|
+
vec2.normalize = function(out, a) {
|
317
|
+
var x = a[0],
|
318
|
+
y = a[1];
|
319
|
+
var len = x*x + y*y;
|
320
|
+
if (len > 0) {
|
321
|
+
//TODO: evaluate use of glm_invsqrt here?
|
322
|
+
len = 1 / Math.sqrt(len);
|
323
|
+
out[0] = a[0] * len;
|
324
|
+
out[1] = a[1] * len;
|
325
|
+
}
|
326
|
+
return out;
|
327
|
+
};
|
328
|
+
|
329
|
+
/**
|
330
|
+
* Caclulates the dot product of two vec2's
|
331
|
+
*
|
332
|
+
* @param {vec2} a the first operand
|
333
|
+
* @param {vec2} b the second operand
|
334
|
+
* @returns {Number} dot product of a and b
|
335
|
+
*/
|
336
|
+
vec2.dot = function (a, b) {
|
337
|
+
return a[0] * b[0] + a[1] * b[1];
|
338
|
+
};
|
339
|
+
|
340
|
+
/**
|
341
|
+
* Computes the cross product of two vec2's
|
342
|
+
* Note that the cross product must by definition produce a 3D vector
|
343
|
+
*
|
344
|
+
* @param {vec3} out the receiving vector
|
345
|
+
* @param {vec2} a the first operand
|
346
|
+
* @param {vec2} b the second operand
|
347
|
+
* @returns {vec3} out
|
348
|
+
*/
|
349
|
+
vec2.cross = function(out, a, b) {
|
350
|
+
var z = a[0] * b[1] - a[1] * b[0];
|
351
|
+
out[0] = out[1] = 0;
|
352
|
+
out[2] = z;
|
353
|
+
return out;
|
354
|
+
};
|
355
|
+
|
356
|
+
/**
|
357
|
+
* Performs a linear interpolation between two vec2's
|
358
|
+
*
|
359
|
+
* @param {vec3} out the receiving vector
|
360
|
+
* @param {vec2} a the first operand
|
361
|
+
* @param {vec2} b the second operand
|
362
|
+
* @param {Number} t interpolation amount between the two inputs
|
363
|
+
* @returns {vec2} out
|
364
|
+
*/
|
365
|
+
vec2.lerp = function (out, a, b, t) {
|
366
|
+
var ax = a[0],
|
367
|
+
ay = a[1];
|
368
|
+
out[0] = ax + t * (b[0] - ax);
|
369
|
+
out[1] = ay + t * (b[1] - ay);
|
370
|
+
return out;
|
371
|
+
};
|
372
|
+
|
373
|
+
/**
|
374
|
+
* Transforms the vec2 with a mat2
|
375
|
+
*
|
376
|
+
* @param {vec2} out the receiving vector
|
377
|
+
* @param {vec2} a the vector to transform
|
378
|
+
* @param {mat2} m matrix to transform with
|
379
|
+
* @returns {vec2} out
|
380
|
+
*/
|
381
|
+
vec2.transformMat2 = function(out, a, m) {
|
382
|
+
var x = a[0],
|
383
|
+
y = a[1];
|
384
|
+
out[0] = x * m[0] + y * m[1];
|
385
|
+
out[1] = x * m[2] + y * m[3];
|
386
|
+
return out;
|
387
|
+
};
|
388
|
+
|
389
|
+
/**
|
390
|
+
* Perform some operation over an array of vec2s.
|
391
|
+
*
|
392
|
+
* @param {Array} a the array of vectors to iterate over
|
393
|
+
* @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
|
394
|
+
* @param {Number} offset Number of elements to skip at the beginning of the array
|
395
|
+
* @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
|
396
|
+
* @param {Function} fn Function to call for each vector in the array
|
397
|
+
* @param {Object} [arg] additional argument to pass to fn
|
398
|
+
* @returns {Array} a
|
399
|
+
*/
|
400
|
+
vec2.forEach = (function() {
|
401
|
+
var vec = new Float32Array(2);
|
402
|
+
|
403
|
+
return function(a, stride, offset, count, fn, arg) {
|
404
|
+
var i, l;
|
405
|
+
if(!stride) {
|
406
|
+
stride = 2;
|
407
|
+
}
|
408
|
+
|
409
|
+
if(!offset) {
|
410
|
+
offset = 0;
|
411
|
+
}
|
412
|
+
|
413
|
+
if(count) {
|
414
|
+
l = Math.min((count * stride) + offset, a.length);
|
415
|
+
} else {
|
416
|
+
l = a.length;
|
417
|
+
}
|
418
|
+
|
419
|
+
for(i = offset; i < l; i += stride) {
|
420
|
+
vec[0] = a[i]; vec[1] = a[i+1];
|
421
|
+
fn(vec, vec, arg);
|
422
|
+
a[i] = vec[0]; a[i+1] = vec[1];
|
423
|
+
}
|
424
|
+
|
425
|
+
return a;
|
426
|
+
};
|
427
|
+
})();
|
428
|
+
|
429
|
+
/**
|
430
|
+
* Returns a string representation of a vector
|
431
|
+
*
|
432
|
+
* @param {vec2} vec vector to represent as a string
|
433
|
+
* @returns {String} string representation of the vector
|
434
|
+
*/
|
435
|
+
vec2.str = function (a) {
|
436
|
+
return 'vec2(' + a[0] + ', ' + a[1] + ')';
|
437
|
+
};
|
438
|
+
|
439
|
+
if(typeof(exports) !== 'undefined') {
|
440
|
+
exports.vec2 = vec2;
|
441
|
+
}
|
442
|
+
;
|
443
|
+
/* Copyright (c) 2012, Brandon Jones, Colin MacKenzie IV. All rights reserved.
|
444
|
+
|
445
|
+
Redistribution and use in source and binary forms, with or without modification,
|
446
|
+
are permitted provided that the following conditions are met:
|
447
|
+
|
448
|
+
* Redistributions of source code must retain the above copyright notice, this
|
449
|
+
list of conditions and the following disclaimer.
|
450
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
451
|
+
this list of conditions and the following disclaimer in the documentation
|
452
|
+
and/or other materials provided with the distribution.
|
453
|
+
|
454
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
455
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
456
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
457
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
458
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
459
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
460
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
461
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
462
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
463
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
464
|
+
|
465
|
+
/**
|
466
|
+
* @class 3 Dimensional Vector
|
467
|
+
* @name vec3
|
468
|
+
*/
|
469
|
+
|
470
|
+
var vec3 = {};
|
471
|
+
|
472
|
+
if(!GLMAT_EPSILON) {
|
473
|
+
var GLMAT_EPSILON = 0.000001;
|
474
|
+
}
|
475
|
+
|
476
|
+
/**
|
477
|
+
* Creates a new, empty vec3
|
478
|
+
*
|
479
|
+
* @returns {vec3} a new 3D vector
|
480
|
+
*/
|
481
|
+
vec3.create = function() {
|
482
|
+
return new Float32Array(3);
|
483
|
+
};
|
484
|
+
|
485
|
+
/**
|
486
|
+
* Creates a new vec3 initialized with values from an existing vector
|
487
|
+
*
|
488
|
+
* @param {vec3} a vector to clone
|
489
|
+
* @returns {vec3} a new 3D vector
|
490
|
+
*/
|
491
|
+
vec3.clone = function(a) {
|
492
|
+
var out = new Float32Array(3);
|
493
|
+
out[0] = a[0];
|
494
|
+
out[1] = a[1];
|
495
|
+
out[2] = a[2];
|
496
|
+
return out;
|
497
|
+
};
|
498
|
+
|
499
|
+
/**
|
500
|
+
* Creates a new vec3 initialized with the given values
|
501
|
+
*
|
502
|
+
* @param {Number} x X component
|
503
|
+
* @param {Number} y Y component
|
504
|
+
* @param {Number} z Z component
|
505
|
+
* @returns {vec3} a new 3D vector
|
506
|
+
*/
|
507
|
+
vec3.fromValues = function(x, y, z) {
|
508
|
+
var out = new Float32Array(3);
|
509
|
+
out[0] = x;
|
510
|
+
out[1] = y;
|
511
|
+
out[2] = z;
|
512
|
+
return out;
|
513
|
+
};
|
514
|
+
|
515
|
+
/**
|
516
|
+
* Copy the values from one vec3 to another
|
517
|
+
*
|
518
|
+
* @param {vec3} out the receiving vector
|
519
|
+
* @param {vec3} a the source vector
|
520
|
+
* @returns {vec3} out
|
521
|
+
*/
|
522
|
+
vec3.copy = function(out, a) {
|
523
|
+
out[0] = a[0];
|
524
|
+
out[1] = a[1];
|
525
|
+
out[2] = a[2];
|
526
|
+
return out;
|
527
|
+
};
|
528
|
+
|
529
|
+
/**
|
530
|
+
* Set the components of a vec3 to the given values
|
531
|
+
*
|
532
|
+
* @param {vec3} out the receiving vector
|
533
|
+
* @param {Number} x X component
|
534
|
+
* @param {Number} y Y component
|
535
|
+
* @param {Number} z Z component
|
536
|
+
* @returns {vec3} out
|
537
|
+
*/
|
538
|
+
vec3.set = function(out, x, y, z) {
|
539
|
+
out[0] = x;
|
540
|
+
out[1] = y;
|
541
|
+
out[2] = z;
|
542
|
+
return out;
|
543
|
+
};
|
544
|
+
|
545
|
+
/**
|
546
|
+
* Adds two vec3's
|
547
|
+
*
|
548
|
+
* @param {vec3} out the receiving vector
|
549
|
+
* @param {vec3} a the first operand
|
550
|
+
* @param {vec3} b the second operand
|
551
|
+
* @returns {vec3} out
|
552
|
+
*/
|
553
|
+
vec3.add = function(out, a, b) {
|
554
|
+
out[0] = a[0] + b[0];
|
555
|
+
out[1] = a[1] + b[1];
|
556
|
+
out[2] = a[2] + b[2];
|
557
|
+
return out;
|
558
|
+
};
|
559
|
+
|
560
|
+
/**
|
561
|
+
* Subtracts two vec3's
|
562
|
+
*
|
563
|
+
* @param {vec3} out the receiving vector
|
564
|
+
* @param {vec3} a the first operand
|
565
|
+
* @param {vec3} b the second operand
|
566
|
+
* @returns {vec3} out
|
567
|
+
*/
|
568
|
+
vec3.sub = vec3.subtract = function(out, a, b) {
|
569
|
+
out[0] = a[0] - b[0];
|
570
|
+
out[1] = a[1] - b[1];
|
571
|
+
out[2] = a[2] - b[2];
|
572
|
+
return out;
|
573
|
+
};
|
574
|
+
|
575
|
+
/**
|
576
|
+
* Multiplies two vec3's
|
577
|
+
*
|
578
|
+
* @param {vec3} out the receiving vector
|
579
|
+
* @param {vec3} a the first operand
|
580
|
+
* @param {vec3} b the second operand
|
581
|
+
* @returns {vec3} out
|
582
|
+
*/
|
583
|
+
vec3.mul = vec3.multiply = function(out, a, b) {
|
584
|
+
out[0] = a[0] * b[0];
|
585
|
+
out[1] = a[1] * b[1];
|
586
|
+
out[2] = a[2] * b[2];
|
587
|
+
return out;
|
588
|
+
};
|
589
|
+
|
590
|
+
/**
|
591
|
+
* Divides two vec3's
|
592
|
+
*
|
593
|
+
* @param {vec3} out the receiving vector
|
594
|
+
* @param {vec3} a the first operand
|
595
|
+
* @param {vec3} b the second operand
|
596
|
+
* @returns {vec3} out
|
597
|
+
*/
|
598
|
+
vec3.div = vec3.divide = function(out, a, b) {
|
599
|
+
out[0] = a[0] / b[0];
|
600
|
+
out[1] = a[1] / b[1];
|
601
|
+
out[2] = a[2] / b[2];
|
602
|
+
return out;
|
603
|
+
};
|
604
|
+
|
605
|
+
/**
|
606
|
+
* Returns the minimum of two vec3's
|
607
|
+
*
|
608
|
+
* @param {vec3} out the receiving vector
|
609
|
+
* @param {vec3} a the first operand
|
610
|
+
* @param {vec3} b the second operand
|
611
|
+
* @returns {vec3} out
|
612
|
+
*/
|
613
|
+
vec3.min = function(out, a, b) {
|
614
|
+
out[0] = Math.min(a[0], b[0]);
|
615
|
+
out[1] = Math.min(a[1], b[1]);
|
616
|
+
out[2] = Math.min(a[2], b[2]);
|
617
|
+
return out;
|
618
|
+
};
|
619
|
+
|
620
|
+
/**
|
621
|
+
* Returns the maximum of two vec3's
|
622
|
+
*
|
623
|
+
* @param {vec3} out the receiving vector
|
624
|
+
* @param {vec3} a the first operand
|
625
|
+
* @param {vec3} b the second operand
|
626
|
+
* @returns {vec3} out
|
627
|
+
*/
|
628
|
+
vec3.max = function(out, a, b) {
|
629
|
+
out[0] = Math.max(a[0], b[0]);
|
630
|
+
out[1] = Math.max(a[1], b[1]);
|
631
|
+
out[2] = Math.max(a[2], b[2]);
|
632
|
+
return out;
|
633
|
+
};
|
634
|
+
|
635
|
+
/**
|
636
|
+
* Scales a vec3 by a scalar number
|
637
|
+
*
|
638
|
+
* @param {vec3} out the receiving vector
|
639
|
+
* @param {vec3} a the vector to scale
|
640
|
+
* @param {vec3} b amount to scale the vector by
|
641
|
+
* @returns {vec3} out
|
642
|
+
*/
|
643
|
+
vec3.scale = function(out, a, b) {
|
644
|
+
out[0] = a[0] * b;
|
645
|
+
out[1] = a[1] * b;
|
646
|
+
out[2] = a[2] * b;
|
647
|
+
return out;
|
648
|
+
};
|
649
|
+
|
650
|
+
/**
|
651
|
+
* Calculates the euclidian distance between two vec3's
|
652
|
+
*
|
653
|
+
* @param {vec3} a the first operand
|
654
|
+
* @param {vec3} b the second operand
|
655
|
+
* @returns {Number} distance between a and b
|
656
|
+
*/
|
657
|
+
vec3.dist = vec3.distance = function(a, b) {
|
658
|
+
var x = b[0] - a[0],
|
659
|
+
y = b[1] - a[1],
|
660
|
+
z = b[2] - a[2];
|
661
|
+
return Math.sqrt(x*x + y*y + z*z);
|
662
|
+
};
|
663
|
+
|
664
|
+
/**
|
665
|
+
* Calculates the squared euclidian distance between two vec3's
|
666
|
+
*
|
667
|
+
* @param {vec3} a the first operand
|
668
|
+
* @param {vec3} b the second operand
|
669
|
+
* @returns {Number} squared distance between a and b
|
670
|
+
*/
|
671
|
+
vec3.sqrDist = vec3.squaredDistance = function(a, b) {
|
672
|
+
var x = b[0] - a[0],
|
673
|
+
y = b[1] - a[1],
|
674
|
+
z = b[2] - a[2];
|
675
|
+
return x*x + y*y + z*z;
|
676
|
+
};
|
677
|
+
|
678
|
+
/**
|
679
|
+
* Caclulates the length of a vec3
|
680
|
+
*
|
681
|
+
* @param {vec3} a vector to calculate length of
|
682
|
+
* @returns {Number} length of a
|
683
|
+
*/
|
684
|
+
vec3.len = vec3.length = function (a) {
|
685
|
+
var x = a[0],
|
686
|
+
y = a[1],
|
687
|
+
z = a[2];
|
688
|
+
return Math.sqrt(x*x + y*y + z*z);
|
689
|
+
};
|
690
|
+
|
691
|
+
/**
|
692
|
+
* Caclulates the squared length of a vec3
|
693
|
+
*
|
694
|
+
* @param {vec3} a vector to calculate squared length of
|
695
|
+
* @returns {Number} squared length of a
|
696
|
+
*/
|
697
|
+
vec3.sqrLen = vec3.squaredLength = function (a) {
|
698
|
+
var x = a[0],
|
699
|
+
y = a[1],
|
700
|
+
z = a[2];
|
701
|
+
return x*x + y*y + z*z;
|
702
|
+
};
|
703
|
+
|
704
|
+
/**
|
705
|
+
* Negates the components of a vec3
|
706
|
+
*
|
707
|
+
* @param {vec3} out the receiving vector
|
708
|
+
* @param {vec3} a vector to negate
|
709
|
+
* @returns {vec3} out
|
710
|
+
*/
|
711
|
+
vec3.negate = function(out, a) {
|
712
|
+
out[0] = -a[0];
|
713
|
+
out[1] = -a[1];
|
714
|
+
out[2] = -a[2];
|
715
|
+
return out;
|
716
|
+
};
|
717
|
+
|
718
|
+
/**
|
719
|
+
* Normalize a vec3
|
720
|
+
*
|
721
|
+
* @param {vec3} out the receiving vector
|
722
|
+
* @param {vec3} a vector to normalize
|
723
|
+
* @returns {vec3} out
|
724
|
+
*/
|
725
|
+
vec3.normalize = function(out, a) {
|
726
|
+
var x = a[0],
|
727
|
+
y = a[1],
|
728
|
+
z = a[2];
|
729
|
+
var len = x*x + y*y + z*z;
|
730
|
+
if (len > 0) {
|
731
|
+
//TODO: evaluate use of glm_invsqrt here?
|
732
|
+
len = 1 / Math.sqrt(len);
|
733
|
+
out[0] = a[0] * len;
|
734
|
+
out[1] = a[1] * len;
|
735
|
+
out[2] = a[2] * len;
|
736
|
+
}
|
737
|
+
return out;
|
738
|
+
};
|
739
|
+
|
740
|
+
/**
|
741
|
+
* Caclulates the dot product of two vec3's
|
742
|
+
*
|
743
|
+
* @param {vec3} a the first operand
|
744
|
+
* @param {vec3} b the second operand
|
745
|
+
* @returns {Number} dot product of a and b
|
746
|
+
*/
|
747
|
+
vec3.dot = function (a, b) {
|
748
|
+
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
749
|
+
};
|
750
|
+
|
751
|
+
/**
|
752
|
+
* Computes the cross product of two vec3's
|
753
|
+
*
|
754
|
+
* @param {vec3} out the receiving vector
|
755
|
+
* @param {vec3} a the first operand
|
756
|
+
* @param {vec3} b the second operand
|
757
|
+
* @returns {vec3} out
|
758
|
+
*/
|
759
|
+
vec3.cross = function(out, a, b) {
|
760
|
+
var ax = a[0], ay = a[1], az = a[2],
|
761
|
+
bx = b[0], by = b[1], bz = b[2];
|
762
|
+
|
763
|
+
out[0] = ay * bz - az * by;
|
764
|
+
out[1] = az * bx - ax * bz;
|
765
|
+
out[2] = ax * by - ay * bx;
|
766
|
+
return out;
|
767
|
+
};
|
768
|
+
|
769
|
+
/**
|
770
|
+
* Performs a linear interpolation between two vec3's
|
771
|
+
*
|
772
|
+
* @param {vec3} out the receiving vector
|
773
|
+
* @param {vec3} a the first operand
|
774
|
+
* @param {vec3} b the second operand
|
775
|
+
* @param {Number} t interpolation amount between the two inputs
|
776
|
+
* @returns {vec3} out
|
777
|
+
*/
|
778
|
+
vec3.lerp = function (out, a, b, t) {
|
779
|
+
var ax = a[0],
|
780
|
+
ay = a[1],
|
781
|
+
az = a[2];
|
782
|
+
out[0] = ax + t * (b[0] - ax);
|
783
|
+
out[1] = ay + t * (b[1] - ay);
|
784
|
+
out[2] = az + t * (b[2] - az);
|
785
|
+
return out;
|
786
|
+
};
|
787
|
+
|
788
|
+
/**
|
789
|
+
* Transforms the vec3 with a mat4.
|
790
|
+
* 4th vector component is implicitly '1'
|
791
|
+
*
|
792
|
+
* @param {vec3} out the receiving vector
|
793
|
+
* @param {vec3} a the vector to transform
|
794
|
+
* @param {mat4} m matrix to transform with
|
795
|
+
* @returns {vec3} out
|
796
|
+
*/
|
797
|
+
vec3.transformMat4 = function(out, a, m) {
|
798
|
+
var x = a[0], y = a[1], z = a[2];
|
799
|
+
out[0] = m[0] * x + m[4] * y + m[8] * z + m[12];
|
800
|
+
out[1] = m[1] * x + m[5] * y + m[9] * z + m[13];
|
801
|
+
out[2] = m[2] * x + m[6] * y + m[10] * z + m[14];
|
802
|
+
return out;
|
803
|
+
};
|
804
|
+
|
805
|
+
/**
|
806
|
+
* Transforms the vec3 with a quat
|
807
|
+
*
|
808
|
+
* @param {vec3} out the receiving vector
|
809
|
+
* @param {vec3} a the vector to transform
|
810
|
+
* @param {quat} q quaternion to transform with
|
811
|
+
* @returns {vec3} out
|
812
|
+
*/
|
813
|
+
vec3.transformQuat = function(out, a, q) {
|
814
|
+
var x = a[0], y = a[1], z = a[2],
|
815
|
+
qx = q[0], qy = q[1], qz = q[2], qw = q[3],
|
816
|
+
|
817
|
+
// calculate quat * vec
|
818
|
+
ix = qw * x + qy * z - qz * y,
|
819
|
+
iy = qw * y + qz * x - qx * z,
|
820
|
+
iz = qw * z + qx * y - qy * x,
|
821
|
+
iw = -qx * x - qy * y - qz * z;
|
822
|
+
|
823
|
+
// calculate result * inverse quat
|
824
|
+
out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
|
825
|
+
out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
|
826
|
+
out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
|
827
|
+
return out;
|
828
|
+
};
|
829
|
+
|
830
|
+
/**
|
831
|
+
* Perform some operation over an array of vec3s.
|
832
|
+
*
|
833
|
+
* @param {Array} a the array of vectors to iterate over
|
834
|
+
* @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
|
835
|
+
* @param {Number} offset Number of elements to skip at the beginning of the array
|
836
|
+
* @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
|
837
|
+
* @param {Function} fn Function to call for each vector in the array
|
838
|
+
* @param {Object} [arg] additional argument to pass to fn
|
839
|
+
* @returns {Array} a
|
840
|
+
*/
|
841
|
+
vec3.forEach = (function() {
|
842
|
+
var vec = new Float32Array(3);
|
843
|
+
|
844
|
+
return function(a, stride, offset, count, fn, arg) {
|
845
|
+
var i, l;
|
846
|
+
if(!stride) {
|
847
|
+
stride = 3;
|
848
|
+
}
|
849
|
+
|
850
|
+
if(!offset) {
|
851
|
+
offset = 0;
|
852
|
+
}
|
853
|
+
|
854
|
+
if(count) {
|
855
|
+
l = Math.min((count * stride) + offset, a.length);
|
856
|
+
} else {
|
857
|
+
l = a.length;
|
858
|
+
}
|
859
|
+
|
860
|
+
for(i = offset; i < l; i += stride) {
|
861
|
+
vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2];
|
862
|
+
fn(vec, vec, arg);
|
863
|
+
a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2];
|
864
|
+
}
|
865
|
+
|
866
|
+
return a;
|
867
|
+
};
|
868
|
+
})();
|
869
|
+
|
870
|
+
/**
|
871
|
+
* Returns a string representation of a vector
|
872
|
+
*
|
873
|
+
* @param {vec3} vec vector to represent as a string
|
874
|
+
* @returns {String} string representation of the vector
|
875
|
+
*/
|
876
|
+
vec3.str = function (a) {
|
877
|
+
return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')';
|
878
|
+
};
|
879
|
+
|
880
|
+
if(typeof(exports) !== 'undefined') {
|
881
|
+
exports.vec3 = vec3;
|
882
|
+
}
|
883
|
+
;
|
884
|
+
/* Copyright (c) 2012, Brandon Jones, Colin MacKenzie IV. All rights reserved.
|
885
|
+
|
886
|
+
Redistribution and use in source and binary forms, with or without modification,
|
887
|
+
are permitted provided that the following conditions are met:
|
888
|
+
|
889
|
+
* Redistributions of source code must retain the above copyright notice, this
|
890
|
+
list of conditions and the following disclaimer.
|
891
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
892
|
+
this list of conditions and the following disclaimer in the documentation
|
893
|
+
and/or other materials provided with the distribution.
|
894
|
+
|
895
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
896
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
897
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
898
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
899
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
900
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
901
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
902
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
903
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
904
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
905
|
+
|
906
|
+
/**
|
907
|
+
* @class 4 Dimensional Vector
|
908
|
+
* @name vec4
|
909
|
+
*/
|
910
|
+
|
911
|
+
var vec4 = {};
|
912
|
+
|
913
|
+
if(!GLMAT_EPSILON) {
|
914
|
+
var GLMAT_EPSILON = 0.000001;
|
915
|
+
}
|
916
|
+
|
917
|
+
/**
|
918
|
+
* Creates a new, empty vec4
|
919
|
+
*
|
920
|
+
* @returns {vec4} a new 4D vector
|
921
|
+
*/
|
922
|
+
vec4.create = function() {
|
923
|
+
return new Float32Array(4);
|
924
|
+
};
|
925
|
+
|
926
|
+
/**
|
927
|
+
* Creates a new vec4 initialized with values from an existing vector
|
928
|
+
*
|
929
|
+
* @param {vec4} a vector to clone
|
930
|
+
* @returns {vec4} a new 4D vector
|
931
|
+
*/
|
932
|
+
vec4.clone = function(a) {
|
933
|
+
var out = new Float32Array(4);
|
934
|
+
out[0] = a[0];
|
935
|
+
out[1] = a[1];
|
936
|
+
out[2] = a[2];
|
937
|
+
out[3] = a[3];
|
938
|
+
return out;
|
939
|
+
};
|
940
|
+
|
941
|
+
/**
|
942
|
+
* Creates a new vec4 initialized with the given values
|
943
|
+
*
|
944
|
+
* @param {Number} x X component
|
945
|
+
* @param {Number} y Y component
|
946
|
+
* @param {Number} z Z component
|
947
|
+
* @param {Number} w W component
|
948
|
+
* @returns {vec4} a new 4D vector
|
949
|
+
*/
|
950
|
+
vec4.fromValues = function(x, y, z, w) {
|
951
|
+
var out = new Float32Array(4);
|
952
|
+
out[0] = x;
|
953
|
+
out[1] = y;
|
954
|
+
out[2] = z;
|
955
|
+
out[3] = w;
|
956
|
+
return out;
|
957
|
+
};
|
958
|
+
|
959
|
+
/**
|
960
|
+
* Copy the values from one vec4 to another
|
961
|
+
*
|
962
|
+
* @param {vec4} out the receiving vector
|
963
|
+
* @param {vec4} a the source vector
|
964
|
+
* @returns {vec4} out
|
965
|
+
*/
|
966
|
+
vec4.copy = function(out, a) {
|
967
|
+
out[0] = a[0];
|
968
|
+
out[1] = a[1];
|
969
|
+
out[2] = a[2];
|
970
|
+
out[3] = a[3];
|
971
|
+
return out;
|
972
|
+
};
|
973
|
+
|
974
|
+
/**
|
975
|
+
* Set the components of a vec4 to the given values
|
976
|
+
*
|
977
|
+
* @param {vec4} out the receiving vector
|
978
|
+
* @param {Number} x X component
|
979
|
+
* @param {Number} y Y component
|
980
|
+
* @param {Number} z Z component
|
981
|
+
* @param {Number} w W component
|
982
|
+
* @returns {vec4} out
|
983
|
+
*/
|
984
|
+
vec4.set = function(out, x, y, z, w) {
|
985
|
+
out[0] = x;
|
986
|
+
out[1] = y;
|
987
|
+
out[2] = z;
|
988
|
+
out[3] = w;
|
989
|
+
return out;
|
990
|
+
};
|
991
|
+
|
992
|
+
/**
|
993
|
+
* Adds two vec4's
|
994
|
+
*
|
995
|
+
* @param {vec4} out the receiving vector
|
996
|
+
* @param {vec4} a the first operand
|
997
|
+
* @param {vec4} b the second operand
|
998
|
+
* @returns {vec4} out
|
999
|
+
*/
|
1000
|
+
vec4.add = function(out, a, b) {
|
1001
|
+
out[0] = a[0] + b[0];
|
1002
|
+
out[1] = a[1] + b[1];
|
1003
|
+
out[2] = a[2] + b[2];
|
1004
|
+
out[3] = a[3] + b[3];
|
1005
|
+
return out;
|
1006
|
+
};
|
1007
|
+
|
1008
|
+
/**
|
1009
|
+
* Subtracts two vec4's
|
1010
|
+
*
|
1011
|
+
* @param {vec4} out the receiving vector
|
1012
|
+
* @param {vec4} a the first operand
|
1013
|
+
* @param {vec4} b the second operand
|
1014
|
+
* @returns {vec4} out
|
1015
|
+
*/
|
1016
|
+
vec4.sub = vec4.subtract = function(out, a, b) {
|
1017
|
+
out[0] = a[0] - b[0];
|
1018
|
+
out[1] = a[1] - b[1];
|
1019
|
+
out[2] = a[2] - b[2];
|
1020
|
+
out[3] = a[3] - b[3];
|
1021
|
+
return out;
|
1022
|
+
};
|
1023
|
+
|
1024
|
+
/**
|
1025
|
+
* Multiplies two vec4's
|
1026
|
+
*
|
1027
|
+
* @param {vec4} out the receiving vector
|
1028
|
+
* @param {vec4} a the first operand
|
1029
|
+
* @param {vec4} b the second operand
|
1030
|
+
* @returns {vec4} out
|
1031
|
+
*/
|
1032
|
+
vec4.mul = vec4.multiply = function(out, a, b) {
|
1033
|
+
out[0] = a[0] * b[0];
|
1034
|
+
out[1] = a[1] * b[1];
|
1035
|
+
out[2] = a[2] * b[2];
|
1036
|
+
out[3] = a[3] * b[3];
|
1037
|
+
return out;
|
1038
|
+
};
|
1039
|
+
|
1040
|
+
/**
|
1041
|
+
* Divides two vec4's
|
1042
|
+
*
|
1043
|
+
* @param {vec4} out the receiving vector
|
1044
|
+
* @param {vec4} a the first operand
|
1045
|
+
* @param {vec4} b the second operand
|
1046
|
+
* @returns {vec4} out
|
1047
|
+
*/
|
1048
|
+
vec4.div = vec4.divide = function(out, a, b) {
|
1049
|
+
out[0] = a[0] / b[0];
|
1050
|
+
out[1] = a[1] / b[1];
|
1051
|
+
out[2] = a[2] / b[2];
|
1052
|
+
out[3] = a[3] / b[3];
|
1053
|
+
return out;
|
1054
|
+
};
|
1055
|
+
|
1056
|
+
/**
|
1057
|
+
* Returns the minimum of two vec4's
|
1058
|
+
*
|
1059
|
+
* @param {vec4} out the receiving vector
|
1060
|
+
* @param {vec4} a the first operand
|
1061
|
+
* @param {vec4} b the second operand
|
1062
|
+
* @returns {vec4} out
|
1063
|
+
*/
|
1064
|
+
vec4.min = function(out, a, b) {
|
1065
|
+
out[0] = Math.min(a[0], b[0]);
|
1066
|
+
out[1] = Math.min(a[1], b[1]);
|
1067
|
+
out[2] = Math.min(a[2], b[2]);
|
1068
|
+
out[3] = Math.min(a[3], b[3]);
|
1069
|
+
return out;
|
1070
|
+
};
|
1071
|
+
|
1072
|
+
/**
|
1073
|
+
* Returns the maximum of two vec4's
|
1074
|
+
*
|
1075
|
+
* @param {vec4} out the receiving vector
|
1076
|
+
* @param {vec4} a the first operand
|
1077
|
+
* @param {vec4} b the second operand
|
1078
|
+
* @returns {vec4} out
|
1079
|
+
*/
|
1080
|
+
vec4.max = function(out, a, b) {
|
1081
|
+
out[0] = Math.max(a[0], b[0]);
|
1082
|
+
out[1] = Math.max(a[1], b[1]);
|
1083
|
+
out[2] = Math.max(a[2], b[2]);
|
1084
|
+
out[3] = Math.max(a[3], b[3]);
|
1085
|
+
return out;
|
1086
|
+
};
|
1087
|
+
|
1088
|
+
/**
|
1089
|
+
* Scales a vec4 by a scalar number
|
1090
|
+
*
|
1091
|
+
* @param {vec4} out the receiving vector
|
1092
|
+
* @param {vec4} a the vector to scale
|
1093
|
+
* @param {vec4} b amount to scale the vector by
|
1094
|
+
* @returns {vec4} out
|
1095
|
+
*/
|
1096
|
+
vec4.scale = function(out, a, b) {
|
1097
|
+
out[0] = a[0] * b;
|
1098
|
+
out[1] = a[1] * b;
|
1099
|
+
out[2] = a[2] * b;
|
1100
|
+
out[3] = a[3] * b;
|
1101
|
+
return out;
|
1102
|
+
};
|
1103
|
+
|
1104
|
+
/**
|
1105
|
+
* Calculates the euclidian distance between two vec4's
|
1106
|
+
*
|
1107
|
+
* @param {vec4} a the first operand
|
1108
|
+
* @param {vec4} b the second operand
|
1109
|
+
* @returns {Number} distance between a and b
|
1110
|
+
*/
|
1111
|
+
vec4.dist = vec4.distance = function(a, b) {
|
1112
|
+
var x = b[0] - a[0],
|
1113
|
+
y = b[1] - a[1],
|
1114
|
+
z = b[2] - a[2],
|
1115
|
+
w = b[3] - a[3];
|
1116
|
+
return Math.sqrt(x*x + y*y + z*z + w*w);
|
1117
|
+
};
|
1118
|
+
|
1119
|
+
/**
|
1120
|
+
* Calculates the squared euclidian distance between two vec4's
|
1121
|
+
*
|
1122
|
+
* @param {vec4} a the first operand
|
1123
|
+
* @param {vec4} b the second operand
|
1124
|
+
* @returns {Number} squared distance between a and b
|
1125
|
+
*/
|
1126
|
+
vec4.sqrDist = vec4.squaredDistance = function(a, b) {
|
1127
|
+
var x = b[0] - a[0],
|
1128
|
+
y = b[1] - a[1],
|
1129
|
+
z = b[2] - a[2],
|
1130
|
+
w = b[3] - a[3];
|
1131
|
+
return x*x + y*y + z*z + w*w;
|
1132
|
+
};
|
1133
|
+
|
1134
|
+
/**
|
1135
|
+
* Caclulates the length of a vec4
|
1136
|
+
*
|
1137
|
+
* @param {vec4} a vector to calculate length of
|
1138
|
+
* @returns {Number} length of a
|
1139
|
+
*/
|
1140
|
+
vec4.len = vec4.length = function (a) {
|
1141
|
+
var x = a[0],
|
1142
|
+
y = a[1],
|
1143
|
+
z = a[2],
|
1144
|
+
w = a[3];
|
1145
|
+
return Math.sqrt(x*x + y*y + z*z + w*w);
|
1146
|
+
};
|
1147
|
+
|
1148
|
+
/**
|
1149
|
+
* Caclulates the squared length of a vec4
|
1150
|
+
*
|
1151
|
+
* @param {vec4} a vector to calculate squared length of
|
1152
|
+
* @returns {Number} squared length of a
|
1153
|
+
*/
|
1154
|
+
vec4.sqrLen = vec4.squaredLength = function (a) {
|
1155
|
+
var x = a[0],
|
1156
|
+
y = a[1],
|
1157
|
+
z = a[2],
|
1158
|
+
w = a[3];
|
1159
|
+
return x*x + y*y + z*z + w*w;
|
1160
|
+
};
|
1161
|
+
|
1162
|
+
/**
|
1163
|
+
* Negates the components of a vec4
|
1164
|
+
*
|
1165
|
+
* @param {vec4} out the receiving vector
|
1166
|
+
* @param {vec4} a vector to negate
|
1167
|
+
* @returns {vec4} out
|
1168
|
+
*/
|
1169
|
+
vec4.negate = function(out, a) {
|
1170
|
+
out[0] = -a[0];
|
1171
|
+
out[1] = -a[1];
|
1172
|
+
out[2] = -a[2];
|
1173
|
+
out[3] = -a[3];
|
1174
|
+
return out;
|
1175
|
+
};
|
1176
|
+
|
1177
|
+
/**
|
1178
|
+
* Normalize a vec4
|
1179
|
+
*
|
1180
|
+
* @param {vec4} out the receiving vector
|
1181
|
+
* @param {vec4} a vector to normalize
|
1182
|
+
* @returns {vec4} out
|
1183
|
+
*/
|
1184
|
+
vec4.normalize = function(out, a) {
|
1185
|
+
var x = a[0],
|
1186
|
+
y = a[1],
|
1187
|
+
z = a[2],
|
1188
|
+
w = a[3];
|
1189
|
+
var len = x*x + y*y + z*z + w*w;
|
1190
|
+
if (len > 0) {
|
1191
|
+
len = 1 / Math.sqrt(len);
|
1192
|
+
out[0] = a[0] * len;
|
1193
|
+
out[1] = a[1] * len;
|
1194
|
+
out[2] = a[2] * len;
|
1195
|
+
out[3] = a[3] * len;
|
1196
|
+
}
|
1197
|
+
return out;
|
1198
|
+
};
|
1199
|
+
|
1200
|
+
/**
|
1201
|
+
* Caclulates the dot product of two vec4's
|
1202
|
+
*
|
1203
|
+
* @param {vec4} a the first operand
|
1204
|
+
* @param {vec4} b the second operand
|
1205
|
+
* @returns {Number} dot product of a and b
|
1206
|
+
*/
|
1207
|
+
vec4.dot = function (a, b) {
|
1208
|
+
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
|
1209
|
+
};
|
1210
|
+
|
1211
|
+
/**
|
1212
|
+
* Performs a linear interpolation between two vec4's
|
1213
|
+
*
|
1214
|
+
* @param {vec4} out the receiving vector
|
1215
|
+
* @param {vec4} a the first operand
|
1216
|
+
* @param {vec4} b the second operand
|
1217
|
+
* @param {Number} t interpolation amount between the two inputs
|
1218
|
+
* @returns {vec4} out
|
1219
|
+
*/
|
1220
|
+
vec4.lerp = function (out, a, b, t) {
|
1221
|
+
var ax = a[0],
|
1222
|
+
ay = a[1],
|
1223
|
+
az = a[2],
|
1224
|
+
aw = a[3];
|
1225
|
+
out[0] = ax + t * (b[0] - ax);
|
1226
|
+
out[1] = ay + t * (b[1] - ay);
|
1227
|
+
out[2] = az + t * (b[2] - az);
|
1228
|
+
out[3] = aw + t * (b[3] - aw);
|
1229
|
+
return out;
|
1230
|
+
};
|
1231
|
+
|
1232
|
+
/**
|
1233
|
+
* Transforms the vec4 with a mat4.
|
1234
|
+
*
|
1235
|
+
* @param {vec4} out the receiving vector
|
1236
|
+
* @param {vec4} a the vector to transform
|
1237
|
+
* @param {mat4} m matrix to transform with
|
1238
|
+
* @returns {vec4} out
|
1239
|
+
*/
|
1240
|
+
vec4.transformMat4 = function(out, a, m) {
|
1241
|
+
var x = a[0], y = a[1], z = a[2], w = a[3];
|
1242
|
+
out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
|
1243
|
+
out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
|
1244
|
+
out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
|
1245
|
+
out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
|
1246
|
+
return out;
|
1247
|
+
};
|
1248
|
+
|
1249
|
+
/**
|
1250
|
+
* Transforms the vec4 with a quat
|
1251
|
+
*
|
1252
|
+
* @param {vec4} out the receiving vector
|
1253
|
+
* @param {vec4} a the vector to transform
|
1254
|
+
* @param {quat} q quaternion to transform with
|
1255
|
+
* @returns {vec4} out
|
1256
|
+
*/
|
1257
|
+
vec4.transformQuat = function(out, a, q) {
|
1258
|
+
var x = a[0], y = a[1], z = a[2],
|
1259
|
+
qx = q[0], qy = q[1], qz = q[2], qw = q[3],
|
1260
|
+
|
1261
|
+
// calculate quat * vec
|
1262
|
+
ix = qw * x + qy * z - qz * y,
|
1263
|
+
iy = qw * y + qz * x - qx * z,
|
1264
|
+
iz = qw * z + qx * y - qy * x,
|
1265
|
+
iw = -qx * x - qy * y - qz * z;
|
1266
|
+
|
1267
|
+
// calculate result * inverse quat
|
1268
|
+
out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
|
1269
|
+
out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
|
1270
|
+
out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
|
1271
|
+
return out;
|
1272
|
+
};
|
1273
|
+
|
1274
|
+
/**
|
1275
|
+
* Perform some operation over an array of vec4s.
|
1276
|
+
*
|
1277
|
+
* @param {Array} a the array of vectors to iterate over
|
1278
|
+
* @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
|
1279
|
+
* @param {Number} offset Number of elements to skip at the beginning of the array
|
1280
|
+
* @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
|
1281
|
+
* @param {Function} fn Function to call for each vector in the array
|
1282
|
+
* @param {Object} [arg] additional argument to pass to fn
|
1283
|
+
* @returns {Array} a
|
1284
|
+
*/
|
1285
|
+
vec4.forEach = (function() {
|
1286
|
+
var vec = new Float32Array(4);
|
1287
|
+
|
1288
|
+
return function(a, stride, offset, count, fn, arg) {
|
1289
|
+
var i, l;
|
1290
|
+
if(!stride) {
|
1291
|
+
stride = 4;
|
1292
|
+
}
|
1293
|
+
|
1294
|
+
if(!offset) {
|
1295
|
+
offset = 0;
|
1296
|
+
}
|
1297
|
+
|
1298
|
+
if(count) {
|
1299
|
+
l = Math.min((count * stride) + offset, a.length);
|
1300
|
+
} else {
|
1301
|
+
l = a.length;
|
1302
|
+
}
|
1303
|
+
|
1304
|
+
for(i = offset; i < l; i += stride) {
|
1305
|
+
vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; vec[3] = a[i+3];
|
1306
|
+
fn(vec, vec, arg);
|
1307
|
+
a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; a[i+3] = vec[3];
|
1308
|
+
}
|
1309
|
+
|
1310
|
+
return a;
|
1311
|
+
};
|
1312
|
+
})();
|
1313
|
+
|
1314
|
+
/**
|
1315
|
+
* Returns a string representation of a vector
|
1316
|
+
*
|
1317
|
+
* @param {vec4} vec vector to represent as a string
|
1318
|
+
* @returns {String} string representation of the vector
|
1319
|
+
*/
|
1320
|
+
vec4.str = function (a) {
|
1321
|
+
return 'vec4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
|
1322
|
+
};
|
1323
|
+
|
1324
|
+
if(typeof(exports) !== 'undefined') {
|
1325
|
+
exports.vec4 = vec4;
|
1326
|
+
}
|
1327
|
+
;
|
1328
|
+
/* Copyright (c) 2012, Brandon Jones, Colin MacKenzie IV. All rights reserved.
|
1329
|
+
|
1330
|
+
Redistribution and use in source and binary forms, with or without modification,
|
1331
|
+
are permitted provided that the following conditions are met:
|
1332
|
+
|
1333
|
+
* Redistributions of source code must retain the above copyright notice, this
|
1334
|
+
list of conditions and the following disclaimer.
|
1335
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
1336
|
+
this list of conditions and the following disclaimer in the documentation
|
1337
|
+
and/or other materials provided with the distribution.
|
1338
|
+
|
1339
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
1340
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
1341
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
1342
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
1343
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
1344
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
1345
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
1346
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
1347
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
1348
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
1349
|
+
|
1350
|
+
/**
|
1351
|
+
* @class 2x2 Matrix
|
1352
|
+
* @name mat2
|
1353
|
+
*/
|
1354
|
+
|
1355
|
+
var mat2 = {};
|
1356
|
+
|
1357
|
+
var mat2Identity = new Float32Array([
|
1358
|
+
1, 0,
|
1359
|
+
0, 1
|
1360
|
+
]);
|
1361
|
+
|
1362
|
+
if(!GLMAT_EPSILON) {
|
1363
|
+
var GLMAT_EPSILON = 0.000001;
|
1364
|
+
}
|
1365
|
+
|
1366
|
+
/**
|
1367
|
+
* Creates a new identity mat2
|
1368
|
+
*
|
1369
|
+
* @returns {mat2} a new 2x2 matrix
|
1370
|
+
*/
|
1371
|
+
mat2.create = function() {
|
1372
|
+
return new Float32Array(mat2Identity);
|
1373
|
+
};
|
1374
|
+
|
1375
|
+
/**
|
1376
|
+
* Creates a new mat2 initialized with values from an existing matrix
|
1377
|
+
*
|
1378
|
+
* @param {mat2} a matrix to clone
|
1379
|
+
* @returns {mat2} a new 2x2 matrix
|
1380
|
+
*/
|
1381
|
+
mat2.clone = function(a) {
|
1382
|
+
var out = new Float32Array(4);
|
1383
|
+
out[0] = a[0];
|
1384
|
+
out[1] = a[1];
|
1385
|
+
out[2] = a[2];
|
1386
|
+
out[3] = a[3];
|
1387
|
+
return out;
|
1388
|
+
};
|
1389
|
+
|
1390
|
+
/**
|
1391
|
+
* Copy the values from one mat2 to another
|
1392
|
+
*
|
1393
|
+
* @param {mat2} out the receiving matrix
|
1394
|
+
* @param {mat2} a the source matrix
|
1395
|
+
* @returns {mat2} out
|
1396
|
+
*/
|
1397
|
+
mat2.copy = function(out, a) {
|
1398
|
+
out[0] = a[0];
|
1399
|
+
out[1] = a[1];
|
1400
|
+
out[2] = a[2];
|
1401
|
+
out[3] = a[3];
|
1402
|
+
return out;
|
1403
|
+
};
|
1404
|
+
|
1405
|
+
/**
|
1406
|
+
* Set a mat2 to the identity matrix
|
1407
|
+
*
|
1408
|
+
* @param {mat2} out the receiving matrix
|
1409
|
+
* @returns {mat2} out
|
1410
|
+
*/
|
1411
|
+
mat2.identity = function(out) {
|
1412
|
+
out[0] = 1;
|
1413
|
+
out[1] = 0;
|
1414
|
+
out[2] = 0;
|
1415
|
+
out[3] = 1;
|
1416
|
+
return out;
|
1417
|
+
};
|
1418
|
+
|
1419
|
+
/**
|
1420
|
+
* Transpose the values of a mat2
|
1421
|
+
*
|
1422
|
+
* @param {mat2} out the receiving matrix
|
1423
|
+
* @param {mat2} a the source matrix
|
1424
|
+
* @returns {mat2} out
|
1425
|
+
*/
|
1426
|
+
mat2.transpose = function(out, a) {
|
1427
|
+
// If we are transposing ourselves we can skip a few steps but have to cache some values
|
1428
|
+
if (out === a) {
|
1429
|
+
var a1 = a[1];
|
1430
|
+
out[1] = a[2];
|
1431
|
+
out[2] = a1;
|
1432
|
+
} else {
|
1433
|
+
out[0] = a[0];
|
1434
|
+
out[1] = a[2];
|
1435
|
+
out[2] = a[1];
|
1436
|
+
out[3] = a[3];
|
1437
|
+
}
|
1438
|
+
|
1439
|
+
return out;
|
1440
|
+
};
|
1441
|
+
|
1442
|
+
/**
|
1443
|
+
* Inverts a mat2
|
1444
|
+
*
|
1445
|
+
* @param {mat2} out the receiving matrix
|
1446
|
+
* @param {mat2} a the source matrix
|
1447
|
+
* @returns {mat2} out
|
1448
|
+
*/
|
1449
|
+
mat2.invert = function(out, a) {
|
1450
|
+
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
|
1451
|
+
|
1452
|
+
// Calculate the determinant
|
1453
|
+
det = a0 * a3 - a2 * a1;
|
1454
|
+
|
1455
|
+
if (!det) {
|
1456
|
+
return null;
|
1457
|
+
}
|
1458
|
+
det = 1.0 / det;
|
1459
|
+
|
1460
|
+
out[0] = a3 * det;
|
1461
|
+
out[1] = -a1 * det;
|
1462
|
+
out[2] = -a2 * det;
|
1463
|
+
out[3] = a0 * det;
|
1464
|
+
|
1465
|
+
return out;
|
1466
|
+
};
|
1467
|
+
|
1468
|
+
/**
|
1469
|
+
* Caclulates the adjugate of a mat2
|
1470
|
+
*
|
1471
|
+
* @param {mat2} out the receiving matrix
|
1472
|
+
* @param {mat2} a the source matrix
|
1473
|
+
* @returns {mat2} out
|
1474
|
+
*/
|
1475
|
+
mat2.adjoint = function(out, a) {
|
1476
|
+
// Caching this value is nessecary if out == a
|
1477
|
+
var a0 = a[0];
|
1478
|
+
out[0] = a[3];
|
1479
|
+
out[1] = -a[1];
|
1480
|
+
out[2] = -a[2];
|
1481
|
+
out[3] = a0;
|
1482
|
+
|
1483
|
+
return out;
|
1484
|
+
};
|
1485
|
+
|
1486
|
+
/**
|
1487
|
+
* Calculates the determinant of a mat2
|
1488
|
+
*
|
1489
|
+
* @param {mat2} a the source matrix
|
1490
|
+
* @returns {Number} determinant of a
|
1491
|
+
*/
|
1492
|
+
mat2.determinant = function (a) {
|
1493
|
+
return a[0] * a[3] - a[2] * a[1];
|
1494
|
+
};
|
1495
|
+
|
1496
|
+
/**
|
1497
|
+
* Multiplies two mat2's
|
1498
|
+
*
|
1499
|
+
* @param {mat2} out the receiving matrix
|
1500
|
+
* @param {mat2} a the first operand
|
1501
|
+
* @param {mat2} b the second operand
|
1502
|
+
* @returns {mat2} out
|
1503
|
+
*/
|
1504
|
+
mat2.mul = mat2.multiply = function (out, a, b) {
|
1505
|
+
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3];
|
1506
|
+
var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
|
1507
|
+
out[0] = a0 * b0 + a1 * b2;
|
1508
|
+
out[1] = a0 * b1 + a1 * b3;
|
1509
|
+
out[2] = a2 * b0 + a3 * b2;
|
1510
|
+
out[3] = a2 * b1 + a3 * b3;
|
1511
|
+
return out;
|
1512
|
+
};
|
1513
|
+
|
1514
|
+
/**
|
1515
|
+
* Rotates a mat2 by the given angle
|
1516
|
+
*
|
1517
|
+
* @param {mat2} out the receiving matrix
|
1518
|
+
* @param {mat2} a the matrix to rotate
|
1519
|
+
* @param {mat2} rad the angle to rotate the matrix by
|
1520
|
+
* @returns {mat2} out
|
1521
|
+
*/
|
1522
|
+
mat2.rotate = function (out, a, rad) {
|
1523
|
+
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
|
1524
|
+
s = Math.sin(rad),
|
1525
|
+
c = Math.cos(rad);
|
1526
|
+
out[0] = a0 * c + a1 * s;
|
1527
|
+
out[1] = a0 * -s + a1 * c;
|
1528
|
+
out[2] = a2 * c + a3 * s;
|
1529
|
+
out[3] = a2 * -s + a3 * c;
|
1530
|
+
return out;
|
1531
|
+
};
|
1532
|
+
|
1533
|
+
/**
|
1534
|
+
* Scales the mat2 by the dimensions in the given vec2
|
1535
|
+
*
|
1536
|
+
* @param {mat2} out the receiving matrix
|
1537
|
+
* @param {mat2} a the matrix to rotate
|
1538
|
+
* @param {mat2} v the vec2 to scale the matrix by
|
1539
|
+
* @returns {mat2} out
|
1540
|
+
**/
|
1541
|
+
mat2.scale = function(out, a, v) {
|
1542
|
+
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
|
1543
|
+
v0 = v[0], v1 = v[1];
|
1544
|
+
out[0] = a0 * v0;
|
1545
|
+
out[1] = a1 * v1;
|
1546
|
+
out[2] = a2 * v0;
|
1547
|
+
out[3] = a3 * v1;
|
1548
|
+
return out;
|
1549
|
+
};
|
1550
|
+
|
1551
|
+
/**
|
1552
|
+
* Returns a string representation of a mat2
|
1553
|
+
*
|
1554
|
+
* @param {mat2} mat matrix to represent as a string
|
1555
|
+
* @returns {String} string representation of the matrix
|
1556
|
+
*/
|
1557
|
+
mat2.str = function (a) {
|
1558
|
+
return 'mat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
|
1559
|
+
};
|
1560
|
+
|
1561
|
+
if(typeof(exports) !== 'undefined') {
|
1562
|
+
exports.mat2 = mat2;
|
1563
|
+
}
|
1564
|
+
;
|
1565
|
+
/* Copyright (c) 2012, Brandon Jones, Colin MacKenzie IV. All rights reserved.
|
1566
|
+
|
1567
|
+
Redistribution and use in source and binary forms, with or without modification,
|
1568
|
+
are permitted provided that the following conditions are met:
|
1569
|
+
|
1570
|
+
* Redistributions of source code must retain the above copyright notice, this
|
1571
|
+
list of conditions and the following disclaimer.
|
1572
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
1573
|
+
this list of conditions and the following disclaimer in the documentation
|
1574
|
+
and/or other materials provided with the distribution.
|
1575
|
+
|
1576
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
1577
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
1578
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
1579
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
1580
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
1581
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
1582
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
1583
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
1584
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
1585
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
1586
|
+
|
1587
|
+
/**
|
1588
|
+
* @class 3x3 Matrix
|
1589
|
+
* @name mat3
|
1590
|
+
*/
|
1591
|
+
|
1592
|
+
var mat3 = {};
|
1593
|
+
|
1594
|
+
var mat3Identity = new Float32Array([
|
1595
|
+
1, 0, 0,
|
1596
|
+
0, 1, 0,
|
1597
|
+
0, 0, 1
|
1598
|
+
]);
|
1599
|
+
|
1600
|
+
if(!GLMAT_EPSILON) {
|
1601
|
+
var GLMAT_EPSILON = 0.000001;
|
1602
|
+
}
|
1603
|
+
|
1604
|
+
/**
|
1605
|
+
* Creates a new identity mat3
|
1606
|
+
*
|
1607
|
+
* @returns {mat3} a new 3x3 matrix
|
1608
|
+
*/
|
1609
|
+
mat3.create = function() {
|
1610
|
+
return new Float32Array(mat3Identity);
|
1611
|
+
};
|
1612
|
+
|
1613
|
+
/**
|
1614
|
+
* Creates a new mat3 initialized with values from an existing matrix
|
1615
|
+
*
|
1616
|
+
* @param {mat3} a matrix to clone
|
1617
|
+
* @returns {mat3} a new 3x3 matrix
|
1618
|
+
*/
|
1619
|
+
mat3.clone = function(a) {
|
1620
|
+
var out = new Float32Array(9);
|
1621
|
+
out[0] = a[0];
|
1622
|
+
out[1] = a[1];
|
1623
|
+
out[2] = a[2];
|
1624
|
+
out[3] = a[3];
|
1625
|
+
out[4] = a[4];
|
1626
|
+
out[5] = a[5];
|
1627
|
+
out[6] = a[6];
|
1628
|
+
out[7] = a[7];
|
1629
|
+
out[8] = a[8];
|
1630
|
+
return out;
|
1631
|
+
};
|
1632
|
+
|
1633
|
+
/**
|
1634
|
+
* Copy the values from one mat3 to another
|
1635
|
+
*
|
1636
|
+
* @param {mat3} out the receiving matrix
|
1637
|
+
* @param {mat3} a the source matrix
|
1638
|
+
* @returns {mat3} out
|
1639
|
+
*/
|
1640
|
+
mat3.copy = function(out, a) {
|
1641
|
+
out[0] = a[0];
|
1642
|
+
out[1] = a[1];
|
1643
|
+
out[2] = a[2];
|
1644
|
+
out[3] = a[3];
|
1645
|
+
out[4] = a[4];
|
1646
|
+
out[5] = a[5];
|
1647
|
+
out[6] = a[6];
|
1648
|
+
out[7] = a[7];
|
1649
|
+
out[8] = a[8];
|
1650
|
+
return out;
|
1651
|
+
};
|
1652
|
+
|
1653
|
+
/**
|
1654
|
+
* Set a mat3 to the identity matrix
|
1655
|
+
*
|
1656
|
+
* @param {mat3} out the receiving matrix
|
1657
|
+
* @returns {mat3} out
|
1658
|
+
*/
|
1659
|
+
mat3.identity = function(out) {
|
1660
|
+
out[0] = 1;
|
1661
|
+
out[1] = 0;
|
1662
|
+
out[2] = 0;
|
1663
|
+
out[3] = 0;
|
1664
|
+
out[4] = 1;
|
1665
|
+
out[5] = 0;
|
1666
|
+
out[6] = 0;
|
1667
|
+
out[7] = 0;
|
1668
|
+
out[8] = 1;
|
1669
|
+
return out;
|
1670
|
+
};
|
1671
|
+
|
1672
|
+
/**
|
1673
|
+
* Transpose the values of a mat3
|
1674
|
+
*
|
1675
|
+
* @param {mat3} out the receiving matrix
|
1676
|
+
* @param {mat3} a the source matrix
|
1677
|
+
* @returns {mat3} out
|
1678
|
+
*/
|
1679
|
+
mat3.transpose = function(out, a) {
|
1680
|
+
// If we are transposing ourselves we can skip a few steps but have to cache some values
|
1681
|
+
if (out === a) {
|
1682
|
+
var a01 = a[1], a02 = a[2], a12 = a[5];
|
1683
|
+
out[1] = a[3];
|
1684
|
+
out[2] = a[6];
|
1685
|
+
out[3] = a01;
|
1686
|
+
out[5] = a[7];
|
1687
|
+
out[6] = a02;
|
1688
|
+
out[7] = a12;
|
1689
|
+
} else {
|
1690
|
+
out[0] = a[0];
|
1691
|
+
out[1] = a[3];
|
1692
|
+
out[2] = a[6];
|
1693
|
+
out[3] = a[1];
|
1694
|
+
out[4] = a[4];
|
1695
|
+
out[5] = a[7];
|
1696
|
+
out[6] = a[2];
|
1697
|
+
out[7] = a[5];
|
1698
|
+
out[8] = a[8];
|
1699
|
+
}
|
1700
|
+
|
1701
|
+
return out;
|
1702
|
+
};
|
1703
|
+
|
1704
|
+
/**
|
1705
|
+
* Inverts a mat3
|
1706
|
+
*
|
1707
|
+
* @param {mat3} out the receiving matrix
|
1708
|
+
* @param {mat3} a the source matrix
|
1709
|
+
* @returns {mat3} out
|
1710
|
+
*/
|
1711
|
+
mat3.invert = function(out, a) {
|
1712
|
+
var a00 = a[0], a01 = a[1], a02 = a[2],
|
1713
|
+
a10 = a[3], a11 = a[4], a12 = a[5],
|
1714
|
+
a20 = a[6], a21 = a[7], a22 = a[8],
|
1715
|
+
|
1716
|
+
b01 = a22 * a11 - a12 * a21,
|
1717
|
+
b11 = -a22 * a10 + a12 * a20,
|
1718
|
+
b21 = a21 * a10 - a11 * a20,
|
1719
|
+
|
1720
|
+
// Calculate the determinant
|
1721
|
+
det = a00 * b01 + a01 * b11 + a02 * b21;
|
1722
|
+
|
1723
|
+
if (!det) {
|
1724
|
+
return null;
|
1725
|
+
}
|
1726
|
+
det = 1.0 / det;
|
1727
|
+
|
1728
|
+
out[0] = b01 * det;
|
1729
|
+
out[1] = (-a22 * a01 + a02 * a21) * det;
|
1730
|
+
out[2] = (a12 * a01 - a02 * a11) * det;
|
1731
|
+
out[3] = b11 * det;
|
1732
|
+
out[4] = (a22 * a00 - a02 * a20) * det;
|
1733
|
+
out[5] = (-a12 * a00 + a02 * a10) * det;
|
1734
|
+
out[6] = b21 * det;
|
1735
|
+
out[7] = (-a21 * a00 + a01 * a20) * det;
|
1736
|
+
out[8] = (a11 * a00 - a01 * a10) * det;
|
1737
|
+
return out;
|
1738
|
+
};
|
1739
|
+
|
1740
|
+
/**
|
1741
|
+
* Caclulates the adjugate of a mat3
|
1742
|
+
*
|
1743
|
+
* @param {mat3} out the receiving matrix
|
1744
|
+
* @param {mat3} a the source matrix
|
1745
|
+
* @returns {mat3} out
|
1746
|
+
*/
|
1747
|
+
mat3.adjoint = function(out, a) {
|
1748
|
+
var a00 = a[0], a01 = a[1], a02 = a[2],
|
1749
|
+
a10 = a[3], a11 = a[4], a12 = a[5],
|
1750
|
+
a20 = a[6], a21 = a[7], a22 = a[8];
|
1751
|
+
|
1752
|
+
out[0] = (a11 * a22 - a12 * a21);
|
1753
|
+
out[1] = (a02 * a21 - a01 * a22);
|
1754
|
+
out[2] = (a01 * a12 - a02 * a11);
|
1755
|
+
out[3] = (a12 * a20 - a10 * a22);
|
1756
|
+
out[4] = (a00 * a22 - a02 * a20);
|
1757
|
+
out[5] = (a02 * a10 - a00 * a12);
|
1758
|
+
out[6] = (a10 * a21 - a11 * a20);
|
1759
|
+
out[7] = (a01 * a20 - a00 * a21);
|
1760
|
+
out[8] = (a00 * a11 - a01 * a10);
|
1761
|
+
return out;
|
1762
|
+
};
|
1763
|
+
|
1764
|
+
/**
|
1765
|
+
* Calculates the determinant of a mat3
|
1766
|
+
*
|
1767
|
+
* @param {mat3} a the source matrix
|
1768
|
+
* @returns {Number} determinant of a
|
1769
|
+
*/
|
1770
|
+
mat3.determinant = function (a) {
|
1771
|
+
var a00 = a[0], a01 = a[1], a02 = a[2],
|
1772
|
+
a10 = a[3], a11 = a[4], a12 = a[5],
|
1773
|
+
a20 = a[6], a21 = a[7], a22 = a[8];
|
1774
|
+
|
1775
|
+
return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
|
1776
|
+
};
|
1777
|
+
|
1778
|
+
/**
|
1779
|
+
* Multiplies two mat3's
|
1780
|
+
*
|
1781
|
+
* @param {mat3} out the receiving matrix
|
1782
|
+
* @param {mat3} a the first operand
|
1783
|
+
* @param {mat3} b the second operand
|
1784
|
+
* @returns {mat3} out
|
1785
|
+
*/
|
1786
|
+
mat3.mul = mat3.multiply = function (out, a, b) {
|
1787
|
+
var a00 = a[0], a01 = a[1], a02 = a[2],
|
1788
|
+
a10 = a[3], a11 = a[4], a12 = a[5],
|
1789
|
+
a20 = a[6], a21 = a[7], a22 = a[8],
|
1790
|
+
|
1791
|
+
b00 = b[0], b01 = b[1], b02 = b[2],
|
1792
|
+
b10 = b[3], b11 = b[4], b12 = b[5],
|
1793
|
+
b20 = b[6], b21 = b[7], b22 = b[8];
|
1794
|
+
|
1795
|
+
out[0] = b00 * a00 + b01 * a10 + b02 * a20;
|
1796
|
+
out[1] = b00 * a01 + b01 * a11 + b02 * a21;
|
1797
|
+
out[2] = b00 * a02 + b01 * a12 + b02 * a22;
|
1798
|
+
|
1799
|
+
out[3] = b10 * a00 + b11 * a10 + b12 * a20;
|
1800
|
+
out[4] = b10 * a01 + b11 * a11 + b12 * a21;
|
1801
|
+
out[5] = b10 * a02 + b11 * a12 + b12 * a22;
|
1802
|
+
|
1803
|
+
out[6] = b20 * a00 + b21 * a10 + b22 * a20;
|
1804
|
+
out[7] = b20 * a01 + b21 * a11 + b22 * a21;
|
1805
|
+
out[8] = b20 * a02 + b21 * a12 + b22 * a22;
|
1806
|
+
return out;
|
1807
|
+
};
|
1808
|
+
|
1809
|
+
/**
|
1810
|
+
* Returns a string representation of a mat3
|
1811
|
+
*
|
1812
|
+
* @param {mat3} mat matrix to represent as a string
|
1813
|
+
* @returns {String} string representation of the matrix
|
1814
|
+
*/
|
1815
|
+
mat3.str = function (a) {
|
1816
|
+
return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' +
|
1817
|
+
a[3] + ', ' + a[4] + ', ' + a[5] + ', ' +
|
1818
|
+
a[6] + ', ' + a[7] + ', ' + a[8] + ')';
|
1819
|
+
};
|
1820
|
+
|
1821
|
+
if(typeof(exports) !== 'undefined') {
|
1822
|
+
exports.mat3 = mat3;
|
1823
|
+
}
|
1824
|
+
;
|
1825
|
+
/* Copyright (c) 2012, Brandon Jones, Colin MacKenzie IV. All rights reserved.
|
1826
|
+
|
1827
|
+
Redistribution and use in source and binary forms, with or without modification,
|
1828
|
+
are permitted provided that the following conditions are met:
|
1829
|
+
|
1830
|
+
* Redistributions of source code must retain the above copyright notice, this
|
1831
|
+
list of conditions and the following disclaimer.
|
1832
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
1833
|
+
this list of conditions and the following disclaimer in the documentation
|
1834
|
+
and/or other materials provided with the distribution.
|
1835
|
+
|
1836
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
1837
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
1838
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
1839
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
1840
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
1841
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
1842
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
1843
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
1844
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
1845
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
1846
|
+
|
1847
|
+
/**
|
1848
|
+
* @class 4x4 Matrix
|
1849
|
+
* @name mat4
|
1850
|
+
*/
|
1851
|
+
|
1852
|
+
var mat4 = {};
|
1853
|
+
|
1854
|
+
var mat4Identity = new Float32Array([
|
1855
|
+
1, 0, 0, 0,
|
1856
|
+
0, 1, 0, 0,
|
1857
|
+
0, 0, 1, 0,
|
1858
|
+
0, 0, 0, 1
|
1859
|
+
]);
|
1860
|
+
|
1861
|
+
if(!GLMAT_EPSILON) {
|
1862
|
+
var GLMAT_EPSILON = 0.000001;
|
1863
|
+
}
|
1864
|
+
|
1865
|
+
/**
|
1866
|
+
* Creates a new identity mat4
|
1867
|
+
*
|
1868
|
+
* @returns {mat4} a new 4x4 matrix
|
1869
|
+
*/
|
1870
|
+
mat4.create = function() {
|
1871
|
+
return new Float32Array(mat4Identity);
|
1872
|
+
};
|
1873
|
+
|
1874
|
+
/**
|
1875
|
+
* Creates a new mat4 initialized with values from an existing matrix
|
1876
|
+
*
|
1877
|
+
* @param {mat4} a matrix to clone
|
1878
|
+
* @returns {mat4} a new 4x4 matrix
|
1879
|
+
*/
|
1880
|
+
mat4.clone = function(a) {
|
1881
|
+
var out = new Float32Array(16);
|
1882
|
+
out[0] = a[0];
|
1883
|
+
out[1] = a[1];
|
1884
|
+
out[2] = a[2];
|
1885
|
+
out[3] = a[3];
|
1886
|
+
out[4] = a[4];
|
1887
|
+
out[5] = a[5];
|
1888
|
+
out[6] = a[6];
|
1889
|
+
out[7] = a[7];
|
1890
|
+
out[8] = a[8];
|
1891
|
+
out[9] = a[9];
|
1892
|
+
out[10] = a[10];
|
1893
|
+
out[11] = a[11];
|
1894
|
+
out[12] = a[12];
|
1895
|
+
out[13] = a[13];
|
1896
|
+
out[14] = a[14];
|
1897
|
+
out[15] = a[15];
|
1898
|
+
return out;
|
1899
|
+
};
|
1900
|
+
|
1901
|
+
/**
|
1902
|
+
* Copy the values from one mat4 to another
|
1903
|
+
*
|
1904
|
+
* @param {mat4} out the receiving matrix
|
1905
|
+
* @param {mat4} a the source matrix
|
1906
|
+
* @returns {mat4} out
|
1907
|
+
*/
|
1908
|
+
mat4.copy = function(out, a) {
|
1909
|
+
out[0] = a[0];
|
1910
|
+
out[1] = a[1];
|
1911
|
+
out[2] = a[2];
|
1912
|
+
out[3] = a[3];
|
1913
|
+
out[4] = a[4];
|
1914
|
+
out[5] = a[5];
|
1915
|
+
out[6] = a[6];
|
1916
|
+
out[7] = a[7];
|
1917
|
+
out[8] = a[8];
|
1918
|
+
out[9] = a[9];
|
1919
|
+
out[10] = a[10];
|
1920
|
+
out[11] = a[11];
|
1921
|
+
out[12] = a[12];
|
1922
|
+
out[13] = a[13];
|
1923
|
+
out[14] = a[14];
|
1924
|
+
out[15] = a[15];
|
1925
|
+
return out;
|
1926
|
+
};
|
1927
|
+
|
1928
|
+
/**
|
1929
|
+
* Set a mat4 to the identity matrix
|
1930
|
+
*
|
1931
|
+
* @param {mat4} out the receiving matrix
|
1932
|
+
* @returns {mat4} out
|
1933
|
+
*/
|
1934
|
+
mat4.identity = function(out) {
|
1935
|
+
out[0] = 1;
|
1936
|
+
out[1] = 0;
|
1937
|
+
out[2] = 0;
|
1938
|
+
out[3] = 0;
|
1939
|
+
out[4] = 0;
|
1940
|
+
out[5] = 1;
|
1941
|
+
out[6] = 0;
|
1942
|
+
out[7] = 0;
|
1943
|
+
out[8] = 0;
|
1944
|
+
out[9] = 0;
|
1945
|
+
out[10] = 1;
|
1946
|
+
out[11] = 0;
|
1947
|
+
out[12] = 0;
|
1948
|
+
out[13] = 0;
|
1949
|
+
out[14] = 0;
|
1950
|
+
out[15] = 1;
|
1951
|
+
return out;
|
1952
|
+
};
|
1953
|
+
|
1954
|
+
/**
|
1955
|
+
* Transpose the values of a mat4
|
1956
|
+
*
|
1957
|
+
* @param {mat4} out the receiving matrix
|
1958
|
+
* @param {mat4} a the source matrix
|
1959
|
+
* @returns {mat4} out
|
1960
|
+
*/
|
1961
|
+
mat4.transpose = function(out, a) {
|
1962
|
+
// If we are transposing ourselves we can skip a few steps but have to cache some values
|
1963
|
+
if (out === a) {
|
1964
|
+
var a01 = a[1], a02 = a[2], a03 = a[3],
|
1965
|
+
a12 = a[6], a13 = a[7],
|
1966
|
+
a23 = a[11];
|
1967
|
+
|
1968
|
+
out[1] = a[4];
|
1969
|
+
out[2] = a[8];
|
1970
|
+
out[3] = a[12];
|
1971
|
+
out[4] = a01;
|
1972
|
+
out[6] = a[9];
|
1973
|
+
out[7] = a[13];
|
1974
|
+
out[8] = a02;
|
1975
|
+
out[9] = a12;
|
1976
|
+
out[11] = a[14];
|
1977
|
+
out[12] = a03;
|
1978
|
+
out[13] = a13;
|
1979
|
+
out[14] = a23;
|
1980
|
+
} else {
|
1981
|
+
out[0] = a[0];
|
1982
|
+
out[1] = a[4];
|
1983
|
+
out[2] = a[8];
|
1984
|
+
out[3] = a[12];
|
1985
|
+
out[4] = a[1];
|
1986
|
+
out[5] = a[5];
|
1987
|
+
out[6] = a[9];
|
1988
|
+
out[7] = a[13];
|
1989
|
+
out[8] = a[2];
|
1990
|
+
out[9] = a[6];
|
1991
|
+
out[10] = a[10];
|
1992
|
+
out[11] = a[14];
|
1993
|
+
out[12] = a[3];
|
1994
|
+
out[13] = a[7];
|
1995
|
+
out[14] = a[11];
|
1996
|
+
out[15] = a[15];
|
1997
|
+
}
|
1998
|
+
|
1999
|
+
return out;
|
2000
|
+
};
|
2001
|
+
|
2002
|
+
/**
|
2003
|
+
* Inverts a mat4
|
2004
|
+
*
|
2005
|
+
* @param {mat4} out the receiving matrix
|
2006
|
+
* @param {mat4} a the source matrix
|
2007
|
+
* @returns {mat4} out
|
2008
|
+
*/
|
2009
|
+
mat4.invert = function(out, a) {
|
2010
|
+
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
|
2011
|
+
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
|
2012
|
+
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
|
2013
|
+
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
|
2014
|
+
|
2015
|
+
b00 = a00 * a11 - a01 * a10,
|
2016
|
+
b01 = a00 * a12 - a02 * a10,
|
2017
|
+
b02 = a00 * a13 - a03 * a10,
|
2018
|
+
b03 = a01 * a12 - a02 * a11,
|
2019
|
+
b04 = a01 * a13 - a03 * a11,
|
2020
|
+
b05 = a02 * a13 - a03 * a12,
|
2021
|
+
b06 = a20 * a31 - a21 * a30,
|
2022
|
+
b07 = a20 * a32 - a22 * a30,
|
2023
|
+
b08 = a20 * a33 - a23 * a30,
|
2024
|
+
b09 = a21 * a32 - a22 * a31,
|
2025
|
+
b10 = a21 * a33 - a23 * a31,
|
2026
|
+
b11 = a22 * a33 - a23 * a32,
|
2027
|
+
|
2028
|
+
// Calculate the determinant
|
2029
|
+
det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
|
2030
|
+
|
2031
|
+
if (!det) {
|
2032
|
+
return null;
|
2033
|
+
}
|
2034
|
+
det = 1.0 / det;
|
2035
|
+
|
2036
|
+
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
|
2037
|
+
out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
|
2038
|
+
out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
|
2039
|
+
out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
|
2040
|
+
out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
|
2041
|
+
out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
|
2042
|
+
out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
|
2043
|
+
out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
|
2044
|
+
out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
|
2045
|
+
out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
|
2046
|
+
out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
|
2047
|
+
out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
|
2048
|
+
out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
|
2049
|
+
out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
|
2050
|
+
out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
|
2051
|
+
out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
|
2052
|
+
|
2053
|
+
return out;
|
2054
|
+
};
|
2055
|
+
|
2056
|
+
/**
|
2057
|
+
* Caclulates the adjugate of a mat4
|
2058
|
+
*
|
2059
|
+
* @param {mat4} out the receiving matrix
|
2060
|
+
* @param {mat4} a the source matrix
|
2061
|
+
* @returns {mat4} out
|
2062
|
+
*/
|
2063
|
+
mat4.adjoint = function(out, a) {
|
2064
|
+
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
|
2065
|
+
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
|
2066
|
+
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
|
2067
|
+
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
|
2068
|
+
|
2069
|
+
out[0] = (a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22));
|
2070
|
+
out[1] = -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22));
|
2071
|
+
out[2] = (a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12));
|
2072
|
+
out[3] = -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12));
|
2073
|
+
out[4] = -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22));
|
2074
|
+
out[5] = (a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22));
|
2075
|
+
out[6] = -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12));
|
2076
|
+
out[7] = (a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12));
|
2077
|
+
out[8] = (a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21));
|
2078
|
+
out[9] = -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21));
|
2079
|
+
out[10] = (a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11));
|
2080
|
+
out[11] = -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11));
|
2081
|
+
out[12] = -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21));
|
2082
|
+
out[13] = (a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21));
|
2083
|
+
out[14] = -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11));
|
2084
|
+
out[15] = (a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11));
|
2085
|
+
return out;
|
2086
|
+
};
|
2087
|
+
|
2088
|
+
/**
|
2089
|
+
* Calculates the determinant of a mat4
|
2090
|
+
*
|
2091
|
+
* @param {mat4} a the source matrix
|
2092
|
+
* @returns {Number} determinant of a
|
2093
|
+
*/
|
2094
|
+
mat4.determinant = function (a) {
|
2095
|
+
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
|
2096
|
+
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
|
2097
|
+
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
|
2098
|
+
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15],
|
2099
|
+
|
2100
|
+
b00 = a00 * a11 - a01 * a10,
|
2101
|
+
b01 = a00 * a12 - a02 * a10,
|
2102
|
+
b02 = a00 * a13 - a03 * a10,
|
2103
|
+
b03 = a01 * a12 - a02 * a11,
|
2104
|
+
b04 = a01 * a13 - a03 * a11,
|
2105
|
+
b05 = a02 * a13 - a03 * a12,
|
2106
|
+
b06 = a20 * a31 - a21 * a30,
|
2107
|
+
b07 = a20 * a32 - a22 * a30,
|
2108
|
+
b08 = a20 * a33 - a23 * a30,
|
2109
|
+
b09 = a21 * a32 - a22 * a31,
|
2110
|
+
b10 = a21 * a33 - a23 * a31,
|
2111
|
+
b11 = a22 * a33 - a23 * a32;
|
2112
|
+
|
2113
|
+
// Calculate the determinant
|
2114
|
+
return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
|
2115
|
+
};
|
2116
|
+
|
2117
|
+
/**
|
2118
|
+
* Multiplies two mat4's
|
2119
|
+
*
|
2120
|
+
* @param {mat4} out the receiving matrix
|
2121
|
+
* @param {mat4} a the first operand
|
2122
|
+
* @param {mat4} b the second operand
|
2123
|
+
* @returns {mat4} out
|
2124
|
+
*/
|
2125
|
+
mat4.mul = mat4.multiply = function (out, a, b) {
|
2126
|
+
var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3],
|
2127
|
+
a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7],
|
2128
|
+
a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11],
|
2129
|
+
a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
|
2130
|
+
|
2131
|
+
// Cache only the current line of the second matrix
|
2132
|
+
var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
|
2133
|
+
out[0] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
|
2134
|
+
out[1] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
|
2135
|
+
out[2] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
|
2136
|
+
out[3] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
|
2137
|
+
|
2138
|
+
b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7];
|
2139
|
+
out[4] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
|
2140
|
+
out[5] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
|
2141
|
+
out[6] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
|
2142
|
+
out[7] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
|
2143
|
+
|
2144
|
+
b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11];
|
2145
|
+
out[8] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
|
2146
|
+
out[9] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
|
2147
|
+
out[10] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
|
2148
|
+
out[11] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
|
2149
|
+
|
2150
|
+
b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15];
|
2151
|
+
out[12] = b0*a00 + b1*a10 + b2*a20 + b3*a30;
|
2152
|
+
out[13] = b0*a01 + b1*a11 + b2*a21 + b3*a31;
|
2153
|
+
out[14] = b0*a02 + b1*a12 + b2*a22 + b3*a32;
|
2154
|
+
out[15] = b0*a03 + b1*a13 + b2*a23 + b3*a33;
|
2155
|
+
return out;
|
2156
|
+
};
|
2157
|
+
|
2158
|
+
/**
|
2159
|
+
* Translate a mat4 by the given vector
|
2160
|
+
*
|
2161
|
+
* @param {mat4} out the receiving matrix
|
2162
|
+
* @param {mat4} a the matrix to translate
|
2163
|
+
* @param {vec3} v vector to translate by
|
2164
|
+
* @returns {mat4} out
|
2165
|
+
*/
|
2166
|
+
mat4.translate = function (out, a, v) {
|
2167
|
+
var x = v[0], y = v[1], z = v[2],
|
2168
|
+
a00, a01, a02, a03,
|
2169
|
+
a10, a11, a12, a13,
|
2170
|
+
a20, a21, a22, a23;
|
2171
|
+
|
2172
|
+
if (a === out) {
|
2173
|
+
out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
|
2174
|
+
out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
|
2175
|
+
out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
|
2176
|
+
out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
|
2177
|
+
} else {
|
2178
|
+
a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
|
2179
|
+
a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
|
2180
|
+
a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
|
2181
|
+
|
2182
|
+
out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03;
|
2183
|
+
out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13;
|
2184
|
+
out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23;
|
2185
|
+
|
2186
|
+
out[12] = a00 * x + a10 * y + a20 * z + a[12];
|
2187
|
+
out[13] = a01 * x + a11 * y + a21 * z + a[13];
|
2188
|
+
out[14] = a02 * x + a12 * y + a22 * z + a[14];
|
2189
|
+
out[15] = a03 * x + a13 * y + a23 * z + a[15];
|
2190
|
+
}
|
2191
|
+
|
2192
|
+
return out;
|
2193
|
+
};
|
2194
|
+
|
2195
|
+
/**
|
2196
|
+
* Scales the mat4 by the dimensions in the given vec3
|
2197
|
+
*
|
2198
|
+
* @param {mat4} out the receiving matrix
|
2199
|
+
* @param {mat4} a the matrix to scale
|
2200
|
+
* @param {vec3} v the vec3 to scale the matrix by
|
2201
|
+
* @returns {mat4} out
|
2202
|
+
**/
|
2203
|
+
mat4.scale = function(out, a, v) {
|
2204
|
+
var x = v[0], y = v[1], z = v[2];
|
2205
|
+
|
2206
|
+
out[0] = a[0] * x;
|
2207
|
+
out[1] = a[1] * x;
|
2208
|
+
out[2] = a[2] * x;
|
2209
|
+
out[3] = a[3] * x;
|
2210
|
+
out[4] = a[4] * y;
|
2211
|
+
out[5] = a[5] * y;
|
2212
|
+
out[6] = a[6] * y;
|
2213
|
+
out[7] = a[7] * y;
|
2214
|
+
out[8] = a[8] * z;
|
2215
|
+
out[9] = a[9] * z;
|
2216
|
+
out[10] = a[10] * z;
|
2217
|
+
out[11] = a[11] * z;
|
2218
|
+
out[12] = a[12];
|
2219
|
+
out[13] = a[13];
|
2220
|
+
out[14] = a[14];
|
2221
|
+
out[15] = a[15];
|
2222
|
+
return out;
|
2223
|
+
};
|
2224
|
+
|
2225
|
+
/**
|
2226
|
+
* Rotates a mat4 by the given angle
|
2227
|
+
*
|
2228
|
+
* @param {mat4} out the receiving matrix
|
2229
|
+
* @param {mat4} a the matrix to rotate
|
2230
|
+
* @param {Number} rad the angle to rotate the matrix by
|
2231
|
+
* @param {vec3} axis the axis to rotate around
|
2232
|
+
* @returns {mat4} out
|
2233
|
+
*/
|
2234
|
+
mat4.rotate = function (out, a, rad, axis) {
|
2235
|
+
var x = axis[0], y = axis[1], z = axis[2],
|
2236
|
+
len = Math.sqrt(x * x + y * y + z * z),
|
2237
|
+
s, c, t,
|
2238
|
+
a00, a01, a02, a03,
|
2239
|
+
a10, a11, a12, a13,
|
2240
|
+
a20, a21, a22, a23,
|
2241
|
+
b00, b01, b02,
|
2242
|
+
b10, b11, b12,
|
2243
|
+
b20, b21, b22;
|
2244
|
+
|
2245
|
+
if (Math.abs(len) < GLMAT_EPSILON) { return null; }
|
2246
|
+
|
2247
|
+
len = 1 / len;
|
2248
|
+
x *= len;
|
2249
|
+
y *= len;
|
2250
|
+
z *= len;
|
2251
|
+
|
2252
|
+
s = Math.sin(rad);
|
2253
|
+
c = Math.cos(rad);
|
2254
|
+
t = 1 - c;
|
2255
|
+
|
2256
|
+
a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3];
|
2257
|
+
a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7];
|
2258
|
+
a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11];
|
2259
|
+
|
2260
|
+
// Construct the elements of the rotation matrix
|
2261
|
+
b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s;
|
2262
|
+
b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s;
|
2263
|
+
b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c;
|
2264
|
+
|
2265
|
+
// Perform rotation-specific matrix multiplication
|
2266
|
+
out[0] = a00 * b00 + a10 * b01 + a20 * b02;
|
2267
|
+
out[1] = a01 * b00 + a11 * b01 + a21 * b02;
|
2268
|
+
out[2] = a02 * b00 + a12 * b01 + a22 * b02;
|
2269
|
+
out[3] = a03 * b00 + a13 * b01 + a23 * b02;
|
2270
|
+
out[4] = a00 * b10 + a10 * b11 + a20 * b12;
|
2271
|
+
out[5] = a01 * b10 + a11 * b11 + a21 * b12;
|
2272
|
+
out[6] = a02 * b10 + a12 * b11 + a22 * b12;
|
2273
|
+
out[7] = a03 * b10 + a13 * b11 + a23 * b12;
|
2274
|
+
out[8] = a00 * b20 + a10 * b21 + a20 * b22;
|
2275
|
+
out[9] = a01 * b20 + a11 * b21 + a21 * b22;
|
2276
|
+
out[10] = a02 * b20 + a12 * b21 + a22 * b22;
|
2277
|
+
out[11] = a03 * b20 + a13 * b21 + a23 * b22;
|
2278
|
+
|
2279
|
+
if (a !== out) { // If the source and destination differ, copy the unchanged last row
|
2280
|
+
out[12] = a[12];
|
2281
|
+
out[13] = a[13];
|
2282
|
+
out[14] = a[14];
|
2283
|
+
out[15] = a[15];
|
2284
|
+
}
|
2285
|
+
return out;
|
2286
|
+
};
|
2287
|
+
|
2288
|
+
/**
|
2289
|
+
* Rotates a matrix by the given angle around the X axis
|
2290
|
+
*
|
2291
|
+
* @param {mat4} out the receiving matrix
|
2292
|
+
* @param {mat4} a the matrix to rotate
|
2293
|
+
* @param {Number} rad the angle to rotate the matrix by
|
2294
|
+
* @returns {mat4} out
|
2295
|
+
*/
|
2296
|
+
mat4.rotateX = function (out, a, rad) {
|
2297
|
+
var s = Math.sin(rad),
|
2298
|
+
c = Math.cos(rad),
|
2299
|
+
a10 = a[4],
|
2300
|
+
a11 = a[5],
|
2301
|
+
a12 = a[6],
|
2302
|
+
a13 = a[7],
|
2303
|
+
a20 = a[8],
|
2304
|
+
a21 = a[9],
|
2305
|
+
a22 = a[10],
|
2306
|
+
a23 = a[11];
|
2307
|
+
|
2308
|
+
if (a !== out) { // If the source and destination differ, copy the unchanged rows
|
2309
|
+
out[0] = a[0];
|
2310
|
+
out[1] = a[1];
|
2311
|
+
out[2] = a[2];
|
2312
|
+
out[3] = a[3];
|
2313
|
+
out[12] = a[12];
|
2314
|
+
out[13] = a[13];
|
2315
|
+
out[14] = a[14];
|
2316
|
+
out[15] = a[15];
|
2317
|
+
}
|
2318
|
+
|
2319
|
+
// Perform axis-specific matrix multiplication
|
2320
|
+
out[4] = a10 * c + a20 * s;
|
2321
|
+
out[5] = a11 * c + a21 * s;
|
2322
|
+
out[6] = a12 * c + a22 * s;
|
2323
|
+
out[7] = a13 * c + a23 * s;
|
2324
|
+
out[8] = a20 * c - a10 * s;
|
2325
|
+
out[9] = a21 * c - a11 * s;
|
2326
|
+
out[10] = a22 * c - a12 * s;
|
2327
|
+
out[11] = a23 * c - a13 * s;
|
2328
|
+
return out;
|
2329
|
+
};
|
2330
|
+
|
2331
|
+
/**
|
2332
|
+
* Rotates a matrix by the given angle around the Y axis
|
2333
|
+
*
|
2334
|
+
* @param {mat4} out the receiving matrix
|
2335
|
+
* @param {mat4} a the matrix to rotate
|
2336
|
+
* @param {Number} rad the angle to rotate the matrix by
|
2337
|
+
* @returns {mat4} out
|
2338
|
+
*/
|
2339
|
+
mat4.rotateY = function (out, a, rad) {
|
2340
|
+
var s = Math.sin(rad),
|
2341
|
+
c = Math.cos(rad),
|
2342
|
+
a00 = a[0],
|
2343
|
+
a01 = a[1],
|
2344
|
+
a02 = a[2],
|
2345
|
+
a03 = a[3],
|
2346
|
+
a20 = a[8],
|
2347
|
+
a21 = a[9],
|
2348
|
+
a22 = a[10],
|
2349
|
+
a23 = a[11];
|
2350
|
+
|
2351
|
+
if (a !== out) { // If the source and destination differ, copy the unchanged rows
|
2352
|
+
out[4] = a[4];
|
2353
|
+
out[5] = a[5];
|
2354
|
+
out[6] = a[6];
|
2355
|
+
out[7] = a[7];
|
2356
|
+
out[12] = a[12];
|
2357
|
+
out[13] = a[13];
|
2358
|
+
out[14] = a[14];
|
2359
|
+
out[15] = a[15];
|
2360
|
+
}
|
2361
|
+
|
2362
|
+
// Perform axis-specific matrix multiplication
|
2363
|
+
out[0] = a00 * c - a20 * s;
|
2364
|
+
out[1] = a01 * c - a21 * s;
|
2365
|
+
out[2] = a02 * c - a22 * s;
|
2366
|
+
out[3] = a03 * c - a23 * s;
|
2367
|
+
out[8] = a00 * s + a20 * c;
|
2368
|
+
out[9] = a01 * s + a21 * c;
|
2369
|
+
out[10] = a02 * s + a22 * c;
|
2370
|
+
out[11] = a03 * s + a23 * c;
|
2371
|
+
return out;
|
2372
|
+
};
|
2373
|
+
|
2374
|
+
/**
|
2375
|
+
* Rotates a matrix by the given angle around the Z axis
|
2376
|
+
*
|
2377
|
+
* @param {mat4} out the receiving matrix
|
2378
|
+
* @param {mat4} a the matrix to rotate
|
2379
|
+
* @param {Number} rad the angle to rotate the matrix by
|
2380
|
+
* @returns {mat4} out
|
2381
|
+
*/
|
2382
|
+
mat4.rotateZ = function (out, a, rad) {
|
2383
|
+
var s = Math.sin(rad),
|
2384
|
+
c = Math.cos(rad),
|
2385
|
+
a00 = a[0],
|
2386
|
+
a01 = a[1],
|
2387
|
+
a02 = a[2],
|
2388
|
+
a03 = a[3],
|
2389
|
+
a10 = a[4],
|
2390
|
+
a11 = a[5],
|
2391
|
+
a12 = a[6],
|
2392
|
+
a13 = a[7];
|
2393
|
+
|
2394
|
+
if (a !== out) { // If the source and destination differ, copy the unchanged last row
|
2395
|
+
out[8] = a[8];
|
2396
|
+
out[9] = a[9];
|
2397
|
+
out[10] = a[10];
|
2398
|
+
out[11] = a[11];
|
2399
|
+
out[12] = a[12];
|
2400
|
+
out[13] = a[13];
|
2401
|
+
out[14] = a[14];
|
2402
|
+
out[15] = a[15];
|
2403
|
+
}
|
2404
|
+
|
2405
|
+
// Perform axis-specific matrix multiplication
|
2406
|
+
out[0] = a00 * c + a10 * s;
|
2407
|
+
out[1] = a01 * c + a11 * s;
|
2408
|
+
out[2] = a02 * c + a12 * s;
|
2409
|
+
out[3] = a03 * c + a13 * s;
|
2410
|
+
out[4] = a10 * c - a00 * s;
|
2411
|
+
out[5] = a11 * c - a01 * s;
|
2412
|
+
out[6] = a12 * c - a02 * s;
|
2413
|
+
out[7] = a13 * c - a03 * s;
|
2414
|
+
return out;
|
2415
|
+
};
|
2416
|
+
|
2417
|
+
/**
|
2418
|
+
* Creates a matrix from a quaternion rotation and vector translation
|
2419
|
+
* This is equivalent to (but much faster than):
|
2420
|
+
*
|
2421
|
+
* mat4.identity(dest);
|
2422
|
+
* mat4.translate(dest, vec);
|
2423
|
+
* var quatMat = mat4.create();
|
2424
|
+
* quat4.toMat4(quat, quatMat);
|
2425
|
+
* mat4.multiply(dest, quatMat);
|
2426
|
+
*
|
2427
|
+
* @param {mat4} out mat4 receiving operation result
|
2428
|
+
* @param {quat4} q Rotation quaternion
|
2429
|
+
* @param {vec3} v Translation vector
|
2430
|
+
* @returns {mat4} out
|
2431
|
+
*/
|
2432
|
+
mat4.fromRotationTranslation = function (out, q, v) {
|
2433
|
+
// Quaternion math
|
2434
|
+
var x = q[0], y = q[1], z = q[2], w = q[3],
|
2435
|
+
x2 = x + x,
|
2436
|
+
y2 = y + y,
|
2437
|
+
z2 = z + z,
|
2438
|
+
|
2439
|
+
xx = x * x2,
|
2440
|
+
xy = x * y2,
|
2441
|
+
xz = x * z2,
|
2442
|
+
yy = y * y2,
|
2443
|
+
yz = y * z2,
|
2444
|
+
zz = z * z2,
|
2445
|
+
wx = w * x2,
|
2446
|
+
wy = w * y2,
|
2447
|
+
wz = w * z2;
|
2448
|
+
|
2449
|
+
out[0] = 1 - (yy + zz);
|
2450
|
+
out[1] = xy + wz;
|
2451
|
+
out[2] = xz - wy;
|
2452
|
+
out[3] = 0;
|
2453
|
+
out[4] = xy - wz;
|
2454
|
+
out[5] = 1 - (xx + zz);
|
2455
|
+
out[6] = yz + wx;
|
2456
|
+
out[7] = 0;
|
2457
|
+
out[8] = xz + wy;
|
2458
|
+
out[9] = yz - wx;
|
2459
|
+
out[10] = 1 - (xx + yy);
|
2460
|
+
out[11] = 0;
|
2461
|
+
out[12] = v[0];
|
2462
|
+
out[13] = v[1];
|
2463
|
+
out[14] = v[2];
|
2464
|
+
out[15] = 1;
|
2465
|
+
|
2466
|
+
return out;
|
2467
|
+
};
|
2468
|
+
|
2469
|
+
/**
|
2470
|
+
* Generates a frustum matrix with the given bounds
|
2471
|
+
*
|
2472
|
+
* @param {mat4} out mat4 frustum matrix will be written into
|
2473
|
+
* @param {Number} left Left bound of the frustum
|
2474
|
+
* @param {Number} right Right bound of the frustum
|
2475
|
+
* @param {Number} bottom Bottom bound of the frustum
|
2476
|
+
* @param {Number} top Top bound of the frustum
|
2477
|
+
* @param {Number} near Near bound of the frustum
|
2478
|
+
* @param {Number} far Far bound of the frustum
|
2479
|
+
* @returns {mat4} out
|
2480
|
+
*/
|
2481
|
+
mat4.frustum = function (out, left, right, bottom, top, near, far) {
|
2482
|
+
var rl = 1 / (right - left),
|
2483
|
+
tb = 1 / (top - bottom),
|
2484
|
+
nf = 1 / (near - far);
|
2485
|
+
out[0] = (near * 2) * rl;
|
2486
|
+
out[1] = 0;
|
2487
|
+
out[2] = 0;
|
2488
|
+
out[3] = 0;
|
2489
|
+
out[4] = 0;
|
2490
|
+
out[5] = (near * 2) * tb;
|
2491
|
+
out[6] = 0;
|
2492
|
+
out[7] = 0;
|
2493
|
+
out[8] = (right + left) * rl;
|
2494
|
+
out[9] = (top + bottom) * tb;
|
2495
|
+
out[10] = (far + near) * nf;
|
2496
|
+
out[11] = -1;
|
2497
|
+
out[12] = 0;
|
2498
|
+
out[13] = 0;
|
2499
|
+
out[14] = (far * near * 2) * nf;
|
2500
|
+
out[15] = 0;
|
2501
|
+
return out;
|
2502
|
+
};
|
2503
|
+
|
2504
|
+
/**
|
2505
|
+
* Generates a perspective projection matrix with the given bounds
|
2506
|
+
*
|
2507
|
+
* @param {mat4} out mat4 frustum matrix will be written into
|
2508
|
+
* @param {number} fovy Vertical field of view in radians
|
2509
|
+
* @param {number} aspect Aspect ratio. typically viewport width/height
|
2510
|
+
* @param {number} near Near bound of the frustum
|
2511
|
+
* @param {number} far Far bound of the frustum
|
2512
|
+
* @returns {mat4} out
|
2513
|
+
*/
|
2514
|
+
mat4.perspective = function (out, fovy, aspect, near, far) {
|
2515
|
+
var f = 1.0 / Math.tan(fovy / 2),
|
2516
|
+
nf = 1 / (near - far);
|
2517
|
+
out[0] = f / aspect;
|
2518
|
+
out[1] = 0;
|
2519
|
+
out[2] = 0;
|
2520
|
+
out[3] = 0;
|
2521
|
+
out[4] = 0;
|
2522
|
+
out[5] = f;
|
2523
|
+
out[6] = 0;
|
2524
|
+
out[7] = 0;
|
2525
|
+
out[8] = 0;
|
2526
|
+
out[9] = 0;
|
2527
|
+
out[10] = (far + near) * nf;
|
2528
|
+
out[11] = -1;
|
2529
|
+
out[12] = 0;
|
2530
|
+
out[13] = 0;
|
2531
|
+
out[14] = (2 * far * near) * nf;
|
2532
|
+
out[15] = 0;
|
2533
|
+
return out;
|
2534
|
+
};
|
2535
|
+
|
2536
|
+
/**
|
2537
|
+
* Generates a orthogonal projection matrix with the given bounds
|
2538
|
+
*
|
2539
|
+
* @param {mat4} out mat4 frustum matrix will be written into
|
2540
|
+
* @param {number} left Left bound of the frustum
|
2541
|
+
* @param {number} right Right bound of the frustum
|
2542
|
+
* @param {number} bottom Bottom bound of the frustum
|
2543
|
+
* @param {number} top Top bound of the frustum
|
2544
|
+
* @param {number} near Near bound of the frustum
|
2545
|
+
* @param {number} far Far bound of the frustum
|
2546
|
+
* @returns {mat4} out
|
2547
|
+
*/
|
2548
|
+
mat4.ortho = function (out, left, right, bottom, top, near, far) {
|
2549
|
+
var lr = 1 / (left - right),
|
2550
|
+
bt = 1 / (bottom - top),
|
2551
|
+
nf = 1 / (near - far);
|
2552
|
+
out[0] = -2 * lr;
|
2553
|
+
out[1] = 0;
|
2554
|
+
out[2] = 0;
|
2555
|
+
out[3] = 0;
|
2556
|
+
out[4] = 0;
|
2557
|
+
out[5] = -2 * bt;
|
2558
|
+
out[6] = 0;
|
2559
|
+
out[7] = 0;
|
2560
|
+
out[8] = 0;
|
2561
|
+
out[9] = 0;
|
2562
|
+
out[10] = 2 * nf;
|
2563
|
+
out[11] = 0;
|
2564
|
+
out[12] = (left + right) * lr;
|
2565
|
+
out[13] = (top + bottom) * bt;
|
2566
|
+
out[14] = (far + near) * nf;
|
2567
|
+
out[15] = 1;
|
2568
|
+
return out;
|
2569
|
+
};
|
2570
|
+
|
2571
|
+
/**
|
2572
|
+
* Generates a look-at matrix with the given eye position, focal point, and up axis
|
2573
|
+
*
|
2574
|
+
* @param {mat4} out mat4 frustum matrix will be written into
|
2575
|
+
* @param {vec3} eye Position of the viewer
|
2576
|
+
* @param {vec3} center Point the viewer is looking at
|
2577
|
+
* @param {vec3} up vec3 pointing up
|
2578
|
+
* @returns {mat4} out
|
2579
|
+
*/
|
2580
|
+
mat4.lookAt = function (out, eye, center, up) {
|
2581
|
+
var x0, x1, x2, y0, y1, y2, z0, z1, z2, len,
|
2582
|
+
eyex = eye[0],
|
2583
|
+
eyey = eye[1],
|
2584
|
+
eyez = eye[2],
|
2585
|
+
upx = up[0],
|
2586
|
+
upy = up[1],
|
2587
|
+
upz = up[2],
|
2588
|
+
centerx = center[0],
|
2589
|
+
centery = center[1],
|
2590
|
+
centerz = center[2];
|
2591
|
+
|
2592
|
+
if (Math.abs(eyex - centerx) < GLMAT_EPSILON &&
|
2593
|
+
Math.abs(eyey - centery) < GLMAT_EPSILON &&
|
2594
|
+
Math.abs(eyez - centerz) < GLMAT_EPSILON) {
|
2595
|
+
return mat4.identity(out);
|
2596
|
+
}
|
2597
|
+
|
2598
|
+
z0 = eyex - centerx;
|
2599
|
+
z1 = eyey - centery;
|
2600
|
+
z2 = eyez - centerz;
|
2601
|
+
|
2602
|
+
len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
|
2603
|
+
z0 *= len;
|
2604
|
+
z1 *= len;
|
2605
|
+
z2 *= len;
|
2606
|
+
|
2607
|
+
x0 = upy * z2 - upz * z1;
|
2608
|
+
x1 = upz * z0 - upx * z2;
|
2609
|
+
x2 = upx * z1 - upy * z0;
|
2610
|
+
len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
|
2611
|
+
if (!len) {
|
2612
|
+
x0 = 0;
|
2613
|
+
x1 = 0;
|
2614
|
+
x2 = 0;
|
2615
|
+
} else {
|
2616
|
+
len = 1 / len;
|
2617
|
+
x0 *= len;
|
2618
|
+
x1 *= len;
|
2619
|
+
x2 *= len;
|
2620
|
+
}
|
2621
|
+
|
2622
|
+
y0 = z1 * x2 - z2 * x1;
|
2623
|
+
y1 = z2 * x0 - z0 * x2;
|
2624
|
+
y2 = z0 * x1 - z1 * x0;
|
2625
|
+
|
2626
|
+
len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
|
2627
|
+
if (!len) {
|
2628
|
+
y0 = 0;
|
2629
|
+
y1 = 0;
|
2630
|
+
y2 = 0;
|
2631
|
+
} else {
|
2632
|
+
len = 1 / len;
|
2633
|
+
y0 *= len;
|
2634
|
+
y1 *= len;
|
2635
|
+
y2 *= len;
|
2636
|
+
}
|
2637
|
+
|
2638
|
+
out[0] = x0;
|
2639
|
+
out[1] = y0;
|
2640
|
+
out[2] = z0;
|
2641
|
+
out[3] = 0;
|
2642
|
+
out[4] = x1;
|
2643
|
+
out[5] = y1;
|
2644
|
+
out[6] = z1;
|
2645
|
+
out[7] = 0;
|
2646
|
+
out[8] = x2;
|
2647
|
+
out[9] = y2;
|
2648
|
+
out[10] = z2;
|
2649
|
+
out[11] = 0;
|
2650
|
+
out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
|
2651
|
+
out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
|
2652
|
+
out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
|
2653
|
+
out[15] = 1;
|
2654
|
+
|
2655
|
+
return out;
|
2656
|
+
};
|
2657
|
+
|
2658
|
+
/**
|
2659
|
+
* Returns a string representation of a mat4
|
2660
|
+
*
|
2661
|
+
* @param {mat4} mat matrix to represent as a string
|
2662
|
+
* @returns {String} string representation of the matrix
|
2663
|
+
*/
|
2664
|
+
mat4.str = function (a) {
|
2665
|
+
return 'mat4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' +
|
2666
|
+
a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ', ' +
|
2667
|
+
a[8] + ', ' + a[9] + ', ' + a[10] + ', ' + a[11] + ', ' +
|
2668
|
+
a[12] + ', ' + a[13] + ', ' + a[14] + ', ' + a[15] + ')';
|
2669
|
+
};
|
2670
|
+
|
2671
|
+
if(typeof(exports) !== 'undefined') {
|
2672
|
+
exports.mat4 = mat4;
|
2673
|
+
}
|
2674
|
+
;
|
2675
|
+
/* Copyright (c) 2012, Brandon Jones, Colin MacKenzie IV. All rights reserved.
|
2676
|
+
|
2677
|
+
Redistribution and use in source and binary forms, with or without modification,
|
2678
|
+
are permitted provided that the following conditions are met:
|
2679
|
+
|
2680
|
+
* Redistributions of source code must retain the above copyright notice, this
|
2681
|
+
list of conditions and the following disclaimer.
|
2682
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
2683
|
+
this list of conditions and the following disclaimer in the documentation
|
2684
|
+
and/or other materials provided with the distribution.
|
2685
|
+
|
2686
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
2687
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
2688
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
2689
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
2690
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
2691
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
2692
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
2693
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
2694
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
2695
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
|
2696
|
+
|
2697
|
+
/**
|
2698
|
+
* @class Quaternion
|
2699
|
+
* @name quat
|
2700
|
+
*/
|
2701
|
+
|
2702
|
+
var quat = {};
|
2703
|
+
|
2704
|
+
var quatIdentity = new Float32Array([0, 0, 0, 1]);
|
2705
|
+
|
2706
|
+
if(!GLMAT_EPSILON) {
|
2707
|
+
var GLMAT_EPSILON = 0.000001;
|
2708
|
+
}
|
2709
|
+
|
2710
|
+
/**
|
2711
|
+
* Creates a new identity quat
|
2712
|
+
*
|
2713
|
+
* @returns {quat} a new quaternion
|
2714
|
+
*/
|
2715
|
+
quat.create = function() {
|
2716
|
+
return new Float32Array(quatIdentity);
|
2717
|
+
};
|
2718
|
+
|
2719
|
+
/**
|
2720
|
+
* Creates a new quat initialized with values from an existing quaternion
|
2721
|
+
*
|
2722
|
+
* @param {quat} a quaternion to clone
|
2723
|
+
* @returns {quat} a new quaternion
|
2724
|
+
*/
|
2725
|
+
quat.clone = vec4.clone;
|
2726
|
+
|
2727
|
+
/**
|
2728
|
+
* Creates a new quat initialized with the given values
|
2729
|
+
*
|
2730
|
+
* @param {Number} x X component
|
2731
|
+
* @param {Number} y Y component
|
2732
|
+
* @param {Number} z Z component
|
2733
|
+
* @param {Number} w W component
|
2734
|
+
* @returns {quat} a new quaternion
|
2735
|
+
*/
|
2736
|
+
quat.fromValues = vec4.fromValues;
|
2737
|
+
|
2738
|
+
/**
|
2739
|
+
* Copy the values from one quat to another
|
2740
|
+
*
|
2741
|
+
* @param {quat} out the receiving quaternion
|
2742
|
+
* @param {quat} a the source quaternion
|
2743
|
+
* @returns {quat} out
|
2744
|
+
*/
|
2745
|
+
quat.copy = vec4.copy;
|
2746
|
+
|
2747
|
+
/**
|
2748
|
+
* Set the components of a quat to the given values
|
2749
|
+
*
|
2750
|
+
* @param {quat} out the receiving quaternion
|
2751
|
+
* @param {Number} x X component
|
2752
|
+
* @param {Number} y Y component
|
2753
|
+
* @param {Number} z Z component
|
2754
|
+
* @param {Number} w W component
|
2755
|
+
* @returns {quat} out
|
2756
|
+
*/
|
2757
|
+
quat.set = vec4.set;
|
2758
|
+
|
2759
|
+
/**
|
2760
|
+
* Set a quat to the identity quaternion
|
2761
|
+
*
|
2762
|
+
* @param {quat} out the receiving quaternion
|
2763
|
+
* @returns {quat} out
|
2764
|
+
*/
|
2765
|
+
quat.identity = function(out) {
|
2766
|
+
out[0] = 0;
|
2767
|
+
out[1] = 0;
|
2768
|
+
out[2] = 0;
|
2769
|
+
out[3] = 1;
|
2770
|
+
return out;
|
2771
|
+
};
|
2772
|
+
|
2773
|
+
/**
|
2774
|
+
* Sets a quat from the given angle and rotation axis,
|
2775
|
+
* then returns it.
|
2776
|
+
*
|
2777
|
+
* @param {quat} out the receiving quaternion
|
2778
|
+
* @param {vec3} axis the axis around which to rotate
|
2779
|
+
* @param {Number} rad the angle in radians
|
2780
|
+
* @returns {quat} out
|
2781
|
+
**/
|
2782
|
+
quat.setAxisAngle = function(out, axis, rad) {
|
2783
|
+
rad = rad * 0.5;
|
2784
|
+
var s = Math.sin(rad);
|
2785
|
+
out[0] = s * axis[0];
|
2786
|
+
out[1] = s * axis[1];
|
2787
|
+
out[2] = s * axis[2];
|
2788
|
+
out[3] = Math.cos(rad);
|
2789
|
+
return out;
|
2790
|
+
};
|
2791
|
+
|
2792
|
+
/**
|
2793
|
+
* Adds two quat's
|
2794
|
+
*
|
2795
|
+
* @param {quat} out the receiving quaternion
|
2796
|
+
* @param {quat} a the first operand
|
2797
|
+
* @param {quat} b the second operand
|
2798
|
+
* @returns {quat} out
|
2799
|
+
*/
|
2800
|
+
quat.add = vec4.add;
|
2801
|
+
|
2802
|
+
/**
|
2803
|
+
* Multiplies two quat's
|
2804
|
+
*
|
2805
|
+
* @param {quat} out the receiving quaternion
|
2806
|
+
* @param {quat} a the first operand
|
2807
|
+
* @param {quat} b the second operand
|
2808
|
+
* @returns {quat} out
|
2809
|
+
*/
|
2810
|
+
quat.mul = quat.multiply = function(out, a, b) {
|
2811
|
+
var ax = a[0], ay = a[1], az = a[2], aw = a[3],
|
2812
|
+
bx = b[0], by = b[1], bz = b[2], bw = b[3];
|
2813
|
+
|
2814
|
+
out[0] = ax * bw + aw * bx + ay * bz - az * by;
|
2815
|
+
out[1] = ay * bw + aw * by + az * bx - ax * bz;
|
2816
|
+
out[2] = az * bw + aw * bz + ax * by - ay * bx;
|
2817
|
+
out[3] = aw * bw - ax * bx - ay * by - az * bz;
|
2818
|
+
return out;
|
2819
|
+
};
|
2820
|
+
|
2821
|
+
/**
|
2822
|
+
* Scales a quat by a scalar number
|
2823
|
+
*
|
2824
|
+
* @param {quat} out the receiving vector
|
2825
|
+
* @param {quat} a the vector to scale
|
2826
|
+
* @param {quat} b amount to scale the vector by
|
2827
|
+
* @returns {quat} out
|
2828
|
+
*/
|
2829
|
+
quat.scale = vec4.scale;
|
2830
|
+
|
2831
|
+
/**
|
2832
|
+
* Rotates a quaternion by the given angle around the X axis
|
2833
|
+
*
|
2834
|
+
* @param {quat} out quat receiving operation result
|
2835
|
+
* @param {quat} a quat to rotate
|
2836
|
+
* @param {number} rad angle (in radians) to rotate
|
2837
|
+
* @returns {quat} out
|
2838
|
+
*/
|
2839
|
+
quat.rotateX = function (out, a, rad) {
|
2840
|
+
rad *= 0.5;
|
2841
|
+
|
2842
|
+
var ax = a[0], ay = a[1], az = a[2], aw = a[3],
|
2843
|
+
bx = Math.sin(rad), bw = Math.cos(rad);
|
2844
|
+
|
2845
|
+
out[0] = ax * bw + aw * bx;
|
2846
|
+
out[1] = ay * bw + az * bx;
|
2847
|
+
out[2] = az * bw - ay * bx;
|
2848
|
+
out[3] = aw * bw - ax * bx;
|
2849
|
+
return out;
|
2850
|
+
};
|
2851
|
+
|
2852
|
+
/**
|
2853
|
+
* Rotates a quaternion by the given angle around the X axis
|
2854
|
+
*
|
2855
|
+
* @param {quat} out quat receiving operation result
|
2856
|
+
* @param {quat} a quat to rotate
|
2857
|
+
* @param {number} rad angle (in radians) to rotate
|
2858
|
+
* @returns {quat} out
|
2859
|
+
*/
|
2860
|
+
quat.rotateY = function (out, a, rad) {
|
2861
|
+
rad *= 0.5;
|
2862
|
+
|
2863
|
+
var ax = a[0], ay = a[1], az = a[2], aw = a[3],
|
2864
|
+
by = Math.sin(rad), bw = Math.cos(rad);
|
2865
|
+
|
2866
|
+
out[0] = ax * bw - az * by;
|
2867
|
+
out[1] = ay * bw + aw * by;
|
2868
|
+
out[2] = az * bw + ax * by;
|
2869
|
+
out[3] = aw * bw - ay * by;
|
2870
|
+
return out;
|
2871
|
+
};
|
2872
|
+
|
2873
|
+
/**
|
2874
|
+
* Rotates a quaternion by the given angle around the X axis
|
2875
|
+
*
|
2876
|
+
* @param {quat} out quat receiving operation result
|
2877
|
+
* @param {quat} a quat to rotate
|
2878
|
+
* @param {number} rad angle (in radians) to rotate
|
2879
|
+
* @returns {quat} out
|
2880
|
+
*/
|
2881
|
+
quat.rotateZ = function (out, a, rad) {
|
2882
|
+
rad *= 0.5;
|
2883
|
+
|
2884
|
+
var ax = a[0], ay = a[1], az = a[2], aw = a[3],
|
2885
|
+
bz = Math.sin(rad), bw = Math.cos(rad);
|
2886
|
+
|
2887
|
+
out[0] = ax * bw + ay * bz;
|
2888
|
+
out[1] = ay * bw - ax * bz;
|
2889
|
+
out[2] = az * bw + aw * bz;
|
2890
|
+
out[3] = aw * bw - az * bz;
|
2891
|
+
return out;
|
2892
|
+
};
|
2893
|
+
|
2894
|
+
/**
|
2895
|
+
* Calculates the W component of a quat from the X, Y, and Z components.
|
2896
|
+
* Assumes that quaternion is 1 unit in length.
|
2897
|
+
* Any existing W component will be ignored.
|
2898
|
+
*
|
2899
|
+
* @param {quat} out the receiving quaternion
|
2900
|
+
* @param {quat} a quat to calculate W component of
|
2901
|
+
* @returns {quat} out
|
2902
|
+
*/
|
2903
|
+
quat.calculateW = function (out, a) {
|
2904
|
+
var x = a[0], y = a[1], z = a[2];
|
2905
|
+
|
2906
|
+
out[0] = x;
|
2907
|
+
out[1] = y;
|
2908
|
+
out[2] = z;
|
2909
|
+
out[3] = -Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
|
2910
|
+
return out;
|
2911
|
+
};
|
2912
|
+
|
2913
|
+
/**
|
2914
|
+
* Caclulates the dot product of two quat's
|
2915
|
+
*
|
2916
|
+
* @param {quat} a the first operand
|
2917
|
+
* @param {quat} b the second operand
|
2918
|
+
* @returns {Number} dot product of a and b
|
2919
|
+
*/
|
2920
|
+
quat.dot = vec4.dot;
|
2921
|
+
|
2922
|
+
/**
|
2923
|
+
* Performs a linear interpolation between two quat's
|
2924
|
+
*
|
2925
|
+
* @param {quat} out the receiving quaternion
|
2926
|
+
* @param {quat} a the first operand
|
2927
|
+
* @param {quat} b the second operand
|
2928
|
+
* @param {Number} t interpolation amount between the two inputs
|
2929
|
+
* @returns {quat} out
|
2930
|
+
*/
|
2931
|
+
quat.lerp = vec4.lerp;
|
2932
|
+
|
2933
|
+
/**
|
2934
|
+
* Performs a spherical linear interpolation between two quat
|
2935
|
+
*
|
2936
|
+
* @param {quat} out the receiving quaternion
|
2937
|
+
* @param {quat} a the first operand
|
2938
|
+
* @param {quat} b the second operand
|
2939
|
+
* @param {Number} t interpolation amount between the two inputs
|
2940
|
+
* @returns {quat} out
|
2941
|
+
*/
|
2942
|
+
quat.slerp = function (out, a, b, t) {
|
2943
|
+
var ax = a[0], ay = a[1], az = a[2], aw = a[3],
|
2944
|
+
bx = b[0], by = b[1], bz = b[2], bw = a[3];
|
2945
|
+
|
2946
|
+
var cosHalfTheta = ax * bx + ay * by + az * bz + aw * bw,
|
2947
|
+
halfTheta,
|
2948
|
+
sinHalfTheta,
|
2949
|
+
ratioA,
|
2950
|
+
ratioB;
|
2951
|
+
|
2952
|
+
if (Math.abs(cosHalfTheta) >= 1.0) {
|
2953
|
+
if (out !== a) {
|
2954
|
+
out[0] = ax;
|
2955
|
+
out[1] = ay;
|
2956
|
+
out[2] = az;
|
2957
|
+
out[3] = aw;
|
2958
|
+
}
|
2959
|
+
return out;
|
2960
|
+
}
|
2961
|
+
|
2962
|
+
halfTheta = Math.acos(cosHalfTheta);
|
2963
|
+
sinHalfTheta = Math.sqrt(1.0 - cosHalfTheta * cosHalfTheta);
|
2964
|
+
|
2965
|
+
if (Math.abs(sinHalfTheta) < 0.001) {
|
2966
|
+
out[0] = (ax * 0.5 + bx * 0.5);
|
2967
|
+
out[1] = (ay * 0.5 + by * 0.5);
|
2968
|
+
out[2] = (az * 0.5 + bz * 0.5);
|
2969
|
+
out[3] = (aw * 0.5 + bw * 0.5);
|
2970
|
+
return out;
|
2971
|
+
}
|
2972
|
+
|
2973
|
+
ratioA = Math.sin((1 - t) * halfTheta) / sinHalfTheta;
|
2974
|
+
ratioB = Math.sin(t * halfTheta) / sinHalfTheta;
|
2975
|
+
|
2976
|
+
out[0] = (ax * ratioA + bx * ratioB);
|
2977
|
+
out[1] = (ay * ratioA + by * ratioB);
|
2978
|
+
out[2] = (az * ratioA + bz * ratioB);
|
2979
|
+
out[3] = (aw * ratioA + bw * ratioB);
|
2980
|
+
|
2981
|
+
return out;
|
2982
|
+
};
|
2983
|
+
|
2984
|
+
/**
|
2985
|
+
* Calculates the inverse of a quat
|
2986
|
+
*
|
2987
|
+
* @param {quat} out the receiving quaternion
|
2988
|
+
* @param {quat} a quat to calculate inverse of
|
2989
|
+
* @returns {quat} out
|
2990
|
+
*/
|
2991
|
+
quat.invert = function(out, a) {
|
2992
|
+
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
|
2993
|
+
dot = a0*a0 + a1*a1 + a2*a2 + a3*a3,
|
2994
|
+
invDot = dot ? 1.0/dot : 0;
|
2995
|
+
|
2996
|
+
// TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
|
2997
|
+
|
2998
|
+
out[0] = -a0*invDot;
|
2999
|
+
out[1] = -a1*invDot;
|
3000
|
+
out[2] = -a2*invDot;
|
3001
|
+
out[3] = a3*invDot;
|
3002
|
+
return out;
|
3003
|
+
};
|
3004
|
+
|
3005
|
+
/**
|
3006
|
+
* Calculates the conjugate of a quat
|
3007
|
+
* If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
|
3008
|
+
*
|
3009
|
+
* @param {quat} out the receiving quaternion
|
3010
|
+
* @param {quat} a quat to calculate conjugate of
|
3011
|
+
* @returns {quat} out
|
3012
|
+
*/
|
3013
|
+
quat.conjugate = function (out, a) {
|
3014
|
+
out[0] = -a[0];
|
3015
|
+
out[1] = -a[1];
|
3016
|
+
out[2] = -a[2];
|
3017
|
+
out[3] = a[3];
|
3018
|
+
return out;
|
3019
|
+
};
|
3020
|
+
|
3021
|
+
/**
|
3022
|
+
* Caclulates the length of a quat
|
3023
|
+
*
|
3024
|
+
* @param {quat} a vector to calculate length of
|
3025
|
+
* @returns {Number} length of a
|
3026
|
+
*/
|
3027
|
+
quat.len = quat.length = vec4.length;
|
3028
|
+
|
3029
|
+
/**
|
3030
|
+
* Caclulates the squared length of a quat
|
3031
|
+
*
|
3032
|
+
* @param {quat} a vector to calculate squared length of
|
3033
|
+
* @returns {Number} squared length of a
|
3034
|
+
*/
|
3035
|
+
quat.sqrLen = quat.squaredLength = vec4.squaredLength;
|
3036
|
+
|
3037
|
+
/**
|
3038
|
+
* Normalize a quat
|
3039
|
+
*
|
3040
|
+
* @param {quat} out the receiving quaternion
|
3041
|
+
* @param {quat} a quaternion to normalize
|
3042
|
+
* @returns {quat} out
|
3043
|
+
*/
|
3044
|
+
quat.normalize = vec4.normalize;
|
3045
|
+
|
3046
|
+
/**
|
3047
|
+
* Returns a string representation of a quatenion
|
3048
|
+
*
|
3049
|
+
* @param {quat} vec vector to represent as a string
|
3050
|
+
* @returns {String} string representation of the vector
|
3051
|
+
*/
|
3052
|
+
quat.str = function (a) {
|
3053
|
+
return 'quat(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
|
3054
|
+
};
|
3055
|
+
|
3056
|
+
if(typeof(exports) !== 'undefined') {
|
3057
|
+
exports.quat = quat;
|
3058
|
+
}
|
3059
|
+
;
|
3060
|
+
|
3061
|
+
|
3062
|
+
|
3063
|
+
|
3064
|
+
|
3065
|
+
|
3066
|
+
|
3067
|
+
|
3068
|
+
|
3069
|
+
|
3070
|
+
})(shim.exports);
|
3071
|
+
})();
|