1 /** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008, Luke Benstead. 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without modification, 7 are permitted provided that the following conditions are met: 8 9 Redistributions of source code must retain the above copyright notice, 10 this list of conditions and the following disclaimer. 11 Redistributions in binary form must reproduce the above copyright notice, 12 this list of conditions and the following disclaimer in the documentation 13 and/or other materials provided with the distribution. 14 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 22 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 cc.kmVec4 = function (x, y, z, w) { 28 this.x = x || 0; 29 this.y = y || 0; 30 this.z = z || 0; 31 this.w = w || 0; 32 }; 33 34 35 cc.kmVec4Fill = function(outVec, x, y ,z, w){ 36 outVec.x = x; 37 outVec.y = y; 38 outVec.z = z; 39 outVec.w = w; 40 return outVec; 41 }; 42 43 cc.kmVec4Add = function(outVec, pV1, pV2){ 44 outVec.x = pV1.x + pV2.x; 45 outVec.y = pV1.y + pV2.y; 46 outVec.z = pV1.z + pV2.z; 47 outVec.w = pV1.w + pV2.w; 48 49 return outVec; 50 }; 51 52 cc.kmVec4Dot = function( vec1, vec2){ 53 return ( vec1.x * vec2.x 54 + vec1.y * vec2.y 55 + vec1.z * vec2.z 56 + vec1.w * vec2.w ); 57 }; 58 59 cc.kmVec4Length = function(inVec){ 60 return Math.sqrt(cc.kmSQR(inVec.x) + cc.kmSQR(inVec.y) + cc.kmSQR(inVec.z) + cc.kmSQR(inVec.w)); 61 }; 62 63 cc.kmVec4LengthSq = function(inVec){ 64 return cc.kmSQR(inVec.x) + cc.kmSQR(inVec.y) + cc.kmSQR(inVec.z) + cc.kmSQR(inVec.w); 65 }; 66 67 cc.kmVec4Lerp = function(outVec, pV1, pV2, t){ 68 return outVec; 69 }; 70 71 cc.kmVec4Normalize = function(outVec, inVec){ 72 var l = 1.0 / cc.kmVec4Length(inVec); 73 74 outVec.x *= l; 75 outVec.y *= l; 76 outVec.z *= l; 77 outVec.w *= l; 78 79 return outVec; 80 }; 81 82 cc.kmVec4Scale = function(outVec, inVec, scale){ 83 cc.kmVec4Normalize(outVec, inVec); 84 85 outVec.x *= scale; 86 outVec.y *= scale; 87 outVec.z *= scale; 88 outVec.w *= scale; 89 return outVec; 90 }; 91 92 cc.kmVec4Subtract = function(outVec,vec1, vec2){ 93 outVec.x = vec1.x - vec2.x; 94 outVec.y = vec1.y - vec2.y; 95 outVec.z = vec1.z - vec2.z; 96 outVec.w = vec1.w - vec2.w; 97 98 return outVec; 99 }; 100 101 cc.kmVec4Transform = function(outVec, vec,mat4Obj){ 102 outVec.x = vec.x * mat4Obj.mat[0] + vec.y * mat4Obj.mat[4] + vec.z * mat4Obj.mat[8] + vec.w * mat4Obj.mat[12]; 103 outVec.y = vec.x * mat4Obj.mat[1] + vec.y * mat4Obj.mat[5] + vec.z * mat4Obj.mat[9] + vec.w * mat4Obj.mat[13]; 104 outVec.z = vec.x * mat4Obj.mat[2] + vec.y * mat4Obj.mat[6] + vec.z * mat4Obj.mat[10] + vec.w * mat4Obj.mat[14]; 105 outVec.w = vec.x * mat4Obj.mat[3] + vec.y * mat4Obj.mat[7] + vec.z * mat4Obj.mat[11] + vec.w * mat4Obj.mat[15]; 106 return outVec; 107 }; 108 109 cc.kmVec4TransformArray = function(outVec,outStride,vecObj,stride,mat4Obj,count){ 110 var i = 0; 111 //Go through all of the vectors 112 while (i < count) { 113 var currIn = vecObj + (i * stride); //Get a pointer to the current input 114 var out = outVec + (i * outStride); //and the current output 115 cc.kmVec4Transform(out, currIn, mat4Obj); //Perform transform on it 116 ++i; 117 } 118 119 return outVec; 120 }; 121 122 cc.kmVec4AreEqual = function(vec1,vec2){ 123 return ( 124 (vec1.x < vec2.x + cc.kmEpsilon && vec1.x > vec2.x - cc.kmEpsilon) && 125 (vec1.y < vec2.y + cc.kmEpsilon && vec1.y > vec2.y - cc.kmEpsilon) && 126 (vec1.z < vec2.z + cc.kmEpsilon && vec1.z > vec2.z - cc.kmEpsilon) && 127 (vec1.w < vec2.w + cc.kmEpsilon && vec1.w > vec2.w - cc.kmEpsilon) 128 ); 129 }; 130 131 cc.kmVec4Assign = function(destVec, srcVec){ 132 if(destVec == srcVec){ 133 cc.log("destVec and srcVec are same object"); 134 return destVec; 135 } 136 137 destVec.x = srcVec.x; 138 destVec.y = srcVec.y; 139 destVec.z = srcVec.z; 140 destVec.w = srcVec.w; 141 142 return destVec; 143 }; 144 145 cc.kmVec4ToTypeArray = function(vecValue){ 146 if(!vecValue) 147 return null; 148 149 var tyArr = new Float32Array(4); 150 tyArr[0] = vecValue.x; 151 tyArr[1] = vecValue.y; 152 tyArr[2] = vecValue.z; 153 tyArr[3] = vecValue.w; 154 return tyArr; 155 }; 156 157