MyGUI 3.5.3
MyGUI_PolygonalSkin.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
10#include "MyGUI_RenderItem.h"
12#include "MyGUI_RenderManager.h"
14
15namespace MyGUI
16{
17
18 PolygonalSkin::PolygonalSkin()
19 {
20 mVertexFormat = RenderManager::getInstance().getVertexFormat();
21 }
22
23 inline float len(float x, float y)
24 {
25 return std::sqrt(x * x + y * y);
26 }
27
28 void PolygonalSkin::setPoints(const std::vector<FloatPoint>& _points)
29 {
30 mLinePoints.clear();
31 mLinePoints.reserve(_points.size());
32 mLineLength = 0;
33 for (const auto& point : _points)
34 {
35 if (!mLinePoints.empty())
36 {
37 const auto& previous = mLinePoints.back();
38 if (previous == point)
39 continue;
40 mLineLength += len(point.left - previous.left, point.top - previous.top);
41 }
42 mLinePoints.push_back(point);
43 }
44
45 const size_t segments = mLinePoints.empty() ? 0 : mLinePoints.size() - 1;
46 // Each segment emits two triangles, plus at most two triangles for its join.
47 size_t count = segments * 12;
48#ifndef MYGUI_NO_POLYGONAL_SKIN_CROPPING
49 // Clipping a triangle against four sides adds at most four vertices:
50 // seven vertices triangulate into at most five triangles.
51 count *= 5;
52#endif
53 if (count > mVertexCount)
54 {
55 mVertexCount = count;
56 if (nullptr != mRenderItem)
57 mRenderItem->reallockDrawItem(this, mVertexCount);
58 }
59 _updateView();
60 }
61
62 void PolygonalSkin::setWidth(float _width)
63 {
64 if (mLineWidth == _width)
65 return;
66 mLineWidth = _width;
67 _updateView();
68 }
69
70 void PolygonalSkin::setStroke(size_t _value)
71 {
72 if (mLineStroke == _value)
73 return;
74 mLineStroke = _value;
75 _updateView();
76 }
77
78 void PolygonalSkin::setVisible(bool _visible)
79 {
80 if (mVisible == _visible)
81 return;
82
83 mVisible = _visible;
84 _correctView();
85 }
86
87 void PolygonalSkin::setAlpha(float _alpha)
88 {
89 uint32 alpha = ((uint8)(_alpha * 255) << 24);
90 if ((mCurrentColour & 0xFF000000) == alpha)
91 return;
92 mCurrentColour = (mCurrentColour & 0x00FFFFFF) | (alpha & 0xFF000000);
93
94 if (nullptr != mNode)
95 mNode->outOfDate(mRenderItem);
96 }
97
98 void PolygonalSkin::_correctView()
99 {
100 mGeometryOutdated = true;
101
102 if (nullptr != mNode)
103 mNode->outOfDate(mRenderItem);
104 }
105
106 void PolygonalSkin::_setAlign(const IntSize& _oldsize)
107 {
108 auto [coord, resize, move] = CoordConverter::applyAlign(mAlign, mCoord, mCroppedParent->getSize(), _oldsize);
109 mCoord = coord;
110 if (resize)
111 mIsMargin = true;
112 mCurrentCoord = mCoord;
113 _updateView();
114 }
115
116 void PolygonalSkin::_updateView()
117 {
118 bool margin = _checkMargin();
119
120 mEmptyView = ((0 >= _getViewWidth()) || (0 >= _getViewHeight()));
121
122 mGeometryOutdated = true;
123
124 mCurrentCoord.left = mCoord.left + mMargin.left;
125 mCurrentCoord.top = mCoord.top + mMargin.top;
126
127 // viewport changed
128 if (margin)
129 {
130 if (_checkOutsideUnrotated())
131 {
132 mIsMargin = margin;
133
134 if (nullptr != mNode)
135 mNode->outOfDate(mRenderItem);
136 return;
137 }
138 }
139
140 // we are clipped or were clipped
141 if (mIsMargin || margin)
142 {
143 mCurrentCoord.width = _getViewWidth();
144 mCurrentCoord.height = _getViewHeight();
145 }
146
147 mIsMargin = margin;
148
149 if (nullptr != mNode)
150 mNode->outOfDate(mRenderItem);
151 }
152
153 void PolygonalSkin::createDrawItem(ITexture* _texture, ILayerNode* _node)
154 {
155 MYGUI_ASSERT(!mRenderItem, "mRenderItem must be nullptr");
156
157 mNode = _node;
158 mRenderItem = mNode->addToRenderItem(_texture, true);
159 mRenderItem->addDrawItem(this, mVertexCount);
160 }
161
162 void PolygonalSkin::destroyDrawItem()
163 {
164 MYGUI_ASSERT(mRenderItem, "mRenderItem must be not nullptr");
165
166 mNode = nullptr;
167 mRenderItem->removeDrawItem(this);
168 mRenderItem = nullptr;
169 }
170
171 void PolygonalSkin::doRender()
172 {
173 if (!mVisible || mEmptyView)
174 return;
175
176 Vertex* verticies = mRenderItem->getCurrentVertexBuffer();
177
178 float vertex_z = mNode->getNodeDepth();
179
180 if (mGeometryOutdated || mRenderItem->getCurrentUpdate())
181 {
182 _rebuildGeometry();
183 }
184
185 size_t size = mResultVerticiesPos.size();
186
187 for (size_t i = 0; i < size; ++i)
188 {
189 verticies[i].set(
190 mResultVerticiesPos[i].left,
191 mResultVerticiesPos[i].top,
192 vertex_z,
193 mResultVerticiesUV[i].left,
194 mResultVerticiesUV[i].top,
195 mCurrentColour);
196 }
197
198 mRenderItem->setLastVertexCount(size);
199 }
200
201 void PolygonalSkin::_setColour(const Colour& _value)
202 {
203 uint32 colour = texture_utility::toNativeColour(_value, mVertexFormat);
204 mCurrentColour = (colour & 0x00FFFFFF) | (mCurrentColour & 0xFF000000);
205
206 if (nullptr != mNode)
207 mNode->outOfDate(mRenderItem);
208 }
209
210 void PolygonalSkin::setStateData(IStateInfo* _data)
211 {
212 _setUVSet(_data->castType<SubSkinStateInfo>()->getRect());
213 }
214
215 void PolygonalSkin::_setUVSet(const FloatRect& _rect)
216 {
217 mCurrentTexture = _rect;
218
219 _correctView();
220 }
221
222 void PolygonalSkin::_rebuildGeometry()
223 {
224 if (!mRenderItem || !mRenderItem->getRenderTarget())
225 return;
226
227 mGeometryOutdated = false;
228 mResultVerticiesPos.clear();
229 mResultVerticiesUV.clear();
230 if (mLinePoints.size() < 2)
231 return;
232
233 // using mCurrentCoord as rectangle where we draw polygons
234
235 // base texture coordinates
236 FloatPoint baseVerticiesUV[4] = {
237 FloatPoint(mCurrentTexture.left, mCurrentTexture.top),
238 FloatPoint(mCurrentTexture.right, mCurrentTexture.top),
239 FloatPoint(mCurrentTexture.right, mCurrentTexture.bottom),
240 FloatPoint(mCurrentTexture.left, mCurrentTexture.bottom)};
241
242 // UV vectors
243 FloatPoint vectorU = baseVerticiesUV[1] - baseVerticiesUV[0];
244 //FloatPoint vectorV = baseVerticiesUV[3] - baseVerticiesUV[0];
245
246 // add first two verticies
247 FloatPoint normal = _getPerpendicular(mLinePoints[0], mLinePoints[1]);
248
249 FloatPoint points[2] = {mLinePoints[0] + normal, mLinePoints[0] - normal};
250 FloatPoint pointsUV[2] = {baseVerticiesUV[0], baseVerticiesUV[3]};
251
252 bool draw = true;
253 size_t stroke = 0;
254
255 // add other verticies
256 float currentLength = 0.0f;
257 for (size_t i = 1; i < mLinePoints.size(); ++i)
258 {
259 if (mLineStroke != 0)
260 {
261 stroke++;
262 if (stroke == mLineStroke)
263 {
264 stroke = 0;
265 draw = !draw;
266 }
267 }
268
269 currentLength +=
270 len(mLinePoints[i - 1].left - mLinePoints[i].left, mLinePoints[i - 1].top - mLinePoints[i].top);
271
272 // getting normal between previous and next point
273 if (i != mLinePoints.size() - 1)
274 normal = _getMiddleLine(mLinePoints[i - 1], mLinePoints[i + 1], mLinePoints[i]);
275 else
276 normal = _getPerpendicular(mLinePoints[i - 1], mLinePoints[i]);
277
278 bool edge = false;
279 bool sharp = false;
280 if (normal == FloatPoint())
281 {
282 edge = true;
283 normal = _getPerpendicular(mLinePoints[i - 1], mLinePoints[i]);
284 }
285 else if (len(normal.left, normal.top) > mLineWidth * 1.5f)
286 {
287 sharp = true;
288 normal = _getPerpendicular(mLinePoints[i - 1], mLinePoints[i]);
289 }
290
291 // check orientation
292 FloatPoint lineDir = mLinePoints[i] - mLinePoints[i - 1];
293 if (lineDir.left * normal.top - lineDir.top * normal.left < 0)
294 {
295 normal.left = -normal.left;
296 normal.top = -normal.top;
297 }
298
299 FloatPoint UVoffset(currentLength / mLineLength * vectorU.left, currentLength / mLineLength * vectorU.top);
300
301 if (draw)
302 {
303 mResultVerticiesPos.push_back(points[0]);
304 mResultVerticiesPos.push_back(points[1]);
305 mResultVerticiesPos.push_back(mLinePoints[i] + normal);
306 mResultVerticiesUV.push_back(pointsUV[0]);
307 mResultVerticiesUV.push_back(pointsUV[1]);
308 mResultVerticiesUV.push_back(baseVerticiesUV[0] + UVoffset);
309
310 mResultVerticiesPos.push_back(points[1]);
311 mResultVerticiesPos.push_back(mLinePoints[i] - normal);
312 mResultVerticiesPos.push_back(mLinePoints[i] + normal);
313 mResultVerticiesUV.push_back(pointsUV[1]);
314 mResultVerticiesUV.push_back(baseVerticiesUV[3] + UVoffset);
315 mResultVerticiesUV.push_back(baseVerticiesUV[0] + UVoffset);
316 }
317
318 points[edge ? 1 : 0] = mLinePoints[i] + normal;
319 points[edge ? 0 : 1] = mLinePoints[i] - normal;
320 pointsUV[0] = baseVerticiesUV[0] + UVoffset;
321 pointsUV[1] = baseVerticiesUV[3] + UVoffset;
322
323 if (sharp)
324 {
325 normal = _getMiddleLine(mLinePoints[i - 1], mLinePoints[i + 1], mLinePoints[i]);
326
327 float length = len(normal.left, normal.top);
328 float sharpness = length / mLineWidth;
329 normal.left *= 2 * mLineWidth / length / (sharpness - 0.5f);
330 normal.top *= 2 * mLineWidth / length / (sharpness - 0.5f);
331
332 // check orientation
333 lineDir = mLinePoints[i] - mLinePoints[i - 1];
334 if (lineDir.left * normal.top - lineDir.top * normal.left < 0)
335 {
336 normal.left = -normal.left;
337 normal.top = -normal.top;
338 }
339 FloatPoint lineDir1 = mLinePoints[i] - mLinePoints[i - 1];
340 FloatPoint lineDir2 = mLinePoints[i + 1] - mLinePoints[i];
341 if (lineDir1.left * lineDir2.top - lineDir1.top * lineDir2.left > 0)
342 {
343 normal.left = -normal.left;
344 normal.top = -normal.top;
345 }
346
347 // check orientation
348 FloatPoint normal2 = _getPerpendicular(mLinePoints[i], mLinePoints[i + 1]);
349 lineDir = mLinePoints[i - 1] - mLinePoints[i];
350 if (lineDir.left * normal2.top - lineDir.top * normal2.left < 0)
351 {
352 normal2.left = -normal2.left;
353 normal2.top = -normal2.top;
354 }
355
356 FloatPoint UVcenter(
357 (baseVerticiesUV[0].left + baseVerticiesUV[3].left) / 2,
358 (baseVerticiesUV[0].top + baseVerticiesUV[3].top) / 2);
359
360 if (draw)
361 {
362 mResultVerticiesPos.push_back(points[0]);
363 mResultVerticiesPos.push_back(mLinePoints[i] + normal);
364 mResultVerticiesPos.push_back(mLinePoints[i]);
365 mResultVerticiesUV.push_back(pointsUV[0]);
366 mResultVerticiesUV.push_back(baseVerticiesUV[0] + UVoffset);
367 mResultVerticiesUV.push_back(UVcenter + UVoffset);
368
369 mResultVerticiesPos.push_back(mLinePoints[i] + normal);
370 mResultVerticiesPos.push_back(mLinePoints[i] + normal2);
371 mResultVerticiesPos.push_back(mLinePoints[i]);
372 mResultVerticiesUV.push_back(baseVerticiesUV[0] + UVoffset);
373 mResultVerticiesUV.push_back(baseVerticiesUV[0] + UVoffset);
374 mResultVerticiesUV.push_back(UVcenter + UVoffset);
375 }
376
377 points[0] = mLinePoints[i] + normal2;
378 points[1] = mLinePoints[i] - normal2;
379 pointsUV[0] = baseVerticiesUV[0] + UVoffset;
380 pointsUV[1] = baseVerticiesUV[3] + UVoffset;
381 }
382 }
383
384
385#ifndef MYGUI_NO_POLYGONAL_SKIN_CROPPING
386 // crop triangles
387 IntCoord cropRectangle(mCurrentCoord.left, mCurrentCoord.top, mCurrentCoord.width, mCurrentCoord.height);
388
389 VectorFloatPoint newResultVerticiesPos;
390 VectorFloatPoint newResultVerticiesUV;
391 newResultVerticiesPos.reserve(mResultVerticiesPos.size());
392 newResultVerticiesUV.reserve(mResultVerticiesPos.size());
393 for (size_t i = 0; i < mResultVerticiesPos.size(); i += 3)
394 {
395 VectorFloatPoint croppedTriangle = geometry_utility::cropPolygon(&mResultVerticiesPos[i], 3, cropRectangle);
396 const FloatPoint v0 = mResultVerticiesUV[i + 2] - mResultVerticiesUV[i];
397 const FloatPoint v1 = mResultVerticiesUV[i + 1] - mResultVerticiesUV[i];
398 for (size_t j = 1; j + 1 < croppedTriangle.size(); ++j)
399 {
400 for (size_t index : {size_t(0), j, j + 1})
401 {
402 const FloatPoint& vertex = croppedTriangle[index];
403 newResultVerticiesPos.push_back(vertex);
405 vertex,
406 mResultVerticiesPos[i],
407 mResultVerticiesPos[i + 1],
408 mResultVerticiesPos[i + 2]);
409 newResultVerticiesUV.push_back(
410 geometry_utility::getUVFromPositionInsideRect(position, v0, v1, mResultVerticiesUV[i]));
411 }
412 }
413 }
414 std::swap(mResultVerticiesPos, newResultVerticiesPos);
415 std::swap(mResultVerticiesUV, newResultVerticiesUV);
416#endif
417
418
420 mResultVerticiesPos,
421 mRenderItem->getRenderTarget()->getInfo(),
422 mCroppedParent->getAbsolutePosition());
423 }
424
425 FloatPoint PolygonalSkin::_getPerpendicular(const FloatPoint& _point1, const FloatPoint& _point2) const
426 {
427 // dy, -dx
428 FloatPoint result(_point1.top - _point2.top, -(_point1.left - _point2.left));
429 // normalise
430 float length = len(result.top, result.left);
431 result.left /= length;
432 result.top /= length;
433 result.left *= mLineWidth / 2;
434 result.top *= mLineWidth / 2;
435 return result;
436 }
437
438 FloatPoint PolygonalSkin::_getMiddleLine(
439 const FloatPoint& _point1,
440 const FloatPoint& _point2,
441 const FloatPoint& _point3) const
442 {
443 // bisectrix
444 FloatPoint line1 = _point3 - _point1;
445 FloatPoint line2 = _point3 - _point2;
446 float length = len(line1.top, line1.left);
447 line1.left /= length;
448 line1.top /= length;
449 length = len(line2.top, line2.left);
450 line2.left /= length;
451 line2.top /= length;
452 FloatPoint result = line1 + line2;
453 // normalise
454 length = len(result.top, result.left);
455 if (length < 1e-6f)
456 {
457 return _getPerpendicular(_point1, _point2);
458 }
459 result.left /= length;
460 result.top /= length;
461
462 float cos = result.left * line1.left + result.top * line1.top;
463 float angle = std::acos(std::clamp(cos, -1.0f, 1.0f));
464
465 // too sharp angle
466 if (std::fabs(angle) < 1e-6f)
467 return {};
468
469 float width = mLineWidth / 2 / std::sin(angle);
470 result.left *= width;
471 result.top *= width;
472
473 MYGUI_SUPPRESS_CLANG("-Wnrvo")
474 return result;
475 MYGUI_UNSUPPRESS_CLANG()
476 }
477
478} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
FloatPoint getUVFromPositionInsideRect(const FloatPoint &_point, const FloatPoint &_v0, const FloatPoint &_v1, const FloatPoint &_baseUV)
VectorFloatPoint cropPolygon(const FloatPoint *_baseVerticiesPos, size_t _size, const IntCoord &_cropRectangle)
FloatPoint getPositionInsideRect(const FloatPoint &_point, const FloatPoint &_corner0, const FloatPoint &_corner1, const FloatPoint &_corner2)
void toRenderTargetInPlace(FloatPoint *_points, size_t _count, const RenderTargetInfo &_info, IntPoint _origin={})
uint32 toNativeColour(const Colour &_colour, VertexColourType _format)
uint32_t uint32
Definition MyGUI_Types.h:47
uint8_t uint8
Definition MyGUI_Types.h:45
types::TRect< float > FloatRect
Definition MyGUI_Types.h:33
types::TPoint< float > FloatPoint
Definition MyGUI_Types.h:27
std::vector< FloatPoint > VectorFloatPoint
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29
float len(float x, float y)