added mesh command

This commit is contained in:
songchengjiang 2014-08-29 12:08:28 +08:00
parent 43eb725c27
commit 71d722854c
3 changed files with 190 additions and 124 deletions

View File

@ -23,12 +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"
#include "renderer/CCVertexIndexBuffer.h"
NS_CC_BEGIN
@ -46,6 +46,7 @@ BillBorad* BillBorad::createWithTexture(Texture2D *texture)
BillBorad *billborad = new (std::nothrow) BillBorad();
if (billborad && billborad->initWithTexture(texture))
{
billborad->genRenderInformation();
billborad->autorelease();
return billborad;
}
@ -59,6 +60,7 @@ BillBorad* BillBorad::create(const std::string& filename)
BillBorad *billborad = new (std::nothrow) BillBorad();
if (billborad && billborad->initWithFile(filename))
{
billborad->genRenderInformation();
billborad->autorelease();
return billborad;
}
@ -71,6 +73,7 @@ BillBorad* BillBorad::create(const std::string& filename, const Rect& rect)
BillBorad *billborad = new (std::nothrow) BillBorad();
if (billborad && billborad->initWithFile(filename, rect))
{
billborad->genRenderInformation();
billborad->autorelease();
return billborad;
}
@ -83,6 +86,7 @@ BillBorad* BillBorad::create()
BillBorad *billborad = new (std::nothrow) BillBorad();
if (billborad && billborad->init())
{
billborad->genRenderInformation();
billborad->autorelease();
return billborad;
}
@ -105,8 +109,26 @@ void BillBorad::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
Mat4 modelViewMat = camera->getViewMatrix() * transMat;
_quadCommand.init(-modelViewMat.m[14], _texture->getName(), getGLProgramState(), _blendFunc, &_quad, 1, transMat);
renderer->addTransparentCommand(&_quadCommand);
_meshCommand.init(-modelViewMat.m[14], _texture->getName(), getGLProgramState(), _blendFunc, _vertexBuffer->getVBO(), _indexBuffer->getVBO(), GL_TRIANGLES, GL_UNSIGNED_SHORT, _indexBuffer->getIndexNumber(), transMat);
renderer->addTransparentCommand(&_meshCommand);
}
}
void BillBorad::genRenderInformation()
{
_vertexBuffer = VertexBuffer::create(sizeof(_quad), 1);
_vertexBuffer->updateVertices((void*)&_quad, 1, 0);
std::vector<unsigned short> indices;
indices.push_back(0);indices.push_back(1);indices.push_back(2);
indices.push_back(3);indices.push_back(2);indices.push_back(1);
_indexBuffer = IndexBuffer::create(IndexBuffer::IndexType::INDEX_TYPE_SHORT_16, indices.size());
_indexBuffer->updateIndices((void*)&indices, (int)indices.size(), 0);
#define kQuadSize sizeof(_quad.bl)
getGLProgramState()->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, vertices));
getGLProgramState()->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, colors));
getGLProgramState()->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof(V3F_C4B_T2F, texCoords));
}
NS_CC_END

View File

@ -30,7 +30,9 @@
NS_CC_BEGIN
class MeshIndexData;
class VertexBuffer;
class IndexBuffer;
/**
* Inherit from Sprite, achieve BillBorad.
*/
@ -86,6 +88,16 @@ CC_CONSTRUCTOR_ACCESS:
BillBorad();
virtual ~BillBorad();
protected:
void genRenderInformation();
protected:
MeshCommand _meshCommand;
VertexBuffer *_vertexBuffer;
IndexBuffer *_indexBuffer;
private:
CC_DISALLOW_COPY_AND_ASSIGN(BillBorad);

View File

@ -1597,145 +1597,177 @@ std::string BillBoardTest::subtitle() const
}
void BillBoardTest::addNewBillBoradWithCoords(Vec3 p)
{
_billborad = BillBorad::create("Images/Icon.png");
_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);
for (unsigned int i = 0; i < 100; ++i)
{
auto billborad = BillBorad::create("Images/Icon.png");
billborad->setScale(CCRANDOM_0_1() * 5.0f);
_layerBillBorad->addChild(billborad);
billborad->setPosition3D(Vec3(p.x + CCRANDOM_MINUS1_1() * 200, p.y + CCRANDOM_MINUS1_1() * 200, p.z + CCRANDOM_MINUS1_1() * 200));
billborad->setBlendFunc(cocos2d::BlendFunc::ALPHA_PREMULTIPLIED);
billborad->setOpacity(CCRANDOM_0_1() * 255);
}
//_billborad = BillBorad::create("Images/Icon.png");
//_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 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 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 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 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 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);
//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);
}
void BillBoardTest::addNewAniBillBoradWithCoords(Vec3 p)
{
GLubyte alpha = 255;
auto billboradAni1 = BillBorad::create("Images/grossini.png");
billboradAni1->setScale(0.5f);
billboradAni1->setPosition3D(Vec3(p.x, p.y, p.z - 130));
_layerBillBorad->addChild(billboradAni1,10);
auto animation1 = Animation::create();
for( int i=1;i<15;i++)
{
char szName1[100] = {0};
sprintf(szName1, "Images/grossini_dance_%02d.png", i);
animation1->addSpriteFrameWithFile(szName1);
}
// should last 2.8 seconds. And there are 14 frames.
animation1->setDelayPerUnit(2.8f / 14.0f);
animation1->setRestoreOriginalFrame(true);
for (unsigned int i = 0; i < 100; ++i)
{
auto billboradAni = BillBorad::create("Images/grossini.png");
billboradAni->setScale(CCRANDOM_0_1() * 5.0f);
billboradAni->setPosition3D(Vec3(p.x + CCRANDOM_MINUS1_1() * 200, p.y + CCRANDOM_MINUS1_1() * 200, p.z + CCRANDOM_MINUS1_1() * 200));
_layerBillBorad->addChild(billboradAni);
auto action1 = Animate::create(animation1);
billboradAni1->runAction(RepeatForever::create(action1));
billboradAni1->setOpacity(alpha);
auto animation = Animation::create();
for( int i=1;i<15;i++)
{
char szName1[100] = {0};
sprintf(szName1, "Images/grossini_dance_%02d.png", i);
animation->addSpriteFrameWithFile(szName1);
}
// should last 2.8 seconds. And there are 14 frames.
animation->setDelayPerUnit(2.8f / 14.0f);
animation->setRestoreOriginalFrame(true);
auto billboradAni2 = BillBorad::create("Images/grossini.png");
billboradAni2->setScale(0.5f);
billboradAni2->setPosition3D(Vec3(p.x, p.y, p.z - 60));
_layerBillBorad->addChild(billboradAni2,10);
auto animation2 = Animation::create();
for( int i=1;i<15;i++)
{
char szName2[100] = {0};
sprintf(szName2, "Images/grossini_dance_%02d.png", i);
animation2->addSpriteFrameWithFile(szName2);
}
// should last 2.8 seconds. And there are 14 frames.
animation2->setDelayPerUnit(2.8f / 14.0f);
animation2->setRestoreOriginalFrame(true);
auto action = Animate::create(animation);
billboradAni->runAction(RepeatForever::create(action));
billboradAni->setOpacity(CCRANDOM_0_1() * 255);
}
auto action2 = Animate::create(animation2);
billboradAni2->runAction(RepeatForever::create(action2));
billboradAni2->setOpacity(alpha);
//auto billboradAni1 = BillBorad::create("Images/grossini.png");
//billboradAni1->setScale(0.5f);
//billboradAni1->setPosition3D(Vec3(p.x, p.y, p.z - 130));
//_layerBillBorad->addChild(billboradAni1,10);
//
//auto animation1 = Animation::create();
//for( int i=1;i<15;i++)
//{
// char szName1[100] = {0};
// sprintf(szName1, "Images/grossini_dance_%02d.png", i);
// animation1->addSpriteFrameWithFile(szName1);
//}
//// should last 2.8 seconds. And there are 14 frames.
//animation1->setDelayPerUnit(2.8f / 14.0f);
//animation1->setRestoreOriginalFrame(true);
_billboradAni = BillBorad::create("Images/grossini.png");
_billboradAni->setScale(0.5f);
_billboradAni->setPosition3D( p );
_layerBillBorad->addChild(_billboradAni,10);
auto animation = Animation::create();
for( int i=1;i<15;i++)
{
char szName[100] = {0};
sprintf(szName, "Images/grossini_dance_%02d.png", i);
animation->addSpriteFrameWithFile(szName);
}
// should last 2.8 seconds. And there are 14 frames.
animation->setDelayPerUnit(2.8f / 14.0f);
animation->setRestoreOriginalFrame(true);
//auto action1 = Animate::create(animation1);
//billboradAni1->runAction(RepeatForever::create(action1));
//billboradAni1->setOpacity(alpha);
auto action = Animate::create(animation);
_billboradAni->runAction(RepeatForever::create(action));
_billboradAni->setOpacity(alpha);
//auto billboradAni2 = BillBorad::create("Images/grossini.png");
//billboradAni2->setScale(0.5f);
//billboradAni2->setPosition3D(Vec3(p.x, p.y, p.z - 60));
//_layerBillBorad->addChild(billboradAni2,10);
//
//auto animation2 = Animation::create();
//for( int i=1;i<15;i++)
//{
// char szName2[100] = {0};
// sprintf(szName2, "Images/grossini_dance_%02d.png", i);
// animation2->addSpriteFrameWithFile(szName2);
//}
//// should last 2.8 seconds. And there are 14 frames.
//animation2->setDelayPerUnit(2.8f / 14.0f);
//animation2->setRestoreOriginalFrame(true);
auto billboradAni3 = BillBorad::create("Images/grossini.png");
billboradAni3->setScale(0.5f);
billboradAni3->setPosition3D(Vec3(p.x, p.y, p.z + 50));
_layerBillBorad->addChild(billboradAni3,10);
auto animation3 = Animation::create();
for( int i=1;i<15;i++)
{
char szName3[100] = {0};
sprintf(szName3, "Images/grossini_dance_%02d.png", i);
animation3->addSpriteFrameWithFile(szName3);
}
// should last 2.8 seconds. And there are 14 frames.
animation3->setDelayPerUnit(2.8f / 14.0f);
animation3->setRestoreOriginalFrame(true);
//auto action2 = Animate::create(animation2);
//billboradAni2->runAction(RepeatForever::create(action2));
//billboradAni2->setOpacity(alpha);
auto action3 = Animate::create(animation3);
billboradAni3->runAction(RepeatForever::create(action3));
billboradAni3->setOpacity(alpha);
//_billboradAni = BillBorad::create("Images/grossini.png");
//_billboradAni->setScale(0.5f);
//_billboradAni->setPosition3D( p );
//_layerBillBorad->addChild(_billboradAni,10);
//
//auto animation = Animation::create();
//for( int i=1;i<15;i++)
//{
// char szName[100] = {0};
// sprintf(szName, "Images/grossini_dance_%02d.png", i);
// animation->addSpriteFrameWithFile(szName);
//}
//// should last 2.8 seconds. And there are 14 frames.
//animation->setDelayPerUnit(2.8f / 14.0f);
//animation->setRestoreOriginalFrame(true);
auto billboradAni4 = BillBorad::create("Images/grossini.png");
billboradAni4->setScale(0.5f);
billboradAni4->setPosition3D(Vec3(p.x, p.y, p.z + 90));
_layerBillBorad->addChild(billboradAni4,10);
auto animation4 = Animation::create();
for( int i=1;i<15;i++)
{
char szName4[100] = {0};
sprintf(szName4, "Images/grossini_dance_%02d.png", i);
animation4->addSpriteFrameWithFile(szName4);
}
// should last 2.8 seconds. And there are 14 frames.
animation4->setDelayPerUnit(2.8f / 14.0f);
animation4->setRestoreOriginalFrame(true);
//auto action = Animate::create(animation);
//_billboradAni->runAction(RepeatForever::create(action));
//_billboradAni->setOpacity(alpha);
auto action4 = Animate::create(animation4);
billboradAni4->runAction(RepeatForever::create(action4));
billboradAni4->setOpacity(alpha);
//auto billboradAni3 = BillBorad::create("Images/grossini.png");
//billboradAni3->setScale(0.5f);
//billboradAni3->setPosition3D(Vec3(p.x, p.y, p.z + 50));
//_layerBillBorad->addChild(billboradAni3,10);
//
//auto animation3 = Animation::create();
//for( int i=1;i<15;i++)
//{
// char szName3[100] = {0};
// sprintf(szName3, "Images/grossini_dance_%02d.png", i);
// animation3->addSpriteFrameWithFile(szName3);
//}
//// should last 2.8 seconds. And there are 14 frames.
//animation3->setDelayPerUnit(2.8f / 14.0f);
//animation3->setRestoreOriginalFrame(true);
//auto action3 = Animate::create(animation3);
//billboradAni3->runAction(RepeatForever::create(action3));
//billboradAni3->setOpacity(alpha);
//auto billboradAni4 = BillBorad::create("Images/grossini.png");
//billboradAni4->setScale(0.5f);
//billboradAni4->setPosition3D(Vec3(p.x, p.y, p.z + 90));
//_layerBillBorad->addChild(billboradAni4,10);
//
//auto animation4 = Animation::create();
//for( int i=1;i<15;i++)
//{
// char szName4[100] = {0};
// sprintf(szName4, "Images/grossini_dance_%02d.png", i);
// animation4->addSpriteFrameWithFile(szName4);
//}
//// should last 2.8 seconds. And there are 14 frames.
//animation4->setDelayPerUnit(2.8f / 14.0f);
//animation4->setRestoreOriginalFrame(true);
//auto action4 = Animate::create(animation4);
//billboradAni4->runAction(RepeatForever::create(action4));
//billboradAni4->setOpacity(alpha);
}
void BillBoardTest::update(float dt)
{