1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga 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 var cc = cc = cc || {}; 27 28 29 /** 30 A CCCamera is used in every CCNode. 31 Useful to look at the object from different views. 32 The OpenGL gluLookAt() function is used to locate the 33 camera. 34 35 If the object is transformed by any of the scale, rotation or 36 position attributes, then they will override the camera. 37 38 IMPORTANT: Either your use the camera or the rotation/scale/position properties. You can't use both. 39 World coordinates won't work if you use the camera. 40 41 Limitations: 42 43 - Some nodes, like CCParallaxNode, CCParticle uses world node coordinates, and they won't work properly if you move them (or any of their ancestors) 44 using the camera. 45 46 - It doesn't work on batched nodes like CCSprite objects when they are parented to a CCSpriteBatchNode object. 47 48 - 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. 49 50 */ 51 cc.Camera = cc.Class.extend({ 52 /*protected:*/ 53 _eyeX:null, 54 _eyeY:null, 55 _eyeZ:null, 56 _centerX:null, 57 _centerY:null, 58 _centerZ:null, 59 _upX:null, 60 _upY:null, 61 _upZ:null, 62 _dirty:null, 63 64 /*public:*/ 65 ctor:function () { 66 this.restore(); 67 }, 68 description:function () { 69 return "<CCCamera | center =(" + this._centerX + "," + this._centerY + "," + this._centerZ + ")>"; 70 }, 71 /** sets the dirty value */ 72 setDirty:function (value) { 73 this._dirty = value; 74 }, 75 /** get the dirty value */ 76 getDirty:function () { 77 return this._dirty; 78 }, 79 80 /** sets the camera in the default position */ 81 restore:function () { 82 this._eyeX = this._eyeY = 0.0; 83 this._eyeZ = cc.Camera.getZEye(); 84 85 this._centerX = this._centerY = this._centerZ = 0.0; 86 87 this._upX = 0.0; 88 this._upY = 1.0; 89 this._upZ = 0.0; 90 91 this._dirty = false; 92 }, 93 /** Sets the camera using gluLookAt using its eye, center and up_vector */ 94 locate:function () { 95 if (this._dirty) { 96 //TODO gl 97 //gluLookAt(this._eyeX, this._eyeY, this._eyeZ,this._centerX, this._centerY, this._centerZ,this._upX, this._upY, this._upZ); 98 } 99 100 }, 101 /** sets the eye values in points */ 102 setEyeXYZ:function (eyeX, eyeY, eyeZ) { 103 this._eyeX = eyeX * cc.CONTENT_SCALE_FACTOR; 104 this._eyeY = eyeY * cc.CONTENT_SCALE_FACTOR; 105 this._eyeZ = eyeZ * cc.CONTENT_SCALE_FACTOR; 106 107 this._dirty = true; 108 }, 109 /** sets the center values in points */ 110 setCenterXYZ:function (centerX, centerY, fenterZ) { 111 this._centerX = centerX * cc.CONTENT_SCALE_FACTOR; 112 this._centerY = centerY * cc.CONTENT_SCALE_FACTOR; 113 this._centerZ = fenterZ * cc.CONTENT_SCALE_FACTOR; 114 115 this._dirty = true; 116 }, 117 /** sets the up values */ 118 setUpXYZ:function (upX, upY, upZ) { 119 this._upX = upX; 120 this._upY = upY; 121 this._upZ = upZ; 122 123 this._dirty = true; 124 }, 125 126 /** get the eye vector values in points */ 127 getEyeXYZ:function (eyeX, eyeY, eyeZ) { 128 eyeX = this._eyeX / cc.CONTENT_SCALE_FACTOR; 129 eyeY = this._eyeY / cc.CONTENT_SCALE_FACTOR; 130 eyeZ = this._eyeZ / cc.CONTENT_SCALE_FACTOR; 131 }, 132 /** get the center vector values int points */ 133 getCenterXYZ:function (centerX, centerY, centerZ) { 134 centerX = this._centerX / cc.CONTENT_SCALE_FACTOR; 135 centerY = this._centerY / cc.CONTENT_SCALE_FACTOR; 136 centerZ = this._centerZ / cc.CONTENT_SCALE_FACTOR; 137 }, 138 /** get the up vector values */ 139 getUpXYZ:function (upX, upY, upZ) { 140 upX = this._upX; 141 upY = this._upY; 142 upZ = this._upZ; 143 }, 144 145 /*private:*/ 146 _DISALLOW_COPY_AND_ASSIGN:function (CCCamera) { 147 148 } 149 }); 150 /** returns the Z eye */ 151 cc.Camera.getZEye = function () { 152 return cc.FLT_EPSILON; 153 }; 154 155 //cc.CONTENT_SCALE_FACTOR = cc.Director.getInstance().getContentScaleFactor(); 156