This commit is contained in:
2024-01-23 04:00:15 -05:00
parent 52a6aa5b78
commit 09a8c744c2
2 changed files with 78 additions and 64 deletions

View File

@@ -45,51 +45,17 @@ namespace JGL {
float value; float value;
}; };
Vector2 ScreenToViewport(const Vector2& v) Vector2 ScreenToViewport(const Vector2& v);
{
// TODO: Implement (CORRECT!!!) matrix transformation
float x = v.x / 600.f;
float y = v.y / 600.f;
return {
x - .5f, y - .5f
};
}
Vector2 ViewportToScreen(const Vector2& v) Vector2 ViewportToScreen(const Vector2& v)
{ {
// TODO: Implement (CORRECT!!!) matrix transformation // TODO: Implement (CORRECT!!!) matrix transformation
} }
void DrawPixel2D(const Color3& color, const Vector2 &coordinates) void DrawPixel2D(const Color3& color, const Vector2 &coordinates);
{ void DrawPixel2D(const Color3& color, float x, float y);
auto vp_pos = ScreenToViewport(coordinates); void DrawLine2D(const Color3& color, const Vector2 &A, const Vector2 &B, float thickness = 1);
glBegin(GL_POINT); void DrawLine2D(const Color3& color, float x, float y, float w, float h, float thickness = 1);
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 DrawLine3D(const Color3& color, const Vector3 &A, const Vector3 &B);
void DrawCubicBezierCurve2D(); void DrawCubicBezierCurve2D();
void OutlineCircle2D(const Color3& color, const Vector2& center, float radius, int subdivisions); void OutlineCircle2D(const Color3& color, const Vector2& center, float radius, int subdivisions);
@@ -104,31 +70,8 @@ namespace JGL {
void DrawPartialSprite2D(); void DrawPartialSprite2D();
void DrawString2D(); void DrawString2D();
void DrawString3D(); void DrawString3D();
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size) void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size);
{ 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_QUADS);
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 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);
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 Color3& color, const Vector2& pos, const Vector2& size, float radius); 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 OutlineRoundedRect2D(const Color3& color, const Vector2& pos, const Vector2& size, float radius, float thickness = 1);
void OutlinePolygon2D (const Color3& color, std::vector<Vector2> points); void OutlinePolygon2D (const Color3& color, std::vector<Vector2> points);

View File

@@ -1,3 +1,74 @@
// //
// Created by dawsh on 1/17/24. // Created by dawsh on 1/17/24.
// //
#include <JGL/JGL.h>
#include <GL/glut.h>
namespace JGL
{
Vector2 ScreenToViewport(const Vector2 &v) {
// TODO: Implement (CORRECT!!!) matrix transformation
float x = v.x / 600.f;
float y = v.y / 600.f;
return {
x - .5f, y - .5f
};
}
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size) {
auto vp_pos = ScreenToViewport(pos);
auto vp_size = ScreenToViewport(size);
glBegin(GL_QUADS);
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 OutlineRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size, float thickness) {
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);
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 DrawLine2D(const Color3 &color, const Vector2 &A, const Vector2 &B, float thickness) {
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) {
DrawLine2D(color, {x, y}, {w, h}, thickness);
}
void JGL::DrawPixel2D(const Color3 &color, float x, float y) {
DrawPixel2D(color, {x, y});
}
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();
}
}