Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 'use strict';
      6 
      7 base.requireRawScript('../third_party/gl-matrix/src/gl-matrix/common.js');
      8 base.requireRawScript('../third_party/gl-matrix/src/gl-matrix/mat2d.js');
      9 base.requireRawScript('../third_party/gl-matrix/src/gl-matrix/mat4.js');
     10 base.requireRawScript('../third_party/gl-matrix/src/gl-matrix/vec2.js');
     11 base.requireRawScript('../third_party/gl-matrix/src/gl-matrix/vec3.js');
     12 base.requireRawScript('../third_party/gl-matrix/src/gl-matrix/vec4.js');
     13 
     14 base.exportTo('base', function() {
     15   var tmp_vec2 = vec2.create();
     16   var tmp_vec2b = vec2.create();
     17   var tmp_vec4 = vec4.create();
     18   var tmp_mat2d = mat2d.create();
     19 
     20   vec2.createFromArray = function(arr) {
     21     if (arr.length != 2)
     22       throw new Error('Should be length 2');
     23     var v = vec2.create();
     24     vec2.set(v, arr[0], arr[1]);
     25     return v;
     26   };
     27 
     28   vec2.createXY = function(x, y) {
     29     var v = vec2.create();
     30     vec2.set(v, x, y);
     31     return v;
     32   };
     33 
     34   vec2.toString = function(a) {
     35     return '[' + a[0] + ', ' + a[1] + ']';
     36   };
     37 
     38   vec2.addTwoScaledUnitVectors = function(out, u1, scale1, u2, scale2) {
     39     // out = u1 * scale1 + u2 * scale2
     40     vec2.scale(tmp_vec2, u1, scale1);
     41     vec2.scale(tmp_vec2b, u2, scale2);
     42     vec2.add(out, tmp_vec2, tmp_vec2b);
     43   }
     44 
     45   vec3.createXYZ = function(x, y, z) {
     46     var v = vec3.create();
     47     vec3.set(v, x, y, z);
     48     return v;
     49   };
     50 
     51   vec3.toString = function(a) {
     52     return 'vec3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ')';
     53   }
     54 
     55   mat2d.translateXY = function(out, x, y) {
     56     vec2.set(tmp_vec2, x, y);
     57     mat2d.translate(out, out, tmp_vec2);
     58   }
     59 
     60   mat2d.scaleXY = function(out, x, y) {
     61     vec2.set(tmp_vec2, x, y);
     62     mat2d.scale(out, out, tmp_vec2);
     63   }
     64 
     65   vec4.unitize = function(out, a) {
     66     out[0] = a[0] / a[3];
     67     out[1] = a[1] / a[3];
     68     out[2] = a[2] / a[3];
     69     out[3] = 1;
     70     return out;
     71   }
     72 
     73   vec2.copyFromVec4 = function(out, a) {
     74     vec4.unitize(tmp_vec4, a);
     75     vec2.copy(out, tmp_vec4);
     76   }
     77 
     78   return {};
     79 });
     80