CCPURender.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_RENDER_H__
  23. #define __CC_PU_PARTICLE_3D_RENDER_H__
  24. #include <vector>
  25. #include "base/CCRef.h"
  26. #include "math/CCMath.h"
  27. #include "extensions/Particle3D/CCParticle3DRender.h"
  28. #include "renderer/CCRenderState.h"
  29. NS_CC_BEGIN
  30. // particle render for quad
  31. struct PUParticle3D;
  32. class CC_DLL PURender : public Particle3DRender
  33. {
  34. public:
  35. virtual void prepare(){};
  36. virtual void unPrepare(){};
  37. virtual void updateRender(PUParticle3D* particle, float deltaTime, bool firstParticle);
  38. const std::string& getRenderType(void) const {return _renderType;};
  39. void setRenderType(const std::string& observerType) {_renderType = observerType;};
  40. virtual PURender* clone() = 0;
  41. void copyAttributesTo(PURender* render);
  42. public:
  43. bool autoRotate; // Means that the objects to render automatically rotate if the node to which the particle system is attached, rotates.
  44. protected:
  45. std::string _renderType;
  46. };
  47. class CC_DLL PUParticle3DEntityRender : public PURender
  48. {
  49. public:
  50. void copyAttributesTo(PUParticle3DEntityRender *render);
  51. virtual void reset()override;
  52. CC_CONSTRUCTOR_ACCESS:
  53. PUParticle3DEntityRender();
  54. virtual ~PUParticle3DEntityRender();
  55. protected:
  56. bool initRender(const std::string &texFile);
  57. protected:
  58. struct VertexInfo
  59. {
  60. Vec3 position;
  61. Vec2 uv;
  62. Vec4 color;
  63. };
  64. MeshCommand* _meshCommand;
  65. RenderState::StateBlock* _stateBlock;
  66. Texture2D* _texture;
  67. GLProgramState* _glProgramState;
  68. IndexBuffer* _indexBuffer; //index buffer
  69. VertexBuffer* _vertexBuffer; // vertex buffer
  70. std::vector<VertexInfo> _vertices;
  71. std::vector<unsigned short> _indices;
  72. std::string _texFile;
  73. };
  74. class CC_DLL PUParticle3DQuadRender : public PUParticle3DEntityRender
  75. {
  76. public:
  77. enum Type
  78. {
  79. POINT,
  80. ORIENTED_COMMON,
  81. ORIENTED_SELF,
  82. ORIENTED_SHAPE,
  83. PERPENDICULAR_COMMON,
  84. PERPENDICULAR_SELF,
  85. };
  86. enum Origin
  87. {
  88. TOP_LEFT,
  89. TOP_CENTER,
  90. TOP_RIGHT,
  91. CENTER_LEFT,
  92. CENTER,
  93. CENTER_RIGHT,
  94. BOTTOM_LEFT,
  95. BOTTOM_CENTER,
  96. BOTTOM_RIGHT
  97. };
  98. enum RotateType
  99. {
  100. TEXTURE_COORDS,
  101. VERTEX
  102. };
  103. static PUParticle3DQuadRender* create(const std::string& texFile = "");
  104. void setType(Type type);
  105. Type getType() const { return _type; }
  106. void setOrigin(Origin origin) { _origin = origin; }
  107. Origin getOrigin() const { return _origin; }
  108. void setRotateType(RotateType type) { _rotateType = type; }
  109. RotateType getRotateType() const { return _rotateType; }
  110. void setCommonDirection(const Vec3 &dir) { _commonDir = dir; }
  111. const Vec3& getCommonDirection() const { return _commonDir; }
  112. void setCommonUp(const Vec3 &up) { _commonUp = up; }
  113. const Vec3& getCommonUp() const { return _commonUp; }
  114. unsigned short getTextureCoordsRows() const;
  115. void setTextureCoordsRows(unsigned short textureCoordsRows);
  116. unsigned short getTextureCoordsColumns() const;
  117. void setTextureCoordsColumns(unsigned short textureCoordsColumns);
  118. unsigned int getNumTextureCoords();
  119. virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override;
  120. virtual PUParticle3DQuadRender* clone() override;
  121. void copyAttributesTo(PUParticle3DQuadRender *render);
  122. CC_CONSTRUCTOR_ACCESS:
  123. PUParticle3DQuadRender();
  124. virtual ~PUParticle3DQuadRender();
  125. protected:
  126. void getOriginOffset(int &offsetX, int &offsetY);
  127. void determineUVCoords(PUParticle3D *particle);
  128. void fillVertex(unsigned short index, const Vec3 &pos, const Vec4 &color, const Vec2 &uv);
  129. void fillTriangle(unsigned short index, unsigned short v0, unsigned short v1, unsigned short v2);
  130. protected:
  131. Type _type;
  132. Origin _origin;
  133. RotateType _rotateType;
  134. Vec3 _commonDir;
  135. Vec3 _commonUp;
  136. unsigned short _textureCoordsRows;
  137. unsigned short _textureCoordsColumns;
  138. float _textureCoordsRowStep;
  139. float _textureCoordsColStep;
  140. };
  141. // particle render for Sprite3D
  142. class CC_DLL PUParticle3DModelRender : public PURender
  143. {
  144. public:
  145. static PUParticle3DModelRender* create(const std::string& modelFile, const std::string &texFile = "");
  146. virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override;
  147. virtual PUParticle3DModelRender* clone() override;
  148. void copyAttributesTo(PUParticle3DModelRender *render);
  149. virtual void reset()override;
  150. CC_CONSTRUCTOR_ACCESS:
  151. PUParticle3DModelRender();
  152. virtual ~PUParticle3DModelRender();
  153. protected:
  154. std::vector<Sprite3D *> _spriteList;
  155. std::string _modelFile;
  156. std::string _texFile;
  157. Vec3 _spriteSize;
  158. };
  159. class CC_DLL PUParticle3DBoxRender : public PUParticle3DEntityRender
  160. {
  161. public:
  162. static PUParticle3DBoxRender* create(const std::string &texFile = "");
  163. virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override;
  164. virtual PUParticle3DBoxRender* clone() override;
  165. CC_CONSTRUCTOR_ACCESS:
  166. PUParticle3DBoxRender();
  167. virtual ~PUParticle3DBoxRender();
  168. protected:
  169. void reBuildIndices(unsigned short count);
  170. };
  171. class CC_DLL PUSphereRender : public PUParticle3DEntityRender
  172. {
  173. public:
  174. static PUSphereRender* create(const std::string &texFile = "");
  175. virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override;
  176. virtual PUSphereRender* clone() override;
  177. void copyAttributesTo(PUSphereRender *render);
  178. CC_CONSTRUCTOR_ACCESS:
  179. PUSphereRender();
  180. virtual ~PUSphereRender();
  181. protected:
  182. void buildBuffers(unsigned short count);
  183. protected:
  184. unsigned short _numberOfRings;
  185. unsigned short _numberOfSegments;
  186. std::vector<VertexInfo> _vertexTemplate;
  187. };
  188. NS_CC_END
  189. #endif