Accept Color3 & Color4
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 6m0s

This commit is contained in:
2024-07-09 14:40:13 -04:00
parent 5979ae41fc
commit d118ae2f8e
3 changed files with 126 additions and 50 deletions

View File

@@ -59,32 +59,44 @@ namespace JGL {
namespace J2D {
void Begin();
void End();
void DrawPixel(const Color3& color, const Vector2& coordinates);
void DrawPixel(const Color3& color, float x, float y);
void DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness = 1);
void DrawLine(const Color3& color, float x, float y, float w, float h, float thickness = 1);
void DrawPixel(const Color4& color, const Vector2& coordinates);
void DrawPixel(const Color4& color, float x, float y);
void DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness = 1);
void DrawLine(const Color4& color, float x, float y, float w, float h, float thickness = 1);
void DrawCubicBezierCurve();
void OutlineCircle(const Color3& color, const Vector2& center, float radius, int subdivisions, float thickness = 1);
void FillCircle(const Color3& color, const Vector2& center, float radius, int subdivisions);
void OutlineTriangle(const Color3& color, const Triangle2D& tri, float thickness = 1);
void FillTriangle(const Color3& color, const Triangle2D &tri);
void OutlineCircle(const Color4& color, const Vector2& center, float radius, int subdivisions, float thickness = 1);
void FillCircle(const Color4& color, const Vector2& center, float radius, int subdivisions);
void OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness = 1);
void FillTriangle(const Color4& color, const Triangle2D &tri);
void FillTexturedTriangle();
void FillTexturedPolygon();
void DrawSprite();
void DrawPartialSprite();
void DrawString(const Color3& color, std::string text, float x, float y, float scale, u32 size, unsigned int font_index);
void FillRect(const Color3 &color, const Vector2& pos, const Vector2& size);
void OutlineRect ( const Color3& color, const Vector2& pos, const Vector2& size, float thickness = 1);
void FillRoundedRect (const Color3& color, const Vector2& pos, const Vector2& size, float radius);
void OutlineRoundedRect(const Color3& color, const Vector2& pos, const Vector2& size, float radius, float thickness = 1);
void OutlinePolygon (const Color3& color, std::vector<Vector2> points);
void FillPolygon (const Color3& color, std::vector<Vector2> points, float thickness = 1);
void FillRect(const Color4& color, const Vector2& pos, const Vector2& size);
void OutlineRect ( const Color4& color, const Vector2& pos, const Vector2& size, float thickness = 1);
void FillRoundedRect (const Color4& color, const Vector2& pos, const Vector2& size, float radius);
void OutlineRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius, float thickness = 1);
void OutlinePolygon (const Color4& color, std::vector<Vector2> points);
void FillPolygon (const Color4& color, std::vector<Vector2> points, float thickness = 1);
void GradientFillRect ();
void FillRect(const Color3& color, const Vector2& pos, const Vector2& size);
void OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness = 1);
void DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness = 1);
void DrawLine(const Color3& color, float x, float y, float w, float h, float thickness = 1);
void DrawPixel(const Color3& color, const Vector2& coordinates);
void DrawPixel(const Color3& color, float x, float y);
void OutlineCircle(const Color3& color, const Vector2& center, float radius, int subdivisions, float thickness = 1);
void FillCircle(const Color3& color, const Vector2& center, float radius, int subdivisions);
void OutlineTriangle(const Color3& color, const Triangle2D& tri, float thickness = 1);
void FillTriangle(const Color3& color, const Triangle2D &tri);
}
namespace J3D {
void Begin();
void End();
void SetMatrix(const std::vector<GLfloat>& matrix, const Vector2& window_size);
void DrawLine(const Color4& color, const Vector3& A, const Vector3& B, float thickness = 1);
void DrawLine(const Color3& 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

@@ -92,8 +92,9 @@ public:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//camera->angle.y += 1;
camera->render();
///All 3D elements of the scene and JGL elements *must* be rendered before the 2d stuff.
J3D::Begin();
J3D::DrawLine(JGL::Colors::Red, {-0.33,-0.125,1}, {-1,-0.125,1});
@@ -102,10 +103,12 @@ public:
J3D::End();
J2D::Begin();
J2D::OutlineRect(JGL::Colors::Blue, {32, 32}, {100.5, 100.5});
J2D::OutlineTriangle(JGL::Colors::Yellow, {{140, 200},{135, 100},{105, 100}}, 1);
J2D::FillRect(JGL::Colors::Blue, {32, 32}, {100.5, 100.5});
J2D::FillRect({255,0,0,255/2}, {100.5, 100.5}, {32, 32});
//J2D::OutlineTriangle(JGL::Colors::Yellow, {{140, 200},{135, 100},{105, 100}});
J2D::FillCircle(JGL::Colors::White, {120, 200}, 20, 16);
J2D::DrawLine(JGL::Colors::Green, {10, 10}, {200, 300}, 1);
J2D::DrawLine(JGL::Colors::Green, {10, 10}, {200, 300});
J2D::DrawString(JGL::Colors::Green, "Jupteroid Font", 0.f, -48.f, 1.f, 16, Jupiteroid);
J2D::DrawString(JGL::Colors::White, "Position: " + std::to_string(camera->position.x) + " " + std::to_string(camera->position.y) + " " + std::to_string(camera->position.z), 0, -65, 1,16, Jupiteroid);
J2D::DrawString(JGL::Colors::White, "ViewAngle: " + std::to_string(camera->angle.x) + " " + std::to_string(camera->angle.y) + " " + std::to_string(camera->angle.z), 0, -82, 1,16, Jupiteroid);

View File

@@ -8,6 +8,8 @@
bool wasDepthTestEnabled = false;
bool wasVertexArraysEnabled = false;
bool wasCullFaceEnabled = false;
bool wasBlendEnabled = false;
namespace JGL {
using namespace J3ML;
@@ -22,7 +24,6 @@ namespace JGL {
void J2D::Begin() {
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
@@ -36,14 +37,26 @@ namespace JGL {
wasDepthTestEnabled = true,
glDisable(GL_DEPTH_TEST);
wasVertexArraysEnabled = false;
wasVertexArraysEnabled = true;
if (!glIsEnabled(GL_VERTEX_ARRAY))
wasVertexArraysEnabled = false,
glEnable(GL_VERTEX_ARRAY);
wasCullFaceEnabled = true;
if (!glIsEnabled(GL_CULL_FACE))
wasCullFaceEnabled = false,
glEnable(GL_CULL_FACE),
glCullFace(GL_BACK);
wasBlendEnabled = true;
if (!glIsEnabled(GL_BLEND))
wasBlendEnabled = false,
glEnable(GL_BLEND),
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void J2D::End() {
//Change back to the previous projection (The 3D one in Re3D's case.
//Change back to the previous projection (The 3D one in Re3D's case.)
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
@@ -55,52 +68,80 @@ namespace JGL {
if (!wasVertexArraysEnabled)
glDisable(GL_VERTEX_ARRAY);
//std::cout << (int) glIsEnabled(GL_VERTEX_ARRAY) << std::endl;
if (!wasCullFaceEnabled)
glDisable(GL_CULL_FACE);
if (!wasBlendEnabled)
glDisable(GL_BLEND);
}
void J2D::FillRect(const Color3& color, const Vector2& pos, const Vector2& size) {
void J2D::FillRect(const Color4& color, const Vector2& pos, const Vector2& size) {
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}};
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_QUADS, 0, 4);
}
void J2D::OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness) {
void J2D::FillRect(const Color3& color, const Vector2& pos, const Vector2& size) {
J2D::FillRect({color.r, color.g, color.b, 255}, pos, size);
}
void J2D::OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness) {
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}};
glLineWidth(thickness);
glColor3f(color.r, color.g, color.b);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINE_LOOP, 0, 4);
}
void J2D::DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness) {
void J2D::OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness) {
J2D::OutlineRect({color.r, color.g, color.b, 255}, pos, size, thickness);
}
void J2D::DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness) {
Vector2 vertices[] = {A, B};
glLineWidth(thickness);
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 2);
}
void J2D::DrawLine(const Color3& color, float x, float y, float w, float h, float thickness) {
DrawLine(color, {x, y}, {w, h}, thickness);
void J2D::DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness) {
J2D::DrawLine({color.r, color.g, color.b, 255}, A, B, thickness);
}
void J2D::DrawPixel(const Color3& color, const Vector2& coordinates) {
void J2D::DrawLine(const Color4& color, float x, float y, float w, float h, float thickness) {
J2D::DrawLine(color, {x, y}, {w, h}, thickness);
}
void J2D::DrawLine(const Color3& color, float x, float y, float w, float h, float thickness) {
J2D::DrawLine({color.r, color.g, color.b, 255}, x, y, w, h, thickness);
}
void J2D::DrawPixel(const Color4& color, const Vector2& coordinates) {
Vector2 vertices[] = {coordinates};
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 1);
}
void J2D::DrawPixel(const Color3& color, float x, float y) {
void J2D::DrawPixel(const Color3& color, const Vector2& coordinates) {
J2D::DrawPixel({color.r, color.g, color.b, 255}, coordinates);
}
void J2D::DrawPixel(const Color4& color, float x, float y) {
DrawPixel(color, {x, y});
}
void J2D::OutlineCircle(const Color3& color, const Vector2& center, float radius, int subdivisions, float thickness) {
void J2D::DrawPixel(const Color3& color, float x, float y) {
DrawPixel({color.r, color.g, color.b, 255}, {x, y});
}
void J2D::OutlineCircle(const Color4& color, const Vector2& center, float radius, int subdivisions, float thickness) {
float step = (2.f * M_PI) / (float) subdivisions;
std::vector<Vector2> vertices{};
GLfloat angle, x, y;
@@ -112,44 +153,59 @@ namespace JGL {
}
glLineWidth(thickness);
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
}
void J2D::FillCircle(const Color3& color, const Vector2& center, float radius, int subdivisions) {
float step = (2.f * M_PI) / (float) subdivisions;
void J2D::OutlineCircle(const Color3& color, const Vector2& center, float radius, int subdivisions, float thickness) {
J2D::OutlineCircle({color.r, color.g, color.b, 255}, center, radius, subdivisions, thickness);
}
void J2D::FillCircle(const Color4& color, const Vector2& center, float radius, int subdivisions) {
float step = (2.f * M_PI) / (float) subdivisions;;
std::vector<Vector2> vertices{};
GLfloat angle, x, y;
for (angle = 0.0f; angle < (2.f * M_PI); angle += step) {
x = radius * sin(angle) + center.x;
y = radius * cos(angle) + center.y;
vertices.push_back({x, y});
}
for (angle = 0.0f; angle < (2.f * M_PI); angle += step)
x = radius * sin(angle) + center.x,
y = radius * cos(angle) + center.y,
vertices.push_back({x, y});
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_POLYGON, 0, vertices.size());
glDrawArrays(GL_TRIANGLE_FAN, 0, vertices.size());
}
void J2D::OutlineTriangle(const Color3& color, const Triangle2D& tri, float thickness) {
void J2D::FillCircle(const Color3& color, const Vector2& center, float radius, int subdivisions) {
J2D::FillCircle({color.r, color.g, color.b, 255}, center, radius, subdivisions);
}
void J2D::OutlineTriangle(const Color4& color, const Triangle2D& tri, float thickness) {
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
glLineWidth(thickness);
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINE_LOOP, 0, 3);
}
void J2D::FillTriangle(const Color3& color, const Triangle2D &tri) {
void J2D::OutlineTriangle(const Color3& color, const Triangle2D& tri, float thickness) {
J2D::OutlineTriangle({color.r, color.g, color.b, 255}, tri, thickness);
}
void J2D::FillTriangle(const Color4& color, const Triangle2D &tri) {
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void J2D::FillTriangle(const Color3& color, const Triangle2D &tri) {
J2D::FillTriangle({color.r, color.g, color.b, 255}, tri);
}
void J3D::Begin() {
glEnable(GL_TEXTURE_2D);
@@ -173,12 +229,17 @@ namespace JGL {
if (!wasVertexArraysEnabled)
glDisable(GL_VERTEX_ARRAY);
}
void J3D::DrawLine(const Color3& color, const Vector3& A, const Vector3& B, float thickness) {
void J3D::DrawLine(const Color4& color, const Vector3& A, const Vector3& B, float thickness) {
Vector3 vertices[] = {A, B};
glLineWidth(thickness);
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices);
glDrawArrays(GL_LINES, 0, 2);
}
void J3D::DrawLine(const Color3& color, const Vector3& A, const Vector3& B, float thickness) {
J3D::DrawLine({color.r, color.g, color.b, 255}, A, B, thickness);
}
}