Implement FillEllipse and OutlineEllipse
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m30s

This commit is contained in:
2024-10-22 12:26:40 -04:00
parent 55b67ab850
commit aa4a29961c
2 changed files with 86 additions and 16 deletions

View File

@@ -140,8 +140,6 @@ namespace JGL {
/// @param radius The corner-rounding radius (in radians).
void FillChamferRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius = 5);
/// Draws a render-target (runtime-modifiable texture) to the screen.
/// @param render_target A RenderTarget instance to be displayed.
/// @param position The position at which to render this object from it's center-point, defined by the origin parameter.
@@ -155,7 +153,6 @@ namespace JGL {
void DrawRenderTarget(const RenderTarget* render_target, const Vector2& position, float rad_rotation = 0, const Vector2& origin = Vector2(0 , 0),
const Vector2& scale = Vector2(1, 1), const Color4& color = Colors::White, Direction inversion = Direction::None);
/// Draws a sprite (technically, actually a render target) to the screen.
/// @note This similar overload exists because we expect someone will be an idiot and turn all of their sprites into RenderTargets. ~william
/// @param render_target A RenderTarget instance to be displayed.
@@ -237,23 +234,24 @@ namespace JGL {
/// Draws an outline of a triangle on the screen.
/// @param color A 3-or-4 channel color value. @see class Color3, class Color4
/// @param tri The triangle structure / set of 3 points in cartesian space used to draw the triangle.
/// @param tri The triangle defined by its vertices (A, B, and C).
/// @param thickness The line-width of the triangle to be rendered at.
void OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness = 1);
void OutlineTriangle(const Color4& color, const Vector2& triA, const Vector2& triB, const Vector2& triC, float thickness = 1);
/// Draws a filled triangle on the screen.
/// @param color A 3-or-4 channel color value. @see class Color3, class Color4
/// @param tri The triangle structure / set of 3 points in cartesian space used to draw the triangle.
/// @param tri The triangle defined by its vertices (A, B, and C).
void FillTriangle(const Color4& color, const Triangle2D& tri);
void FIllTriangle(const Color4& color, const Vector2& triA, const Vector2& triB, const Vector2& triC);
/// Draws a triangle where each corner is defined by a given color, Smoothly transitioning between them.
/// @param a_color
/// @param b_color
/// @param c_color
/// @param tri
/// Fills a triangle defined by the provided vertices with a gradient that transitions smoothly between the three specified colors at each corner.
/// @param a_color The color at vertex A of the triangle.
/// @param b_color The color at vertex B of the triangle.
/// @param c_color The color at vertex C of the triangle.
/// @param tri The triangle defined by its vertices (A, B, and C).
void FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Triangle2D& tri);
void FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Vector2& tri_a, const Vector2& tri_b, const Vector2& tri_c);
/// Draws a smooth, curved line segment between two control points, with the curve controlled by the two inner points.
/// @param color A 3-or-4 channel color value. @see class Color3, class Color4
@@ -261,6 +259,8 @@ namespace JGL {
/// @param pointA The first inner point, which controls the contour of the curve.
/// @param pointB The second inner point, which controls the contour of the curve.
/// @param controlB The second control point, which can be considered the end of the line.
/// @param subdivisions The accuracy of the approximation of the curve, measured in iteration steps taken.
/// @param thickness The line-width to draw the curve with.
/// @see J3ML::Algorithm::Bezier
void DrawCubicBezierCurve(const Color4& color, const Vector2& controlA, const Vector2& pointA, const Vector2& pointB, const Vector2& controlB,
int subdivisions = 10, float thickness = 1);
@@ -300,8 +300,9 @@ namespace JGL {
/// TODO Implement the following. These ones are going to be extremely annoying.
void FillPolygon(const Color4& color, const std::vector<Vector2>& points);
void FillTexturedPolygon();
void FillTexturedTriangle();
void OutlineEllipse(const Color4& color, const Vector2& position, float radius_x, float radius_y, float thickness = 1, int subdivisions = 8);
void FillEllipse(const Color4& color, const Vector2& position, float radius_x, float radius_y, int subdivisions = 8);
}
/// Drawing functions for primitive 3D Shapes.
@@ -312,6 +313,12 @@ namespace JGL {
void Begin();
void End();
void SetMatrix(const std::vector<GLfloat>& matrix, const Vector2& window_size);
/// Draws a line in 3D space.
/// @param color
/// @param A
/// @param B
/// @param thickness
void DrawLine(const Color4& color, const Vector3& A, const Vector3& B, float thickness = 1);
void FillSphere(const Color3& color, const Sphere& sphere);
void WireframeSphere(const Color3& color, const Sphere& sphere, float thickness = 1);

View File

@@ -621,8 +621,8 @@ namespace JGL {
int i = 0;
for (angle = 0.0f; angle < (2.f * Math::Pi); angle += step) {
x = radius * std::sin(angle) + center.x;
y = radius * std::cos(angle) + center.y;
x = radius * std::cos(angle) + center.x;
y = radius * std::sin(angle) + center.y;
if (i < subdivisions)
vertices[i] = {x, y};
else
@@ -650,8 +650,8 @@ namespace JGL {
* wait around for the container to resize. This gets rid of it for what we can guarantee. */
int i = 0;
for (angle = 0.0f; angle < (2.f * Math::Pi); angle += step) {
x = radius * std::sin(angle) + center.x;
y = radius * std::cos(angle) + center.y;
x = radius * std::cos(angle) + center.x;
y = radius * std::sin(angle) + center.y;
if (i < subdivisions)
vertices[i] = {x, y};
else
@@ -877,6 +877,69 @@ namespace JGL {
FillTriangle(color, {triA, triB, triC});
}
void
J2D::FillGradientTriangle(const Color4 &a_color, const Color4 &b_color, const Color4 &c_color, const Vector2 &tri_a,
const Vector2 &tri_b, const Vector2 &tri_c) {
FillGradientTriangle(a_color, b_color, c_color, {tri_a, tri_b, tri_c});
}
void
J2D::OutlineEllipse(const Color4 &color, const Vector2 &position, float radius_x, float radius_y, float thickness,
int subdivisions) {
if (!inJ2D)
Logger::Error("Drawing J2D element before J2D begin.");
float step = (2.f * Math::Pi) / (float) subdivisions;
std::vector<Vector2> vertices(subdivisions);
GLfloat angle, x, y;
int i = 0;
for (angle = 0.0f; angle < (2.f * Math::Pi); angle += step) {
x = radius_x * std::cos(angle) + position.x;
y = radius_y * std::sin(angle) + position.y;
if (i < subdivisions)
vertices[i] = {x, y};
else
vertices.emplace_back(x, y);
i++;
}
glLineWidth(thickness);
glColor4ubv(color.ptr());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_LINE_LOOP, 0, (int) vertices.size());
glColor4fv(baseColor);
}
void
J2D::FillEllipse(const Color4 &color, const Vector2 &position, float radius_x, float radius_y, int subdivisions) {
if (!inJ2D)
Logger::Error("Drawing J2D element before J2D begin.");
GLfloat angle, x, y;
float step = (2.f * Math::Pi) / (float) subdivisions;
std::vector<Vector2> vertices(subdivisions);
/* Most of the time the vector "vertices" is either the same size or size + 1 of the number of subdivisions.
* Because a float is a decimal, It'd take too long to get rid of the emplace-back making us
* wait around for the container to resize. This gets rid of it for what we can guarantee. */
int i = 0;
for (angle = 0.0f; angle < (2.f * Math::Pi); angle += step) {
x = radius_x * std::cos(angle) + position.x;
y = radius_y * std::sin(angle) + position.y;
if (i < subdivisions)
vertices[i] = {x, y};
else
vertices.emplace_back(x, y);
i++;
}
glColor4ubv(color.ptr());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_TRIANGLE_FAN, 0, (int) vertices.size());
glColor4fv(baseColor);
}
#pragma endregion
#pragma region J3D