1 /**************************************************************************** 2 Copyright (c) 2008-2010 Ricardo Quesada 3 Copyright (c) 2011-2012 cocos2d-x.org 4 Copyright (c) 2013-2014 Chukong Technologies Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * <p> 29 * A CCCamera is used in every CCNode. <br/> 30 * The OpenGL gluLookAt() function is used to locate the camera. <br/> 31 * <br/> 32 * If the object is transformed by any of the scale, rotation or position attributes, then they will override the camera. <br/> 33 * <br/> 34 * IMPORTANT: Either your use the camera or the rotation/scale/position properties. You can't use both. <br/> 35 * World coordinates won't work if you use the camera. <br/> 36 * <br/> 37 * Limitations: <br/> 38 * - Some nodes, like CCParallaxNode, CCParticle uses world node coordinates, and they won't work properly if you move them (or any of their ancestors) <br/> 39 * using the camera. <br/> 40 * <br/> 41 * - It doesn't work on batched nodes like CCSprite objects when they are parented to a CCSpriteBatchNode object. <br/> 42 * <br/> 43 * - It is recommended to use it ONLY if you are going to create 3D effects. For 2D effecs, use the action CCFollow or position/scale/rotate. * 44 * </p> 45 */ 46 cc.Camera = cc.Class.extend({ 47 _eyeX:null, 48 _eyeY:null, 49 _eyeZ:null, 50 51 _centerX:null, 52 _centerY:null, 53 _centerZ:null, 54 55 _upX:null, 56 _upY:null, 57 _upZ:null, 58 59 _dirty:false, 60 _lookupMatrix:null, 61 /** 62 * constructor of cc.Camera 63 */ 64 ctor:function () { 65 this._lookupMatrix = new cc.kmMat4(); 66 this.restore(); 67 }, 68 69 /** 70 * Description of cc.Camera 71 * @return {String} 72 */ 73 description:function () { 74 return "<CCCamera | center =(" + this._centerX + "," + this._centerY + "," + this._centerZ + ")>"; 75 }, 76 77 /** 78 * sets the dirty value 79 * @param value 80 */ 81 setDirty:function (value) { 82 this._dirty = value; 83 }, 84 85 /** 86 * get the dirty value 87 * @return {Boolean} 88 */ 89 isDirty:function () { 90 return this._dirty; 91 }, 92 93 /** 94 * sets the camera in the default position 95 */ 96 restore:function () { 97 this._eyeX = this._eyeY = 0.0; 98 this._eyeZ = cc.Camera.getZEye(); 99 100 this._centerX = this._centerY = this._centerZ = 0.0; 101 102 this._upX = 0.0; 103 this._upY = 1.0; 104 this._upZ = 0.0; 105 106 cc.kmMat4Identity( this._lookupMatrix ); 107 108 this._dirty = false; 109 }, 110 111 /** 112 * Sets the camera using gluLookAt using its eye, center and up_vector 113 */ 114 locate:function () { 115 if (this._dirty) { 116 var eye = new cc.kmVec3(), center = new cc.kmVec3(), up = new cc.kmVec3(); 117 118 cc.kmVec3Fill( eye, this._eyeX, this._eyeY , this._eyeZ ); 119 cc.kmVec3Fill( center, this._centerX, this._centerY, this._centerZ); 120 121 cc.kmVec3Fill( up, this._upX, this._upY, this._upZ); 122 cc.kmMat4LookAt( this._lookupMatrix, eye, center, up); 123 124 this._dirty = false; 125 } 126 cc.kmGLMultMatrix( this._lookupMatrix); 127 }, 128 129 _locateForRenderer: function(matrix){ 130 if (this._dirty) { 131 var eye = new cc.kmVec3(), center = new cc.kmVec3(), up = new cc.kmVec3(); 132 133 cc.kmVec3Fill( eye, this._eyeX, this._eyeY , this._eyeZ ); 134 cc.kmVec3Fill( center, this._centerX, this._centerY, this._centerZ); 135 136 cc.kmVec3Fill( up, this._upX, this._upY, this._upZ); 137 cc.kmMat4LookAt( this._lookupMatrix, eye, center, up); 138 139 this._dirty = false; 140 } 141 cc.kmMat4Multiply(matrix, matrix, this._lookupMatrix); 142 }, 143 144 /** 145 * sets the eye values in points 146 * @param {Number} eyeX 147 * @param {Number} eyeY 148 * @param {Number} eyeZ 149 * @deprecated This function will be deprecated sooner or later please use setEye instead. 150 */ 151 setEyeXYZ:function (eyeX, eyeY, eyeZ) { 152 this.setEye(eyeX,eyeY,eyeZ); 153 }, 154 155 /** 156 * sets the eye values in points 157 * @param {Number} eyeX 158 * @param {Number} eyeY 159 * @param {Number} eyeZ 160 */ 161 setEye:function (eyeX, eyeY, eyeZ) { 162 this._eyeX = eyeX ; 163 this._eyeY = eyeY ; 164 this._eyeZ = eyeZ ; 165 166 this._dirty = true; 167 }, 168 169 /** 170 * sets the center values in points 171 * @param {Number} centerX 172 * @param {Number} centerY 173 * @param {Number} centerZ 174 * @deprecated This function will be deprecated sooner or later please use setCenter instead. 175 */ 176 setCenterXYZ:function (centerX, centerY, centerZ) { 177 this.setCenter(centerX,centerY,centerZ); 178 }, 179 180 /** 181 * sets the center values in points 182 * @param {Number} centerX 183 * @param {Number} centerY 184 * @param {Number} centerZ 185 */ 186 setCenter:function (centerX, centerY, centerZ) { 187 this._centerX = centerX ; 188 this._centerY = centerY ; 189 this._centerZ = centerZ ; 190 191 this._dirty = true; 192 }, 193 194 /** 195 * sets the up values 196 * @param {Number} upX 197 * @param {Number} upY 198 * @param {Number} upZ 199 * @deprecated This function will be deprecated sooner or later. 200 */ 201 setUpXYZ:function (upX, upY, upZ) { 202 this.setUp(upX, upY, upZ); 203 }, 204 205 /** 206 * sets the up values 207 * @param {Number} upX 208 * @param {Number} upY 209 * @param {Number} upZ 210 */ 211 setUp:function (upX, upY, upZ) { 212 this._upX = upX; 213 this._upY = upY; 214 this._upZ = upZ; 215 216 this._dirty = true; 217 }, 218 219 /** 220 * get the eye vector values in points (return an object like {x:1,y:1,z:1} in HTML5) 221 * @param {Number} eyeX 222 * @param {Number} eyeY 223 * @param {Number} eyeZ 224 * @return {Object} 225 * @deprecated This function will be deprecated sooner or later, please use getEye instead. 226 */ 227 getEyeXYZ:function (eyeX, eyeY, eyeZ) { 228 return {x:this._eyeX , y:this._eyeY , z: this._eyeZ }; 229 }, 230 231 /** 232 * get the eye vector values in points (return an object like {x:1,y:1,z:1} in HTML5) 233 * @return {Object} 234 */ 235 getEye:function () { 236 return {x:this._eyeX , y:this._eyeY , z: this._eyeZ }; 237 }, 238 239 /** 240 * get the center vector values int points (return an object like {x:1,y:1,z:1} in HTML5) 241 * @param {Number} centerX 242 * @param {Number} centerY 243 * @param {Number} centerZ 244 * @return {Object} 245 * @deprecated This function will be deprecated sooner or later,please use getCenter instead. 246 */ 247 getCenterXYZ:function (centerX, centerY, centerZ) { 248 return {x:this._centerX ,y:this._centerY ,z:this._centerZ }; 249 }, 250 251 /** 252 * get the center vector values int points (return an object like {x:1,y:1,z:1} in HTML5) 253 * @return {Object} 254 */ 255 getCenter:function () { 256 return {x:this._centerX ,y:this._centerY ,z:this._centerZ }; 257 }, 258 259 /** 260 * get the up vector values (return an object like {x:1,y:1,z:1} in HTML5) 261 * @param {Number} upX 262 * @param {Number} upY 263 * @param {Number} upZ 264 * @return {Object} 265 * @deprecated This function will be deprecated sooner or later,please use getUp instead. 266 */ 267 getUpXYZ:function (upX, upY, upZ) { 268 return {x:this._upX,y:this._upY,z:this._upZ}; 269 }, 270 271 /** 272 * get the up vector values (return an object like {x:1,y:1,z:1} in HTML5) 273 * @return {Object} 274 */ 275 getUp:function () { 276 return {x:this._upX,y:this._upY,z:this._upZ}; 277 }, 278 279 _DISALLOW_COPY_AND_ASSIGN:function (CCCamera) { 280 281 } 282 }); 283 284 /** 285 * returns the Z eye 286 * @return {Number} 287 */ 288 cc.Camera.getZEye = function () { 289 return cc.FLT_EPSILON; 290 }; 291