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:null,
 60     _lookupMatrix:null,
 61 
 62     ctor:function () {
 63         this._lookupMatrix = new cc.kmMat4();
 64         this.restore();
 65     },
 66 
 67     /**
 68      * Description of cc.Camera
 69      * @return {String}
 70      */
 71     description:function () {
 72         return "<CCCamera | center =(" + this._centerX + "," + this._centerY + "," + this._centerZ + ")>";
 73     },
 74 
 75     /**
 76      * sets the dirty value
 77      * @param value
 78      */
 79     setDirty:function (value) {
 80         this._dirty = value;
 81     },
 82 
 83     /**
 84      * get the dirty value
 85      * @return {Boolean}
 86      */
 87     isDirty:function () {
 88         return this._dirty;
 89     },
 90 
 91     /**
 92      * sets the camera in the default position
 93      */
 94     restore:function () {
 95         this._eyeX = this._eyeY = 0.0;
 96         this._eyeZ = cc.Camera.getZEye();
 97 
 98         this._centerX = this._centerY = this._centerZ = 0.0;
 99 
100         this._upX = 0.0;
101         this._upY = 1.0;
102         this._upZ = 0.0;
103 
104         cc.kmMat4Identity( this._lookupMatrix );
105 
106         this._dirty = false;
107     },
108 
109     /**
110      * Sets the camera using gluLookAt using its eye, center and up_vector
111      */
112     locate:function () {
113         if (this._dirty) {
114             var eye = new cc.kmVec3(), center = new cc.kmVec3(), up = new cc.kmVec3();
115 
116             cc.kmVec3Fill( eye, this._eyeX, this._eyeY , this._eyeZ );
117             cc.kmVec3Fill( center, this._centerX, this._centerY, this._centerZ);
118 
119             cc.kmVec3Fill( up, this._upX, this._upY, this._upZ);
120             cc.kmMat4LookAt( this._lookupMatrix, eye, center, up);
121 
122             this._dirty = false;
123         }
124         cc.kmGLMultMatrix( this._lookupMatrix);
125     },
126 
127     /**
128      * sets the eye values in points
129      * @param {Number} eyeX
130      * @param {Number} eyeY
131      * @param {Number} eyeZ
132      * @deprecated This function will be deprecated sooner or later.
133      */
134     setEyeXYZ:function (eyeX, eyeY, eyeZ) {
135         this.setEye(eyeX,eyeY,eyeZ);
136     },
137 
138     /**
139      * sets the eye values in points
140      * @param {Number} eyeX
141      * @param {Number} eyeY
142      * @param {Number} eyeZ
143      */
144     setEye:function (eyeX, eyeY, eyeZ) {
145         this._eyeX = eyeX ;
146         this._eyeY = eyeY ;
147         this._eyeZ = eyeZ ;
148 
149         this._dirty = true;
150     },
151 
152     /**
153      * sets the center values in points
154      * @param {Number} centerX
155      * @param {Number} centerY
156      * @param {Number} centerZ
157      * @deprecated  This function will be deprecated sooner or later.
158      */
159     setCenterXYZ:function (centerX, centerY, centerZ) {
160         this.setCenter(centerX,centerY,centerZ);
161     },
162 
163     /**
164      * sets the center values in points
165      * @param {Number} centerX
166      * @param {Number} centerY
167      * @param {Number} centerZ
168      */
169     setCenter:function (centerX, centerY, centerZ) {
170         this._centerX = centerX ;
171         this._centerY = centerY ;
172         this._centerZ = centerZ ;
173 
174         this._dirty = true;
175     },
176 
177     /**
178      * sets the up values
179      * @param {Number} upX
180      * @param {Number} upY
181      * @param {Number} upZ
182      * @deprecated This function will be deprecated sooner or later.
183      */
184     setUpXYZ:function (upX, upY, upZ) {
185         this.setUp(upX, upY, upZ);
186     },
187 
188     /**
189      * sets the up values
190      * @param {Number} upX
191      * @param {Number} upY
192      * @param {Number} upZ
193      */
194     setUp:function (upX, upY, upZ) {
195         this._upX = upX;
196         this._upY = upY;
197         this._upZ = upZ;
198 
199         this._dirty = true;
200     },
201 
202     /**
203      * get the eye vector values in points  (return an object like {x:1,y:1,z:1} in HTML5)
204      * @param {Number} eyeX
205      * @param {Number} eyeY
206      * @param {Number} eyeZ
207      * @return {Object}
208      * @deprecated This function will be deprecated sooner or later.
209      */
210     getEyeXYZ:function (eyeX, eyeY, eyeZ) {
211         return {x:this._eyeX , y:this._eyeY , z: this._eyeZ };
212     },
213 
214     /**
215      * get the eye vector values in points  (return an object like {x:1,y:1,z:1} in HTML5)
216      * @return {Object}
217      */
218     getEye:function () {
219         return {x:this._eyeX , y:this._eyeY , z: this._eyeZ };
220     },
221 
222     /**
223      * get the center vector values int points (return an object like {x:1,y:1,z:1} in HTML5)
224      * @param {Number} centerX
225      * @param {Number} centerY
226      * @param {Number} centerZ
227      * @return {Object}
228      * @deprecated This function will be deprecated sooner or later.
229      */
230     getCenterXYZ:function (centerX, centerY, centerZ) {
231         return {x:this._centerX ,y:this._centerY ,z:this._centerZ };
232     },
233 
234     /**
235      * get the center vector values int points (return an object like {x:1,y:1,z:1} in HTML5)
236      * @return {Object}
237      */
238     getCenter:function () {
239         return {x:this._centerX ,y:this._centerY ,z:this._centerZ };
240     },
241 
242     /**
243      * get the up vector values (return an object like {x:1,y:1,z:1} in HTML5)
244      * @param {Number} upX
245      * @param {Number} upY
246      * @param {Number} upZ
247      * @return {Object}
248      * @deprecated This function will be deprecated sooner or later.
249      */
250     getUpXYZ:function (upX, upY, upZ) {
251         return {x:this._upX,y:this._upY,z:this._upZ};
252     },
253 
254     /**
255      * get the up vector values (return an object like {x:1,y:1,z:1} in HTML5)
256      * @return {Object}
257      */
258     getUp:function () {
259         return {x:this._upX,y:this._upY,z:this._upZ};
260     },
261 
262     _DISALLOW_COPY_AND_ASSIGN:function (CCCamera) {
263 
264     }
265 });
266 
267 /**
268  * returns the Z eye
269  * @return {Number}
270  */
271 cc.Camera.getZEye = function () {
272     return cc.FLT_EPSILON;
273 };
274