CCCamera.js 9.1 KB

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