diff --git a/cocos/2d/CCRenderTexture.cpp b/cocos/2d/CCRenderTexture.cpp index 4316d5113e..2ed6962961 100644 --- a/cocos/2d/CCRenderTexture.cpp +++ b/cocos/2d/CCRenderTexture.cpp @@ -686,6 +686,7 @@ void RenderTexture::onClear() GLfloat oldClearColor[4] = {0.0f}; GLfloat oldDepthClearValue = 0.0f; GLint oldStencilClearValue = 0; + GLboolean oldDepthWrite = GL_FALSE; // backup and set if (_clearFlags & GL_COLOR_BUFFER_BIT) @@ -698,6 +699,9 @@ void RenderTexture::onClear() { glGetFloatv(GL_DEPTH_CLEAR_VALUE, &oldDepthClearValue); glClearDepth(_clearDepth); + + glGetBooleanv(GL_DEPTH_WRITEMASK, &oldDepthWrite); + glDepthMask(GL_TRUE); } if (_clearFlags & GL_STENCIL_BUFFER_BIT) @@ -717,6 +721,7 @@ void RenderTexture::onClear() if (_clearFlags & GL_DEPTH_BUFFER_BIT) { glClearDepth(oldDepthClearValue); + glDepthMask(oldDepthWrite); } if (_clearFlags & GL_STENCIL_BUFFER_BIT) { diff --git a/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp b/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp index 97c22d2727..5fbab1a31a 100644 --- a/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp +++ b/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.cpp @@ -13,6 +13,7 @@ RenderTextureTests::RenderTextureTests() ADD_TEST_CASE(SpriteRenderTextureBug); ADD_TEST_CASE(RenderTexturePartTest); ADD_TEST_CASE(Issue16113Test); + ADD_TEST_CASE(RenderTextureWithSprite3DIssue16894); }; /** @@ -735,3 +736,93 @@ std::string Issue16113Test::subtitle() const { return "aaa.png file without white border on iOS"; } + +// +// RenderTextureWithSprite3DIssue16894 +// +RenderTextureWithSprite3DIssue16894::RenderTextureWithSprite3DIssue16894() +{ + auto visibleSize = Director::getInstance()->getVisibleSize(); + Vec2 origin = Director::getInstance()->getVisibleOrigin(); + + for (int i = 0; i < 3; ++i) + { + // Ship - Model is from cocos2d-x test project + auto ship = Sprite3D::create("Sprite3DTest/boss.c3b"); + ship->setScale(6); + ship->setRotation3D(Vec3(180,45,0)); + ship->setPosition(Vec2(visibleSize.width/4 + origin.x, visibleSize.height/2 + origin.y)); + ship->setForce2DQueue(true); + ship->retain(); + + if (i == 0) + { + addChild(ship, 1); + // Rotate Ship + auto spin = RotateBy::create(4, Vec3(0,180,0)); + auto repeatspin = RepeatForever::create(spin); + ship->runAction(repeatspin); + } + _ship[i] = ship; + } + + // RenderTextures + _renderTexDefault = RenderTexture::create(visibleSize.width, visibleSize.height, Texture2D::PixelFormat::RGBA8888); + _renderTexDefault->setKeepMatrix(true); + addChild(_renderTexDefault); + _renderTexDefault->setPosition(visibleSize.width/4 * 3, visibleSize.height/2); + + _renderTexWithBuffer = RenderTexture::create(visibleSize.width, visibleSize.height, Texture2D::PixelFormat::RGBA8888, GL_DEPTH24_STENCIL8); + _renderTexWithBuffer->setKeepMatrix(true); + addChild(_renderTexWithBuffer); + _renderTexWithBuffer->setPosition(visibleSize.width/4 * 4, visibleSize.height/2); + + // Update + scheduleUpdate(); + + auto label1 = Label::createWithTTF("Normal Sprite3D\n", "fonts/arial.ttf", 10); + label1->setPosition(Vec2(visibleSize.width/4 * 1, 60)); + this->addChild(label1, 1); + + auto label2 = Label::createWithTTF("RenderTexture\nDefault - No depth buffer", "fonts/arial.ttf", 10); + label2->setPosition(Vec2(visibleSize.width/4 * 2, 60)); + this->addChild(label2, 1); + + auto label3 = Label::createWithTTF("RenderTexture\nGL_DEPTH24_STENCIL8", "fonts/arial.ttf", 10); + label3->setPosition(Vec2(visibleSize.width/4 * 3, 60)); + this->addChild(label3, 1); +} + +RenderTextureWithSprite3DIssue16894::~RenderTextureWithSprite3DIssue16894() +{ + for (int i = 0; i < 3; ++i) + { + _ship[i]->release(); + } +} + +void RenderTextureWithSprite3DIssue16894::visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags) +{ + RenderTextureTest::visit(renderer, parentTransform, parentFlags); + + _ship[1]->setRotation3D(_ship[0]->getRotation3D()); + _ship[2]->setRotation3D(_ship[0]->getRotation3D()); + + _renderTexDefault->beginWithClear(0, 0, 0, 0, 0, 0); + _ship[1]->visit(Director::getInstance()->getRenderer(), Mat4::IDENTITY, 0); + _renderTexDefault->end(); + + _renderTexWithBuffer->beginWithClear(0, 0, 0, 0, 1, 0); + _ship[2]->visit(Director::getInstance()->getRenderer(), Mat4::IDENTITY, 0); + _renderTexWithBuffer->end(); +} + +std::string RenderTextureWithSprite3DIssue16894::title() const +{ + return "Issue16894: Render Sprite3D to texture"; +} + +std::string RenderTextureWithSprite3DIssue16894::subtitle() const +{ + return "3 ships, 1st & 3rd are the same"; +} diff --git a/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.h b/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.h index 839c167ae9..6d626b0eab 100644 --- a/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.h +++ b/tests/cpp-tests/Classes/RenderTextureTest/RenderTextureTest.h @@ -155,4 +155,23 @@ private: cocos2d::Sprite* _spriteDraw; }; +class RenderTextureWithSprite3DIssue16894 : public RenderTextureTest +{ +public: + CREATE_FUNC(RenderTextureWithSprite3DIssue16894); + RenderTextureWithSprite3DIssue16894(); + virtual ~RenderTextureWithSprite3DIssue16894(); + + virtual void visit(cocos2d::Renderer *renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags) override; + + virtual std::string title() const override; + virtual std::string subtitle() const override; + +private: + cocos2d::Sprite3D* _ship[3]; + + cocos2d::RenderTexture* _renderTexDefault; + cocos2d::RenderTexture* _renderTexWithBuffer; +}; + #endif