CCPUBillboardChain.cpp 29.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
/****************************************************************************
 Copyright (C) 2013 Henry van Merode. All rights reserved.
 Copyright (c) 2015-2016 Chukong Technologies Inc.
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/
#include "extensions/Particle3D/PU/CCPUBillboardChain.h"
#include <stddef.h> // offsetof
#include "base/ccTypes.h"
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
#include "base/CCDirector.h"
#include "renderer/ccShaders.h"
#include "renderer/CCMeshCommand.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCTextureCache.h"
#include "renderer/backend/Types.h"
#include "renderer/backend/Buffer.h"
#include "renderer/backend/Device.h"
#include "2d/CCCamera.h"
#include "3d/CCSprite3D.h"

NS_CC_BEGIN

const size_t PUBillboardChain::SEGMENT_EMPTY = std::numeric_limits<size_t>::max();
//-----------------------------------------------------------------------
PUBillboardChain::Element::Element()
{
}
//-----------------------------------------------------------------------
PUBillboardChain::Element::Element(const Vec3 &pos,
                                   float w,
                                   float tex,
                                   const Vec4 &col,
                                   const Quaternion &ori) :
position(pos),
width(w),
texCoord(tex),
color(col),
orientation(ori)
{
}
//-----------------------------------------------------------------------
PUBillboardChain::PUBillboardChain(const std::string& /*name*/, const std::string &texFile, size_t maxElements,
                               size_t numberOfChains, bool useTextureCoords, bool useColours, bool dynamic)
                               :_maxElementsPerChain(maxElements),
                               _chainCount(numberOfChains),
                               _useTexCoords(useTextureCoords),
                               _useVertexColour(useColours),
                               _dynamic(dynamic),
                               _vertexDeclDirty(true),
                               _buffersNeedRecreating(true),
                               _boundsDirty(true),
                               _indexContentDirty(true),
                               _vertexContentDirty(true),
                               _texCoordDir(TCD_U),
                               _faceCamera(true),
                               _normalBase(Vec3::UNIT_X),
                               _texture(nullptr),
                               _programState(nullptr),
                               _indexBuffer(nullptr),
                               _vertexBuffer(nullptr),
                               _texFile(texFile)
{

    _stateBlock.setCullFace(false);
    _stateBlock.setCullFaceSide(backend::CullMode::BACK);
    _stateBlock.setDepthTest(false);
    _stateBlock.setDepthWrite(false);
    _stateBlock.setBlend(true);

    _otherTexCoordRange[0] = 0.0f;
    _otherTexCoordRange[1] = 1.0f;

    setupChainContainers();

    init(texFile);
}
//-----------------------------------------------------------------------
PUBillboardChain::~PUBillboardChain()
{
    //CC_SAFE_RELEASE(_texture);
    CC_SAFE_RELEASE(_programState);
    CC_SAFE_RELEASE(_vertexBuffer);
    CC_SAFE_RELEASE(_indexBuffer);
}
//-----------------------------------------------------------------------
void PUBillboardChain::setupChainContainers()
{
    // Allocate enough space for everything
    _chainElementList.resize(_chainCount * _maxElementsPerChain);
    //_vertexData->vertexCount = _chainElementList.size() * 2;

    // Configure chains
    _chainSegmentList.resize(_chainCount);
    for (size_t i = 0; i < _chainCount; ++i)
    {
        ChainSegment& seg = _chainSegmentList[i];
        seg.start = i * _maxElementsPerChain;
        seg.tail = seg.head = SEGMENT_EMPTY;
    }
}
//-----------------------------------------------------------------------
void PUBillboardChain::setupVertexDeclaration()
{
    //if (_vertexDeclDirty)
    //{
    //	VertexDeclaration* decl = _vertexData->vertexDeclaration;
    //	decl->removeAllElements();

    //	size_t offset = 0;
    //	// Add a description for the buffer of the positions of the vertices
    //	decl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
    //	offset += VertexElement::getTypeSize(VET_FLOAT3);

    //	if (_useVertexColour)
    //	{
    //		decl->addElement(0, offset, VET_COLOUR, VES_DIFFUSE);
    //		offset += VertexElement::getTypeSize(VET_COLOUR);
    //	}

    //	if (_useTexCoords)
    //	{
    //		decl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES);
    //	}

    //	if (!_useTexCoords && !_useVertexColour)
    //	{
    //		LogManager::getSingleton().logMessage(
    //			"Error - BillboardChain '" + mName + "' is using neither "
    //			"texture coordinates or vertex colours; it will not be "
    //			"visible on some rendering APIs so you should change this "
    //			"so you use one or the other.");
    //	}
    //	_vertexDeclDirty = false;
    //}
}
//-----------------------------------------------------------------------
void PUBillboardChain::setupBuffers()
{
    //setupVertexDeclaration();
    if (_buffersNeedRecreating)
    {
        CC_SAFE_RELEASE_NULL(_vertexBuffer);
        CC_SAFE_RELEASE_NULL(_indexBuffer);

        size_t stride = sizeof(VertexInfo);
        _vertexBuffer = backend::Device::getInstance()->newBuffer(stride * _chainElementList.size() * 2, backend::BufferType::VERTEX, backend::BufferUsage::DYNAMIC);
        VertexInfo vi = {Vec3(0.0f, 0.0f, 0.0f), Vec2(0.0f, 0.0f), Vec4::ONE};
        _vertices.resize(_chainElementList.size() * 2, vi);

        _indexBuffer = backend::Device::getInstance()->newBuffer(_chainCount * _maxElementsPerChain * 6 * sizeof(uint16_t), backend::BufferType::VERTEX, backend::BufferUsage::DYNAMIC);

        _indices.resize(_chainCount * _maxElementsPerChain * 6, 0);

        //// Create the vertex buffer (always dynamic due to the camera adjust)
        //HardwareVertexBufferSharedPtr pBuffer =
        //	HardwareBufferManager::getSingleton().createVertexBuffer(
        //	_vertexData->vertexDeclaration->getVertexSize(0),
        //	_vertexData->vertexCount,
        //	HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE);

        //// (re)Bind the buffer
        //// Any existing buffer will lose its reference count and be destroyed
        //_vertexData->vertexBufferBinding->setBinding(0, pBuffer);

        //_indexData->indexBuffer =
        //	HardwareBufferManager::getSingleton().createIndexBuffer(
        //	HardwareIndexBuffer::IT_16BIT,
        //	_chainCount * _maxElementsPerChain * 6, // max we can use
        //	_dynamic? HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY : HardwareBuffer::HBU_STATIC_WRITE_ONLY);
        //// NB we don't set the indexCount on IndexData here since we will
        //// probably use less than the maximum number of indices

        _buffersNeedRecreating = false;
    }
}
//-----------------------------------------------------------------------
void PUBillboardChain::setMaxChainElements(size_t maxElements)
{
    _maxElementsPerChain = maxElements;
    setupChainContainers();
    _buffersNeedRecreating = _indexContentDirty = _vertexContentDirty = true;
}
//-----------------------------------------------------------------------
void PUBillboardChain::setNumberOfChains(size_t numChains)
{
    _chainCount = numChains;
    setupChainContainers();
    _buffersNeedRecreating = _indexContentDirty = _vertexContentDirty = true;
}
//-----------------------------------------------------------------------
void PUBillboardChain::setUseTextureCoords(bool use)
{
    _useTexCoords = use;
    _vertexDeclDirty = _buffersNeedRecreating = true;
    _indexContentDirty = _vertexContentDirty = true;
}
//-----------------------------------------------------------------------
void PUBillboardChain::setTextureCoordDirection(PUBillboardChain::TexCoordDirection dir)
{
    _texCoordDir = dir;
    _vertexContentDirty = true;
}
//-----------------------------------------------------------------------
void PUBillboardChain::setOtherTextureCoordRange(float start, float end)
{
    _otherTexCoordRange[0] = start;
    _otherTexCoordRange[1] = end;
    _vertexContentDirty = true;
}
//-----------------------------------------------------------------------
void PUBillboardChain::setUseVertexColours(bool use)
{
    _useVertexColour = use;
    _vertexDeclDirty = _buffersNeedRecreating = true;
    _indexContentDirty = _vertexContentDirty = true;
}
//-----------------------------------------------------------------------
void PUBillboardChain::setDynamic(bool dyn)
{
    _dynamic = dyn;
    _buffersNeedRecreating = _indexContentDirty = _vertexContentDirty = true;
}
//-----------------------------------------------------------------------
void PUBillboardChain::addChainElement(size_t chainIndex,
                                     const PUBillboardChain::Element& dtls)
{
    CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
    ChainSegment& seg = _chainSegmentList[chainIndex];
    if (seg.head == SEGMENT_EMPTY)
    {
        // Tail starts at end, head grows backwards
        seg.tail = _maxElementsPerChain - 1;
        seg.head = seg.tail;
    }
    else
    {
        if (seg.head == 0)
        {
            // Wrap backwards
            seg.head = _maxElementsPerChain - 1;
        }
        else
        {
            // Just step backward
            --seg.head;
        }
        // Run out of elements?
        if (seg.head == seg.tail)
        {
            // Move tail backwards too, losing the end of the segment and re-using
            // it in the head
            if (seg.tail == 0)
                seg.tail = _maxElementsPerChain - 1;
            else
                --seg.tail;
        }
    }

    // Set the details
    _chainElementList[seg.start + seg.head] = dtls;

    _vertexContentDirty = true;
    _indexContentDirty = true;
    _boundsDirty = true;
    //// tell parent node to update bounds
    //if (mParentNode)
    //	mParentNode->needUpdate();

}
//-----------------------------------------------------------------------
void PUBillboardChain::removeChainElement(size_t chainIndex)
{
    CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
    ChainSegment& seg = _chainSegmentList[chainIndex];
    if (seg.head == SEGMENT_EMPTY)
        return; // do nothing, nothing to remove


    if (seg.tail == seg.head)
    {
        // last item
        seg.head = seg.tail = SEGMENT_EMPTY;
    }
    else if (seg.tail == 0)
    {
        seg.tail = _maxElementsPerChain - 1;
    }
    else
    {
        --seg.tail;
    }

    // we removed an entry so indexes need updating
    _vertexContentDirty = true;
    _indexContentDirty = true;
    _boundsDirty = true;
    //// tell parent node to update bounds
    //if (mParentNode)
    //	mParentNode->needUpdate();

}
//-----------------------------------------------------------------------
void PUBillboardChain::clearChain(size_t chainIndex)
{
    CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
    ChainSegment& seg = _chainSegmentList[chainIndex];

    // Just reset head & tail
    seg.tail = seg.head = SEGMENT_EMPTY;

    // we removed an entry so indexes need updating
    _vertexContentDirty = true;
    _indexContentDirty = true;
    _boundsDirty = true;
    //// tell parent node to update bounds
    //if (mParentNode)
    //	mParentNode->needUpdate();

}
//-----------------------------------------------------------------------
void PUBillboardChain::clearAllChains()
{
    for (size_t i = 0; i < _chainCount; ++i)
    {
        clearChain(i);
    }

}
//-----------------------------------------------------------------------
void PUBillboardChain::setFaceCamera( bool faceCamera, const Vec3 &normalVector )
{
    _faceCamera = faceCamera;
    _normalBase = normalVector;
    _normalBase.normalize();
    _vertexContentDirty = true;
}
//-----------------------------------------------------------------------
void PUBillboardChain::updateChainElement(size_t chainIndex, size_t elementIndex,
                                        const PUBillboardChain::Element& dtls)
{
    CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
    ChainSegment& seg = _chainSegmentList[chainIndex];
    CCASSERT(seg.head != SEGMENT_EMPTY, "Chain segment is empty");

    size_t idx = seg.head + elementIndex;
    // adjust for the edge and start
    idx = (idx % _maxElementsPerChain) + seg.start;

    _chainElementList[idx] = dtls;

    _vertexContentDirty = true;
    _boundsDirty = true;

    //// tell parent node to update bounds
    //if (mParentNode)
    //	mParentNode->needUpdate();
}
//-----------------------------------------------------------------------
const PUBillboardChain::Element&
    PUBillboardChain::getChainElement(size_t chainIndex, size_t elementIndex) const
{
    CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
    const ChainSegment& seg = _chainSegmentList[chainIndex];

    size_t idx = seg.head + elementIndex;
    // adjust for the edge and start
    idx = (idx % _maxElementsPerChain) + seg.start;

    return _chainElementList[idx];
}
//-----------------------------------------------------------------------
size_t PUBillboardChain::getNumChainElements(size_t chainIndex) const
{
    CCASSERT(chainIndex < _chainCount, "chainIndex out of bounds");
    const ChainSegment& seg = _chainSegmentList[chainIndex];

    if( seg.tail < seg.head )
    {
        return seg.tail - seg.head + _maxElementsPerChain + 1;
    }
    else
    {
        return seg.tail - seg.head + 1;
    }
}
//-----------------------------------------------------------------------
void PUBillboardChain::updateVertexBuffer(const Mat4 &camMat)
{
    setupBuffers();

    // The contents of the vertex buffer are correct if they are not dirty
    // and the camera used to build the vertex buffer is still the current 
    // camera.
    if (!_vertexContentDirty)
        return;

    VertexInfo vi = {Vec3(0.0f, 0.0f, 0.0f), Vec2(0.0f, 0.0f), Vec4::ONE};
    _vertices.assign(_vertices.size(), vi);
    //HardwareVertexBufferSharedPtr pBuffer =
    //	_vertexData->vertexBufferBinding->getBuffer(0);
    //void* pBufferStart = pBuffer->lock(HardwareBuffer::HBL_DISCARD);

    //const Vector3& camPos = cam->getDerivedPosition();
    //Vector3 eyePos = mParentNode->_getDerivedOrientation().Inverse() *
    //	(camPos - mParentNode->_getDerivedPosition()) / mParentNode->_getDerivedScale();

    Vec3 eyePos(camMat.m[12], camMat.m[13], camMat.m[14]);

    Vec3 chainTangent;
    for (ChainSegmentList::iterator segi = _chainSegmentList.begin();
        segi != _chainSegmentList.end(); ++segi)
    {
        ChainSegment& seg = *segi;

        // Skip 0 or 1 element segment counts
        if (seg.head != SEGMENT_EMPTY && seg.head != seg.tail)
        {
            size_t laste = seg.head;
            for (size_t e = seg.head; ; ++e) // until break
            {
                // Wrap forwards
                if (e == _maxElementsPerChain)
                    e = 0;

                Element& elem = _chainElementList[e + seg.start];
                CCASSERT (((e + seg.start) * 2) < 65536, "Too many elements!");
                unsigned short vertexIndex = static_cast<unsigned short>((e + seg.start) * 2);
                //// Determine base pointer to vertex #1
                //void* pBase = static_cast<void*>(
                //	static_cast<char*>(pBufferStart) +
                //	pBuffer->getVertexSize() * baseIdx);

                // Get index of next item
                size_t nexte = e + 1;
                if (nexte == _maxElementsPerChain)
                    nexte = 0;

                if (e == seg.head)
                {
                    // No laste, use next item
                    chainTangent = _chainElementList[nexte + seg.start].position - elem.position;
                }
                else if (e == seg.tail)
                {
                    // No nexte, use only last item
                    chainTangent = elem.position - _chainElementList[laste + seg.start].position;
                }
                else
                {
                    // A mid position, use tangent across both prev and next
                    chainTangent = _chainElementList[nexte + seg.start].position - _chainElementList[laste + seg.start].position;

                }

                Vec3 vP1ToEye;

                //if( _faceCamera )
                    vP1ToEye = eyePos - elem.position;
                //else
                //	vP1ToEye = elem.orientation * _normalBase;

                Vec3 vPerpendicular;
                Vec3::cross(chainTangent, vP1ToEye, &vPerpendicular);
                vPerpendicular.normalize();
                vPerpendicular *= (elem.width * 0.5f);

                Vec3 pos0 = elem.position - vPerpendicular;
                Vec3 pos1 = elem.position + vPerpendicular;

                //float* pFloat = static_cast<float*>(pBase);
                //// pos1
                //*pFloat++ = pos0.x;
                //*pFloat++ = pos0.y;
                //*pFloat++ = pos0.z;
                _vertices[vertexIndex + 0].position = pos0;


                //pBase = static_cast<void*>(pFloat);

                if (_useVertexColour)
                {
                    //RGBA* pCol = static_cast<RGBA*>(pBase);
                    //Root::getSingleton().convertColourValue(elem.colour, pCol);
                    //pCol++;
                    //pBase = static_cast<void*>(pCol);
                    _vertices[vertexIndex + 0].color = elem.color;
                }

                if (_useTexCoords)
                {
                    //pFloat = static_cast<float*>(pBase);
                    if (_texCoordDir == TCD_U)
                    {
                        //*pFloat++ = elem.texCoord;
                        //*pFloat++ = _otherTexCoordRange[0];
                        _vertices[vertexIndex + 0].uv.x = elem.texCoord;
                        _vertices[vertexIndex + 0].uv.y = _otherTexCoordRange[0];
                    }
                    else
                    {
                        //*pFloat++ = _otherTexCoordRange[0];
                        //*pFloat++ = elem.texCoord;
                        _vertices[vertexIndex + 0].uv.x = _otherTexCoordRange[0];
                        _vertices[vertexIndex + 0].uv.y = elem.texCoord;
                    }
                    //pBase = static_cast<void*>(pFloat);
                }

                // pos2
                //pFloat = static_cast<float*>(pBase);
                //*pFloat++ = pos1.x;
                //*pFloat++ = pos1.y;
                //*pFloat++ = pos1.z;
                //pBase = static_cast<void*>(pFloat);
                _vertices[vertexIndex + 1].position = pos1;

                if (_useVertexColour)
                {
                    //RGBA* pCol = static_cast<RGBA*>(pBase);
                    //Root::getSingleton().convertColourValue(elem.colour, pCol);
                    //pCol++;
                    //pBase = static_cast<void*>(pCol);
                    _vertices[vertexIndex + 1].color = elem.color;
                }

                if (_useTexCoords)
                {
                    //pFloat = static_cast<float*>(pBase);
                    if (_texCoordDir == TCD_U)
                    {
                        //*pFloat++ = elem.texCoord;
                        //*pFloat++ = _otherTexCoordRange[1];
                        _vertices[vertexIndex + 1].uv.x = elem.texCoord;
                        _vertices[vertexIndex + 1].uv.y = _otherTexCoordRange[1];
                    }
                    else
                    {
                        //*pFloat++ = _otherTexCoordRange[1];
                        //*pFloat++ = elem.texCoord;
                        _vertices[vertexIndex + 1].uv.x = _otherTexCoordRange[1];
                        _vertices[vertexIndex + 1].uv.y = elem.texCoord;
                    }
                }

                if (e == seg.tail)
                    break; // last one

                laste = e;
                //vertexIndex += 2;
            } // element
        } // segment valid?

    } // each segment

    _vertexBuffer->updateData(&_vertices[0], sizeof(_vertices[0]) * _vertices.size());
    //pBuffer->unlock();
    //_vertexCameraUsed = cam;
    _vertexContentDirty = false;

}
//-----------------------------------------------------------------------
void PUBillboardChain::updateIndexBuffer()
{

    setupBuffers();
    if (_indexContentDirty)
    {

        //uint16* pShort = static_cast<uint16*>(
        //	_indexData->indexBuffer->lock(HardwareBuffer::HBL_DISCARD));
        //_indexData->indexCount = 0;
        // indexes
        unsigned short index = 0;
        for (ChainSegmentList::iterator segi = _chainSegmentList.begin();
            segi != _chainSegmentList.end(); ++segi)
        {
            ChainSegment& seg = *segi;

            // Skip 0 or 1 element segment counts
            if (seg.head != SEGMENT_EMPTY && seg.head != seg.tail)
            {
                // Start from head + 1 since it's only useful in pairs
                size_t laste = seg.head;
                while(1) // until break
                {
                    size_t e = laste + 1;
                    // Wrap forwards
                    if (e == _maxElementsPerChain)
                        e = 0;
                    // indexes of this element are (e * 2) and (e * 2) + 1
                    // indexes of the last element are the same, -2
                    CCASSERT (((e + seg.start) * 2) < 65536, "Too many elements!");
                    unsigned short baseIdx = static_cast<unsigned short>((e + seg.start) * 2);
                    unsigned short lastBaseIdx = static_cast<unsigned short>((laste + seg.start) * 2);


                    //*pShort++ = lastBaseIdx;
                    //*pShort++ = lastBaseIdx + 1;
                    //*pShort++ = baseIdx;
                    //*pShort++ = lastBaseIdx + 1;
                    //*pShort++ = baseIdx + 1;
                    //*pShort++ = baseIdx;
                    //_indexData->indexCount += 6;

                    _indices[index++] = lastBaseIdx;
                    _indices[index++] = lastBaseIdx + 1;
                    _indices[index++] = baseIdx;
                    _indices[index++] = lastBaseIdx + 1;
                    _indices[index++] = baseIdx + 1;
                    _indices[index++] = baseIdx;

                    if (e == seg.tail)
                        break; // last one

                    laste = e;

                }
            }

        }

        _indexBuffer->updateData(&_indices[0], sizeof(_indices[0]) * _indices.size());
        //_indexData->indexBuffer->unlock();
        _indexContentDirty = false;
    }

}
//-----------------------------------------------------------------------
void PUBillboardChain::init( const std::string &texFile )
{
    CC_SAFE_RELEASE_NULL(_programState);

    if (!texFile.empty())
    {
        auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
        if (tex)
        {
            _texture = tex;
            auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_TEXTURE_3D);
            _programState = new backend::ProgramState(program);
        }
    }
    
    if(!_programState)
    {
        auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_COLOR_3D);
        _programState = new backend::ProgramState(program);
    }

    auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();
    pipelineDescriptor.programState = _programState;
    auto layout = _programState->getVertexLayout();
    const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
    auto iter = attributeInfo.find("a_position");
    if(iter != attributeInfo.end())
    {
        layout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3, offsetof(VertexInfo, position), false);
    }
    iter = attributeInfo.find("a_texCoord");
    if(iter != attributeInfo.end())
    {
        layout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, offsetof(VertexInfo, uv), false);
    }
    iter = attributeInfo.find("a_color");
    if(iter != attributeInfo.end())
    {
        layout->setAttribute("a_color", iter->second.location, backend::VertexFormat::FLOAT4, offsetof(VertexInfo, color), false);
    }
    layout->setLayout(sizeof(VertexInfo));
    
    _locColor = _programState->getUniformLocation("u_color");
    _locPMatrix = _programState->getUniformLocation("u_PMatrix");
    _locTexture = _programState->getUniformLocation("u_texture");

    _meshCommand.setTransparent(true);
    _meshCommand.setSkipBatching(true);

    _stateBlock.setDepthTest(true);
    _stateBlock.setDepthWrite(false);
    _stateBlock.setCullFaceSide(backend::CullMode::BACK);
    _stateBlock.setCullFace(true);

    _meshCommand.setBeforeCallback(CC_CALLBACK_0(PUBillboardChain::onBeforeDraw, this));
    _meshCommand.setAfterCallback(CC_CALLBACK_0(PUBillboardChain::onAfterDraw, this));
}

void PUBillboardChain::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
{
    auto camera = Camera::getVisitingCamera();
    auto cameraMat = camera->getNodeToWorldTransform();

    if (!_chainSegmentList.empty())
    {
        updateVertexBuffer(cameraMat);
        updateIndexBuffer();

        _meshCommand.setVertexBuffer(_vertexBuffer);
        _meshCommand.setIndexBuffer(_indexBuffer, MeshCommand::IndexFormat::U_SHORT);
        _meshCommand.setIndexDrawInfo(0, _indices.size());

        if (!_vertices.empty() && !_indices.empty())
        {
            _meshCommand.init(0.0);
            _stateBlock.setBlendFunc(particleSystem->getBlendFunc());

            auto &projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
            _programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));

            if (_texture)
            {
                _programState->setTexture(_locTexture, 0, _texture->getBackendTexture());
            }

            auto uColor = Vec4(1, 1, 1, 1);
            _programState->setUniform(_locColor, &uColor, sizeof(uColor));

            renderer->addCommand(&_meshCommand);
        }
    }
}

void PUBillboardChain::setDepthTest( bool isDepthTest )
{
    _stateBlock.setDepthTest(isDepthTest);
}

void PUBillboardChain::setDepthWrite( bool isDepthWrite )
{
    _stateBlock.setDepthWrite(isDepthWrite);
}

void PUBillboardChain::setBlendFunc(const BlendFunc& blendFunc)
{
    _stateBlock.setBlendFunc(blendFunc);
}

void PUBillboardChain::onBeforeDraw()
{
    auto *renderer = Director::getInstance()->getRenderer();
    auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();
    _rendererDepthTestEnabled = renderer->getDepthTest();
    _rendererDepthCmpFunc = renderer->getDepthCompareFunction();
    _rendererCullMode = renderer->getCullMode();
    _rendererDepthWrite = renderer->getDepthWrite();
    _rendererWinding = renderer->getWinding();
    _stateBlock.bind(&pipelineDescriptor);
    renderer->setDepthTest(true);
}

void PUBillboardChain::onAfterDraw()
{
    auto *renderer = Director::getInstance()->getRenderer();
    renderer->setDepthTest(_rendererDepthTestEnabled);
    renderer->setDepthCompareFunction(_rendererDepthCmpFunc);
    renderer->setCullMode(_rendererCullMode);
    renderer->setDepthWrite(_rendererDepthWrite);
    renderer->setWinding(_rendererWinding);
}

//-----------------------------------------------------------------------
//void PUBillboardChain::_updateRenderQueue(RenderQueue* queue)
//{
//	updateIndexBuffer();
//
//	if (mIndexData->indexCount > 0)
//	{
//		if (mRenderQueuePrioritySet)
//			queue->addRenderable(this, mRenderQueueID, mRenderQueuePriority);
//		else if (mRenderQueueIDSet)
//			queue->addRenderable(this, mRenderQueueID);
//		else
//			queue->addRenderable(this);
//	}
//
//}
//-----------------------------------------------------------------------
//void PUBillboardChain::getRenderOperation(RenderOperation& op)
//{
//	op.indexData = mIndexData;
//	op.operationType = RenderOperation::OT_TRIANGLE_LIST;
//	op.srcRenderable = this;
//	op.useIndexes = true;
//	op.vertexData = mVertexData;
//}
//-----------------------------------------------------------------------
//bool PUBillboardChain::preRender(SceneManager* sm, RenderSystem* rsys)
//{
//	// Retrieve the current viewport from the scene manager.
//	// The viewport is only valid during a viewport update.
//	Viewport *currentViewport = sm->getCurrentViewport();
//	if( !currentViewport )
//		return false;
//
//	updateVertexBuffer(currentViewport->getCamera());
//	return true;
//}
//-----------------------------------------------------------------------
//void PUBillboardChain::getWorldTransforms(Matrix4* xform) const
//{
//	*xform = _getParentNodeFullTransform();
//}
//---------------------------------------------------------------------

NS_CC_END