CCPUBillboardChain.h 12 KB

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