CCDirectorWebGL.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. cc._tmp.DirectorWebGL = function () {
  23. /**
  24. * OpenGL projection protocol
  25. * @class
  26. * @extends cc.Class
  27. */
  28. cc.DirectorDelegate = cc.Class.extend(/** @lends cc.DirectorDelegate# */{
  29. /**
  30. * Called by CCDirector when the projection is updated, and "custom" projection is used
  31. */
  32. updateProjection: function () {
  33. }
  34. });
  35. var _p = cc.Director.prototype;
  36. _p.setProjection = function (projection) {
  37. var _t = this;
  38. var size = _t._winSizeInPoints;
  39. _t.setViewport();
  40. var view = _t._openGLView,
  41. ox = view._viewPortRect.x / view._scaleX,
  42. oy = view._viewPortRect.y / view._scaleY;
  43. switch (projection) {
  44. case cc.Director.PROJECTION_2D:
  45. cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
  46. cc.kmGLLoadIdentity();
  47. var orthoMatrix = new cc.kmMat4();
  48. cc.kmMat4OrthographicProjection(orthoMatrix, 0, size.width, 0, size.height, -1024, 1024);
  49. cc.kmGLMultMatrix(orthoMatrix);
  50. cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
  51. cc.kmGLLoadIdentity();
  52. break;
  53. case cc.Director.PROJECTION_3D:
  54. var zeye = _t.getZEye();
  55. var matrixPerspective = new cc.kmMat4(), matrixLookup = new cc.kmMat4();
  56. cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
  57. cc.kmGLLoadIdentity();
  58. // issue #1334
  59. cc.kmMat4PerspectiveProjection(matrixPerspective, 60, size.width / size.height, 0.1, zeye * 2);
  60. cc.kmGLMultMatrix(matrixPerspective);
  61. cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
  62. cc.kmGLLoadIdentity();
  63. var eye = cc.kmVec3Fill(null, -ox + size.width / 2, -oy + size.height / 2, zeye);
  64. var center = cc.kmVec3Fill(null, -ox + size.width / 2, -oy + size.height / 2, 0.0);
  65. var up = cc.kmVec3Fill(null, 0.0, 1.0, 0.0);
  66. cc.kmMat4LookAt(matrixLookup, eye, center, up);
  67. cc.kmGLMultMatrix(matrixLookup);
  68. break;
  69. case cc.Director.PROJECTION_CUSTOM:
  70. if (_t._projectionDelegate)
  71. _t._projectionDelegate.updateProjection();
  72. break;
  73. default:
  74. cc.log(cc._LogInfos.Director_setProjection);
  75. break;
  76. }
  77. _t._projection = projection;
  78. cc.eventManager.dispatchEvent(_t._eventProjectionChanged);
  79. cc.setProjectionMatrixDirty();
  80. };
  81. _p.setDepthTest = function (on) {
  82. var loc_gl = cc._renderContext;
  83. if (on) {
  84. loc_gl.clearDepth(1.0);
  85. loc_gl.enable(loc_gl.DEPTH_TEST);
  86. loc_gl.depthFunc(loc_gl.LEQUAL);
  87. //cc._renderContext.hint(cc._renderContext.PERSPECTIVE_CORRECTION_HINT, cc._renderContext.NICEST);
  88. } else {
  89. loc_gl.disable(loc_gl.DEPTH_TEST);
  90. }
  91. //cc.checkGLErrorDebug();
  92. };
  93. _p.setOpenGLView = function (openGLView) {
  94. var _t = this;
  95. // set size
  96. _t._winSizeInPoints.width = cc._canvas.width; //_t._openGLView.getDesignResolutionSize();
  97. _t._winSizeInPoints.height = cc._canvas.height;
  98. _t._openGLView = openGLView || cc.view;
  99. // Configuration. Gather GPU info
  100. var conf = cc.configuration;
  101. conf.gatherGPUInfo();
  102. conf.dumpInfo();
  103. // set size
  104. //_t._winSizeInPoints = _t._openGLView.getDesignResolutionSize();
  105. //_t._winSizeInPixels = cc.size(_t._winSizeInPoints.width * _t._contentScaleFactor, _t._winSizeInPoints.height * _t._contentScaleFactor);
  106. //if (_t._openGLView != openGLView) {
  107. // because EAGLView is not kind of CCObject
  108. _t._createStatsLabel();
  109. //if (_t._openGLView)
  110. _t.setGLDefaultValues();
  111. /* if (_t._contentScaleFactor != 1) {
  112. _t.updateContentScaleFactor();
  113. }*/
  114. //}
  115. if (cc.eventManager)
  116. cc.eventManager.setEnabled(true);
  117. };
  118. _p._clear = function () {
  119. var gl = cc._renderContext;
  120. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  121. };
  122. _p._beforeVisitScene = function () {
  123. cc.kmGLPushMatrix();
  124. };
  125. _p._afterVisitScene = function () {
  126. cc.kmGLPopMatrix();
  127. };
  128. _p._createStatsLabel = function () {
  129. var _t = this;
  130. if (!cc.LabelAtlas){
  131. _t._createStatsLabelForCanvas();
  132. return
  133. }
  134. if ((cc.Director._fpsImageLoaded == null) || (cc.Director._fpsImageLoaded == false))
  135. return;
  136. var texture = new cc.Texture2D();
  137. texture.initWithElement(cc.Director._fpsImage);
  138. texture.handleLoadedTexture();
  139. /*
  140. We want to use an image which is stored in the file named ccFPSImage.c
  141. for any design resolutions and all resource resolutions.
  142. To achieve this,
  143. Firstly, we need to ignore 'contentScaleFactor' in 'CCAtlasNode' and 'CCLabelAtlas'.
  144. So I added a new method called 'setIgnoreContentScaleFactor' for 'CCAtlasNode',
  145. this is not exposed to game developers, it's only used for displaying FPS now.
  146. Secondly, the size of this image is 480*320, to display the FPS label with correct size,
  147. a factor of design resolution ratio of 480x320 is also needed.
  148. */
  149. var factor = cc.view.getDesignResolutionSize().height / 320.0;
  150. if (factor === 0)
  151. factor = _t._winSizeInPoints.height / 320.0;
  152. var tmpLabel = new cc.LabelAtlas();
  153. tmpLabel._setIgnoreContentScaleFactor(true);
  154. tmpLabel.initWithString("00.0", texture, 12, 32, '.');
  155. tmpLabel.scale = factor;
  156. _t._FPSLabel = tmpLabel;
  157. tmpLabel = new cc.LabelAtlas();
  158. tmpLabel._setIgnoreContentScaleFactor(true);
  159. tmpLabel.initWithString("0.000", texture, 12, 32, '.');
  160. tmpLabel.scale = factor;
  161. _t._SPFLabel = tmpLabel;
  162. tmpLabel = new cc.LabelAtlas();
  163. tmpLabel._setIgnoreContentScaleFactor(true);
  164. tmpLabel.initWithString("000", texture, 12, 32, '.');
  165. tmpLabel.scale = factor;
  166. _t._drawsLabel = tmpLabel;
  167. var locStatsPosition = cc.DIRECTOR_STATS_POSITION;
  168. _t._drawsLabel.setPosition(locStatsPosition.x, 34 * factor + locStatsPosition.y);
  169. _t._SPFLabel.setPosition(locStatsPosition.x, 17 * factor + locStatsPosition.y);
  170. _t._FPSLabel.setPosition(locStatsPosition);
  171. };
  172. _p._createStatsLabelForCanvas = function () {
  173. var _t = this;
  174. //The original _createStatsLabelForCanvas method
  175. //Because the referenced by a cc.Director.prototype._createStatsLabel
  176. var fontSize = 0;
  177. if (_t._winSizeInPoints.width > _t._winSizeInPoints.height)
  178. fontSize = 0 | (_t._winSizeInPoints.height / 320 * 24);
  179. else
  180. fontSize = 0 | (_t._winSizeInPoints.width / 320 * 24);
  181. _t._FPSLabel = cc.LabelTTF.create("000.0", "Arial", fontSize);
  182. _t._SPFLabel = cc.LabelTTF.create("0.000", "Arial", fontSize);
  183. _t._drawsLabel = cc.LabelTTF.create("0000", "Arial", fontSize);
  184. var locStatsPosition = cc.DIRECTOR_STATS_POSITION;
  185. _t._drawsLabel.setPosition(_t._drawsLabel.width / 2 + locStatsPosition.x, _t._drawsLabel.height * 5 / 2 + locStatsPosition.y);
  186. _t._SPFLabel.setPosition(_t._SPFLabel.width / 2 + locStatsPosition.x, _t._SPFLabel.height * 3 / 2 + locStatsPosition.y);
  187. _t._FPSLabel.setPosition(_t._FPSLabel.width / 2 + locStatsPosition.x, _t._FPSLabel.height / 2 + locStatsPosition.y);
  188. };
  189. _p.convertToGL = function (uiPoint) {
  190. var transform = new cc.kmMat4();
  191. cc.GLToClipTransform(transform);
  192. var transformInv = new cc.kmMat4();
  193. cc.kmMat4Inverse(transformInv, transform);
  194. // Calculate z=0 using -> transform*[0, 0, 0, 1]/w
  195. var zClip = transform.mat[14] / transform.mat[15];
  196. var glSize = this._openGLView.getDesignResolutionSize();
  197. var clipCoord = new cc.kmVec3(2.0 * uiPoint.x / glSize.width - 1.0, 1.0 - 2.0 * uiPoint.y / glSize.height, zClip);
  198. var glCoord = new cc.kmVec3();
  199. cc.kmVec3TransformCoord(glCoord, clipCoord, transformInv);
  200. return cc.p(glCoord.x, glCoord.y);
  201. };
  202. _p.convertToUI = function (glPoint) {
  203. var transform = new cc.kmMat4();
  204. cc.GLToClipTransform(transform);
  205. var clipCoord = new cc.kmVec3();
  206. // Need to calculate the zero depth from the transform.
  207. var glCoord = new cc.kmVec3(glPoint.x, glPoint.y, 0.0);
  208. cc.kmVec3TransformCoord(clipCoord, glCoord, transform);
  209. var glSize = this._openGLView.getDesignResolutionSize();
  210. return cc.p(glSize.width * (clipCoord.x * 0.5 + 0.5), glSize.height * (-clipCoord.y * 0.5 + 0.5));
  211. };
  212. _p.getVisibleSize = function () {
  213. //if (this._openGLView) {
  214. return this._openGLView.getVisibleSize();
  215. //} else {
  216. //return this.getWinSize();
  217. //}
  218. };
  219. _p.getVisibleOrigin = function () {
  220. //if (this._openGLView) {
  221. return this._openGLView.getVisibleOrigin();
  222. //} else {
  223. //return cc.p(0,0);
  224. //}
  225. };
  226. _p.getZEye = function () {
  227. return (this._winSizeInPoints.height / 1.1566 );
  228. };
  229. _p.setViewport = function () {
  230. var view = this._openGLView;
  231. if (view) {
  232. var locWinSizeInPoints = this._winSizeInPoints;
  233. view.setViewPortInPoints(-view._viewPortRect.x/view._scaleX, -view._viewPortRect.y/view._scaleY, locWinSizeInPoints.width, locWinSizeInPoints.height);
  234. }
  235. };
  236. _p.getOpenGLView = function () {
  237. return this._openGLView;
  238. };
  239. _p.getProjection = function () {
  240. return this._projection;
  241. };
  242. _p.setAlphaBlending = function (on) {
  243. if (on)
  244. cc.glBlendFunc(cc.BLEND_SRC, cc.BLEND_DST);
  245. else
  246. cc.glBlendFunc(cc._renderContext.ONE, cc._renderContext.ZERO);
  247. //cc.checkGLErrorDebug();
  248. };
  249. _p.setGLDefaultValues = function () {
  250. var _t = this;
  251. _t.setAlphaBlending(true);
  252. // XXX: Fix me, should enable/disable depth test according the depth format as cocos2d-iphone did
  253. // [self setDepthTest: view_.depthFormat];
  254. _t.setDepthTest(false);
  255. _t.setProjection(_t._projection);
  256. // set other opengl default values
  257. cc._renderContext.clearColor(0.0, 0.0, 0.0, 1.0);
  258. };
  259. }