1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsPrerequisites.h"
6#include "Image/BsColor.h"
7#include "Math/BsAABox.h"
8
9namespace bs
10{
11 /** @addtogroup Utility-Engine
12 * @{
13 */
14
15 /** Helper class for easily creating common 2D shapes. */
16 class BS_EXPORT ShapeMeshes2D
17 {
18 public:
19 /**
20 * Fills the mesh data with vertices representing a quad (2 triangles).
21 *
22 * @param[in] area Area in which to draw the quad.
23 * @param[out] meshData Mesh data that will be populated.
24 * @param[in] vertexOffset Offset in number of vertices from the start of the buffer to start writing at.
25 * @param[in] indexOffset Offset in number of indices from the start of the buffer to start writing at.
26 *
27 * @note
28 * Provided MeshData must have some specific elements at least:
29 * Vector2 VES_POSITION
30 * 32bit index buffer
31 * Enough space for 4 vertices and 6 indices
32 * @note
33 * Primitives are output in the form of a triangle list.
34 */
35 static void solidQuad(const Rect2& area, const SPtr<MeshData>& meshData, UINT32 vertexOffset, UINT32 indexOffset);
36
37 /**
38 * Fills the mesh data with vertices representing a per-pixel line.
39 *
40 * @param[in] a Start point of the line.
41 * @param[in] b End point of the line.
42 * @param[out] meshData Mesh data that will be populated.
43 * @param[in] vertexOffset Offset in number of vertices from the start of the buffer to start writing at.
44 * @param[in] indexOffset Offset in number of indices from the start of the buffer to start writing at.
45 *
46 * @note
47 * Provided MeshData must have some specific elements at least:
48 * Vector2 VES_POSITION
49 * 32bit index buffer
50 * Enough space for 2 vertices and 2 indices
51 * @note
52 * Primitives are output in the form of a line list.
53 */
54 static void pixelLine(const Vector2& a, const Vector2& b, const SPtr<MeshData>& meshData, UINT32 vertexOffset,
55 UINT32 indexOffset);
56
57 /**
58 * Fills the mesh data with vertices representing a line of specific width as a quad.
59 *
60 * @param[in] a Start point of the line.
61 * @param[in] b End point of the line.
62 * @param[in] width Width of the line.
63 * @param[in] border Optional border that will increase the width and the length at both end-points.
64 * Useful if you are using some kind of filtering for the line rendering, as the
65 * filtered pixels can belong to the border region.
66 * @param[in] color Color of the line.
67 * @param[out] meshData Mesh data that will be populated by this method.
68 * @param[in] vertexOffset Offset in number of vertices from the start of the buffer to start writing at.
69 * @param[in] indexOffset Offset in number of indices from the start of the buffer to start writing at.
70 *
71 * @note
72 * Provided MeshData must have some specific elements at least:
73 * Vector2 VES_POSITION
74 * UINT32 VES_COLOR
75 * 32bit index buffer
76 * Enough space for 4 vertices and 6 indices
77 * @note
78 * Primitives are output in the form of a triangle list.
79 */
80 static void quadLine(const Vector2& a, const Vector2& b, float width, float border, const Color& color,
81 const SPtr<MeshData>& meshData, UINT32 vertexOffset, UINT32 indexOffset);
82
83 /**
84 * Fills the mesh data with vertices representing per-pixel lines.
85 *
86 * @param[in] linePoints A list of start and end points for the lines. Must be a multiple of 2.
87 * @param[out] meshData Mesh data that will be populated.
88 * @param[in] vertexOffset Offset in number of vertices from the start of the buffer to start writing at.
89 * @param[in] indexOffset Offset in number of indices from the start of the buffer to start writing at.
90 *
91 * @note
92 * Provided MeshData must have some specific elements at least:
93 * Vector2 VES_POSITION
94 * 32bit index buffer
95 * Enough space for (numLines * 2) vertices and (numLines * 2) indices
96 * @note
97 * Primitives are output in the form of a line list.
98 */
99 static void pixelLineList(const Vector<Vector2>& linePoints, const SPtr<MeshData>& meshData, UINT32 vertexOffset,
100 UINT32 indexOffset);
101
102 /**
103 * Fills the mesh data with vertices representing a polyline of specific width as a set of quads.
104 *
105 * @param[in] linePoints A list of start and end points for the lines.
106 * @param[in] width Width of the line.
107 * @param[in] border Optional border that will increase the width and the length at both end-points.
108 * Useful if you are using some kind of filtering for the line rendering, as the
109 * filtered pixels can belong to the border region.
110 * @param[in] color Color of the line.
111 * @param[out] meshData Mesh data that will be populated by this method.
112 * @param[in] vertexOffset Offset in number of vertices from the start of the buffer to start writing at.
113 * @param[in] indexOffset Offset in number of indices from the start of the buffer to start writing at.
114 *
115 * @note
116 * Provided MeshData must have some specific elements at least:
117 * Vector2 VES_POSITION
118 * UINT32 VES_COLOR
119 * 32bit index buffer
120 * Enough space for (numLines * 2) + 2 vertices and numLines * 6 indices
121 * @note
122 * Primitives are output in the form of a triangle list.
123 */
124 static void quadLineList(const Vector<Vector2>& linePoints, float width, float border,
125 const Color& color, const SPtr<MeshData>& meshData, UINT32 vertexOffset, UINT32 indexOffset);
126
127 /**
128 * Fills the provided buffers with vertices representing a polyline of specific width as a set of quads
129 * (triangle list).
130 *
131 * @param[in] linePoints A list of start and end points for the lines.
132 * @param[in] numPoints Number of points in the @p linePoints buffer.
133 * @param[in] width Width of the line.
134 * @param[in] border Optional border that will increase the width and the length at both end-points.
135 * Useful if you are using some kind of filtering for the line rendering, as the
136 * filtered pixels can belong to the border region.
137 * @param[out] outVertices Pre-allocated buffer for the vertices, of size ((numLines * 2) + 2) * @p vertexStride
138 * if @p indexed is true, or (numLines * 6) * @p vertexStride if false.
139 * @param[in] vertexStride Distance between two vertices in the output buffer. Must be at least sizeof(Vector2).
140 * @param[in] indexed If true there will be ((numLines * 2) + 2) vertices generated, assuming an index
141 * buffer will be used for rendering. If false then (numLines * 6) vertices will be
142 * generated.
143 */
144 static void quadLineList(const Vector2* linePoints, UINT32 numPoints, float width, float border, UINT8* outVertices,
145 UINT32 vertexStride, bool indexed);
146
147 static const UINT32 NUM_VERTICES_AA_LINE;
148 static const UINT32 NUM_INDICES_AA_LINE;
149 protected:
150 /**
151 * Fills the provided buffers with vertices representing a per-pixel line.
152 *
153 * @param[in] a Start point of the line.
154 * @param[in] b End point of the line.
155 * @param[out] outVertices Output buffer that will store the vertex position data.
156 * @param[in] vertexOffset Offset in number of vertices from the start of the buffer to start writing at.
157 * @param[in] vertexStride Size of a single vertex, in bytes. (Same for both position and color buffer)
158 * @param[out] outIndices Output buffer that will store the index data. Indices are 32bit.
159 * @param[in] indexOffset Offset in number of indices from the start of the buffer to start writing at.
160 */
161 static void pixelLine(const Vector2& a, const Vector2& b, UINT8* outVertices,
162 UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset);
163
164 /**
165 * Fills the provided buffers with position data and indices representing an inner
166 * area of a polygon (basically a normal non-antialiased polygon).
167 *
168 * @param[in] points Points defining the polygon. First point is assumed to be the start and end point.
169 * @param[out] outVertices Output buffer that will store the vertex position data.
170 * @param[in] vertexOffset Offset in number of vertices from the start of the buffer to start writing at.
171 * @param[in] vertexStride Size of a single vertex, in bytes. (Same for both position and color buffer)
172 * @param[out] outIndices Output buffer that will store the index data. Indices are 32bit.
173 * @param[in] indexOffset Offset in number of indices from the start of the buffer to start writing at.
174 */
175 static void pixelSolidPolygon(const Vector<Vector2>& points, UINT8* outVertices,
176 UINT32 vertexOffset, UINT32 vertexStride, UINT32* outIndices, UINT32 indexOffset);
177 };
178
179 /** @} */
180}