// // CCDrawNode3D.h // Redream2 // // Created by 徐俊杰 on 2021/9/6. // #ifndef CCDrawNode3D_h #define CCDrawNode3D_h #include "2d/CCNode.h" #include "base/ccTypes.h" #include "renderer/CCCustomCommand.h" #include "math/CCMath.h" NS_CC_BEGIN static const int DEFAULT_LINE_WIDTH = 2; class PointArray; /** * @addtogroup _2d * @{ */ /** @class DrawNode3D * @brief Node that draws dots, segments and polygons. * Faster than the "drawing primitives" since they draws everything in one single batch. * @since v2.1 */ class CC_DLL DrawNode3D : public Node { public: /** creates and initialize a DrawNode3D node. * * @return Return an autorelease object. */ static DrawNode3D* create(GLfloat defaultLineWidth = DEFAULT_LINE_WIDTH, bool drawOnce = false); /** Draw a point. * * @param point A Vec3 used to point. * @param pointSize The point size. * @param color The point color. * @js NA */ void drawPoint(const Vec3& point, const float pointSize, const Color4F &color); /** Draw a group point. * * @param position A Vec3 pointer. * @param numberOfPoints The number of points. * @param color The point color. * @js NA */ void drawPoints(const Vec3 *position, unsigned int numberOfPoints, const Color4F &color); /** Draw a group point. * * @param position A Vec3 pointer. * @param numberOfPoints The number of points. * @param pointSize The point size. * @param color The point color. * @js NA */ void drawPoints(const Vec3 *position, unsigned int numberOfPoints, const float pointSize, const Color4F &color); /** Draw an line from origin to destination with color. * * @param origin The line origin. * @param destination The line destination. * @param color The line color. * @js NA */ void drawLine(const Vec3 &origin, const Vec3 &destination, const Color4F &color, const Mat4& transform); /** Draws a polygon given a pointer to point coordinates and the number of vertices measured in points. * The polygon can be closed or open. * * @param poli A pointer to point coordinates. * @param numberOfPoints The number of vertices measured in points. * @param closePolygon The polygon can be closed or open. * @param color The polygon color. */ void drawPoly(const Vec3 *poli, unsigned int numberOfPoints, bool closePolygon, const Color4F &color, const Mat4& transform); void drawWireArc( const Vec3& center, const Vec3& normal, const Vec3& from, float angle, float radius, const Color4F &color, const Mat4& transform); void drawWireDisk (const Vec3 ¢er, const Vec3 &normal, float radius, const Color4F &color, const Mat4& transform); void drawWireSphere(const Vec3 ¢er, float radius, const Color4F &color, const Mat4& transform); void drawWireCube (const Vec3& center, const Vec3& siz, bool depthTest, const Color4F &color, const Mat4& transform); /** draw a dot at a position, with a given radius and color. * * @param pos The dot center. * @param radius The dot radius. * @param color The dot color. */ void drawDot(const Vec3 &pos, float radius, const Color4F &color); /** Draws a rectangle with 4 points. * * @param p1 The rectangle vertex point. * @param p2 The rectangle vertex point. * @param p3 The rectangle vertex point. * @param p4 The rectangle vertex point. * @param color The rectangle color. */ void drawRect(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3, const Vec3& p4, const Color4F &color); /** Draws a solid polygon given a pointer to CGPoint coordinates, the number of vertices measured in points, and a color. * * @param poli A solid polygon given a pointer to CGPoint coordinates. * @param numberOfPoints The number of vertices measured in points. * @param color The solid polygon color. * @js NA */ void drawSolidPoly(const Vec3 *poli, unsigned int numberOfPoints, const Color4F &color); /** draw a polygon with a fill color and line color * @code * When this function bound into js or lua,the parameter will be changed * In js: var drawPolygon(var Arrayofpoints, var fillColor, var width, var borderColor) * In lua:local drawPolygon(local pointTable,local tableCount,local fillColor,local width,local borderColor) * @endcode * @param verts A pointer to point coordinates. * @param count The number of verts measured in points. * @param fillColor The color will fill in polygon. * @param borderWidth The border of line width. * @param borderColor The border of line color. * @js NA */ void drawPolygon(const Vec3 *verts, int count, const Color4F &fillColor, float borderWidth, const Color4F &borderColor); /** draw a triangle with color. * * @param p1 The triangle vertex point. * @param p2 The triangle vertex point. * @param p3 The triangle vertex point. * @param color The triangle color. * @js NA */ void drawTriangle(const Vec3 &p1, const Vec3 &p2, const Vec3 &p3, const Color4F &color); /** Clear the geometry in the node's buffer. */ void clear(); /** Get the color mixed mode. * @lua NA */ const BlendFunc& getBlendFunc() const; /** Set the color mixed mode. * @code * When this function bound into js or lua,the parameter will be changed * In js: var setBlendFunc(var src, var dst) * @endcode * @lua NA */ void setBlendFunc(const BlendFunc &blendFunc); /** * @js NA */ virtual void onDraw(const Mat4 &transform, uint32_t flags); /** * @js NA */ virtual void onDrawGLLine(const Mat4 &transform, uint32_t flags); /** * @js NA */ virtual void onDrawGLPoint(const Mat4 &transform, uint32_t flags); // Overrides virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override; void setLineWidth(GLfloat lineWidth); // Get CocosStudio guide lines width. GLfloat getLineWidth(); CC_CONSTRUCTOR_ACCESS: DrawNode3D(GLfloat lineWidth = DEFAULT_LINE_WIDTH, bool drawOnce = false); virtual ~DrawNode3D(); virtual bool init() override; protected: void ensureCapacity(int count); void ensureCapacityGLPoint(int count); void ensureCapacityGLLine(int count); GLuint _vao; GLuint _vbo; GLuint _vaoGLPoint; GLuint _vboGLPoint; GLuint _vaoGLLine; GLuint _vboGLLine; int _bufferCapacity; GLsizei _bufferCount; V3F_C4B_T2F *_buffer; int _bufferCapacityGLPoint; GLsizei _bufferCountGLPoint; V3F_C4B_T2F *_bufferGLPoint; Color4F _pointColor; int _pointSize; int _bufferCapacityGLLine; GLsizei _bufferCountGLLine; V3F_C4B_T2F *_bufferGLLine; BlendFunc _blendFunc; CustomCommand _customCommand; CustomCommand _customCommandGLPoint; CustomCommand _customCommandGLLine; bool _dirty; bool _dirtyGLPoint; bool _dirtyGLLine; bool _drawOnce; GLfloat _lineWidth; GLfloat _defaultLineWidth; private: CC_DISALLOW_COPY_AND_ASSIGN(DrawNode3D); }; /** @} */ NS_CC_END #endif /* CCDrawNode3D_h */