From 3a0e9be97ef9e20e9a29a142d563fd0666cb825c Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Wed, 17 Sep 2014 18:33:18 +0800 Subject: [PATCH 1/2] new version of avoid z fighting in pageTurn --- cocos/2d/CCActionPageTurn3D.cpp | 8 +++++ cocos/2d/CCActionPageTurn3D.h | 1 + cocos/2d/CCGrid.cpp | 31 ++++++++++++++++--- cocos/2d/CCGrid.h | 12 +++++-- cocos/2d/CCTransitionPageTurn.cpp | 29 ----------------- cocos/2d/CCTransitionPageTurn.h | 10 +----- tests/cpp-tests/Classes/BugsTest/Bug-1159.cpp | 2 -- .../TransitionsTest/TransitionsTest.cpp | 2 -- 8 files changed, 47 insertions(+), 48 deletions(-) diff --git a/cocos/2d/CCActionPageTurn3D.cpp b/cocos/2d/CCActionPageTurn3D.cpp index 0433127d64..7799d8d9ec 100644 --- a/cocos/2d/CCActionPageTurn3D.cpp +++ b/cocos/2d/CCActionPageTurn3D.cpp @@ -24,6 +24,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #include "2d/CCActionPageTurn3D.h" +#include "2d/CCGrid.h" NS_CC_BEGIN @@ -55,6 +56,13 @@ PageTurn3D *PageTurn3D::clone() const return a; } +GridBase* PageTurn3D::getGrid() +{ + auto result = Grid3D::create(_gridSize); + result->setNeedDepthTestForBlit(true); + return result; +} + /* * Update each tick * Time is the percentage of the way through the duration diff --git a/cocos/2d/CCActionPageTurn3D.h b/cocos/2d/CCActionPageTurn3D.h index 35b2dd607e..b1c119fcf3 100644 --- a/cocos/2d/CCActionPageTurn3D.h +++ b/cocos/2d/CCActionPageTurn3D.h @@ -47,6 +47,7 @@ NS_CC_BEGIN class CC_DLL PageTurn3D : public Grid3DAction { public: + virtual GridBase* getGrid(); /** create the action */ static PageTurn3D* create(float duration, const Size& gridSize); diff --git a/cocos/2d/CCGrid.cpp b/cocos/2d/CCGrid.cpp index 20fa4a653f..1d0fd11527 100644 --- a/cocos/2d/CCGrid.cpp +++ b/cocos/2d/CCGrid.cpp @@ -232,7 +232,9 @@ void GridBase::afterDraw(cocos2d::Node *target) // restore projection for default FBO .fixed bug #543 #544 //TODO: Director::getInstance()->setProjection(Director::getInstance()->getProjection()); //TODO: Director::getInstance()->applyOrientation(); + beforeBlit(); blit(); + afterBlit(); } void GridBase::blit(void) @@ -294,10 +296,11 @@ Grid3D* Grid3D::create(const Size& gridSize) Grid3D::Grid3D() - : _texCoordinates(nullptr) - , _vertices(nullptr) - , _originalVertices(nullptr) - , _indices(nullptr) +: _texCoordinates(nullptr) +, _vertices(nullptr) +, _originalVertices(nullptr) +, _indices(nullptr) +, _needDepthTestForBlit(false) { } @@ -310,6 +313,26 @@ Grid3D::~Grid3D(void) CC_SAFE_FREE(_originalVertices); } +void Grid3D::beforeBlit() +{ + if(_needDepthTestForBlit) + { + _oldDepthTestValue = glIsEnabled(GL_DEPTH_TEST); + glEnable(GL_DEPTH_TEST); + } +} + +void Grid3D::afterBlit() +{ + if(_needDepthTestForBlit) + { + if(_oldDepthTestValue) + glEnable(GL_DEPTH_TEST); + else + glDisable(GL_DEPTH_TEST); + } +} + void Grid3D::blit(void) { int n = _gridSize.width * _gridSize.height; diff --git a/cocos/2d/CCGrid.h b/cocos/2d/CCGrid.h index 54ef7f46d4..e30e06ff74 100644 --- a/cocos/2d/CCGrid.h +++ b/cocos/2d/CCGrid.h @@ -82,6 +82,8 @@ public: void beforeDraw(void); void afterDraw(Node *target); + virtual void beforeBlit() {} + virtual void afterBlit() {} virtual void blit(void); virtual void reuse(void); virtual void calculateVertexPoints(void); @@ -146,17 +148,23 @@ public: * @lua NA */ void setVertex(const Vec2& pos, const Vec3& vertex); - + + virtual void beforeBlit() override; + virtual void afterBlit() override; // Overrides virtual void blit() override; virtual void reuse() override; virtual void calculateVertexPoints() override; - + + void setNeedDepthTestForBlit( bool neededDepthTest) { _needDepthTestForBlit = neededDepthTest; } + bool getNeedDepthTestForBlit() const { return _needDepthTestForBlit; } protected: GLvoid *_texCoordinates; GLvoid *_vertices; GLvoid *_originalVertices; GLushort *_indices; + bool _needDepthTestForBlit; + bool _oldDepthTestValue; }; /** diff --git a/cocos/2d/CCTransitionPageTurn.cpp b/cocos/2d/CCTransitionPageTurn.cpp index cbeaa0a019..2346b77db8 100644 --- a/cocos/2d/CCTransitionPageTurn.cpp +++ b/cocos/2d/CCTransitionPageTurn.cpp @@ -32,9 +32,6 @@ THE SOFTWARE. NS_CC_BEGIN -float TransitionPageTurn::POLYGON_OFFSET_FACTOR = -20.f; -float TransitionPageTurn::POLYGON_OFFSET_UNITS = -20.f; - TransitionPageTurn::TransitionPageTurn() { _inSceneProxy = NodeGrid::create(); @@ -77,43 +74,17 @@ void TransitionPageTurn::sceneOrder() _isInSceneOnTop = _back; } -void TransitionPageTurn::onEnablePolygonOffset() -{ - glEnable(GL_POLYGON_OFFSET_FILL); - glPolygonOffset(POLYGON_OFFSET_FACTOR, POLYGON_OFFSET_UNITS); -} - -void TransitionPageTurn::onDisablePolygonOffset() -{ - glDisable(GL_POLYGON_OFFSET_FILL); - glPolygonOffset(0, 0); -} - void TransitionPageTurn::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) { Scene::draw(renderer, transform, flags); if( _isInSceneOnTop ) { _outSceneProxy->visit(renderer, transform, flags); - _enableOffsetCmd.init(_globalZOrder); - _enableOffsetCmd.func = CC_CALLBACK_0(TransitionPageTurn::onEnablePolygonOffset, this); - renderer->addCommand(&_enableOffsetCmd); _inSceneProxy->visit(renderer, transform, flags); - _disableOffsetCmd.init(_globalZOrder); - _disableOffsetCmd.func = CC_CALLBACK_0(TransitionPageTurn::onDisablePolygonOffset, this); - renderer->addCommand(&_disableOffsetCmd); } else { _inSceneProxy->visit(renderer, transform, flags); - - _enableOffsetCmd.init(_globalZOrder); - _enableOffsetCmd.func = CC_CALLBACK_0(TransitionPageTurn::onEnablePolygonOffset, this); - renderer->addCommand(&_enableOffsetCmd); - _outSceneProxy->visit(renderer, transform, flags); - _disableOffsetCmd.init(_globalZOrder); - _disableOffsetCmd.func = CC_CALLBACK_0(TransitionPageTurn::onDisablePolygonOffset, this); - renderer->addCommand(&_disableOffsetCmd); } } diff --git a/cocos/2d/CCTransitionPageTurn.h b/cocos/2d/CCTransitionPageTurn.h index 3291566d33..815bdce557 100644 --- a/cocos/2d/CCTransitionPageTurn.h +++ b/cocos/2d/CCTransitionPageTurn.h @@ -97,15 +97,7 @@ protected: protected: NodeGrid* _inSceneProxy; NodeGrid* _outSceneProxy; - bool _back; - static float POLYGON_OFFSET_FACTOR; - static float POLYGON_OFFSET_UNITS; - -protected: - CustomCommand _enableOffsetCmd; - CustomCommand _disableOffsetCmd; - void onEnablePolygonOffset(); - void onDisablePolygonOffset(); + bool _back; }; // end of transition group diff --git a/tests/cpp-tests/Classes/BugsTest/Bug-1159.cpp b/tests/cpp-tests/Classes/BugsTest/Bug-1159.cpp index e291ca8476..eafe9c33b9 100644 --- a/tests/cpp-tests/Classes/BugsTest/Bug-1159.cpp +++ b/tests/cpp-tests/Classes/BugsTest/Bug-1159.cpp @@ -22,7 +22,6 @@ bool Bug1159Layer::init() { if (BugsTestBaseLayer::init()) { - Director::getInstance()->setDepthTest(true); auto s = Director::getInstance()->getWinSize(); auto background = LayerColor::create(Color4B(255, 0, 255, 255)); @@ -63,6 +62,5 @@ void Bug1159Layer::callBack(Ref* sender) void Bug1159Layer::onExit() { - Director::getInstance()->setDepthTest(false); BugsTestBaseLayer::onExit(); } diff --git a/tests/cpp-tests/Classes/TransitionsTest/TransitionsTest.cpp b/tests/cpp-tests/Classes/TransitionsTest/TransitionsTest.cpp index df90498ac0..7f78f2a9a7 100644 --- a/tests/cpp-tests/Classes/TransitionsTest/TransitionsTest.cpp +++ b/tests/cpp-tests/Classes/TransitionsTest/TransitionsTest.cpp @@ -151,7 +151,6 @@ class PageTransitionForward : public TransitionPageTurn public: static TransitionScene* create(float t, Scene* s) { - Director::getInstance()->setDepthTest(true); return TransitionPageTurn::create(t, s, false); } }; @@ -161,7 +160,6 @@ class PageTransitionBackward : public TransitionPageTurn public: static TransitionScene* create(float t, Scene* s) { - Director::getInstance()->setDepthTest(true); return TransitionPageTurn::create(t, s, true); } }; From 39c6f9bfbdf5272b1b996b19a95d108500a0e866 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Thu, 18 Sep 2014 10:00:36 +0800 Subject: [PATCH 2/2] remove setDepthTest(true) because it is not needed to be set for pageTurn --- tests/cpp-tests/Classes/EffectsTest/EffectsTest.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/cpp-tests/Classes/EffectsTest/EffectsTest.cpp b/tests/cpp-tests/Classes/EffectsTest/EffectsTest.cpp index ca2d0896d5..85300cb0e6 100644 --- a/tests/cpp-tests/Classes/EffectsTest/EffectsTest.cpp +++ b/tests/cpp-tests/Classes/EffectsTest/EffectsTest.cpp @@ -275,7 +275,6 @@ class PageTurn3DDemo : public PageTurn3D public: static ActionInterval* create(float t) { - Director::getInstance()->setDepthTest(true); return PageTurn3D::create(t, Size(15,10)); } };