Skip to content

Commit 843c0ee

Browse files
committed
Issue cocos2d#2698: rename cc.kmMat4 to cc.math.Matrix4 and refactor math library for better performance.
1 parent 552c219 commit 843c0ee

File tree

17 files changed

+1182
-960
lines changed

17 files changed

+1182
-960
lines changed

cocos2d/core/CCCamera.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ cc.Camera = cc.Class.extend({
6262
* constructor of cc.Camera
6363
*/
6464
ctor:function () {
65-
this._lookupMatrix = new cc.kmMat4();
65+
this._lookupMatrix = new cc.math.Matrix4();
6666
this.restore();
6767
},
6868

@@ -103,7 +103,7 @@ cc.Camera = cc.Class.extend({
103103
this._upY = 1.0;
104104
this._upZ = 0.0;
105105

106-
cc.kmMat4Identity( this._lookupMatrix );
106+
this._lookupMatrix.identity();
107107

108108
this._dirty = false;
109109
},
@@ -116,7 +116,7 @@ cc.Camera = cc.Class.extend({
116116
var eye = new cc.math.Vec3(this._eyeX, this._eyeY , this._eyeZ),
117117
center = new cc.math.Vec3(this._centerX, this._centerY, this._centerZ),
118118
up = new cc.math.Vec3(this._upX, this._upY, this._upZ);
119-
cc.kmMat4LookAt( this._lookupMatrix, eye, center, up);
119+
this._lookupMatrix.lookAt(eye, center, up);
120120
this._dirty = false;
121121
}
122122
cc.kmGLMultMatrix( this._lookupMatrix);
@@ -127,10 +127,10 @@ cc.Camera = cc.Class.extend({
127127
var eye = new cc.math.Vec3(this._eyeX, this._eyeY , this._eyeZ),
128128
center = new cc.math.Vec3(this._centerX, this._centerY, this._centerZ),
129129
up = new cc.math.Vec3(this._upX, this._upY, this._upZ);
130-
cc.kmMat4LookAt( this._lookupMatrix, eye, center, up);
130+
this._lookupMatrix.lookAt(eye, center, up);
131131
this._dirty = false;
132132
}
133-
cc.kmMat4Multiply(matrix, matrix, this._lookupMatrix);
133+
matrix.multiply(this._lookupMatrix);
134134
},
135135

136136
/**

cocos2d/core/CCDirector.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
cc.g_NumberOfDraws = 0;
2828

2929
cc.GLToClipTransform = function (transformOut) {
30-
var projection = new cc.kmMat4();
31-
cc.kmGLGetMatrix(cc.KM_GL_PROJECTION, projection);
30+
//var projection = new cc.math.Matrix4();
31+
//cc.kmGLGetMatrix(cc.KM_GL_PROJECTION, projection);
32+
cc.kmGLGetMatrix(cc.KM_GL_PROJECTION, transformOut);
3233

33-
var modelview = new cc.kmMat4();
34+
var modelview = new cc.math.Matrix4();
3435
cc.kmGLGetMatrix(cc.KM_GL_MODELVIEW, modelview);
3536

36-
cc.kmMat4Multiply(transformOut, projection, modelview);
37+
transformOut.multiply(modelview);
3738
};
3839
//----------------------------------------------------------------------------------------------------------------------
3940

cocos2d/core/CCDirectorWebGL.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,24 @@ if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
5757
case cc.Director.PROJECTION_2D:
5858
cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
5959
cc.kmGLLoadIdentity();
60-
var orthoMatrix = new cc.kmMat4();
61-
cc.kmMat4OrthographicProjection(
62-
orthoMatrix,
60+
var orthoMatrix = cc.math.Matrix4.createOrthographicProjection(
6361
-ox,
64-
size.width - ox,
62+
size.width - ox,
6563
-oy,
66-
size.height - oy,
64+
size.height - oy,
6765
-1024, 1024);
6866
cc.kmGLMultMatrix(orthoMatrix);
6967
cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
7068
cc.kmGLLoadIdentity();
7169
break;
7270
case cc.Director.PROJECTION_3D:
7371
var zeye = _t.getZEye();
74-
var matrixPerspective = new cc.kmMat4(), matrixLookup = new cc.kmMat4();
72+
var matrixPerspective = new cc.math.Matrix4(), matrixLookup = new cc.math.Matrix4();
7573
cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
7674
cc.kmGLLoadIdentity();
7775

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

8179
cc.kmGLMultMatrix(matrixPerspective);
8280

@@ -85,7 +83,7 @@ if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
8583
var eye = new cc.math.Vec3(-ox + size.width / 2, -oy + size.height / 2, zeye);
8684
var center = new cc.math.Vec3( -ox + size.width / 2, -oy + size.height / 2, 0.0);
8785
var up = new cc.math.Vec3( 0.0, 1.0, 0.0);
88-
cc.kmMat4LookAt(matrixLookup, eye, center, up);
86+
matrixLookup.lookAt(eye, center, up);
8987
cc.kmGLMultMatrix(matrixLookup);
9088
break;
9189
case cc.Director.PROJECTION_CUSTOM:
@@ -238,11 +236,10 @@ if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
238236
};
239237

240238
_p.convertToGL = function (uiPoint) {
241-
var transform = new cc.kmMat4();
239+
var transform = new cc.math.Matrix4();
242240
cc.GLToClipTransform(transform);
243241

244-
var transformInv = new cc.kmMat4();
245-
cc.kmMat4Inverse(transformInv, transform);
242+
var transformInv = transform.inverse();
246243

247244
// Calculate z=0 using -> transform*[0, 0, 0, 1]/w
248245
var zClip = transform.mat[14] / transform.mat[15];
@@ -253,7 +250,7 @@ if (cc._renderType === cc._RENDER_TYPE_WEBGL) {
253250
};
254251

255252
_p.convertToUI = function (glPoint) {
256-
var transform = new cc.kmMat4();
253+
var transform = new cc.math.Matrix4();
257254
cc.GLToClipTransform(transform);
258255

259256
var clipCoord = new cc.math.Vec3(glPoint.x, glPoint.y, 0.0);

cocos2d/core/base-nodes/CCNodeWebGLRenderCmd.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
cc.Node.WebGLRenderCmd = function (renderable) {
2727
cc.Node.RenderCmd.call(this, renderable);
2828

29-
var mat4 = new cc.kmMat4();
30-
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;
31-
mat4.mat[10] = mat4.mat[15] = 1.0;
29+
var mat4 = new cc.math.Matrix4(), mat = mat4.mat;
30+
mat[2] = mat[3] = mat[6] = mat[7] = mat[8] = mat[9] = mat[11] = mat[14] = 0.0;
31+
mat[10] = mat[15] = 1.0;
3232
this._transform4x4 = mat4;
33-
this._stackMatrix = new cc.kmMat4();
33+
this._stackMatrix = new cc.math.Matrix4();
3434
this._shaderProgram = null;
3535

3636
this._camera = null;
@@ -223,15 +223,15 @@
223223
apy = 0 | apy;
224224
}
225225
//cc.kmGLTranslatef(apx, apy, 0);
226-
var translation = new cc.kmMat4();
227-
cc.kmMat4Translation(translation, apx, apy, 0);
228-
cc.kmMat4Multiply(stackMatrix, stackMatrix, translation);
226+
var translation = cc.math.Matrix4.createByTranslation(apx, apy, 0, t4x4); //t4x4 as a temp matrix
227+
stackMatrix.multiply(translation);
229228

230229
node._camera._locateForRenderer(stackMatrix);
231230

232-
//cc.kmGLTranslatef(-apx, -apy, 0);
233-
cc.kmMat4Translation(translation, -apx, -apy, 0);
234-
cc.kmMat4Multiply(stackMatrix, stackMatrix, translation);
231+
//cc.kmGLTranslatef(-apx, -apy, 0); optimize at here : kmGLTranslatef
232+
translation = cc.math.Matrix4.createByTranslation(apx, apy, 0, translation);
233+
stackMatrix.multiply(translation);
234+
t4x4.identity(); //reset t4x4;
235235
} else {
236236
node._camera._locateForRenderer(stackMatrix);
237237
}

cocos2d/effects/CCGrid.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,15 @@ cc.GridBase = cc.Class.extend(/** @lends cc.GridBase# */{
229229
// XXX: Camera should be applied in the AnchorPoint
230230
//
231231
//cc.kmGLTranslatef(offset.x, offset.y, 0);
232-
var translation = new cc.kmMat4();
233-
cc.kmMat4Translation(translation, offset.x, offset.y, 0);
234-
cc.kmMat4Multiply(stackMatrix, stackMatrix, translation);
232+
var translation = cc.math.Matrix4.createByTranslation(offset.x, offset.y, 0);
233+
stackMatrix.multiply(translation);
235234

236235
//target.getCamera().locate();
237236
target._camera._locateForRenderer(stackMatrix);
238237

239238
//cc.kmGLTranslatef(-offset.x, -offset.y, 0);
240-
cc.kmMat4Translation(translation, -offset.x, -offset.y, 0);
241-
cc.kmMat4Multiply(stackMatrix, stackMatrix, translation);
239+
translation = cc.math.Matrix4.createByTranslation(-offset.x, -offset.y, 0, translation);
240+
stackMatrix.multiply(translation);
242241
}
243242

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

268-
var orthoMatrix = new cc.kmMat4();
269-
cc.kmMat4OrthographicProjection(orthoMatrix, 0, winSize.width, 0, winSize.height, -1, 1);
267+
var orthoMatrix = cc.math.Matrix4.createOrthographicProjection(0, winSize.width, 0, winSize.height, -1, 1);
270268
cc.kmGLMultMatrix(orthoMatrix);
271269

272270
cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);

cocos2d/kazmath/aabb.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cc.math.AABB = function (min, max) {
3939

4040
/**
4141
* Returns true if point is in the specified AABB, returns false otherwise.
42-
* @param {cc.kmVec3} point
42+
* @param {cc.math.Vec3} point
4343
* @returns {boolean}
4444
*/
4545
cc.math.AABB.prototype.containsPoint = function (point) {
@@ -62,17 +62,17 @@ cc.math.AABB.containsPoint = function (pPoint, pBox) {
6262
* Assigns aabb to current AABB object
6363
* @param {cc.math.AABB} aabb
6464
*/
65-
cc.math.AABB.prototype.assign = function(aabb){
66-
this.min.assign(aabb.min);
67-
this.max.assign(aabb.max);
65+
cc.math.AABB.prototype.assignFrom = function(aabb){
66+
this.min.assignFrom(aabb.min);
67+
this.max.assignFrom(aabb.max);
6868
};
6969

7070
/**
7171
* Assigns pIn to pOut, returns pOut.
7272
*/
7373
cc.math.AABB.assign = function (pOut, pIn) { //cc.kmAABBAssign
74-
pOut.min.assign(pIn.min);
75-
pOut.max.assign(pIn.max);
74+
pOut.min.assignFrom(pIn.min);
75+
pOut.max.assignFrom(pIn.max);
7676
return pOut;
7777
};
7878

cocos2d/kazmath/gl/mat4stack.js

+48-27
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,54 @@
2626
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727
*/
2828

29-
cc.km_mat4_stack = function(capacity, item_count, top, stack){
30-
this.top = top ;
31-
this.stack = stack ;
32-
};
33-
34-
cc.km_mat4_stack.INITIAL_SIZE = 30;
35-
36-
cc.km_mat4_stack_initialize = function(stack){
37-
stack.stack = []; //allocate the memory
38-
stack.top = null; //Set the top to NULL
39-
};
40-
41-
cc.km_mat4_stack_push = function(stack, item){
42-
stack.stack.push(stack.top);
43-
stack.top = new cc.kmMat4();
44-
cc.kmMat4Assign(stack.top, item);
45-
};
46-
47-
cc.km_mat4_stack_pop = function(stack, pOut){
48-
stack.top = stack.stack.pop();
49-
};
50-
51-
cc.km_mat4_stack_release = function(stack){
52-
stack.stack = null;
53-
stack.top = null;
54-
stack = null;
55-
};
29+
(function(cc){
30+
/**
31+
* The stack of cc.math.Matrix4
32+
* @param {cc.math.Matrix4} [top]
33+
* @param {Array} [stack]
34+
* @constructor
35+
*/
36+
cc.math.Matrix4Stack = function(top, stack) {
37+
this.top = top;
38+
this.stack = stack || [];
39+
};
40+
cc.km_mat4_stack = cc.math.Matrix4Stack;
41+
var proto = cc.math.Matrix4Stack.prototype;
42+
43+
proto.initialize = function() { //cc.km_mat4_stack_initialize
44+
this.stack.length = 0;
45+
this.top = null;
46+
};
47+
48+
//for compatibility
49+
cc.km_mat4_stack_push = function(stack, item){
50+
stack.stack.push(stack.top);
51+
stack.top = new cc.math.Matrix4(item);
52+
};
53+
54+
cc.km_mat4_stack_pop = function(stack, pOut){
55+
stack.top = stack.stack.pop();
56+
};
57+
58+
cc.km_mat4_stack_release = function(stack){
59+
stack.stack = null;
60+
stack.top = null;
61+
};
62+
63+
proto.push = function(item) {
64+
this.stack.push(this.top);
65+
this.top = new cc.math.Matrix4(item);
66+
};
67+
68+
proto.pop = function() {
69+
this.top = this.stack.pop();
70+
};
71+
72+
proto.release = function(){
73+
this.stack = null;
74+
this.top = null;
75+
};
76+
})(cc);
5677

5778

5879

0 commit comments

Comments
 (0)