diff --git a/cocos/2d/CCActionCamera.cpp b/cocos/2d/CCActionCamera.cpp index 4799dd46e4..87d377372c 100644 --- a/cocos/2d/CCActionCamera.cpp +++ b/cocos/2d/CCActionCamera.cpp @@ -98,7 +98,7 @@ void ActionCamera::updateTransform() bool needsTranslation = !anchorPoint.equals(Vector2::ZERO); - Matrix mv = Matrix::identity(); + Matrix mv = Matrix::IDENTITY; if(needsTranslation) { Matrix t; diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 7296f3dec8..5a0daeba6d 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -135,7 +135,7 @@ Node::Node(void) ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine(); _scriptType = engine != nullptr ? engine->getScriptType() : kScriptTypeNone; #endif - _transform = _inverse = _additionalTransform = Matrix::identity(); + _transform = _inverse = _additionalTransform = Matrix::IDENTITY; } Node::~Node() diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 34305cae42..4059c1f87a 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -1004,7 +1004,7 @@ void Sprite::setBatchNode(SpriteBatchNode *spriteBatchNode) } else { // using batch - _transformToBatch = Matrix::identity(); + _transformToBatch = Matrix::IDENTITY; setTextureAtlas(_batchNode->getTextureAtlas()); // weak ref } } diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 53e9b53eea..01f2d392d6 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -285,19 +285,17 @@ void Director::drawScene() pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); - Matrix identity = Matrix::identity(); - // draw the scene if (_runningScene) { - _runningScene->visit(_renderer, identity, false); + _runningScene->visit(_renderer, Matrix::IDENTITY, false); _eventDispatcher->dispatchEvent(_eventAfterVisit); } // draw the notifications node if (_notificationNode) { - _notificationNode->visit(_renderer, identity, false); + _notificationNode->visit(_renderer, Matrix::IDENTITY, false); } if (_displayStats) @@ -451,9 +449,9 @@ void Director::initMatrixStack() _textureMatrixStack.pop(); } - _modelViewMatrixStack.push(Matrix::identity()); - _projectionMatrixStack.push(Matrix::identity()); - _textureMatrixStack.push(Matrix::identity()); + _modelViewMatrixStack.push(Matrix::IDENTITY); + _projectionMatrixStack.push(Matrix::IDENTITY); + _textureMatrixStack.push(Matrix::IDENTITY); } void Director::resetMatrixStack() @@ -485,15 +483,15 @@ void Director::loadIdentityMatrix(MATRIX_STACK_TYPE type) { if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type) { - _modelViewMatrixStack.top() = Matrix::identity(); + _modelViewMatrixStack.top() = Matrix::IDENTITY; } else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type) { - _projectionMatrixStack.top() = Matrix::identity(); + _projectionMatrixStack.top() = Matrix::IDENTITY; } else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type) { - _textureMatrixStack.top() = Matrix::identity(); + _textureMatrixStack.top() = Matrix::IDENTITY; } else { @@ -1082,7 +1080,7 @@ void Director::showStats() prevVerts = currentVerts; } - Matrix identity = Matrix::identity(); + Matrix identity = Matrix::IDENTITY; _drawnVerticesLabel->visit(_renderer, identity, false); _drawnBatchesLabel->visit(_renderer, identity, false); diff --git a/cocos/deprecated/CCDeprecated.cpp b/cocos/deprecated/CCDeprecated.cpp index 0faa03ca17..34e13a210a 100644 --- a/cocos/deprecated/CCDeprecated.cpp +++ b/cocos/deprecated/CCDeprecated.cpp @@ -275,7 +275,7 @@ Matrix* kmMat4Assign(Matrix* pOut, const Matrix* pIn) Matrix* kmMat4Identity(Matrix* pOut) { - *pOut = Matrix::identity(); + *pOut = Matrix::IDENTITY; return pOut; } diff --git a/cocos/editor-support/cocostudio/CCBone.cpp b/cocos/editor-support/cocostudio/CCBone.cpp index d7b06e2617..530cc47089 100644 --- a/cocos/editor-support/cocostudio/CCBone.cpp +++ b/cocos/editor-support/cocostudio/CCBone.cpp @@ -72,7 +72,7 @@ Bone::Bone() _displayManager = nullptr; _ignoreMovementBoneData = false; // _worldTransform = AffineTransformMake(1, 0, 0, 1, 0, 0); - _worldTransform = Matrix::identity(); + _worldTransform = Matrix::IDENTITY; _boneTransformDirty = true; _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED; _blendDirty = false; diff --git a/cocos/editor-support/cocostudio/CCSkin.cpp b/cocos/editor-support/cocostudio/CCSkin.cpp index 7af7220625..ea1f678e1d 100644 --- a/cocos/editor-support/cocostudio/CCSkin.cpp +++ b/cocos/editor-support/cocostudio/CCSkin.cpp @@ -86,7 +86,7 @@ Skin::Skin() , _armature(nullptr) , _displayName("") { - _skinTransform = Matrix::identity(); + _skinTransform = Matrix::IDENTITY; } bool Skin::initWithSpriteFrameName(const std::string& spriteFrameName) diff --git a/cocos/editor-support/cocostudio/CCTransformHelp.cpp b/cocos/editor-support/cocostudio/CCTransformHelp.cpp index f30b1a13e0..453331521f 100644 --- a/cocos/editor-support/cocostudio/CCTransformHelp.cpp +++ b/cocos/editor-support/cocostudio/CCTransformHelp.cpp @@ -120,7 +120,7 @@ void TransformHelp::nodeToMatrix(const BaseData &node, AffineTransform &matrix) void TransformHelp::nodeToMatrix(const BaseData &node, Matrix &matrix) { - matrix = Matrix::identity(); + matrix = Matrix::IDENTITY; if (node.skewX == -node.skewY) { diff --git a/cocos/math/Matrix.cpp b/cocos/math/Matrix.cpp index b6cf7462ab..5f3503fb3d 100644 --- a/cocos/math/Matrix.cpp +++ b/cocos/math/Matrix.cpp @@ -25,17 +25,9 @@ NS_CC_MATH_BEGIN -static const float MATRIX_IDENTITY[16] = -{ - 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f -}; - Matrix::Matrix() { - *this = Matrix::identity(); + *this = IDENTITY; } Matrix::Matrix(float m11, float m12, float m13, float m14, float m21, float m22, float m23, float m24, @@ -58,26 +50,6 @@ Matrix::~Matrix() { } -const Matrix& Matrix::identity() -{ - static Matrix m( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1 ); - return m; -} - -const Matrix& Matrix::zero() -{ - static Matrix m( - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0 ); - return m; -} - void Matrix::createLookAt(const Vector3& eyePosition, const Vector3& targetPosition, const Vector3& up, Matrix* dst) { createLookAt(eyePosition.x, eyePosition.y, eyePosition.z, targetPosition.x, targetPosition.y, targetPosition.z, @@ -250,7 +222,7 @@ void Matrix::createScale(const Vector3& scale, Matrix* dst) { GP_ASSERT(dst); - memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE); + memcpy(dst, &IDENTITY, MATRIX_SIZE); dst->m[0] = scale.x; dst->m[5] = scale.y; @@ -261,7 +233,7 @@ void Matrix::createScale(float xScale, float yScale, float zScale, Matrix* dst) { GP_ASSERT(dst); - memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE); + memcpy(dst, &IDENTITY, MATRIX_SIZE); dst->m[0] = xScale; dst->m[5] = yScale; @@ -371,7 +343,7 @@ void Matrix::createRotationX(float angle, Matrix* dst) { GP_ASSERT(dst); - memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE); + memcpy(dst, &IDENTITY, MATRIX_SIZE); float c = cos(angle); float s = sin(angle); @@ -386,7 +358,7 @@ void Matrix::createRotationY(float angle, Matrix* dst) { GP_ASSERT(dst); - memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE); + memcpy(dst, &IDENTITY, MATRIX_SIZE); float c = cos(angle); float s = sin(angle); @@ -401,7 +373,7 @@ void Matrix::createRotationZ(float angle, Matrix* dst) { GP_ASSERT(dst); - memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE); + memcpy(dst, &IDENTITY, MATRIX_SIZE); float c = cos(angle); float s = sin(angle); @@ -416,7 +388,7 @@ void Matrix::createTranslation(const Vector3& translation, Matrix* dst) { GP_ASSERT(dst); - memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE); + memcpy(dst, &IDENTITY, MATRIX_SIZE); dst->m[12] = translation.x; dst->m[13] = translation.y; @@ -427,7 +399,7 @@ void Matrix::createTranslation(float xTranslation, float yTranslation, float zTr { GP_ASSERT(dst); - memcpy(dst, MATRIX_IDENTITY, MATRIX_SIZE); + memcpy(dst, &IDENTITY, MATRIX_SIZE); dst->m[12] = xTranslation; dst->m[13] = yTranslation; @@ -712,7 +684,7 @@ bool Matrix::inverse() bool Matrix::isIdentity() const { - return (memcmp(m, MATRIX_IDENTITY, MATRIX_SIZE) == 0); + return (memcmp(m, &IDENTITY, MATRIX_SIZE) == 0); } void Matrix::multiply(float scalar) @@ -882,7 +854,7 @@ void Matrix::set(const Matrix& mat) void Matrix::setIdentity() { - memcpy(m, MATRIX_IDENTITY, MATRIX_SIZE); + memcpy(m, &IDENTITY, MATRIX_SIZE); } void Matrix::setZero() @@ -978,4 +950,16 @@ Matrix Matrix::getTransposed() const return mat; } +const Matrix Matrix::IDENTITY = Matrix( + 1.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f); + +const Matrix Matrix::ZERO = Matrix( + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0 ); + NS_CC_MATH_END diff --git a/cocos/math/Matrix.h b/cocos/math/Matrix.h index c5aa436a03..4ee9f927ee 100644 --- a/cocos/math/Matrix.h +++ b/cocos/math/Matrix.h @@ -138,25 +138,6 @@ public: */ ~Matrix(); - /** - * Returns the identity matrix: - * - * 1 0 0 0 - * 0 1 0 0 - * 0 0 1 0 - * 0 0 0 1 - * - * @return The identity matrix. - */ - static const Matrix& identity(); - - /** - * Returns the matrix with all zeros. - * - * @return The matrix with all zeros. - */ - static const Matrix& zero(); - /** * Creates a view matrix based on the specified input parameters. * @@ -940,7 +921,12 @@ public: * @return This matrix, after the multiplication occurs. */ inline Matrix& operator*=(const Matrix& mat); - + + /** equals to a matrix full of zeros */ + static const Matrix ZERO; + /** equals to the identity matrix */ + static const Matrix IDENTITY; + private: static void createBillboardHelper(const Vector3& objectPosition, const Vector3& cameraPosition,