CCCamera.js 9.1 KB

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