From 52a6aa5b780b7d87d13051d0b724e9a258f7a9f3 Mon Sep 17 00:00:00 2001 From: josh Date: Sat, 20 Jan 2024 15:54:25 -0500 Subject: [PATCH] Implement DrawLine2D + DrawPixel2D --- include/JGL/JGL.h | 61 ++++++++++++++++++++++++++++++++++------------- main.cpp | 5 ++-- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/include/JGL/JGL.h b/include/JGL/JGL.h index ab27be5..8ad30a7 100644 --- a/include/JGL/JGL.h +++ b/include/JGL/JGL.h @@ -60,23 +60,51 @@ namespace JGL { // TODO: Implement (CORRECT!!!) matrix transformation } - void DrawPixel2D(const Color3 &, int x, int y); - void DrawPixel2D(const Color3& color, const Vector2 &coordinates); - void DrawLine2D(const Color3& color, const Vector2 &A, const Vector2 &B); + + void DrawPixel2D(const Color3& color, const Vector2 &coordinates) + { + auto vp_pos = ScreenToViewport(coordinates); + glBegin(GL_POINT); + glColor3f(color.r, color.g, color.b); + glVertex2f(vp_pos.x, vp_pos.y); + glEnd(); + } + void DrawPixel2D(const Color3& color, float x, float y) + { + DrawPixel2D(color, {x, y}); + } + void DrawLine2D(const Color3& color, const Vector2 &A, const Vector2 &B, float thickness = 1) + { + auto vp_a = ScreenToViewport(A); + auto vp_b = ScreenToViewport(B); + + glBegin(GL_LINE); + glLineWidth(thickness); + glColor3f(color.r, color.g, color.b); + glVertex2f(vp_a.x, vp_a.y); + glVertex2f(vp_b.x, vp_b.y); + glEnd(); + + } + void DrawLine2D(const Color3& color, float x, float y, float w, float h, float thickness = 1) + { + DrawLine2D(color, {x, y}, {w, h}, thickness); + } void DrawLine3D(const Color3& color, const Vector3 &A, const Vector3 &B); void DrawCubicBezierCurve2D(); void OutlineCircle2D(const Color3& color, const Vector2& center, float radius, int subdivisions); void FillSphere3D(); void WireframeSphere3D(); void FillCircle2D(); - void OutlineTriangle(); - void FillTriangle(); - void FillTexturedTriangle(); - void FillTexturedPolygon(); - void DrawSprite(); - void DrawPartialSprite(); - void DrawString(); - void FillRect2D(const Vector2 &pos, const Vector2 &size, const Color3 &color) + void OutlineTriangle2D(); + void FillTriangle2D(); + void FillTexturedTriangle2D(); + void FillTexturedPolygon2D(); + void DrawSprite2D(); + void DrawPartialSprite2D(); + void DrawString2D(); + void DrawString3D(); + void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size) { auto vp_pos = ScreenToViewport(pos); auto vp_size = ScreenToViewport(size); @@ -88,11 +116,12 @@ 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 Color3& color, const Vector2& pos, const Vector2& size, float thickness = 1) { auto vp_pos = ScreenToViewport(pos); auto vp_size = ScreenToViewport(size); glBegin(GL_LINE_LOOP); + glLineWidth(thickness); glColor3f(color.r, color.g, color.b); glVertex2f(vp_pos.x, vp_pos.y); glVertex2f(vp_pos.x, vp_pos.y + vp_size.y); @@ -100,10 +129,10 @@ namespace JGL { 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 (); - void FillPolygon2D (); + void FillRoundedRect2D (const Color3& color, const Vector2& pos, const Vector2& size, float radius); + void OutlineRoundedRect2D(const Color3& color, const Vector2& pos, const Vector2& size, float radius, float thickness = 1); + void OutlinePolygon2D (const Color3& color, std::vector points); + void FillPolygon2D (const Color3& color, std::vector points, float thickness = 1); void GradientFillRect2D (); void DrawMatrixGizmo (const Matrix3x3&, const Vector3&); void DrawMatrixGizmo (const Matrix4x4&); diff --git a/main.cpp b/main.cpp index 1f78667..077888c 100644 --- a/main.cpp +++ b/main.cpp @@ -42,8 +42,9 @@ void display() { glVertex2f(-0.9f, -0.3f); glEnd(); - JGL::FillRect2D({0, 0}, {128, 128}, JGL::Colors::White); - JGL::OutlineRect2D({0, 0}, {128, 128}, JGL::Colors::BrightRed); + JGL::FillRect2D(JGL::Colors::White, {0, 0}, {128, 128}); + JGL::OutlineRect2D(JGL::Colors::BrightRed, {0, 0}, {128, 128}, 4); + JGL::DrawPixel2D(JGL::Colors::BrightGreen, {0, 0}); glBegin(GL_TRIANGLES); glColor3f(0.0f, 0.0f, 1.f); // Blue