Code: Select all
draw2DPolygon()
Code: Select all
draw2DPolygon()
Code: Select all
struct CircleSettings
{
vector2di center; // in screen coordinates
f32 radius; // in pixels
video::SColor color;
u32 numVertices = 21; // including center
};
void makeCircle(irr::core::array<irr::video::S3DVertex>& vertices, irr::core::array<irr::u16>& indices, const CircleSettings& settings)
{
const f64 stepSize = 360.0 / (f64)(settings.numVertices-1); // degree angles between vertex points on circle
indices.set_used(settings.numVertices+1); // one more as first and last vertex in circle is identical
for ( u32 i=0; i<settings.numVertices; ++i)
indices[i] = i;
indices[settings.numVertices] = 1;
const vector2df centerf((f32)settings.center.X, (f32)settings.center.Y);
vertices.set_used(settings.numVertices);
vertices[0] = video::S3DVertex(centerf.X, centerf.Y, 0.f, 0.f, 1.f, 0.f, settings.color, 0.5f, 0.5f);
for ( u32 i=0; i < settings.numVertices-1; ++i )
{
vector2df offset(0.f, settings.radius;);
offset.rotateBy(i*stepSize);
vertices[i+1] = video::S3DVertex(centerf.X+offset.X, centerf.Y+offset.Y, 0.f, 0.f, 1.f, 0.f, settings.color, 0.5f, 0.5f);
}
}
// create the circle
irr::core::array<irr::video::S3DVertex> verticesCircle;
irr::core::array<irr::u16> indicesCircle;
CircleSettings circle;
// circle.center = some value;
// circle.radius = same value;
circle.color = video::SColor(255, 135, 206, 235); // any value
makeCircle(verticesCircle, indicesCircle, circle);
// draw it
driver->draw2DVertexPrimitiveList(verticesCircle.pointer(), verticesCircle.size(),
indicesCircle.pointer(), indicesCircle.size()-2, video::EVT_STANDARD, scene::EPT_TRIANGLE_FAN,
video::EIT_16BIT);
Code: Select all
//! based on draw2DPolygon, however unlike that, this fills also with the given color the internal area of the polygon
void GUISkin::drawColored2DPolygon(const v2s32 center, u32 radius,
u32 vertex_count, // count of vertices, not counting a center one
u32 border_thickness,
const video::SColor* colors)
{
if (!colors)
colors = Colors;
if (border_thickness >= radius)
return;
// This code in the loop is necessary for drawing border
for (u32 i = 0; i < border_thickness; i++)
{
Driver->draw2DPolygon(center, (f32)radius, colors[EGDC_3D_DARK_SHADOW], vertex_count);
radius--;
}
const f64 step_size = 360.0f / vertex_count;
core::array<video::S3DVertex> vertices{vertex_count+1}; // + 1 for the center vertex
core::array<u32> indices{vertex_count+2}; // one more index for the same starting vertex
for (u32 i = 0; i < vertex_count + 1; i++)
indices[i] = i;
indices[vertex_count + 1] = 1;
vertices[0] = video::S3DVertex{(f32)center.X, (f32)center.Y, 0.f, 0.f, 1.f, 0.f, colors[EGDC_3D_DARK_SHADOW], 0.5f, 0.5f};
v2f32 offset{0.f, (f32)radius};
for (u32 i = 1; i <= vertex_count; i++)
{
offset.rotateBy(step_size*i, v2f32{(f32)center.X, (f32)center.Y});
vertices[i] = video::S3DVertex{(f32)center.X + offset.X, (f32)center.Y + offset.Y, 0.f, 0.f, 1.f, 0.f, colors[EGDC_3D_DARK_SHADOW], 0.5f, 0.5f};
}
Driver->draw2DVertexPrimitiveList(vertices.pointer(), vertices.size(), indices.pointer(),
indices.size()-2, video::EVT_STANDARD, scene::EPT_TRIANGLE_FAN, video::EIT_16BIT);
}
Yes, color is defined by vertex colors. Which are set with the CircleSettings::color parameter in my case.Andrey01 wrote:Hmm, but how do you get each triangle colored? I suppose the color is determined by summing the colors of its vertices? And also I don`t quite understand why you need one more a vertice, although really is the only starting vertice not sufficient to close the circle?