Skip to content

Issue #2698: rename cc.kmMat4 to cc.math.Matrix4 and refactor math library for better performance. #2731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 13, 2015
30 changes: 11 additions & 19 deletions cocos2d/core/CCCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cc.Camera = cc.Class.extend({
* constructor of cc.Camera
*/
ctor:function () {
this._lookupMatrix = new cc.kmMat4();
this._lookupMatrix = new cc.math.Matrix4();
this.restore();
},

Expand Down Expand Up @@ -103,7 +103,7 @@ cc.Camera = cc.Class.extend({
this._upY = 1.0;
this._upZ = 0.0;

cc.kmMat4Identity( this._lookupMatrix );
this._lookupMatrix.identity();

this._dirty = false;
},
Expand All @@ -113,32 +113,24 @@ cc.Camera = cc.Class.extend({
*/
locate:function () {
if (this._dirty) {
var eye = new cc.kmVec3(), center = new cc.kmVec3(), up = new cc.kmVec3();

cc.kmVec3Fill( eye, this._eyeX, this._eyeY , this._eyeZ );
cc.kmVec3Fill( center, this._centerX, this._centerY, this._centerZ);

cc.kmVec3Fill( up, this._upX, this._upY, this._upZ);
cc.kmMat4LookAt( this._lookupMatrix, eye, center, up);

var eye = new cc.math.Vec3(this._eyeX, this._eyeY , this._eyeZ),
center = new cc.math.Vec3(this._centerX, this._centerY, this._centerZ),
up = new cc.math.Vec3(this._upX, this._upY, this._upZ);
this._lookupMatrix.lookAt(eye, center, up);
this._dirty = false;
}
cc.kmGLMultMatrix( this._lookupMatrix);
},

_locateForRenderer: function(matrix){
if (this._dirty) {
var eye = new cc.kmVec3(), center = new cc.kmVec3(), up = new cc.kmVec3();

cc.kmVec3Fill( eye, this._eyeX, this._eyeY , this._eyeZ );
cc.kmVec3Fill( center, this._centerX, this._centerY, this._centerZ);

cc.kmVec3Fill( up, this._upX, this._upY, this._upZ);
cc.kmMat4LookAt( this._lookupMatrix, eye, center, up);

var eye = new cc.math.Vec3(this._eyeX, this._eyeY , this._eyeZ),
center = new cc.math.Vec3(this._centerX, this._centerY, this._centerZ),
up = new cc.math.Vec3(this._upX, this._upY, this._upZ);
this._lookupMatrix.lookAt(eye, center, up);
this._dirty = false;
}
cc.kmMat4Multiply(matrix, matrix, this._lookupMatrix);
matrix.multiply(this._lookupMatrix);
},

/**
Expand Down
9 changes: 5 additions & 4 deletions cocos2d/core/CCDirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
cc.g_NumberOfDraws = 0;

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

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

cc.kmMat4Multiply(transformOut, projection, modelview);
transformOut.multiply(modelview);
};
//----------------------------------------------------------------------------------------------------------------------

Expand Down
41 changes: 16 additions & 25 deletions cocos2d/core/CCDirectorWebGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,33 @@ if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
case cc.Director.PROJECTION_2D:
cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
cc.kmGLLoadIdentity();
var orthoMatrix = new cc.kmMat4();
cc.kmMat4OrthographicProjection(
orthoMatrix,
var orthoMatrix = cc.math.Matrix4.createOrthographicProjection(
-ox,
size.width - ox,
size.width - ox,
-oy,
size.height - oy,
size.height - oy,
-1024, 1024);
cc.kmGLMultMatrix(orthoMatrix);
cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
cc.kmGLLoadIdentity();
break;
case cc.Director.PROJECTION_3D:
var zeye = _t.getZEye();
var matrixPerspective = new cc.kmMat4(), matrixLookup = new cc.kmMat4();
var matrixPerspective = new cc.math.Matrix4(), matrixLookup = new cc.math.Matrix4();
cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
cc.kmGLLoadIdentity();

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

cc.kmGLMultMatrix(matrixPerspective);

cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
cc.kmGLLoadIdentity();
var eye = cc.kmVec3Fill(null, -ox + size.width / 2, -oy + size.height / 2, zeye);
var center = cc.kmVec3Fill(null, -ox + size.width / 2, -oy + size.height / 2, 0.0);
var up = cc.kmVec3Fill(null, 0.0, 1.0, 0.0);
cc.kmMat4LookAt(matrixLookup, eye, center, up);
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);
matrixLookup.lookAt(eye, center, up);
cc.kmGLMultMatrix(matrixLookup);
break;
case cc.Director.PROJECTION_CUSTOM:
Expand Down Expand Up @@ -238,38 +236,31 @@ if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
};

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

var transformInv = new cc.kmMat4();
cc.kmMat4Inverse(transformInv, 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 clipCoord = new cc.kmVec3(2.0 * uiPoint.x / glSize.width - 1.0, 1.0 - 2.0 * uiPoint.y / glSize.height, zClip);

var glCoord = new cc.kmVec3();
cc.kmVec3TransformCoord(glCoord, clipCoord, transformInv);

var glCoord = new cc.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.kmMat4();
var transform = new cc.math.Matrix4();
cc.GLToClipTransform(transform);

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

var glSize = this._openGLView.getDesignResolutionSize();
return cc.p(glSize.width * (clipCoord.x * 0.5 + 0.5), glSize.height * (-clipCoord.y * 0.5 + 0.5));
};


_p.getVisibleSize = function () {
//if (this._openGLView) {
return this._openGLView.getVisibleSize();
Expand Down
20 changes: 10 additions & 10 deletions cocos2d/core/base-nodes/CCNodeWebGLRenderCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
cc.Node.WebGLRenderCmd = function (renderable) {
cc.Node.RenderCmd.call(this, renderable);

var mat4 = new cc.kmMat4();
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;
mat4.mat[10] = mat4.mat[15] = 1.0;
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.kmMat4();
this._stackMatrix = new cc.math.Matrix4();
this._shaderProgram = null;

this._camera = null;
Expand Down Expand Up @@ -223,15 +223,15 @@
apy = 0 | apy;
}
//cc.kmGLTranslatef(apx, apy, 0);
var translation = new cc.kmMat4();
cc.kmMat4Translation(translation, apx, apy, 0);
cc.kmMat4Multiply(stackMatrix, stackMatrix, translation);
var translation = cc.math.Matrix4.createByTranslation(apx, apy, 0, t4x4); //t4x4 as a temp matrix
stackMatrix.multiply(translation);

node._camera._locateForRenderer(stackMatrix);

//cc.kmGLTranslatef(-apx, -apy, 0);
cc.kmMat4Translation(translation, -apx, -apy, 0);
cc.kmMat4Multiply(stackMatrix, stackMatrix, translation);
//cc.kmGLTranslatef(-apx, -apy, 0); optimize at here : kmGLTranslatef
translation = cc.math.Matrix4.createByTranslation(apx, apy, 0, translation);
stackMatrix.multiply(translation);
t4x4.identity(); //reset t4x4;
} else {
node._camera._locateForRenderer(stackMatrix);
}
Expand Down
12 changes: 5 additions & 7 deletions cocos2d/effects/CCGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,15 @@ cc.GridBase = cc.Class.extend(/** @lends cc.GridBase# */{
// XXX: Camera should be applied in the AnchorPoint
//
//cc.kmGLTranslatef(offset.x, offset.y, 0);
var translation = new cc.kmMat4();
cc.kmMat4Translation(translation, offset.x, offset.y, 0);
cc.kmMat4Multiply(stackMatrix, stackMatrix, translation);
var translation = cc.math.Matrix4.createByTranslation(offset.x, offset.y, 0);
stackMatrix.multiply(translation);

//target.getCamera().locate();
target._camera._locateForRenderer(stackMatrix);

//cc.kmGLTranslatef(-offset.x, -offset.y, 0);
cc.kmMat4Translation(translation, -offset.x, -offset.y, 0);
cc.kmMat4Multiply(stackMatrix, stackMatrix, translation);
translation = cc.math.Matrix4.createByTranslation(-offset.x, -offset.y, 0, translation);
stackMatrix.multiply(translation);
}

cc.glBindTexture2D(this._texture);
Expand All @@ -265,8 +264,7 @@ cc.GridBase = cc.Class.extend(/** @lends cc.GridBase# */{
cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
cc.kmGLLoadIdentity();

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

cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
Expand Down
52 changes: 31 additions & 21 deletions cocos2d/kazmath/aabb.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,52 @@
*/

/**
* A struture that represents an axis-aligned
* bounding box.
* A structure that represents an axis-aligned bounding box.
* cc.kmAABB => cc.math.AABB
*/
cc.kmAABB = function (min, max) {
cc.math.AABB = function (min, max) {
/** The max corner of the box */
this.min = min || new cc.kmVec3();
this.min = min || new cc.math.Vec3();
/** The min corner of the box */
this.max = max || new cc.kmVec3();
this.max = max || new cc.math.Vec3();
};

/**
* Returns KM_TRUE if point is in the specified AABB, returns
* KM_FALSE otherwise.
* Returns true if point is in the specified AABB, returns false otherwise.
* @param {cc.math.Vec3} point
* @returns {boolean}
*/
cc.kmAABBContainsPoint = function (pPoint, pBox) {
if (pPoint.x >= pBox.min.x && pPoint.x <= pBox.max.x &&
cc.math.AABB.prototype.containsPoint = function (point) {
return (point.x >= this.min.x && point.x <= this.max.x &&
point.y >= this.min.y && point.y <= this.max.y &&
point.z >= this.min.z && point.z <= this.max.z);
};

/**
* Returns true if point is in the specified AABB, returns
* false otherwise.
*/
cc.math.AABB.containsPoint = function (pPoint, pBox) {
return (pPoint.x >= pBox.min.x && pPoint.x <= pBox.max.x &&
pPoint.y >= pBox.min.y && pPoint.y <= pBox.max.y &&
pPoint.z >= pBox.min.z && pPoint.z <= pBox.max.z) {
return cc.KM_TRUE;
}
return cc.KM_FALSE;
pPoint.z >= pBox.min.z && pPoint.z <= pBox.max.z);
};

/**
* Assigns pIn to pOut, returns pOut.
* Assigns aabb to current AABB object
* @param {cc.math.AABB} aabb
*/
cc.kmAABBAssign = function (pOut, pIn) {
cc.kmVec3Assign(pOut.min, pIn.min);
cc.kmVec3Assign(pOut.max, pIn.max);
return pOut;
cc.math.AABB.prototype.assignFrom = function(aabb){
this.min.assignFrom(aabb.min);
this.max.assignFrom(aabb.max);
};

/**
* Scales pIn by s, stores the resulting AABB in pOut. Returns pOut
* Assigns pIn to pOut, returns pOut.
*/
cc.kmAABBScale = function (pOut, pIn, s) {
cc.log("cc.kmAABBScale hasn't been supported.");
cc.math.AABB.assign = function (pOut, pIn) { //cc.kmAABBAssign
pOut.min.assignFrom(pIn.min);
pOut.max.assignFrom(pIn.max);
return pOut;
};

75 changes: 48 additions & 27 deletions cocos2d/kazmath/gl/mat4stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,54 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

cc.km_mat4_stack = function(capacity, item_count, top, stack){
this.top = top ;
this.stack = stack ;
};

cc.km_mat4_stack.INITIAL_SIZE = 30;

cc.km_mat4_stack_initialize = function(stack){
stack.stack = []; //allocate the memory
stack.top = null; //Set the top to NULL
};

cc.km_mat4_stack_push = function(stack, item){
stack.stack.push(stack.top);
stack.top = new cc.kmMat4();
cc.kmMat4Assign(stack.top, item);
};

cc.km_mat4_stack_pop = function(stack, pOut){
stack.top = stack.stack.pop();
};

cc.km_mat4_stack_release = function(stack){
stack.stack = null;
stack.top = null;
stack = null;
};
(function(cc){
/**
* The stack of cc.math.Matrix4
* @param {cc.math.Matrix4} [top]
* @param {Array} [stack]
* @constructor
*/
cc.math.Matrix4Stack = function(top, stack) {
this.top = top;
this.stack = stack || [];
};
cc.km_mat4_stack = cc.math.Matrix4Stack;
var proto = cc.math.Matrix4Stack.prototype;

proto.initialize = function() { //cc.km_mat4_stack_initialize
this.stack.length = 0;
this.top = null;
};

//for compatibility
cc.km_mat4_stack_push = function(stack, item){
stack.stack.push(stack.top);
stack.top = new cc.math.Matrix4(item);
};

cc.km_mat4_stack_pop = function(stack, pOut){
stack.top = stack.stack.pop();
};

cc.km_mat4_stack_release = function(stack){
stack.stack = null;
stack.top = null;
};

proto.push = function(item) {
this.stack.push(this.top);
this.top = new cc.math.Matrix4(item);
};

proto.pop = function() {
this.top = this.stack.pop();
};

proto.release = function(){
this.stack = null;
this.top = null;
};
})(cc);



Expand Down
Loading