calculated Z in View

Transparent Render
This commit is contained in:
songchengjiang 2014-08-28 19:22:01 +08:00
parent 7751762893
commit e6f055f3fd
4 changed files with 46 additions and 32 deletions

View File

@ -23,10 +23,12 @@
****************************************************************************/ ****************************************************************************/
#include "3d/CCBillBoard.h" #include "3d/CCBillBoard.h"
#include "3d/CCMeshVertexIndexData.h"
#include "2d/CCSpriteFrameCache.h" #include "2d/CCSpriteFrameCache.h"
#include "base/CCDirector.h" #include "base/CCDirector.h"
#include "base/CCCamera.h" #include "base/CCCamera.h"
#include "renderer/CCRenderer.h" #include "renderer/CCRenderer.h"
#include "renderer/CCGLProgramCache.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -37,7 +39,6 @@ BillBorad::BillBorad()
BillBorad::~BillBorad() BillBorad::~BillBorad()
{ {
} }
BillBorad* BillBorad::createWithTexture(Texture2D *texture) BillBorad* BillBorad::createWithTexture(Texture2D *texture)
@ -91,27 +92,30 @@ BillBorad* BillBorad::create()
void BillBorad::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) 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; Mat4 transMat = transform;
transMat *= _BillBoradMat; transMat *= viewInverseMat;
// Don't do calculate the culling if the transform was not updated // Don't do calculate the culling if the transform was not updated
_insideBounds = (flags & FLAGS_TRANSFORM_DIRTY) ? renderer->checkVisibility(transMat, _contentSize) : _insideBounds; _insideBounds = (flags & FLAGS_TRANSFORM_DIRTY) ? renderer->checkVisibility(transMat, _contentSize) : _insideBounds;
if(_insideBounds) if(_insideBounds)
{ {
_quadCommand.init(_globalZOrder, _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, transMat); if (_displayedOpacity < 255)
renderer->addCommand(&_quadCommand); {
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 NS_CC_END

View File

@ -26,9 +26,11 @@
#define __CCBILLBOARD_H__ #define __CCBILLBOARD_H__
#include "2d/CCSprite.h" #include "2d/CCSprite.h"
#include "renderer/CCMeshCommand.h"
NS_CC_BEGIN NS_CC_BEGIN
class MeshIndexData;
/** /**
* Inherit from Sprite, achieve BillBorad. * Inherit from Sprite, achieve BillBorad.
*/ */
@ -84,11 +86,6 @@ CC_CONSTRUCTOR_ACCESS:
BillBorad(); BillBorad();
virtual ~BillBorad(); virtual ~BillBorad();
protected:
Mat4 _BillBoradMat;
void calculateBillBoradMatrix(Mat4 &dst);
private: private:
CC_DISALLOW_COPY_AND_ASSIGN(BillBorad); CC_DISALLOW_COPY_AND_ASSIGN(BillBorad);

View File

@ -396,6 +396,7 @@ void Renderer::visitTransparentRenderQueue(const TransparentRenderQueue& queue)
auto cmd = static_cast<QuadCommand*>(command); auto cmd = static_cast<QuadCommand*>(command);
_batchedQuadCommands.push_back(cmd); _batchedQuadCommands.push_back(cmd);
_numQuads = cmd->getQuadCount();
memcpy(_quads, cmd->getQuads(), sizeof(V3F_C4B_T2F_Quad) * cmd->getQuadCount()); memcpy(_quads, cmd->getQuads(), sizeof(V3F_C4B_T2F_Quad) * cmd->getQuadCount());
convertToWorldCoordinates(_quads, cmd->getQuadCount(), cmd->getModelView()); convertToWorldCoordinates(_quads, cmd->getQuadCount(), cmd->getModelView());
drawBatchedQuads(); drawBatchedQuads();
@ -459,7 +460,9 @@ void Renderer::render()
if (_transparentRenderGroups.size()) if (_transparentRenderGroups.size())
{ {
_transparentRenderGroups.sort(); _transparentRenderGroups.sort();
glEnable(GL_DEPTH_TEST);
visitTransparentRenderQueue(_transparentRenderGroups); visitTransparentRenderQueue(_transparentRenderGroups);
glDisable(GL_DEPTH_TEST);
} }
} }
clean(); clean();

View File

@ -1010,20 +1010,20 @@ Sprite3DReskinTest::Sprite3DReskinTest()
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
TTFConfig ttfConfig("fonts/arial.ttf", 20); TTFConfig ttfConfig("fonts/arial.ttf", 20);
auto label1 = Label::createWithTTF(ttfConfig,"Hair"); 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 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 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 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 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 ) ); 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 ) ); 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 ) ); 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 ) ); 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 ) ); item5->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *8 ) );
auto pMenu1 = CCMenu::create(item1,item2,item3,item4,item5,NULL); auto pMenu1 = CCMenu::create(item1,item2,item3,item4,item5,NULL);
pMenu1->setPosition(Vec2(0,0)); pMenu1->setPosition(Vec2(0,0));
this->addChild(pMenu1, 10); this->addChild(pMenu1, 10);
@ -1549,9 +1549,9 @@ BillBoardTest::BillBoardTest()
_layerBillBorad->addChild(_camera); _layerBillBorad->addChild(_camera);
} }
addNewBillBoradWithCoords(Vec3(20,0,0)); addNewBillBoradWithCoords(Vec3(20,5,0));
addNewAniBillBoradWithCoords(Vec3(-20,0,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)); _camera->lookAt(Vec3(0,0,0), Vec3(0,1,0));
for( int j =-20; j<=20 ;j++) for( int j =-20; j<=20 ;j++)
@ -1566,7 +1566,7 @@ BillBoardTest::BillBoardTest()
//draw y //draw y
//line->drawLine(Vec3(0, -50, 0),Vec3(0,0,0),Color4F(0,0.5,0,1)); //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)); //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); TTFConfig ttfConfig("fonts/arial.ttf", 20);
auto label1 = Label::createWithTTF(ttfConfig,"rotate+"); auto label1 = Label::createWithTTF(ttfConfig,"rotate+");
@ -1601,26 +1601,36 @@ void BillBoardTest::addNewBillBoradWithCoords(Vec3 p)
_billborad->setScale(0.5f); _billborad->setScale(0.5f);
_layerBillBorad->addChild(_billborad,10); _layerBillBorad->addChild(_billborad,10);
_billborad->setPosition(Vec2( p.x,p.y )); _billborad->setPosition(Vec2( p.x,p.y ));
_billborad->setBlendFunc(cocos2d::BlendFunc::ALPHA_PREMULTIPLIED);
_billborad->setOpacity(200);
auto billborad1 = BillBorad::create("Images/Icon.png"); auto billborad1 = BillBorad::create("Images/Icon.png");
billborad1->setScale(0.5f); billborad1->setScale(0.5f);
_layerBillBorad->addChild(billborad1,10); _layerBillBorad->addChild(billborad1,10);
billborad1->setPosition3D(Vec3(p.x,p.y,p.z - 130)); 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"); auto billborad2 = BillBorad::create("Images/Icon.png");
billborad2->setScale(0.5f); billborad2->setScale(0.5f);
_layerBillBorad->addChild(billborad2,10); _layerBillBorad->addChild(billborad2,10);
billborad2->setPosition3D(Vec3(p.x,p.y,p.z + 50)); 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"); auto billborad3 = BillBorad::create("Images/Icon.png");
billborad3->setScale(0.5f); billborad3->setScale(0.5f);
_layerBillBorad->addChild(billborad3,10); _layerBillBorad->addChild(billborad3,10);
billborad3->setPosition3D(Vec3(p.x,p.y,p.z + 90)); 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"); auto billborad4 = BillBorad::create("Images/Icon.png");
billborad4->setScale(0.5f); billborad4->setScale(0.5f);
_layerBillBorad->addChild(billborad4,10); _layerBillBorad->addChild(billborad4,10);
billborad4->setPosition3D(Vec3(p.x,p.y,p.z - 60)); billborad4->setPosition3D(Vec3(p.x,p.y,p.z - 60));
billborad4->setBlendFunc(cocos2d::BlendFunc::ALPHA_PREMULTIPLIED);
billborad4->setOpacity(200);
} }