Blame view

ios/cocos2d/extensions/Particle3D/PU/CCPURender.h 7.86 KB
520389e3   xiaoyu   接入cocos源码,编译未通过,继续修改
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
  /****************************************************************************
   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.
   ****************************************************************************/
  
  #pragma once
  
  #include <vector>
  
  #include "base/CCRef.h"
  #include "math/CCMath.h"
  #include "extensions/Particle3D/CCParticle3DRender.h"
  #include "renderer/CCRenderState.h"
  #include "renderer/backend/Types.h"
  #include "renderer/backend/Buffer.h"
  
  NS_CC_BEGIN
  
  // particle render for quad
  struct PUParticle3D;
  
  class CC_DLL PURender : public Particle3DRender
  {
  public:
  
      virtual void prepare(){};
      virtual void unPrepare(){};
      virtual void updateRender(PUParticle3D* particle, float deltaTime, bool firstParticle);
  
      const std::string& getRenderType() const {return _renderType;};
      void setRenderType(const std::string& observerType) {_renderType = observerType;};
  
      virtual PURender* clone() = 0;
      void copyAttributesTo(PURender* render);
  
  public:
  
      bool autoRotate; // Means that the objects to render automatically rotate if the node to which the particle system is attached, rotates.
  
  protected:
  
      std::string _renderType;
  };
  
  class CC_DLL PUParticle3DEntityRender : public PURender
  {
  public:
      void copyAttributesTo(PUParticle3DEntityRender *render);
      virtual void reset()override;
  CC_CONSTRUCTOR_ACCESS:
      PUParticle3DEntityRender();
      virtual ~PUParticle3DEntityRender();
  
  protected:
  
      bool initRender(const std::string &texFile);
  
      void onBeforeDraw();
  
      void onAfterDraw();
  
  protected:
  
      struct VertexInfo
      {
          Vec3 position;
          Vec2 uv;
          Vec4 color;
      };
      
      MeshCommand           _meshCommand;
  
      RenderState::StateBlock     _stateBlock;
      Texture2D*                  _texture        = nullptr;
      backend::ProgramState*      _programState   = nullptr;
      backend::Buffer*            _indexBuffer    = nullptr; //index buffer
      backend::Buffer*            _vertexBuffer   = nullptr; // vertex buffer
  
      std::vector<VertexInfo>     _vertices;
      std::vector<uint16_t> _indices;
  
      std::string _texFile;
  
      backend::UniformLocation    _locColor;
      backend::UniformLocation    _locTexture;
      backend::UniformLocation    _locPMatrix;
  
      //renderer state cache variables
      bool                        _rendererDepthTestEnabled   = true;
      backend::CompareFunction    _rendererDepthCmpFunc       = backend::CompareFunction::LESS;
      backend::CullMode           _rendererCullMode           = backend::CullMode::BACK;
      backend::Winding            _rendererWinding            = backend::Winding::COUNTER_CLOCK_WISE;
      bool                        _rendererDepthWrite         = false;
  };
  
  class CC_DLL PUParticle3DQuadRender : public PUParticle3DEntityRender
  {
  public:
  
      enum Type
      {
          POINT,
          ORIENTED_COMMON,
          ORIENTED_SELF,
          ORIENTED_SHAPE,
          PERPENDICULAR_COMMON,
          PERPENDICULAR_SELF,
      };
  
      enum Origin
      {
          TOP_LEFT,
          TOP_CENTER,
          TOP_RIGHT,
          CENTER_LEFT,
          CENTER,
          CENTER_RIGHT,
          BOTTOM_LEFT,
          BOTTOM_CENTER,
          BOTTOM_RIGHT
      };
  
      enum RotateType
      {
          TEXTURE_COORDS,
          VERTEX
      };
  
      static PUParticle3DQuadRender* create(const std::string& texFile = "");
      
      void setType(Type type);
      Type getType() const { return _type; }
      void setOrigin(Origin origin) { _origin = origin; }
      Origin getOrigin() const { return _origin; }
      void setRotateType(RotateType type) { _rotateType = type; }
      RotateType getRotateType() const { return _rotateType; }
      void setCommonDirection(const Vec3 &dir) { _commonDir = dir; }
      const Vec3& getCommonDirection() const { return _commonDir; }
      void setCommonUp(const Vec3 &up) { _commonUp = up; }
      const Vec3& getCommonUp() const { return _commonUp; }
  
      unsigned short getTextureCoordsRows() const;
      void setTextureCoordsRows(unsigned short textureCoordsRows);
      unsigned short getTextureCoordsColumns() const;
      void setTextureCoordsColumns(unsigned short textureCoordsColumns);
      unsigned int getNumTextureCoords();
  
      virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override;
  
      virtual PUParticle3DQuadRender* clone() override;
      void copyAttributesTo(PUParticle3DQuadRender *render);
      
  CC_CONSTRUCTOR_ACCESS:
      PUParticle3DQuadRender();
      virtual ~PUParticle3DQuadRender();
  
  protected:
  
      void getOriginOffset(int &offsetX, int &offsetY);
      void determineUVCoords(PUParticle3D *particle);
      void fillVertex(unsigned short index, const Vec3 &pos, const Vec4 &color, const Vec2 &uv);
      void fillTriangle(unsigned short index, unsigned short v0, unsigned short v1, unsigned short v2);
  
  protected:
  
      Type _type;
      Origin _origin;
      RotateType _rotateType;
      Vec3 _commonDir;
      Vec3 _commonUp;
  
      unsigned short _textureCoordsRows;
      unsigned short _textureCoordsColumns;
      float _textureCoordsRowStep;
      float _textureCoordsColStep;
  };
  
  // particle render for Sprite3D
  class CC_DLL PUParticle3DModelRender : public PURender
  {
  public:
      static PUParticle3DModelRender* create(const std::string& modelFile, const std::string &texFile = "");
  
      virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override;
  
      virtual PUParticle3DModelRender* clone() override;
      void copyAttributesTo(PUParticle3DModelRender *render);
  
      virtual void reset()override;
  CC_CONSTRUCTOR_ACCESS:
      PUParticle3DModelRender();
      virtual ~PUParticle3DModelRender();
  
  protected:
  
      std::vector<Sprite3D *> _spriteList;
      std::string _modelFile;
      std::string _texFile;
      Vec3 _spriteSize;
  };
  
  class CC_DLL PUParticle3DBoxRender : public PUParticle3DEntityRender
  {
  public:
  
      static PUParticle3DBoxRender* create(const std::string &texFile = "");
  
      virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override;
  
      virtual PUParticle3DBoxRender* clone() override;
  
  CC_CONSTRUCTOR_ACCESS:
      PUParticle3DBoxRender();
      virtual ~PUParticle3DBoxRender();
  
  protected:
  
      void reBuildIndices(unsigned short count);
  };
  
  class CC_DLL PUSphereRender : public PUParticle3DEntityRender
  {
  public:
  
      static PUSphereRender* create(const std::string &texFile = "");
  
      virtual void render(Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem) override;
  
      virtual PUSphereRender* clone() override;
      void copyAttributesTo(PUSphereRender *render);
  
  CC_CONSTRUCTOR_ACCESS:
      PUSphereRender();
      virtual ~PUSphereRender();
  
  protected:
  
      void buildBuffers(unsigned short count);
  
  protected:
  
      unsigned short _numberOfRings;
      unsigned short _numberOfSegments;
      std::vector<VertexInfo> _vertexTemplate;
  };
  
  NS_CC_END