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 
 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  * @class
 46  * @extends cc.Class
 47  */
 48 cc.Camera = cc.Class.extend(/** @lends cc.Action# */{
 49     _eyeX:null,
 50     _eyeY:null,
 51     _eyeZ:null,
 52 
 53     _centerX:null,
 54     _centerY:null,
 55     _centerZ:null,
 56 
 57     _upX:null,
 58     _upY:null,
 59     _upZ:null,
 60 
 61     _dirty:null,
 62     _lookupMatrix:null,
 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     /**
130      * sets the eye values in points
131      * @param {Number} eyeX
132      * @param {Number} eyeY
133      * @param {Number} eyeZ
134      * @deprecated This function will be deprecated sooner or later.
135      */
136     setEyeXYZ:function (eyeX, eyeY, eyeZ) {
137         this.setEye(eyeX,eyeY,eyeZ);
138     },
139 
140     /**
141      * sets the eye values in points
142      * @param {Number} eyeX
143      * @param {Number} eyeY
144      * @param {Number} eyeZ
145      */
146     setEye:function (eyeX, eyeY, eyeZ) {
147         this._eyeX = eyeX ;
148         this._eyeY = eyeY ;
149         this._eyeZ = eyeZ ;
150 
151         this._dirty = true;
152     },
153 
154     /**
155      * sets the center values in points
156      * @param {Number} centerX
157      * @param {Number} centerY
158      * @param {Number} centerZ
159      * @deprecated  This function will be deprecated sooner or later.
160      */
161     setCenterXYZ:function (centerX, centerY, centerZ) {
162         this.setCenter(centerX,centerY,centerZ);
163     },
164 
165     /**
166      * sets the center values in points
167      * @param {Number} centerX
168      * @param {Number} centerY
169      * @param {Number} centerZ
170      */
171     setCenter:function (centerX, centerY, centerZ) {
172         this._centerX = centerX ;
173         this._centerY = centerY ;
174         this._centerZ = centerZ ;
175 
176         this._dirty = true;
177     },
178 
179     /**
180      * sets the up values
181      * @param {Number} upX
182      * @param {Number} upY
183      * @param {Number} upZ
184      * @deprecated This function will be deprecated sooner or later.
185      */
186     setUpXYZ:function (upX, upY, upZ) {
187         this.setUp(upX, upY, upZ);
188     },
189 
190     /**
191      * sets the up values
192      * @param {Number} upX
193      * @param {Number} upY
194      * @param {Number} upZ
195      */
196     setUp:function (upX, upY, upZ) {
197         this._upX = upX;
198         this._upY = upY;
199         this._upZ = upZ;
200 
201         this._dirty = true;
202     },
203 
204     /**
205      * get the eye vector values in points  (return an object like {x:1,y:1,z:1} in HTML5)
206      * @param {Number} eyeX
207      * @param {Number} eyeY
208      * @param {Number} eyeZ
209      * @return {Object}
210      * @deprecated This function will be deprecated sooner or later.
211      */
212     getEyeXYZ:function (eyeX, eyeY, eyeZ) {
213         return {x:this._eyeX , y:this._eyeY , z: this._eyeZ };
214     },
215 
216     /**
217      * get the eye vector values in points  (return an object like {x:1,y:1,z:1} in HTML5)
218      * @return {Object}
219      */
220     getEye:function () {
221         return {x:this._eyeX , y:this._eyeY , z: this._eyeZ };
222     },
223 
224     /**
225      * get the center vector values int points (return an object like {x:1,y:1,z:1} in HTML5)
226      * @param {Number} centerX
227      * @param {Number} centerY
228      * @param {Number} centerZ
229      * @return {Object}
230      * @deprecated This function will be deprecated sooner or later.
231      */
232     getCenterXYZ:function (centerX, centerY, centerZ) {
233         return {x:this._centerX ,y:this._centerY ,z:this._centerZ };
234     },
235 
236     /**
237      * get the center vector values int points (return an object like {x:1,y:1,z:1} in HTML5)
238      * @return {Object}
239      */
240     getCenter:function () {
241         return {x:this._centerX ,y:this._centerY ,z:this._centerZ };
242     },
243 
244     /**
245      * get the up vector values (return an object like {x:1,y:1,z:1} in HTML5)
246      * @param {Number} upX
247      * @param {Number} upY
248      * @param {Number} upZ
249      * @return {Object}
250      * @deprecated This function will be deprecated sooner or later.
251      */
252     getUpXYZ:function (upX, upY, upZ) {
253         return {x:this._upX,y:this._upY,z:this._upZ};
254     },
255 
256     /**
257      * get the up vector values (return an object like {x:1,y:1,z:1} in HTML5)
258      * @return {Object}
259      */
260     getUp:function () {
261         return {x:this._upX,y:this._upY,z:this._upZ};
262     },
263 
264     _DISALLOW_COPY_AND_ASSIGN:function (CCCamera) {
265 
266     }
267 });
268 
269 /**
270  * returns the Z eye
271  * @return {Number}
272  */
273 cc.Camera.getZEye = function () {
274     return cc.FLT_EPSILON;
275 };
276 
277 //cc.CONTENT_SCALE_FACTOR = cc.Director.getInstance().getContentScaleFactor();
278