CCPUBillboardChain.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CC_PU_PARTICLE_3D_BILLBOARD_CHAIN_H__
  23. #define __CC_PU_PARTICLE_3D_BILLBOARD_CHAIN_H__
  24. #include <vector>
  25. #include "renderer/CCRenderState.h"
  26. #include "base/CCRef.h"
  27. #include "math/CCMath.h"
  28. NS_CC_BEGIN
  29. class MeshCommand;
  30. class GLProgramState;
  31. class IndexBuffer;
  32. class VertexBuffer;
  33. class Texture2D;
  34. class ParticleSystem3D;
  35. class Renderer;
  36. class PUBillboardChain
  37. {
  38. public:
  39. /** Contains the data of an element of the BillboardChain.
  40. */
  41. class Element
  42. {
  43. public:
  44. Element();
  45. Element(const Vec3& position,
  46. float width,
  47. float texCoord,
  48. const Vec4& colour,
  49. const Quaternion& orientation);
  50. Vec3 position;
  51. float width;
  52. /// U or V texture coord depending on options
  53. float texCoord;
  54. Vec4 color;
  55. //Only used when mFaceCamera == false
  56. Quaternion orientation;
  57. };
  58. typedef std::vector<Element> ElementList;
  59. /** Constructor
  60. @param name The name to give this object
  61. @param maxElements The maximum number of elements per chain
  62. @param numberOfChains The number of separate chain segments contained in this object
  63. @param useTextureCoords If true, use texture coordinates from the chain elements
  64. @param useVertexColours If true, use vertex colours from the chain elements
  65. @param dynamic If true, buffers are created with the intention of being updated
  66. */
  67. PUBillboardChain(const std::string& name, const std::string& texFile = "", size_t maxElements = 20, size_t numberOfChains = 1,
  68. bool useTextureCoords = true, bool useColours = true, bool dynamic = true);
  69. /// destructor
  70. virtual ~PUBillboardChain();
  71. /** Set the maximum number of chain elements per chain
  72. */
  73. virtual void setMaxChainElements(size_t maxElements);
  74. /** Get the maximum number of chain elements per chain
  75. */
  76. virtual size_t getMaxChainElements(void) const { return _maxElementsPerChain; }
  77. /** Set the number of chain segments (this class can render multiple chains
  78. at once using the same material).
  79. */
  80. virtual void setNumberOfChains(size_t numChains);
  81. /** Get the number of chain segments (this class can render multiple chains
  82. at once using the same material).
  83. */
  84. virtual size_t getNumberOfChains(void) const { return _chainCount; }
  85. /** Sets whether texture coordinate information should be included in the
  86. final buffers generated.
  87. @note You must use either texture coordinates or vertex colour since the
  88. vertices have no normals and without one of these there is no source of
  89. colour for the vertices.
  90. */
  91. virtual void setUseTextureCoords(bool use);
  92. /** Gets whether texture coordinate information should be included in the
  93. final buffers generated.
  94. */
  95. virtual bool getUseTextureCoords(void) const { return _useTexCoords; }
  96. /** The direction in which texture coordinates from elements of the
  97. chain are used.
  98. */
  99. enum TexCoordDirection
  100. {
  101. /// Tex coord in elements is treated as the 'u' texture coordinate
  102. TCD_U,
  103. /// Tex coord in elements is treated as the 'v' texture coordinate
  104. TCD_V
  105. };
  106. /** Sets the direction in which texture coords specified on each element
  107. are deemed to run along the length of the chain.
  108. @param dir The direction, default is TCD_U.
  109. */
  110. virtual void setTextureCoordDirection(TexCoordDirection dir);
  111. /** Gets the direction in which texture coords specified on each element
  112. are deemed to run.
  113. */
  114. virtual TexCoordDirection getTextureCoordDirection(void) { return _texCoordDir; }
  115. /** Set the range of the texture coordinates generated across the width of
  116. the chain elements.
  117. @param start Start coordinate, default 0.0
  118. @param end End coordinate, default 1.0
  119. */
  120. virtual void setOtherTextureCoordRange(float start, float end);
  121. /** Get the range of the texture coordinates generated across the width of
  122. the chain elements.
  123. */
  124. virtual const float* getOtherTextureCoordRange(void) const { return _otherTexCoordRange; }
  125. /** Sets whether vertex colour information should be included in the
  126. final buffers generated.
  127. @note You must use either texture coordinates or vertex colour since the
  128. vertices have no normals and without one of these there is no source of
  129. colour for the vertices.
  130. */
  131. virtual void setUseVertexColours(bool use);
  132. /** Gets whether vertex colour information should be included in the
  133. final buffers generated.
  134. */
  135. virtual bool getUseVertexColours(void) const { return _useVertexColour; }
  136. /** Sets whether or not the buffers created for this object are suitable
  137. for dynamic alteration.
  138. */
  139. virtual void setDynamic(bool dyn);
  140. /** Gets whether or not the buffers created for this object are suitable
  141. for dynamic alteration.
  142. */
  143. virtual bool getDynamic(void) const { return _dynamic; }
  144. /** Add an element to the 'head' of a chain.
  145. @remarks
  146. If this causes the number of elements to exceed the maximum elements
  147. per chain, the last element in the chain (the 'tail') will be removed
  148. to allow the additional element to be added.
  149. @param chainIndex The index of the chain
  150. @param billboardChainElement The details to add
  151. */
  152. virtual void addChainElement(size_t chainIndex,
  153. const Element& billboardChainElement);
  154. /** Remove an element from the 'tail' of a chain.
  155. @param chainIndex The index of the chain
  156. */
  157. virtual void removeChainElement(size_t chainIndex);
  158. /** Update the details of an existing chain element.
  159. @param chainIndex The index of the chain
  160. @param elementIndex The element index within the chain, measured from
  161. the 'head' of the chain
  162. @param billboardChainElement The details to set
  163. */
  164. virtual void updateChainElement(size_t chainIndex, size_t elementIndex,
  165. const Element& billboardChainElement);
  166. /** Get the detail of a chain element.
  167. @param chainIndex The index of the chain
  168. @param elementIndex The element index within the chain, measured from
  169. the 'head' of the chain
  170. */
  171. virtual const Element& getChainElement(size_t chainIndex, size_t elementIndex) const;
  172. /** Returns the number of chain elements. */
  173. virtual size_t getNumChainElements(size_t chainIndex) const;
  174. /** Remove all elements of a given chain (but leave the chain intact). */
  175. virtual void clearChain(size_t chainIndex);
  176. /** Remove all elements from all chains (but leave the chains themselves intact). */
  177. virtual void clearAllChains(void);
  178. /** Sets whether the billboard should always be facing the camera or a custom direction
  179. set by each point element.
  180. @remarks
  181. Billboards facing the camera are useful for smoke trails, light beams, etc by
  182. simulating a cylinder. However, because of this property, wide trails can cause
  183. several artefacts unless the head is properly covered.
  184. Therefore, non-camera-facing billboards are much more convenient for leaving big
  185. trails of movement from thin objects, for example a sword swing as seen in many
  186. fighting games.
  187. @param faceCamera True to be always facing the camera (Default value: True)
  188. @param normalVector Only used when faceCamera == false. Must be a non-zero vector.
  189. This vector is the "point of reference" for each point orientation. For example,
  190. if normalVector is Vector3::UNIT_Z, and the point's orientation is an identity
  191. matrix, the segment corresponding to that point will be facing towards UNIT_Z
  192. This vector is internally normalized.
  193. */
  194. void setFaceCamera( bool faceCamera, const Vec3& normalVector=Vec3::UNIT_X );
  195. void setDepthTest(bool isDepthTest);
  196. void setDepthWrite(bool isDepthWrite);
  197. void setBlendFunc(const BlendFunc& blendFunc);
  198. void render(Renderer* renderer, const Mat4& transform, ParticleSystem3D* particleSystem);
  199. // Overridden members follow
  200. //void _updateRenderQueue(RenderQueue*);
  201. //void getRenderOperation(RenderOperation&);
  202. //virtual bool preRender(SceneManager* sm, RenderSystem* rsys);
  203. //void getWorldTransforms(Matrix4*) const;
  204. /// @copydoc MovableObject::visitRenderables
  205. GLuint getTextureName();
  206. protected:
  207. /// Setup the STL collections
  208. virtual void setupChainContainers(void);
  209. /// Setup vertex declaration
  210. virtual void setupVertexDeclaration(void);
  211. // Setup buffers
  212. virtual void setupBuffers(void);
  213. /// Update the contents of the vertex buffer
  214. virtual void updateVertexBuffer(const Mat4& camMat);
  215. /// Update the contents of the index buffer
  216. virtual void updateIndexBuffer(void);
  217. void init(const std::string& texFile);
  218. protected:
  219. /// Maximum length of each chain
  220. size_t _maxElementsPerChain;
  221. /// Number of chains
  222. size_t _chainCount;
  223. /// Use texture coords?
  224. bool _useTexCoords;
  225. /// Use vertex colour?
  226. bool _useVertexColour;
  227. /// Dynamic use?
  228. bool _dynamic;
  229. /// Is the vertex declaration dirty?
  230. bool _vertexDeclDirty;
  231. /// Do the buffers need recreating?
  232. bool _buffersNeedRecreating;
  233. /// Do the bounds need redefining?
  234. mutable bool _boundsDirty;
  235. /// Is the index buffer dirty?
  236. bool _indexContentDirty;
  237. /// Is the vertex buffer dirty?
  238. bool _vertexContentDirty;
  239. /// Texture coord direction
  240. TexCoordDirection _texCoordDir;
  241. /// Other texture coord range
  242. float _otherTexCoordRange[2];
  243. /// When true, the billboards always face the camera
  244. bool _faceCamera;
  245. /// Used when mFaceCamera == false; determines the billboard's "normal". i.e.
  246. /// when the orientation is identity, the billboard is perpendicular to this
  247. /// vector
  248. Vec3 _normalBase;
  249. /// The list holding the chain elements
  250. ElementList _chainElementList;
  251. /** Simple struct defining a chain segment by referencing a subset of
  252. the preallocated buffer (which will be mMaxElementsPerChain * mChainCount
  253. long), by it's chain index, and a head and tail value which describe
  254. the current chain. The buffer subset wraps at mMaxElementsPerChain
  255. so that head and tail can move freely. head and tail are inclusive,
  256. when the chain is empty head and tail are filled with high-values.
  257. */
  258. struct ChainSegment
  259. {
  260. /// The start of this chains subset of the buffer
  261. size_t start;
  262. /// The 'head' of the chain, relative to start
  263. size_t head;
  264. /// The 'tail' of the chain, relative to start
  265. size_t tail;
  266. };
  267. typedef std::vector<ChainSegment> ChainSegmentList;
  268. ChainSegmentList _chainSegmentList;
  269. /// Chain segment has no elements
  270. static const size_t SEGMENT_EMPTY;
  271. struct VertexInfo
  272. {
  273. Vec3 position;
  274. Vec2 uv;
  275. Vec4 color;
  276. };
  277. MeshCommand* _meshCommand;
  278. RenderState::StateBlock* _stateBlock;
  279. Texture2D* _texture;
  280. GLProgramState* _glProgramState;
  281. IndexBuffer* _indexBuffer; //index buffer
  282. VertexBuffer* _vertexBuffer; // vertex buffer
  283. std::vector<VertexInfo> _vertices;
  284. std::vector<unsigned short> _indices;
  285. std::string _texFile;
  286. };
  287. NS_CC_END
  288. #endif