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/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)
{
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

View File

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

View File

@ -396,6 +396,7 @@ void Renderer::visitTransparentRenderQueue(const TransparentRenderQueue& queue)
auto cmd = static_cast<QuadCommand*>(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();

View File

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