Skip to content

Commit 9933a52

Browse files
committed
Merge pull request #2731 from dingpinglv/Iss2698_namespace
Issue #2698: rename cc.kmMat4 to cc.math.Matrix4 and refactor math library for better performance.
2 parents 2764163 + a2114e0 commit 9933a52

21 files changed

+2510
-2411
lines changed

cocos2d/core/CCCamera.js

+11-19
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
},
@@ -113,32 +113,24 @@ cc.Camera = cc.Class.extend({
113113
*/
114114
locate:function () {
115115
if (this._dirty) {
116-
var eye = new cc.kmVec3(), center = new cc.kmVec3(), up = new cc.kmVec3();
117-
118-
cc.kmVec3Fill( eye, this._eyeX, this._eyeY , this._eyeZ );
119-
cc.kmVec3Fill( center, this._centerX, this._centerY, this._centerZ);
120-
121-
cc.kmVec3Fill( up, this._upX, this._upY, this._upZ);
122-
cc.kmMat4LookAt( this._lookupMatrix, eye, center, up);
123-
116+
var eye = new cc.math.Vec3(this._eyeX, this._eyeY , this._eyeZ),
117+
center = new cc.math.Vec3(this._centerX, this._centerY, this._centerZ),
118+
up = new cc.math.Vec3(this._upX, this._upY, this._upZ);
119+
this._lookupMatrix.lookAt(eye, center, up);
124120
this._dirty = false;
125121
}
126122
cc.kmGLMultMatrix( this._lookupMatrix);
127123
},
128124

129125
_locateForRenderer: function(matrix){
130126
if (this._dirty) {
131-
var eye = new cc.kmVec3(), center = new cc.kmVec3(), up = new cc.kmVec3();
132-
133-
cc.kmVec3Fill( eye, this._eyeX, this._eyeY , this._eyeZ );
134-
cc.kmVec3Fill( center, this._centerX, this._centerY, this._centerZ);
135-
136-
cc.kmVec3Fill( up, this._upX, this._upY, this._upZ);
137-
cc.kmMat4LookAt( this._lookupMatrix, eye, center, up);
138-
127+
var eye = new cc.math.Vec3(this._eyeX, this._eyeY , this._eyeZ),
128+
center = new cc.math.Vec3(this._centerX, this._centerY, this._centerZ),
129+
up = new cc.math.Vec3(this._upX, this._upY, this._upZ);
130+
this._lookupMatrix.lookAt(eye, center, up);
139131
this._dirty = false;
140132
}
141-
cc.kmMat4Multiply(matrix, matrix, this._lookupMatrix);
133+
matrix.multiply(this._lookupMatrix);
142134
},
143135

144136
/**

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

+16-25
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,33 @@ 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

8381
cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
8482
cc.kmGLLoadIdentity();
85-
var eye = cc.kmVec3Fill(null, -ox + size.width / 2, -oy + size.height / 2, zeye);
86-
var center = cc.kmVec3Fill(null, -ox + size.width / 2, -oy + size.height / 2, 0.0);
87-
var up = cc.kmVec3Fill(null, 0.0, 1.0, 0.0);
88-
cc.kmMat4LookAt(matrixLookup, eye, center, up);
83+
var eye = new cc.math.Vec3(-ox + size.width / 2, -oy + size.height / 2, zeye);
84+
var center = new cc.math.Vec3( -ox + size.width / 2, -oy + size.height / 2, 0.0);
85+
var up = new cc.math.Vec3( 0.0, 1.0, 0.0);
86+
matrixLookup.lookAt(eye, center, up);
8987
cc.kmGLMultMatrix(matrixLookup);
9088
break;
9189
case cc.Director.PROJECTION_CUSTOM:
@@ -238,38 +236,31 @@ 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];
249-
250246
var glSize = this._openGLView.getDesignResolutionSize();
251-
var clipCoord = new cc.kmVec3(2.0 * uiPoint.x / glSize.width - 1.0, 1.0 - 2.0 * uiPoint.y / glSize.height, zClip);
252-
253-
var glCoord = new cc.kmVec3();
254-
cc.kmVec3TransformCoord(glCoord, clipCoord, transformInv);
255-
247+
var glCoord = new cc.math.Vec3(2.0 * uiPoint.x / glSize.width - 1.0, 1.0 - 2.0 * uiPoint.y / glSize.height, zClip);
248+
glCoord.transformCoord(transformInv);
256249
return cc.p(glCoord.x, glCoord.y);
257250
};
258251

259252
_p.convertToUI = function (glPoint) {
260-
var transform = new cc.kmMat4();
253+
var transform = new cc.math.Matrix4();
261254
cc.GLToClipTransform(transform);
262255

263-
var clipCoord = new cc.kmVec3();
256+
var clipCoord = new cc.math.Vec3(glPoint.x, glPoint.y, 0.0);
264257
// Need to calculate the zero depth from the transform.
265-
var glCoord = new cc.kmVec3(glPoint.x, glPoint.y, 0.0);
266-
cc.kmVec3TransformCoord(clipCoord, glCoord, transform);
258+
clipCoord.transformCoord(transform);
267259

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

272-
273264
_p.getVisibleSize = function () {
274265
//if (this._openGLView) {
275266
return this._openGLView.getVisibleSize();

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

+31-21
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,52 @@
2727
*/
2828

2929
/**
30-
* A struture that represents an axis-aligned
31-
* bounding box.
30+
* A structure that represents an axis-aligned bounding box.
31+
* cc.kmAABB => cc.math.AABB
3232
*/
33-
cc.kmAABB = function (min, max) {
33+
cc.math.AABB = function (min, max) {
3434
/** The max corner of the box */
35-
this.min = min || new cc.kmVec3();
35+
this.min = min || new cc.math.Vec3();
3636
/** The min corner of the box */
37-
this.max = max || new cc.kmVec3();
37+
this.max = max || new cc.math.Vec3();
3838
};
3939

4040
/**
41-
* Returns KM_TRUE if point is in the specified AABB, returns
42-
* KM_FALSE otherwise.
41+
* Returns true if point is in the specified AABB, returns false otherwise.
42+
* @param {cc.math.Vec3} point
43+
* @returns {boolean}
4344
*/
44-
cc.kmAABBContainsPoint = function (pPoint, pBox) {
45-
if (pPoint.x >= pBox.min.x && pPoint.x <= pBox.max.x &&
45+
cc.math.AABB.prototype.containsPoint = function (point) {
46+
return (point.x >= this.min.x && point.x <= this.max.x &&
47+
point.y >= this.min.y && point.y <= this.max.y &&
48+
point.z >= this.min.z && point.z <= this.max.z);
49+
};
50+
51+
/**
52+
* Returns true if point is in the specified AABB, returns
53+
* false otherwise.
54+
*/
55+
cc.math.AABB.containsPoint = function (pPoint, pBox) {
56+
return (pPoint.x >= pBox.min.x && pPoint.x <= pBox.max.x &&
4657
pPoint.y >= pBox.min.y && pPoint.y <= pBox.max.y &&
47-
pPoint.z >= pBox.min.z && pPoint.z <= pBox.max.z) {
48-
return cc.KM_TRUE;
49-
}
50-
return cc.KM_FALSE;
58+
pPoint.z >= pBox.min.z && pPoint.z <= pBox.max.z);
5159
};
5260

5361
/**
54-
* Assigns pIn to pOut, returns pOut.
62+
* Assigns aabb to current AABB object
63+
* @param {cc.math.AABB} aabb
5564
*/
56-
cc.kmAABBAssign = function (pOut, pIn) {
57-
cc.kmVec3Assign(pOut.min, pIn.min);
58-
cc.kmVec3Assign(pOut.max, pIn.max);
59-
return pOut;
65+
cc.math.AABB.prototype.assignFrom = function(aabb){
66+
this.min.assignFrom(aabb.min);
67+
this.max.assignFrom(aabb.max);
6068
};
6169

6270
/**
63-
* Scales pIn by s, stores the resulting AABB in pOut. Returns pOut
71+
* Assigns pIn to pOut, returns pOut.
6472
*/
65-
cc.kmAABBScale = function (pOut, pIn, s) {
66-
cc.log("cc.kmAABBScale hasn't been supported.");
73+
cc.math.AABB.assign = function (pOut, pIn) { //cc.kmAABBAssign
74+
pOut.min.assignFrom(pIn.min);
75+
pOut.max.assignFrom(pIn.max);
76+
return pOut;
6777
};
6878

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)