From e6f055f3fdfb95a66cfac43127db4ee9c7dc17d0 Mon Sep 17 00:00:00 2001 From: songchengjiang Date: Thu, 28 Aug 2014 19:22:01 +0800 Subject: [PATCH] calculated Z in View Transparent Render --- cocos/3d/CCBillBoard.cpp | 34 +++++++++++-------- cocos/3d/CCBillBoard.h | 7 ++-- cocos/renderer/CCRenderer.cpp | 3 ++ .../Classes/Sprite3DTest/Sprite3DTest.cpp | 34 ++++++++++++------- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/cocos/3d/CCBillBoard.cpp b/cocos/3d/CCBillBoard.cpp index d3e09b9394..ca53e82042 100644 --- a/cocos/3d/CCBillBoard.cpp +++ b/cocos/3d/CCBillBoard.cpp @@ -23,10 +23,12 @@ ****************************************************************************/ #include "3d/CCBillBoard.h" +#include "3d/CCMeshVertexIndexData.h" #include "2d/CCSpriteFrameCache.h" #include "base/CCDirector.h" #include "base/CCCamera.h" #include "renderer/CCRenderer.h" +#include "renderer/CCGLProgramCache.h" NS_CC_BEGIN @@ -37,7 +39,6 @@ BillBorad::BillBorad() BillBorad::~BillBorad() { - } BillBorad* BillBorad::createWithTexture(Texture2D *texture) @@ -91,27 +92,30 @@ BillBorad* BillBorad::create() void BillBorad::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) { - calculateBillBoradMatrix(_BillBoradMat); + auto camera = Camera::getVisitingCamera(); + Mat4 viewInverseMat = camera->getViewMatrix().getInversed(); + viewInverseMat.m[12] = viewInverseMat.m[13] = viewInverseMat.m[14] = 0; + Mat4 transMat = transform; - transMat *= _BillBoradMat; + transMat *= viewInverseMat; // Don't do calculate the culling if the transform was not updated _insideBounds = (flags & FLAGS_TRANSFORM_DIRTY) ? renderer->checkVisibility(transMat, _contentSize) : _insideBounds; if(_insideBounds) { - _quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, transMat); - renderer->addCommand(&_quadCommand); + if (_displayedOpacity < 255) + { + Mat4 modelViewMat = camera->getViewMatrix() * transMat; + _quadCommand.init(-modelViewMat.m[14], _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, transMat); + renderer->addTransparentCommand(&_quadCommand); + } + else + { + _quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, transMat); + renderer->addCommand(&_quadCommand); + } + } } -void BillBorad::calculateBillBoradMatrix(Mat4 &dst) -{ - auto camera = Camera::getVisitingCamera(); - dst = camera->getViewMatrix().getInversed(); - - dst.m[12]=0; - dst.m[13]=0; - dst.m[14]=0; -} - NS_CC_END diff --git a/cocos/3d/CCBillBoard.h b/cocos/3d/CCBillBoard.h index 69a6099154..2778b0473d 100644 --- a/cocos/3d/CCBillBoard.h +++ b/cocos/3d/CCBillBoard.h @@ -26,9 +26,11 @@ #define __CCBILLBOARD_H__ #include "2d/CCSprite.h" +#include "renderer/CCMeshCommand.h" NS_CC_BEGIN +class MeshIndexData; /** * Inherit from Sprite, achieve BillBorad. */ @@ -84,11 +86,6 @@ CC_CONSTRUCTOR_ACCESS: BillBorad(); virtual ~BillBorad(); -protected: - Mat4 _BillBoradMat; - - void calculateBillBoradMatrix(Mat4 &dst); - private: CC_DISALLOW_COPY_AND_ASSIGN(BillBorad); diff --git a/cocos/renderer/CCRenderer.cpp b/cocos/renderer/CCRenderer.cpp index 3badba3092..25c3e02d27 100644 --- a/cocos/renderer/CCRenderer.cpp +++ b/cocos/renderer/CCRenderer.cpp @@ -396,6 +396,7 @@ void Renderer::visitTransparentRenderQueue(const TransparentRenderQueue& queue) auto cmd = static_cast(command); _batchedQuadCommands.push_back(cmd); + _numQuads = cmd->getQuadCount(); memcpy(_quads, cmd->getQuads(), sizeof(V3F_C4B_T2F_Quad) * cmd->getQuadCount()); convertToWorldCoordinates(_quads, cmd->getQuadCount(), cmd->getModelView()); drawBatchedQuads(); @@ -459,7 +460,9 @@ void Renderer::render() if (_transparentRenderGroups.size()) { _transparentRenderGroups.sort(); + glEnable(GL_DEPTH_TEST); visitTransparentRenderQueue(_transparentRenderGroups); + glDisable(GL_DEPTH_TEST); } } clean(); diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index d6eccdf757..5c7596bf51 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1010,20 +1010,20 @@ Sprite3DReskinTest::Sprite3DReskinTest() _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); TTFConfig ttfConfig("fonts/arial.ttf", 20); auto label1 = Label::createWithTTF(ttfConfig,"Hair"); - auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchHair,this) ); + auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchHair,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Glasses"); - auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchGlasses,this) ); + auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchGlasses,this) ); auto label3 = Label::createWithTTF(ttfConfig,"Coat"); - auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchCoat,this) ); + auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchCoat,this) ); auto label4 = Label::createWithTTF(ttfConfig,"Pants"); - auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchPants,this) ); + auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchPants,this) ); auto label5 = Label::createWithTTF(ttfConfig,"Shoes"); - auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchShoes,this) ); + auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchShoes,this) ); item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) ); - item2->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *5 ) ); - item3->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*6 ) ); - item4->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *7 ) ); - item5->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *8 ) ); + item2->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *5 ) ); + item3->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*6 ) ); + item4->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *7 ) ); + item5->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *8 ) ); auto pMenu1 = CCMenu::create(item1,item2,item3,item4,item5,NULL); pMenu1->setPosition(Vec2(0,0)); this->addChild(pMenu1, 10); @@ -1549,9 +1549,9 @@ BillBoardTest::BillBoardTest() _layerBillBorad->addChild(_camera); } - addNewBillBoradWithCoords(Vec3(20,0,0)); + addNewBillBoradWithCoords(Vec3(20,5,0)); addNewAniBillBoradWithCoords(Vec3(-20,0,0)); - _camera->setPosition3D(Vec3(0, 130, 130)); + _camera->setPosition3D(Vec3(0, 0, 130)); _camera->lookAt(Vec3(0,0,0), Vec3(0,1,0)); for( int j =-20; j<=20 ;j++) @@ -1566,7 +1566,7 @@ BillBoardTest::BillBoardTest() //draw y //line->drawLine(Vec3(0, -50, 0),Vec3(0,0,0),Color4F(0,0.5,0,1)); //line->drawLine(Vec3(0, 0, 0),Vec3(0,50,0),Color4F(0,1,0,1)); - _layerBillBorad->addChild(line,1); + _layerBillBorad->addChild(line); TTFConfig ttfConfig("fonts/arial.ttf", 20); auto label1 = Label::createWithTTF(ttfConfig,"rotate+"); @@ -1601,26 +1601,36 @@ void BillBoardTest::addNewBillBoradWithCoords(Vec3 p) _billborad->setScale(0.5f); _layerBillBorad->addChild(_billborad,10); _billborad->setPosition(Vec2( p.x,p.y )); + _billborad->setBlendFunc(cocos2d::BlendFunc::ALPHA_PREMULTIPLIED); + _billborad->setOpacity(200); auto billborad1 = BillBorad::create("Images/Icon.png"); billborad1->setScale(0.5f); _layerBillBorad->addChild(billborad1,10); billborad1->setPosition3D(Vec3(p.x,p.y,p.z - 130)); + billborad1->setBlendFunc(cocos2d::BlendFunc::ALPHA_PREMULTIPLIED); + billborad1->setOpacity(200); auto billborad2 = BillBorad::create("Images/Icon.png"); billborad2->setScale(0.5f); _layerBillBorad->addChild(billborad2,10); billborad2->setPosition3D(Vec3(p.x,p.y,p.z + 50)); + billborad2->setBlendFunc(cocos2d::BlendFunc::ALPHA_PREMULTIPLIED); + billborad2->setOpacity(200); auto billborad3 = BillBorad::create("Images/Icon.png"); billborad3->setScale(0.5f); _layerBillBorad->addChild(billborad3,10); billborad3->setPosition3D(Vec3(p.x,p.y,p.z + 90)); + billborad3->setBlendFunc(cocos2d::BlendFunc::ALPHA_PREMULTIPLIED); + billborad3->setOpacity(200); auto billborad4 = BillBorad::create("Images/Icon.png"); billborad4->setScale(0.5f); _layerBillBorad->addChild(billborad4,10); billborad4->setPosition3D(Vec3(p.x,p.y,p.z - 60)); + billborad4->setBlendFunc(cocos2d::BlendFunc::ALPHA_PREMULTIPLIED); + billborad4->setOpacity(200); }