From 1d8823b0461c6dd4d9e716073ceb24abd2b09a68 Mon Sep 17 00:00:00 2001 From: Redacted Date: Thu, 22 Aug 2024 11:55:56 -0400 Subject: [PATCH] Outline Polygon --- include/JGL/JGL.h | 7 ++--- main.cpp | 2 +- src/JGL.cpp | 69 ++++++++++++++++++++++++++++------------------- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/include/JGL/JGL.h b/include/JGL/JGL.h index 5534fed..9023849 100644 --- a/include/JGL/JGL.h +++ b/include/JGL/JGL.h @@ -121,7 +121,7 @@ namespace JGL { const Color4& color = Colors::White, Inversion inversion = Inversion::None); - ///Draws a piece of a sprite to the screen, similar to DrawSprite. + /// Draws a piece of a sprite to the screen, similar to DrawSprite. /// @param texture /// @param position /// @param sub_texture_position The top left corner of the sub-texture to be drawn. @@ -215,8 +215,9 @@ namespace JGL { int subdivisions = 10, float thickness = 1); - void OutlinePolygon (const Color4& color, std::vector points); - void FillPolygon (const Color4& color, std::vector points, float thickness = 1); + /// Draws a series of lines where the last line will *always* connect to the beginning of the first. + void OutlinePolygon (const Color4& color, const std::vector& points, float thickness = 1); + void FillPolygon (const Color4& color, std::vector points); void OutlineRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius = 5, float thickness = 1); } diff --git a/main.cpp b/main.cpp index 2cc1caf..79297d4 100644 --- a/main.cpp +++ b/main.cpp @@ -192,7 +192,7 @@ public: J2D::DrawString(Colors::Green, "Jupteroid Font", 0.f, 0, 1.f, 16, Jupiteroid); J2D::DrawString(Colors::White, "Position: " + std::to_string(camera->position.x) + " " + std::to_string(camera->position.y) + " " + std::to_string(camera->position.z), 0, 16, 1,16, Jupiteroid); J2D::DrawString(Colors::White, "ViewAngle: " + std::to_string(camera->angle.x) + " " + std::to_string(camera->angle.y) + " " + std::to_string(camera->angle.z), 0, 33, 1,16, Jupiteroid); - + J2D::OutlinePolygon(Colors::White, {{200, 400}, {220, 420}, {220, 430}, {230, 410}, {200, 400}}); J2D::DrawCubicBezierCurve(Colors::Blues::CornflowerBlue, a.position, b.position, diff --git a/src/JGL.cpp b/src/JGL.cpp index d336640..b7576b0 100644 --- a/src/JGL.cpp +++ b/src/JGL.cpp @@ -130,7 +130,7 @@ namespace JGL { void J2D::DrawSprite(const Texture& texture, const Vector2& pos, const Vector2& origin, const Vector2& scale, const Color4& color, Inversion inversion) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") const Vector2 size = texture.GetDimensions(); @@ -182,7 +182,7 @@ namespace JGL { void J2D::DrawPartialSprite(const Texture& texture, const Vector2& position, const Vector2& sub_texture_position, const Vector2& sub_texture_size, const Vector2& origin, const Vector2& scale, const Color4& color, Inversion inversion) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin."); + ERROR("Drawing J2D element before J2D begin."); const Vector2 textureSize = texture.GetDimensions(); @@ -238,7 +238,7 @@ namespace JGL { void J2D::FillQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") Vector2 vertices[] = {v1, v2, v3, v4}; glColor4f(color.RedChannelNormalized(), @@ -256,7 +256,7 @@ namespace JGL { void J2D::OutlineQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") Vector2 vertices[] = {v1, v2, v3, v4}; glLineWidth(thickness); @@ -277,7 +277,7 @@ namespace JGL { void J2D::FillRect(const Color4& color, const Vector2& pos, const Vector2& size) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}}; glColor4f(color.RedChannelNormalized(), @@ -295,27 +295,27 @@ namespace JGL { void J2D::FillGradientRect(const Color4& color1, const Color4& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}}; std::vector colors = {}; if (gradient == Gradient::Horizontal) - colors = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f, color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f, - color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f, color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f}; + colors = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(), color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(), + color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(), color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized()}; else if (gradient == Gradient::Vertical) - colors = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f, color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f, - color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f, color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f}; + colors = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(), color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(), + color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(), color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized()}; else if (gradient == Gradient::DiagonalBottomLeft) colors = {(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, (color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f, - color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, - (color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f, color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f}; + color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(),(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, + (color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f, color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized()}; else if (gradient == Gradient::DiagonalTopLeft) - colors = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f,(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, - (color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f,color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f, + colors = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(),(color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, + (color1.b + color2.b) / 2.f / 255.f, (color1.a + color2.a) / 2.f / 255.f,color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized(), (color1.r + color2.r) / 2.f / 255.f, (color1.g + color2.g) / 2.f / 255.f, (color1.b + color2.b) / 2.f / 255.f,(color1.a + color2.a) / 2.f / 255.f}; glEnableClientState(GL_COLOR_ARRAY); @@ -332,7 +332,7 @@ namespace JGL { void J2D::FillRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius, unsigned int subdivisions) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") J2D::FillRect(color, {pos.x + radius, pos.y}, {size.x - 2 * radius, size.y}); J2D::FillRect(color, {pos.x, pos.y + radius}, {size.x, size.y - 2 * radius}); @@ -349,7 +349,7 @@ namespace JGL { void J2D::OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}}; @@ -369,7 +369,7 @@ namespace JGL { void J2D::DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin."); + ERROR("Drawing J2D element before J2D begin."); Vector2 vertices[] = {A, B}; @@ -397,15 +397,15 @@ namespace JGL { void J2D::DrawGradientLine(const Color4& color1, const Color4& color2, const Vector2& A, const Vector2& B, float thickness) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin."); + ERROR("Drawing J2D element before J2D begin."); Vector2 vertices[] = {A, B}; - GLfloat colors[8] = {color1.r / 255.f, color1.g / 255.f, color1.b / 255.f, color1.a / 255.f, - color2.r / 255.f, color2.g / 255.f, color2.b / 255.f, color2.a / 255.f}; + GLfloat colors[8] = {color1.RedChannelNormalized(), color1.GreenChannelNormalized(), color1.BlueChannelNormalized(), color1.AlphaChannelNormalized(), + color2.RedChannelNormalized(), color2.GreenChannelNormalized(), color2.BlueChannelNormalized(), color2.AlphaChannelNormalized()}; glEnableClientState(GL_COLOR_ARRAY); glLineWidth(thickness); - glColorPointer(4,GL_FLOAT,sizeof(GL_FLOAT) * 4, colors); + glColorPointer(4,GL_FLOAT,sizeof(Color4), colors); glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices); glDrawArrays(GL_LINES, 0, 2); glDisableClientState(GL_COLOR_ARRAY); @@ -423,9 +423,22 @@ namespace JGL { J2D::DrawGradientLine({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, {x, y}, {w, h}, thickness); } + void J2D::OutlinePolygon(const Color4 &color, const std::vector& points, float thickness) { + if (!inJ2D) + ERROR("Drawing J2D element before J2D begin."); + + if (points.front() != points.back()) + throw std::runtime_error("J2D::OutlinePolygon: The first point and the last point must connect."); + + glLineWidth(thickness); + glColor4f(color.RedChannelNormalized(), color.GreenChannelNormalized(), color.BlueChannelNormalized(), color.AlphaChannelNormalized()); + glVertexPointer(2, GL_FLOAT, sizeof(Vector2), points.data()); + glDrawArrays(GL_LINE_LOOP, 0, points.size()); + glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]); + } void J2D::DrawPoint(const Color4& color, const Vector2& coordinates, float radius) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin."); + ERROR("Drawing J2D element before J2D begin."); Vector2 vertices[] = {coordinates}; @@ -453,7 +466,7 @@ namespace JGL { void J2D::OutlineCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions, float thickness) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") float step = (2.f * Math::Pi) / (float) subdivisions; std::vector vertices{}; @@ -481,7 +494,7 @@ namespace JGL { void J2D::FillCircle(const Color4& color, const Vector2& center, float radius, unsigned int subdivisions) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") GLfloat angle, x, y; float step = (2.f * Math::Pi) / (float) subdivisions; @@ -504,7 +517,7 @@ namespace JGL { void J2D::OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}}; @@ -521,7 +534,7 @@ namespace JGL { void J2D::FillTriangle(const Color4& color, const Triangle2D& tri) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}}; @@ -533,7 +546,7 @@ namespace JGL { void J2D::FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Triangle2D& tri) { if (!inJ2D) - ERROR("Attempt to Render J2D element before J2D begin.") + ERROR("Drawing J2D element before J2D begin.") Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}}; GLfloat colors[] = {a_color.r / 255.f, a_color.g / 255.f, a_color.b / 255.f, a_color.a / 255.f,b_color.r / 255.f, @@ -566,7 +579,7 @@ namespace JGL { Vector2 last = controlA; - Vector2 first = controlB; + const Vector2& first = controlB; for (int i = 0; i < subdivisions; ++i) { float alpha = (float)i / (float)subdivisions;