BaseNodesWebGL.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.WebGLCCNode = function () {
  23. /**
  24. * CCNode
  25. * @type {Object|Function|cc.Node|*}
  26. * @private
  27. */
  28. var _p = cc.Node.prototype;
  29. _p._transform4x4 = null;
  30. _p._stackMatrix = null;
  31. _p._glServerState = null;
  32. _p._camera = null;
  33. _p.ctor = function () {
  34. var _t = this;
  35. _t._initNode();
  36. //WebGL
  37. var mat4 = new cc.kmMat4();
  38. mat4.mat[2] = mat4.mat[3] = mat4.mat[6] = mat4.mat[7] = mat4.mat[8] = mat4.mat[9] = mat4.mat[11] = mat4.mat[14] = 0.0;
  39. mat4.mat[10] = mat4.mat[15] = 1.0;
  40. _t._transform4x4 = mat4;
  41. _t._glServerState = 0;
  42. _t._stackMatrix = new cc.kmMat4();
  43. };
  44. _p.setNodeDirty = function () {
  45. this._transformDirty === false && (this._transformDirty = this._inverseDirty = true);
  46. };
  47. _p.visit = function () {
  48. var _t = this;
  49. // quick return if not visible
  50. if (!_t._visible)
  51. return;
  52. var context = cc._renderContext, i, currentStack = cc.current_stack;
  53. //cc.kmGLPushMatrixWitMat4(_t._stackMatrix);
  54. //optimize performance for javascript
  55. currentStack.stack.push(currentStack.top);
  56. cc.kmMat4Assign(_t._stackMatrix, currentStack.top);
  57. currentStack.top = _t._stackMatrix;
  58. var locGrid = _t.grid;
  59. if (locGrid && locGrid._active)
  60. locGrid.beforeDraw();
  61. _t.transform();
  62. var locChildren = _t._children;
  63. if (locChildren && locChildren.length > 0) {
  64. var childLen = locChildren.length;
  65. _t.sortAllChildren();
  66. // draw children zOrder < 0
  67. for (i = 0; i < childLen; i++) {
  68. if (locChildren[i] && locChildren[i]._localZOrder < 0)
  69. locChildren[i].visit();
  70. else
  71. break;
  72. }
  73. _t.draw(context);
  74. // draw children zOrder >= 0
  75. for (; i < childLen; i++) {
  76. if (locChildren[i]) {
  77. locChildren[i].visit();
  78. }
  79. }
  80. } else
  81. _t.draw(context);
  82. _t.arrivalOrder = 0;
  83. if (locGrid && locGrid._active)
  84. locGrid.afterDraw(_t);
  85. //cc.kmGLPopMatrix();
  86. //optimize performance for javascript
  87. currentStack.top = currentStack.stack.pop();
  88. };
  89. _p.transform = function () {
  90. var _t = this;
  91. //optimize performance for javascript
  92. var t4x4 = _t._transform4x4, topMat4 = cc.current_stack.top;
  93. // Convert 3x3 into 4x4 matrix
  94. //cc.CGAffineToGL(_t.nodeToParentTransform(), _t._transform4x4.mat);
  95. var trans = _t.nodeToParentTransform();
  96. var t4x4Mat = t4x4.mat;
  97. t4x4Mat[0] = trans.a;
  98. t4x4Mat[4] = trans.c;
  99. t4x4Mat[12] = trans.tx;
  100. t4x4Mat[1] = trans.b;
  101. t4x4Mat[5] = trans.d;
  102. t4x4Mat[13] = trans.ty;
  103. // Update Z vertex manually
  104. //_t._transform4x4.mat[14] = _t._vertexZ;
  105. t4x4Mat[14] = _t._vertexZ;
  106. //optimize performance for Javascript
  107. cc.kmMat4Multiply(topMat4, topMat4, t4x4); // = cc.kmGLMultMatrix(_t._transform4x4);
  108. // XXX: Expensive calls. Camera should be integrated into the cached affine matrix
  109. if (_t._camera != null && !(_t.grid != null && _t.grid.isActive())) {
  110. var apx = _t._anchorPointInPoints.x, apy = _t._anchorPointInPoints.y;
  111. var translate = (apx !== 0.0 || apy !== 0.0);
  112. if (translate){
  113. if(!cc.SPRITEBATCHNODE_RENDER_SUBPIXEL) {
  114. apx = 0 | apx;
  115. apy = 0 | apy;
  116. }
  117. cc.kmGLTranslatef(apx, apy, 0);
  118. _t._camera.locate();
  119. cc.kmGLTranslatef(-apx, -apy, 0);
  120. } else {
  121. _t._camera.locate();
  122. }
  123. }
  124. };
  125. _p.getNodeToParentTransform = _p._getNodeToParentTransformForWebGL;
  126. };