CCDrawNode3D.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // CCDrawNode3D.h
  3. // Redream2
  4. //
  5. // Created by 徐俊杰 on 2021/9/6.
  6. //
  7. #ifndef CCDrawNode3D_h
  8. #define CCDrawNode3D_h
  9. #include "2d/CCNode.h"
  10. #include "base/ccTypes.h"
  11. #include "renderer/CCCustomCommand.h"
  12. #include "math/CCMath.h"
  13. NS_CC_BEGIN
  14. static const int DEFAULT_LINE_WIDTH = 2;
  15. class PointArray;
  16. /**
  17. * @addtogroup _2d
  18. * @{
  19. */
  20. /** @class DrawNode3D
  21. * @brief Node that draws dots, segments and polygons.
  22. * Faster than the "drawing primitives" since they draws everything in one single batch.
  23. * @since v2.1
  24. */
  25. class CC_DLL DrawNode3D : public Node
  26. {
  27. public:
  28. /** creates and initialize a DrawNode3D node.
  29. *
  30. * @return Return an autorelease object.
  31. */
  32. static DrawNode3D* create(GLfloat defaultLineWidth = DEFAULT_LINE_WIDTH, bool drawOnce = false);
  33. /** Draw a point.
  34. *
  35. * @param point A Vec3 used to point.
  36. * @param pointSize The point size.
  37. * @param color The point color.
  38. * @js NA
  39. */
  40. void drawPoint(const Vec3& point, const float pointSize, const Color4F &color);
  41. /** Draw a group point.
  42. *
  43. * @param position A Vec3 pointer.
  44. * @param numberOfPoints The number of points.
  45. * @param color The point color.
  46. * @js NA
  47. */
  48. void drawPoints(const Vec3 *position, unsigned int numberOfPoints, const Color4F &color);
  49. /** Draw a group point.
  50. *
  51. * @param position A Vec3 pointer.
  52. * @param numberOfPoints The number of points.
  53. * @param pointSize The point size.
  54. * @param color The point color.
  55. * @js NA
  56. */
  57. void drawPoints(const Vec3 *position, unsigned int numberOfPoints, const float pointSize, const Color4F &color);
  58. /** Draw an line from origin to destination with color.
  59. *
  60. * @param origin The line origin.
  61. * @param destination The line destination.
  62. * @param color The line color.
  63. * @js NA
  64. */
  65. void drawLine(const Vec3 &origin, const Vec3 &destination, const Color4F &color, const Mat4& transform);
  66. /** Draws a polygon given a pointer to point coordinates and the number of vertices measured in points.
  67. * The polygon can be closed or open.
  68. *
  69. * @param poli A pointer to point coordinates.
  70. * @param numberOfPoints The number of vertices measured in points.
  71. * @param closePolygon The polygon can be closed or open.
  72. * @param color The polygon color.
  73. */
  74. void drawPoly(const Vec3 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color, const Mat4& transform);
  75. void drawWireArc( const Vec3& center, const Vec3& normal, const Vec3& from, float angle, float radius, const Color4F &color, const Mat4& transform);
  76. void drawWireDisk (const Vec3 &center, const Vec3 &normal, float radius, const Color4F &color, const Mat4& transform);
  77. void drawWireSphere(const Vec3 &center, float radius, const Color4F &color, const Mat4& transform);
  78. void drawWireCube (const Vec3& center, const Vec3& siz, bool depthTest, const Color4F &color, const Mat4& transform);
  79. /** draw a dot at a position, with a given radius and color.
  80. *
  81. * @param pos The dot center.
  82. * @param radius The dot radius.
  83. * @param color The dot color.
  84. */
  85. void drawDot(const Vec3 &pos, float radius, const Color4F &color);
  86. /** Draws a rectangle with 4 points.
  87. *
  88. * @param p1 The rectangle vertex point.
  89. * @param p2 The rectangle vertex point.
  90. * @param p3 The rectangle vertex point.
  91. * @param p4 The rectangle vertex point.
  92. * @param color The rectangle color.
  93. */
  94. void drawRect(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3, const Vec3& p4, const Color4F &color);
  95. /** Draws a solid polygon given a pointer to CGPoint coordinates, the number of vertices measured in points, and a color.
  96. *
  97. * @param poli A solid polygon given a pointer to CGPoint coordinates.
  98. * @param numberOfPoints The number of vertices measured in points.
  99. * @param color The solid polygon color.
  100. * @js NA
  101. */
  102. void drawSolidPoly(const Vec3 *poli, unsigned int numberOfPoints, const Color4F &color);
  103. /** draw a polygon with a fill color and line color
  104. * @code
  105. * When this function bound into js or lua,the parameter will be changed
  106. * In js: var drawPolygon(var Arrayofpoints, var fillColor, var width, var borderColor)
  107. * In lua:local drawPolygon(local pointTable,local tableCount,local fillColor,local width,local borderColor)
  108. * @endcode
  109. * @param verts A pointer to point coordinates.
  110. * @param count The number of verts measured in points.
  111. * @param fillColor The color will fill in polygon.
  112. * @param borderWidth The border of line width.
  113. * @param borderColor The border of line color.
  114. * @js NA
  115. */
  116. void drawPolygon(const Vec3 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor);
  117. /** draw a triangle with color.
  118. *
  119. * @param p1 The triangle vertex point.
  120. * @param p2 The triangle vertex point.
  121. * @param p3 The triangle vertex point.
  122. * @param color The triangle color.
  123. * @js NA
  124. */
  125. void drawTriangle(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3, const Color4F &color);
  126. /** Clear the geometry in the node's buffer. */
  127. void clear();
  128. /** Get the color mixed mode.
  129. * @lua NA
  130. */
  131. const BlendFunc& getBlendFunc() const;
  132. /** Set the color mixed mode.
  133. * @code
  134. * When this function bound into js or lua,the parameter will be changed
  135. * In js: var setBlendFunc(var src, var dst)
  136. * @endcode
  137. * @lua NA
  138. */
  139. void setBlendFunc(const BlendFunc &blendFunc);
  140. /**
  141. * @js NA
  142. */
  143. virtual void onDraw(const Mat4 &transform, uint32_t flags);
  144. /**
  145. * @js NA
  146. */
  147. virtual void onDrawGLLine(const Mat4 &transform, uint32_t flags);
  148. /**
  149. * @js NA
  150. */
  151. virtual void onDrawGLPoint(const Mat4 &transform, uint32_t flags);
  152. // Overrides
  153. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  154. void setLineWidth(GLfloat lineWidth);
  155. // Get CocosStudio guide lines width.
  156. GLfloat getLineWidth();
  157. CC_CONSTRUCTOR_ACCESS:
  158. DrawNode3D(GLfloat lineWidth = DEFAULT_LINE_WIDTH, bool drawOnce = false);
  159. virtual ~DrawNode3D();
  160. virtual bool init() override;
  161. protected:
  162. void ensureCapacity(int count);
  163. void ensureCapacityGLPoint(int count);
  164. void ensureCapacityGLLine(int count);
  165. GLuint _vao;
  166. GLuint _vbo;
  167. GLuint _vaoGLPoint;
  168. GLuint _vboGLPoint;
  169. GLuint _vaoGLLine;
  170. GLuint _vboGLLine;
  171. int _bufferCapacity;
  172. GLsizei _bufferCount;
  173. V3F_C4B_T2F *_buffer;
  174. int _bufferCapacityGLPoint;
  175. GLsizei _bufferCountGLPoint;
  176. V3F_C4B_T2F *_bufferGLPoint;
  177. Color4F _pointColor;
  178. int _pointSize;
  179. int _bufferCapacityGLLine;
  180. GLsizei _bufferCountGLLine;
  181. V3F_C4B_T2F *_bufferGLLine;
  182. BlendFunc _blendFunc;
  183. CustomCommand _customCommand;
  184. CustomCommand _customCommandGLPoint;
  185. CustomCommand _customCommandGLLine;
  186. bool _dirty;
  187. bool _dirtyGLPoint;
  188. bool _dirtyGLLine;
  189. bool _drawOnce;
  190. GLfloat _lineWidth;
  191. GLfloat _defaultLineWidth;
  192. private:
  193. CC_DISALLOW_COPY_AND_ASSIGN(DrawNode3D);
  194. };
  195. /** @} */
  196. NS_CC_END
  197. #endif /* CCDrawNode3D_h */