Skip to content

Commit

Permalink
Cleanup kazmath module
Browse files Browse the repository at this point in the history
  • Loading branch information
pandamicro committed Mar 14, 2016
1 parent 8306801 commit 6a3bc53
Show file tree
Hide file tree
Showing 44 changed files with 3,067 additions and 3,150 deletions.
24 changes: 12 additions & 12 deletions cocos2d/clipping-nodes/CCClippingNodeWebGLRenderCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,28 @@

proto._drawFullScreenQuadClearStencil = function () {
// draw a fullscreen solid rectangle to clear the stencil buffer
var projStack = cc.projection_matrix_stack;
//cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
//cc.kmGLPushMatrix();
//cc.kmGLLoadIdentity();
var projStack = cc.math.projection_matrix_stack;
//cc.math.glMatrixMode(cc.math.KM_GL_PROJECTION);
//cc.math.glPushMatrix();
//cc.math.glLoadIdentity();
projStack.push();
projStack.top.identity();

//cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
//cc.kmGLPushMatrix();
//cc.kmGLLoadIdentity();
var modelViewStack = cc.modelview_matrix_stack;
//cc.math.glMatrixMode(cc.math.KM_GL_MODELVIEW);
//cc.math.glPushMatrix();
//cc.math.glLoadIdentity();
var modelViewStack = cc.math.modelview_matrix_stack;
modelViewStack.push();
modelViewStack.top.identity();

cc._drawingUtil.drawSolidRect(cc.p(-1, -1), cc.p(1, 1), cc.color(255, 255, 255, 255));

//cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
//cc.kmGLPopMatrix();
//cc.math.glMatrixMode(cc.math.KM_GL_PROJECTION);
//cc.math.glPopMatrix();
projStack.pop();

//cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
//cc.kmGLPopMatrix();
//cc.math.glMatrixMode(cc.math.KM_GL_MODELVIEW);
//cc.math.glPopMatrix();
modelViewStack.pop();
};

Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/CCCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ cc.Camera = cc._Class.extend({
this._lookupMatrix.lookAt(eye, center, up);
this._dirty = false;
}
cc.kmGLMultMatrix( this._lookupMatrix);
cc.math.glMultMatrix( this._lookupMatrix);
},

_locateForRenderer: function(matrix){
Expand Down
59 changes: 31 additions & 28 deletions cocos2d/core/CCDirectorWebGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@

require('./CCDirector');
require('./CCGame');
require('../kazmath');

var math = cc.math;

var GLToClipTransform = function (transformOut) {
//var projection = new cc.math.Matrix4();
//cc.kmGLGetMatrix(cc.KM_GL_PROJECTION, projection);
cc.kmGLGetMatrix(cc.KM_GL_PROJECTION, transformOut);
//var projection = new math.Matrix4();
//math.glGetMatrix(math.KM_GL_PROJECTION, projection);
math.glGetMatrix(math.KM_GL_PROJECTION, transformOut);

var modelview = new cc.math.Matrix4();
cc.kmGLGetMatrix(cc.KM_GL_MODELVIEW, modelview);
var modelview = new math.Matrix4();
math.glGetMatrix(math.KM_GL_MODELVIEW, modelview);

transformOut.multiply(modelview);
};
Expand Down Expand Up @@ -89,36 +92,36 @@ cc.game.once(cc.game.EVENT_RENDERER_INITED, function () {

switch (projection) {
case cc.Director.PROJECTION_2D:
cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
cc.kmGLLoadIdentity();
var orthoMatrix = cc.math.Matrix4.createOrthographicProjection(
math.glMatrixMode(math.KM_GL_PROJECTION);
math.glLoadIdentity();
var orthoMatrix = math.Matrix4.createOrthographicProjection(
-ox,
size.width - ox,
-oy,
size.height - oy,
-1024, 1024);
cc.kmGLMultMatrix(orthoMatrix);
cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
cc.kmGLLoadIdentity();
math.glMultMatrix(orthoMatrix);
math.glMatrixMode(math.KM_GL_MODELVIEW);
math.glLoadIdentity();
break;
case cc.Director.PROJECTION_3D:
var zeye = _t.getZEye();
var matrixPerspective = new cc.math.Matrix4(), matrixLookup = new cc.math.Matrix4();
cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
cc.kmGLLoadIdentity();
var matrixPerspective = new math.Matrix4(), matrixLookup = new math.Matrix4();
math.glMatrixMode(math.KM_GL_PROJECTION);
math.glLoadIdentity();

// issue #1334
matrixPerspective = cc.math.Matrix4.createPerspectiveProjection(60, size.width / size.height, 0.1, zeye * 2);
matrixPerspective = math.Matrix4.createPerspectiveProjection(60, size.width / size.height, 0.1, zeye * 2);

cc.kmGLMultMatrix(matrixPerspective);
math.glMultMatrix(matrixPerspective);

cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
cc.kmGLLoadIdentity();
var eye = new cc.math.Vec3(-ox + size.width / 2, -oy + size.height / 2, zeye);
var center = new cc.math.Vec3( -ox + size.width / 2, -oy + size.height / 2, 0.0);
var up = new cc.math.Vec3( 0.0, 1.0, 0.0);
math.glMatrixMode(math.KM_GL_MODELVIEW);
math.glLoadIdentity();
var eye = new math.Vec3(-ox + size.width / 2, -oy + size.height / 2, zeye);
var center = new math.Vec3( -ox + size.width / 2, -oy + size.height / 2, 0.0);
var up = new math.Vec3( 0.0, 1.0, 0.0);
matrixLookup.lookAt(eye, center, up);
cc.kmGLMultMatrix(matrixLookup);
math.glMultMatrix(matrixLookup);
break;
case cc.Director.PROJECTION_CUSTOM:
if (_t._projectionDelegate)
Expand Down Expand Up @@ -179,32 +182,32 @@ cc.game.once(cc.game.EVENT_RENDERER_INITED, function () {
};

_p._beforeVisitScene = function () {
cc.kmGLPushMatrix();
math.glPushMatrix();
};

_p._afterVisitScene = function () {
cc.kmGLPopMatrix();
math.glPopMatrix();
};

_p.convertToGL = function (uiPoint) {
var transform = new cc.math.Matrix4();
var transform = new math.Matrix4();
GLToClipTransform(transform);

var transformInv = transform.inverse();

// Calculate z=0 using -> transform*[0, 0, 0, 1]/w
var zClip = transform.mat[14] / transform.mat[15];
var glSize = this._openGLView.getDesignResolutionSize();
var glCoord = new cc.math.Vec3(2.0 * uiPoint.x / glSize.width - 1.0, 1.0 - 2.0 * uiPoint.y / glSize.height, zClip);
var glCoord = new math.Vec3(2.0 * uiPoint.x / glSize.width - 1.0, 1.0 - 2.0 * uiPoint.y / glSize.height, zClip);
glCoord.transformCoord(transformInv);
return cc.p(glCoord.x, glCoord.y);
};

_p.convertToUI = function (glPoint) {
var transform = new cc.math.Matrix4();
var transform = new math.Matrix4();
GLToClipTransform(transform);

var clipCoord = new cc.math.Vec3(glPoint.x, glPoint.y, 0.0);
var clipCoord = new math.Vec3(glPoint.x, glPoint.y, 0.0);
// Need to calculate the zero depth from the transform.
clipCoord.transformCoord(transform);

Expand Down
161 changes: 81 additions & 80 deletions cocos2d/core/base-nodes/CCSGNodeWebGLRenderCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,83 +22,84 @@
THE SOFTWARE.
****************************************************************************/
// ------------------------------ The ccsg.Node's render command for WebGL ----------------------------------
(function() {
_ccsg.Node.WebGLRenderCmd = function (renderable) {
_ccsg.Node.RenderCmd.call(this, renderable);

var mat4 = new cc.math.Matrix4(), mat = mat4.mat;
mat[2] = mat[3] = mat[6] = mat[7] = mat[8] = mat[9] = mat[11] = mat[14] = 0.0;
mat[10] = mat[15] = 1.0;
this._transform4x4 = mat4;
this._stackMatrix = new cc.math.Matrix4();
this._shaderProgram = null;

this._camera = null;
};

var proto = _ccsg.Node.WebGLRenderCmd.prototype = Object.create(_ccsg.Node.RenderCmd.prototype);
proto.constructor = _ccsg.Node.WebGLRenderCmd;

proto._updateColor = function(){};

proto.visit = function (parentCmd) {
var node = this._node;
// quick return if not visible
if (!node._visible)
return;

parentCmd = parentCmd || this.getParentRenderCmd();
if (node._parent && node._parent._renderCmd)
this._curLevel = node._parent._renderCmd._curLevel + 1;

var currentStack = cc.current_stack;

//optimize performance for javascript
currentStack.stack.push(currentStack.top);
this._syncStatus(parentCmd);
currentStack.top = this._stackMatrix;
this.visitChildren();
//optimize performance for javascript
currentStack.top = currentStack.stack.pop();
};

proto.transform = function (parentCmd, recursive) {
var t4x4 = this._transform4x4, stackMatrix = this._stackMatrix, node = this._node;
parentCmd = parentCmd || this.getParentRenderCmd();
var parentMatrix = (parentCmd ? parentCmd._stackMatrix : cc.current_stack.top);

// Convert 3x3 into 4x4 matrix
var trans = this.getNodeToParentTransform();

this._dirtyFlag = this._dirtyFlag & _ccsg.Node._dirtyFlags.transformDirty ^ this._dirtyFlag;

var t4x4Mat = t4x4.mat;
t4x4Mat[0] = trans.a;
t4x4Mat[4] = trans.c;
t4x4Mat[12] = trans.tx;
t4x4Mat[1] = trans.b;
t4x4Mat[5] = trans.d;
t4x4Mat[13] = trans.ty;

// Update Z vertex manually
t4x4Mat[14] = node._vertexZ;

//optimize performance for Javascript
cc.kmMat4Multiply(stackMatrix, parentMatrix, t4x4);

if(!recursive || !node._children || node._children.length === 0)
return;
var i, len, locChildren = node._children;
for(i = 0, len = locChildren.length; i< len; i++){
locChildren[i]._renderCmd.transform(this, recursive);
}
};

proto.setShaderProgram = function (shaderProgram) {
this._shaderProgram = shaderProgram;
};

proto.getShaderProgram = function () {
return this._shaderProgram;
};
})();

var math = cc.math;

_ccsg.Node.WebGLRenderCmd = function (renderable) {
_ccsg.Node.RenderCmd.call(this, renderable);

var mat4 = new math.Matrix4(), mat = mat4.mat;
mat[2] = mat[3] = mat[6] = mat[7] = mat[8] = mat[9] = mat[11] = mat[14] = 0.0;
mat[10] = mat[15] = 1.0;
this._transform4x4 = mat4;
this._stackMatrix = new math.Matrix4();
this._shaderProgram = null;

this._camera = null;
};

var proto = _ccsg.Node.WebGLRenderCmd.prototype = Object.create(_ccsg.Node.RenderCmd.prototype);
proto.constructor = _ccsg.Node.WebGLRenderCmd;

proto._updateColor = function(){};

proto.visit = function (parentCmd) {
var node = this._node;
// quick return if not visible
if (!node._visible)
return;

parentCmd = parentCmd || this.getParentRenderCmd();
if (node._parent && node._parent._renderCmd)
this._curLevel = node._parent._renderCmd._curLevel + 1;

var currentStack = cc.current_stack;

//optimize performance for javascript
currentStack.stack.push(currentStack.top);
this._syncStatus(parentCmd);
currentStack.top = this._stackMatrix;
this.visitChildren();
//optimize performance for javascript
currentStack.top = currentStack.stack.pop();
};

proto.transform = function (parentCmd, recursive) {
var t4x4 = this._transform4x4, stackMatrix = this._stackMatrix, node = this._node;
parentCmd = parentCmd || this.getParentRenderCmd();
var parentMatrix = (parentCmd ? parentCmd._stackMatrix : cc.current_stack.top);

// Convert 3x3 into 4x4 matrix
var trans = this.getNodeToParentTransform();

this._dirtyFlag = this._dirtyFlag & _ccsg.Node._dirtyFlags.transformDirty ^ this._dirtyFlag;

var t4x4Mat = t4x4.mat;
t4x4Mat[0] = trans.a;
t4x4Mat[4] = trans.c;
t4x4Mat[12] = trans.tx;
t4x4Mat[1] = trans.b;
t4x4Mat[5] = trans.d;
t4x4Mat[13] = trans.ty;

// Update Z vertex manually
t4x4Mat[14] = node._vertexZ;

//optimize performance for Javascript
math.mat4Multiply(stackMatrix, parentMatrix, t4x4);

if(!recursive || !node._children || node._children.length === 0)
return;
var i, len, locChildren = node._children;
for(i = 0, len = locChildren.length; i< len; i++){
locChildren[i]._renderCmd.transform(this, recursive);
}
};

proto.setShaderProgram = function (shaderProgram) {
this._shaderProgram = shaderProgram;
};

proto.getShaderProgram = function () {
return this._shaderProgram;
};
4 changes: 2 additions & 2 deletions cocos2d/core/sprites/CCSGSpriteWebGLRenderCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@
if (cc.Macro.SPRITE_DEBUG_DRAW === 0 && !node._showNode)
return;

cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
//cc.kmGLPushMatrixWitMat4(node._stackMatrix);
cc.math.glMatrixMode(cc.math.KM_GL_MODELVIEW);
//cc.math.glPushMatrixWitMat4(node._stackMatrix);
cc.current_stack.stack.push(cc.current_stack.top);
cc.current_stack.top = this._stackMatrix;

Expand Down
12 changes: 6 additions & 6 deletions cocos2d/effects/CCGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,15 @@ cc.GridBase = cc._Class.extend(/** @lends cc.GridBase# */{

var gl = cc._renderContext;
gl.viewport(0, 0, winSize.width , winSize.height);
cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
cc.kmGLLoadIdentity();
cc.math.glMatrixMode(cc.math.KM_GL_PROJECTION);
cc.math.glLoadIdentity();

var orthoMatrix = cc.math.Matrix4.createOrthographicProjection(0, winSize.width, 0, winSize.height, -1, 1);
cc.kmGLMultMatrix(orthoMatrix);
cc.math.glMultMatrix(orthoMatrix);

cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
cc.kmGLLoadIdentity();
cc.setProjectionMatrixDirty()
cc.math.glMatrixMode(cc.math.KM_GL_MODELVIEW);
cc.math.glLoadIdentity();
cc.setProjectionMatrixDirty();
}
});

Expand Down
Loading

0 comments on commit 6a3bc53

Please sign in to comment.