Merge pull request #8091 from dabingnn/v3_PageTurnZFighting

new version of avoid z fighting in pageTurn
This commit is contained in:
minggo 2014-09-18 10:50:31 +08:00
commit 442f30af1f
9 changed files with 47 additions and 49 deletions

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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;
};
/**

View File

@ -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);
}
}

View File

@ -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

View File

@ -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();
}

View File

@ -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));
}
};

View File

@ -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);
}
};