From c5f60e3699d51517f3865654a8de11036c43f61d Mon Sep 17 00:00:00 2001 From: josh Date: Sat, 20 Jan 2024 14:02:43 -0500 Subject: [PATCH] Implement OutlineRect2D --- include/JGL/JGL.h | 17 ++++++++++++++--- main.cpp | 3 ++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/JGL/JGL.h b/include/JGL/JGL.h index 2b8cdcd..ab27be5 100644 --- a/include/JGL/JGL.h +++ b/include/JGL/JGL.h @@ -47,7 +47,7 @@ namespace JGL { Vector2 ScreenToViewport(const Vector2& v) { - // TODO: Implement matrix transformation + // TODO: Implement (CORRECT!!!) matrix transformation float x = v.x / 600.f; float y = v.y / 600.f; @@ -57,7 +57,7 @@ namespace JGL { } Vector2 ViewportToScreen(const Vector2& v) { - // TODO: Implement matrix transformation + // TODO: Implement (CORRECT!!!) matrix transformation } void DrawPixel2D(const Color3 &, int x, int y); @@ -88,7 +88,18 @@ namespace JGL { glVertex2f(vp_pos.x + vp_size.x, vp_pos.y); glEnd(); } - void OutlineRect2D (const Vector2& pos, const Vector2& size, const Color3& color, float thickness = 1); + void OutlineRect2D (const Vector2& pos, const Vector2& size, const Color3& color, float thickness = 1) + { + auto vp_pos = ScreenToViewport(pos); + auto vp_size = ScreenToViewport(size); + glBegin(GL_LINE_LOOP); + glColor3f(color.r, color.g, color.b); + glVertex2f(vp_pos.x, vp_pos.y); + glVertex2f(vp_pos.x, vp_pos.y + vp_size.y); + glVertex2f(vp_pos.x + vp_size.x, vp_pos.y + vp_size.y); + glVertex2f(vp_pos.x + vp_size.x, vp_pos.y); + glEnd(); + } void FillRoundedRect2D (const Vector2& pos, const Vector2& size, const Color3& color, float radius); void OutlineRoundedRect2D(const Vector2& pos, const Vector2& size, const Color3& color, float radius, float thickness = 1); void OutlinePolygon2D (); diff --git a/main.cpp b/main.cpp index 53f4379..1f78667 100644 --- a/main.cpp +++ b/main.cpp @@ -42,7 +42,8 @@ void display() { glVertex2f(-0.9f, -0.3f); glEnd(); - JGL::FillRect2D({64, 64}, {128, 128}, JGL::Colors::White); + JGL::FillRect2D({0, 0}, {128, 128}, JGL::Colors::White); + JGL::OutlineRect2D({0, 0}, {128, 128}, JGL::Colors::BrightRed); glBegin(GL_TRIANGLES); glColor3f(0.0f, 0.0f, 1.f); // Blue