Skip to content

Commit 8175eb2

Browse files
authored
DrawNodeV2 0.94 (currently DrawNodeEx) (#2008)
1 parent 5ffd6ce commit 8175eb2

File tree

4 files changed

+134
-151
lines changed

4 files changed

+134
-151
lines changed

extensions/DrawNodeEx/src/DrawNodeEx/DrawNodeEx.cpp

Lines changed: 76 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
NS_AX_EXT_BEGIN
4848

4949

50-
5150
/** Is a polygon convex?
5251
* @param verts A pointer to point coordinates.
5352
* @param count The number of verts measured in points.
@@ -149,13 +148,13 @@ DrawNodeEx::~DrawNodeEx()
149148
{
150149
AX_SAFE_FREE(_bufferTriangle);
151150

152-
#if defined(DRAWNODE_DRAW_LINE_POINT)
151+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
153152
AX_SAFE_FREE(_bufferPoint);
154153
AX_SAFE_FREE(_bufferLine);
155154
#endif
156155

157156
freeShaderInternal(_customCommandTriangle);
158-
#if defined(DRAWNODE_DRAW_LINE_POINT)
157+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
159158
freeShaderInternal(_customCommandPoint);
160159
freeShaderInternal(_customCommandLine);
161160
#endif
@@ -191,7 +190,7 @@ void DrawNodeEx::ensureCapacityTriangle(int count)
191190
}
192191
}
193192

194-
#if defined(DRAWNODE_DRAW_LINE_POINT)
193+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
195194
void DrawNodeEx::ensureCapacityPoint(int count)
196195
{
197196
AXASSERT(count >= 0, "capacity must be >= 0");
@@ -230,7 +229,7 @@ bool DrawNodeEx::init()
230229
ensureCapacityTriangle(512);
231230
_dirtyTriangle = true;
232231

233-
#if defined(DRAWNODE_DRAW_LINE_POINT)
232+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
234233
ensureCapacityPoint(64);
235234
ensureCapacityLine(256);
236235
_dirtyLine = true;
@@ -244,7 +243,7 @@ void DrawNodeEx::updateShader()
244243
{
245244
updateShaderInternal(_customCommandTriangle, backend::ProgramType::POSITION_COLOR_LENGTH_TEXTURE,
246245
CustomCommand::DrawType::ARRAY, CustomCommand::PrimitiveType::TRIANGLE);
247-
#if defined(DRAWNODE_DRAW_LINE_POINT)
246+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
248247
updateShaderInternal(_customCommandPoint, backend::ProgramType::POSITION_COLOR_TEXTURE_AS_POINTSIZE,
249248
CustomCommand::DrawType::ARRAY, CustomCommand::PrimitiveType::POINT);
250249

@@ -324,7 +323,7 @@ void DrawNodeEx::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
324323
_customCommandTriangle.init(_globalZOrder);
325324
renderer->addCommand(&_customCommandTriangle);
326325
}
327-
#if defined(DRAWNODE_DRAW_LINE_POINT)
326+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
328327
if (_bufferCountPoint)
329328
{
330329
updateBlendState(_customCommandPoint);
@@ -345,16 +344,16 @@ void DrawNodeEx::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
345344

346345
void DrawNodeEx::drawPoint(const Vec2& position, const float pointSize, const Color4B& color)
347346
{
348-
#if defined(DRAWNODE_DRAW_LINE_POINT)
347+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
349348
if (_drawOrder == true)
350349
{
351350
#endif
352351
drawSolidCircle(position, pointSize, 0.f, 12, 1.f, 1.f, color, 0.f, color);
353-
#if defined(DRAWNODE_DRAW_LINE_POINT)
352+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
354353
return;
355354
}
356355
#endif
357-
#if defined(DRAWNODE_DRAW_LINE_POINT)
356+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
358357
ensureCapacityPoint(1);
359358

360359
V2F_C4B_T2F* point = _bufferPoint + _bufferCountPoint;
@@ -377,19 +376,19 @@ void DrawNodeEx::drawPoints(const Vec2* position,
377376
const float pointSize,
378377
const Color4B& color)
379378
{
380-
#if defined(DRAWNODE_DRAW_LINE_POINT)
379+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
381380
if (_drawOrder == true)
382381
{
383382
#endif
384383
for (unsigned int i = 0; i < numberOfPoints; i++)
385384
{
386385
drawSolidCircle(position[i], pointSize, 0.f, 12, 1.f, 1.f, color, 0.f, color);
387386
}
388-
#if defined(DRAWNODE_DRAW_LINE_POINT)
387+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
389388
return;
390389
}
391390
#endif
392-
#if defined(DRAWNODE_DRAW_LINE_POINT)
391+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
393392
ensureCapacityPoint(numberOfPoints);
394393

395394
V2F_C4B_T2F* point = _bufferPoint + _bufferCountPoint;
@@ -415,16 +414,16 @@ void DrawNodeEx::drawLine(const Vec2& origin, const Vec2& destination, const Col
415414
}
416415
else
417416
{
418-
#if defined(DRAWNODE_DRAW_LINE_POINT)
417+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
419418
if (_drawOrder == true)
420419
{
421420
#endif
422421
drawSegment(origin, destination, thickness / 3, color);
423-
#if defined(DRAWNODE_DRAW_LINE_POINT)
422+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
424423
return;
425424
}
426425
#endif
427-
#if defined(DRAWNODE_DRAW_LINE_POINT)
426+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
428427
Vec2 line[2] = { origin, destination };
429428
Vec2* _vertices = transform(line, 2);
430429

@@ -476,16 +475,16 @@ void DrawNodeEx::drawPoly(const Vec2* poli,
476475
}
477476
else
478477
{
479-
#if defined(DRAWNODE_DRAW_LINE_POINT)
478+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
480479
if (_drawOrder == true)
481480
{
482481
#endif
483482
_drawPolygon(poli, numberOfPoints, Color4B::TRANSPARENT, thickness / 3, color, closePolygon);
484-
#if defined(DRAWNODE_DRAW_LINE_POINT)
483+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
485484
return;
486485
}
487486
#endif
488-
#if defined(DRAWNODE_DRAW_LINE_POINT)
487+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
489488
Vec2* _vertices = transform(poli, numberOfPoints);
490489

491490
unsigned int vertex_count;
@@ -762,7 +761,7 @@ void DrawNodeEx::drawRect(const Vec2& p1,
762761
}
763762
}
764763

765-
void DrawNodeEx::drawSegment(const Vec2& from, const Vec2& to, float radius, const Color4B& color, DrawNodeEx::EndType endType)
764+
void DrawNodeEx::drawSegment(const Vec2& from, const Vec2& to, float radius, const Color4B& color, DrawNodeEx::EndType etStart, DrawNodeEx::EndType etEnd)
766765
{
767766
Vec2 line[2] = { from, to };
768767
Vec2* _vertices = transform(line, 2);
@@ -790,9 +789,9 @@ void DrawNodeEx::drawSegment(const Vec2& from, const Vec2& to, float radius, con
790789

791790
//
792791
Color4B col = color;
793-
switch (endType)
792+
// End
793+
switch (etEnd)
794794
{
795-
796795
case DrawNodeEx::EndType::Butt:
797796
break;
798797

@@ -841,8 +840,8 @@ void DrawNodeEx::drawSegment(const Vec2& from, const Vec2& to, float radius, con
841840
{v5, col, Tex2F(n)},
842841
};
843842

844-
// End
845-
switch (endType)
843+
// Start
844+
switch (etStart)
846845
{
847846
case DrawNodeEx::EndType::Butt:
848847
break;
@@ -938,11 +937,18 @@ void DrawNodeEx::drawPie(const Vec2& center,
938937
DrawMode drawMode,
939938
float thickness)
940939
{
941-
// not a real line!
940+
#define DEGREES 360
941+
bool _circle = false;
942+
943+
// Not a real line!
942944
if (startAngle == endAngle)
943945
return;
944946

945-
#define DEGREES 360
947+
// Its a circle?
948+
if (MAX((startAngle - endAngle), (endAngle-startAngle)) == DEGREES)
949+
{
950+
_circle = true;
951+
}
946952

947953
const float coef = 2.0f * (float)M_PI / DEGREES;
948954
Vec2* vertices = _abuf.get<Vec2>(DEGREES + 2);
@@ -953,9 +959,7 @@ void DrawNodeEx::drawPie(const Vec2& center,
953959

954960
if (startAngle > endAngle)
955961
{
956-
int tmp = endAngle;
957-
endAngle = startAngle;
958-
startAngle = tmp;
962+
std::swap(endAngle, startAngle);
959963
}
960964

961965
for (int i = 0; i <= DEGREES; i++)
@@ -976,17 +980,28 @@ void DrawNodeEx::drawPie(const Vec2& center,
976980
{
977981
case DrawMode::Fill:
978982
vertices[n++] = center;
983+
if (_circle)
984+
{
985+
this->drawSolidCircle(center, radius, 0, 36, scaleX, scaleY, fillColor, thickness, borderColor);
986+
break;
987+
}
979988
_drawPolygon(vertices, n, fillColor, thickness, borderColor, true);
980989
break;
981990
case DrawMode::Outline:
982991
vertices[n++] = center;
992+
if (_circle)
993+
{
994+
this->drawCircle(center, radius, 0, 36, false, scaleX, scaleY, borderColor, thickness);
995+
break;
996+
}
983997
_drawPolygon(vertices, n, Color4B::TRANSPARENT, thickness, borderColor, true);
984998
break;
985999
case DrawMode::Line:
1000+
vertices[n++] = center;
9861001
_drawPolygon(vertices, n, Color4B::TRANSPARENT, thickness, borderColor, false);
9871002
break;
9881003
case DrawMode::Semi:
989-
_drawPolygon(vertices, n, fillColor, thickness, borderColor, true);
1004+
_drawPolygon(vertices, n-1, fillColor, thickness, borderColor, true);
9901005
break;
9911006

9921007
default:
@@ -1091,7 +1106,7 @@ void DrawNodeEx::clear()
10911106
{
10921107
_bufferCountTriangle = 0;
10931108
_dirtyTriangle = true;
1094-
#if defined(DRAWNODE_DRAW_LINE_POINT)
1109+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
10951110
_bufferCountLine = 0;
10961111
_dirtyLine = true;
10971112
_bufferCountPoint = 0;
@@ -1137,16 +1152,13 @@ void DrawNodeEx::_drawPolygon(const Vec2* verts,
11371152
unsigned int count,
11381153
const Color4B& fillColor,
11391154
float borderWidth,
1140-
const Color4B& borderColo,
1155+
const Color4B& borderColor,
11411156
bool closedPolygon)
11421157
{
11431158
AXASSERT(count >= 0, "invalid count value");
11441159

11451160
Vec2* _vertices = transform(verts, count);
1146-
1147-
bool outline = (borderColo.a > 0.0f && borderWidth > 0.0f);
1148-
1149-
Color4B borderColor = borderColo;
1161+
bool outline = (borderColor.a > 0.0f && borderWidth > 0.0f);
11501162

11511163
auto triangle_count = outline ? (3 * count - 2) : (count - 2);
11521164
auto vertex_count = 3 * triangle_count;
@@ -1225,39 +1237,7 @@ void DrawNodeEx::_drawPolygon(const Vec2* verts,
12251237
extrude[i] = { offset, n2 };
12261238
}
12271239

1228-
int i = 0;
1229-
int j = (i + 1) % count;
1230-
Vec2 v0 = _vertices[i];
1231-
Vec2 v1 = _vertices[j];
1232-
1233-
Vec2 n0 = extrude[i].n;
1234-
1235-
Vec2 offset0 = extrude[i].offset;
1236-
Vec2 offset1 = extrude[j].offset;
1237-
1238-
Vec2 inner0 = v0 - offset0 * borderWidth;
1239-
Vec2 inner1 = v1 - offset1 * borderWidth;
1240-
Vec2 outer0 = v0 + offset0 * borderWidth;
1241-
Vec2 outer1 = v1 + offset1 * borderWidth;
1242-
borderColor = borderColo;
1243-
//if (i >= (count - 1) && !closedPolygon) // /-2 ??
1244-
//{
1245-
// borderColor = Color4B::TRANSPARENT;
1246-
//}
1247-
1248-
V2F_C4B_T2F_Triangle tmp1 = { {inner0, borderColor, Tex2F(-n0)},
1249-
{inner1, borderColor, Tex2F(-n0)},
1250-
{outer1, borderColor, Tex2F(n0)} };
1251-
*cursor++ = tmp1;
1252-
1253-
V2F_C4B_T2F_Triangle tmp2 = { {inner0, borderColor, Tex2F(-n0)},
1254-
{outer0, borderColor, Tex2F(n0)},
1255-
{outer1, borderColor, Tex2F(n0)} };
1256-
*cursor++ = tmp2;
1257-
1258-
1259-
1260-
for (unsigned int i = 1; i < (count - 1); i++)
1240+
for (unsigned int i = 0; i < (count - 2); i++)
12611241
{
12621242
int j = (i + 1) % count;
12631243
Vec2 v0 = _vertices[i];
@@ -1272,11 +1252,6 @@ void DrawNodeEx::_drawPolygon(const Vec2* verts,
12721252
Vec2 inner1 = v1 - offset1 * borderWidth;
12731253
Vec2 outer0 = v0 + offset0 * borderWidth;
12741254
Vec2 outer1 = v1 + offset1 * borderWidth;
1275-
borderColor = borderColo;
1276-
//if (i >= (count - 1) && !closedPolygon) // /-2 ??
1277-
//{
1278-
// borderColor = Color4B::TRANSPARENT;
1279-
//}
12801255

12811256
V2F_C4B_T2F_Triangle tmp1 = { {inner0, borderColor, Tex2F(-n0)},
12821257
{inner1, borderColor, Tex2F(-n0)},
@@ -1289,35 +1264,35 @@ void DrawNodeEx::_drawPolygon(const Vec2* verts,
12891264
*cursor++ = tmp2;
12901265
}
12911266

1292-
i = count - 1;
1293-
j = (i + 1) % count;
1294-
v0 = _vertices[i];
1295-
v1 = _vertices[j];
1267+
// drawPie stuff
1268+
for (unsigned int i = count - 2; i <= (count - 1); i++)
1269+
{
1270+
int j = (i + 1) % count;
1271+
Vec2 v0 = _vertices[i];
1272+
Vec2 v1 = _vertices[j];
1273+
1274+
Vec2 n0 = extrude[i].n;
12961275

1297-
n0 = extrude[i].n;
1276+
Vec2 offset0 = extrude[i].offset;
1277+
Vec2 offset1 = extrude[j].offset;
12981278

1299-
offset0 = extrude[i].offset;
1300-
offset1 = extrude[j].offset;
1279+
Vec2 inner0 = v0 - offset0 * borderWidth;
1280+
Vec2 inner1 = v1 - offset1 * borderWidth;
1281+
Vec2 outer0 = v0 + offset0 * borderWidth;
1282+
Vec2 outer1 = v1 + offset1 * borderWidth;
13011283

1302-
inner0 = v0 - offset0 * borderWidth;
1303-
inner1 = v1 - offset1 * borderWidth;
1304-
outer0 = v0 + offset0 * borderWidth;
1305-
outer1 = v1 + offset1 * borderWidth;
1306-
borderColor = borderColo;
1307-
//if (i >= (count - 1) && !closedPolygon) // /-2 ??
1308-
//{
1309-
// borderColor = Color4B::TRANSPARENT;
1310-
//}
1284+
Color4B borderColorTemp = (!closedPolygon) ? Color4B::TRANSPARENT : borderColor;
13111285

1312-
tmp1 = { {inner0, borderColor, Tex2F(-n0)},
1313-
{inner1, borderColor, Tex2F(-n0)},
1314-
{outer1, borderColor, Tex2F(n0)} };
1315-
*cursor++ = tmp1;
1286+
V2F_C4B_T2F_Triangle tmp1 = { {inner0, borderColorTemp, Tex2F(-n0)},
1287+
{inner1, borderColorTemp, Tex2F(-n0)},
1288+
{outer1, borderColorTemp, Tex2F(n0)} };
1289+
*cursor++ = tmp1;
13161290

1317-
tmp2 = { {inner0, borderColor, Tex2F(-n0)},
1318-
{outer0, borderColor, Tex2F(n0)},
1319-
{outer1, borderColor, Tex2F(n0)} };
1320-
*cursor++ = tmp2;
1291+
V2F_C4B_T2F_Triangle tmp2 = { {inner0, borderColorTemp, Tex2F(-n0)},
1292+
{outer0, borderColorTemp, Tex2F(n0)},
1293+
{outer1, borderColorTemp, Tex2F(n0)} };
1294+
*cursor++ = tmp2;
1295+
}
13211296

13221297
free(extrude);
13231298
}
@@ -1351,16 +1326,16 @@ void DrawNodeEx::_drawPoly(const Vec2* poli,
13511326
}
13521327
else
13531328
{
1354-
#if defined(DRAWNODE_DRAW_LINE_POINT)
1329+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
13551330
if (_drawOrder == true)
13561331
{
13571332
#endif
13581333
_drawPolygon(poli, numberOfPoints, Color4B::TRANSPARENT, thickness / 3, color, false);
1359-
#if defined(DRAWNODE_DRAW_LINE_POINT)
1334+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
13601335
return;
13611336
}
13621337
#endif
1363-
#if defined(DRAWNODE_DRAW_LINE_POINT)
1338+
#if defined(AX_ENABLE_DRAWNODE_DRAW_LINE_POINT)
13641339
Vec2* _vertices = transform(poli, numberOfPoints);
13651340

13661341
unsigned int vertex_count;

0 commit comments

Comments
 (0)