CCPURender.h 7.1 KB

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