diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index 201cf0cfc1..ca75c01269 100644 --- a/cocos/2d/CCRenderTexture.cpp +++ b/cocos/2d/CCRenderTexture.cpp @@ -553,7 +553,7 @@ void RenderTexture::onBegin() director->setProjection(director->getProjection()); #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 - Mat4 modifiedProjection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); + auto modifiedProjection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); modifiedProjection = GLViewImpl::sharedOpenGLView()->getReverseOrientationMatrix() * modifiedProjection; director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION,modifiedProjection); #endif @@ -572,7 +572,7 @@ void RenderTexture::onBegin() else { #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 - Mat4 modifiedProjection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); + auto modifiedProjection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); modifiedProjection = GLViewImpl::sharedOpenGLView()->getReverseOrientationMatrix() * modifiedProjection; director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, modifiedProjection); #endif diff --git a/cocos/base/CCDirector.cpp b/cocos/base/CCDirector.cpp index 227dfe2796..be72136274 100644 --- a/cocos/base/CCDirector.cpp +++ b/cocos/base/CCDirector.cpp @@ -540,15 +540,15 @@ void Director::multiplyMatrix(MATRIX_STACK_TYPE type, const Mat4& mat) void Director::pushMatrix(MATRIX_STACK_TYPE type) { - if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type) + if(type == MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW) { _modelViewMatrixStack.push(_modelViewMatrixStack.top()); } - else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type) + else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION) { _projectionMatrixStack.push(_projectionMatrixStack.top()); } - else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type) + else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE) { _textureMatrixStack.push(_textureMatrixStack.top()); } @@ -558,36 +558,23 @@ void Director::pushMatrix(MATRIX_STACK_TYPE type) } } -Mat4 Director::getMatrix(MATRIX_STACK_TYPE type) +const Mat4& Director::getMatrix(MATRIX_STACK_TYPE type) { - Mat4 result; - if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type) + if(type == MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW) { - result = _modelViewMatrixStack.top(); + return _modelViewMatrixStack.top(); } - else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type) + else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION) { - result = _projectionMatrixStack.top(); + return _projectionMatrixStack.top(); } - else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type) + else if(type == MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE) { - result = _textureMatrixStack.top(); + return _textureMatrixStack.top(); } - else - { - CCASSERT(false, "unknow matrix stack type, will return modelview matrix instead"); - result = _modelViewMatrixStack.top(); - } -// float diffResult(0); -// for (int index = 0; index <16; ++index) -// { -// diffResult += abs(result2.mat[index] - result.mat[index]); -// } -// if(diffResult > 1e-30) -// { -// CCASSERT(false, "Error in director matrix stack"); -// } - return result; + + CCASSERT(false, "unknow matrix stack type, will return modelview matrix instead"); + return _modelViewMatrixStack.top(); } void Director::setProjection(Projection projection) @@ -717,17 +704,15 @@ static void GLToClipTransform(Mat4 *transformOut) Director* director = Director::getInstance(); CCASSERT(nullptr != director, "Director is null when seting matrix stack"); - - Mat4 projection; - projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); + + auto projection = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 //if needed, we need to undo the rotation for Landscape orientation in order to get the correct positions projection = Director::getInstance()->getOpenGLView()->getReverseOrientationMatrix() * projection; #endif - Mat4 modelview; - modelview = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); + auto modelview = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); *transformOut = projection * modelview; } diff --git a/cocos/base/CCDirector.h b/cocos/base/CCDirector.h index d84464a994..c9baba964a 100644 --- a/cocos/base/CCDirector.h +++ b/cocos/base/CCDirector.h @@ -90,27 +90,12 @@ enum class MATRIX_STACK_TYPE class CC_DLL Director : public Ref { -private: - std::stack _modelViewMatrixStack; - std::stack _projectionMatrixStack; - std::stack _textureMatrixStack; -protected: - void initMatrixStack(); -public: - void pushMatrix(MATRIX_STACK_TYPE type); - void popMatrix(MATRIX_STACK_TYPE type); - void loadIdentityMatrix(MATRIX_STACK_TYPE type); - void loadMatrix(MATRIX_STACK_TYPE type, const Mat4& mat); - void multiplyMatrix(MATRIX_STACK_TYPE type, const Mat4& mat); - Mat4 getMatrix(MATRIX_STACK_TYPE type); - void resetMatrixStack(); public: static const char *EVENT_PROJECTION_CHANGED; static const char* EVENT_AFTER_UPDATE; static const char* EVENT_AFTER_VISIT; static const char* EVENT_AFTER_DRAW; - /** @typedef ccDirectorProjection Possible OpenGL projections used by director */ @@ -399,6 +384,14 @@ public: */ float getFrameRate() const { return _frameRate; } + void pushMatrix(MATRIX_STACK_TYPE type); + void popMatrix(MATRIX_STACK_TYPE type); + void loadIdentityMatrix(MATRIX_STACK_TYPE type); + void loadMatrix(MATRIX_STACK_TYPE type, const Mat4& mat); + void multiplyMatrix(MATRIX_STACK_TYPE type, const Mat4& mat); + const Mat4& getMatrix(MATRIX_STACK_TYPE type); + void resetMatrixStack(); + protected: void purgeDirector(); bool _purgeDirectorInNextLoop; // this flag will be set to true in end() @@ -417,6 +410,12 @@ protected: void initTextureCache(); void destroyTextureCache(); + void initMatrixStack(); + + std::stack _modelViewMatrixStack; + std::stack _projectionMatrixStack; + std::stack _textureMatrixStack; + /** Scheduler associated with this director @since v2.0 */ diff --git a/cocos/editor-support/cocostudio/CCSkin.cpp b/cocos/editor-support/cocostudio/CCSkin.cpp index c1a9ddda72..f83cdc0296 100644 --- a/cocos/editor-support/cocostudio/CCSkin.cpp +++ b/cocos/editor-support/cocostudio/CCSkin.cpp @@ -222,7 +222,7 @@ Mat4 Skin::getNodeToWorldTransformAR() const void Skin::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) { - Mat4 mv = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); + auto mv = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); //TODO: implement z order _quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, mv); diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index 6aa05848ca..f1c59731f4 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -844,15 +844,14 @@ void GLProgram::setUniformsForBuiltins() Director* director = Director::getInstance(); CCASSERT(nullptr != director, "Director is null when seting matrix stack"); - Mat4 matrixMV; - matrixMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); + auto matrixMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); setUniformsForBuiltins(matrixMV); } void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV) { - Mat4 matrixP = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); + auto matrixP = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); if(_flags.usesP) setUniformLocationWithMatrix4fv(_builtInUniforms[UNIFORM_P_MATRIX], matrixP.m, 1); diff --git a/tests/cpp-tests/Classes/Box2DTest/Box2dTest.cpp b/tests/cpp-tests/Classes/Box2DTest/Box2dTest.cpp index 6e7bedf0a9..ad5c79d204 100644 --- a/tests/cpp-tests/Classes/Box2DTest/Box2dTest.cpp +++ b/tests/cpp-tests/Classes/Box2DTest/Box2dTest.cpp @@ -167,9 +167,8 @@ void Box2DTestLayer::onDraw() { Director* director = Director::getInstance(); CCASSERT(nullptr != director, "Director is null when seting matrix stack"); - - Mat4 oldMV; - oldMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); + + auto oldMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewMV); world->DrawDebugData(); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, oldMV); diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index a282f2c15c..516f714fdc 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1447,7 +1447,7 @@ void Sprite3DWithOBBPerfromanceTest::calculateRayByLocationInView(Ray* ray, cons { auto dir = Director::getInstance(); auto view = dir->getWinSize(); - Mat4 mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); + auto mat = dir->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION); Vec3 src = Vec3(location.x, location.y, -1); Vec3 nearPoint;