Performance optimization
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 2m41s

This commit is contained in:
2024-07-08 23:13:45 -04:00
parent 652626b2e4
commit 5979ae41fc
4 changed files with 111 additions and 95 deletions

View File

@@ -59,21 +59,21 @@ 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 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 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);
void FillTriangle(const Color3 &color, const Triangle2D &tri);
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 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 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);
@@ -85,7 +85,7 @@ namespace JGL {
void Begin();
void End();
void SetMatrix(const std::vector<GLfloat>& matrix, const Vector2& window_size);
void DrawLine(const Color3& 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);
void FillOBB(const Color3& color, const OBB& obb);

View File

@@ -102,9 +102,10 @@ public:
J3D::End();
J2D::Begin();
J2D::FillRect(JGL::Colors::Blue, {32, 32}, {100.5, 100.5});
J2D::FillTriangle(JGL::Colors::Yellow, {{140, 200},{135, 100},{105, 100}});
J2D::DrawLine(JGL::Colors::Greens::DarkGreen, {10, 10}, {200, 300});
J2D::OutlineRect(JGL::Colors::Blue, {32, 32}, {100.5, 100.5});
J2D::OutlineTriangle(JGL::Colors::Yellow, {{140, 200},{135, 100},{105, 100}}, 1);
J2D::FillCircle(JGL::Colors::White, {120, 200}, 20, 16);
J2D::DrawLine(JGL::Colors::Green, {10, 10}, {200, 300}, 1);
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);
@@ -113,6 +114,8 @@ public:
void OnRefresh(float elapsed) override {
display();
if (glGetError() != GL_NO_ERROR)
exit(1);
glSwapBuffers();
}

View File

@@ -6,7 +6,8 @@
#include <glad/glad.h>
#include <JGL/Color3.h>
bool wasDepthTest = false;
bool wasDepthTestEnabled = false;
bool wasVertexArraysEnabled = false;
namespace JGL {
using namespace J3ML;
@@ -21,7 +22,7 @@ namespace JGL {
void J2D::Begin() {
glEnable(GL_TEXTURE_2D);
// Switch to orthographic projection
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
@@ -30,10 +31,15 @@ namespace JGL {
glPushMatrix();
glLoadIdentity();
wasDepthTest = false;
wasDepthTestEnabled = false;
if (glIsEnabled(GL_DEPTH_TEST))
wasDepthTest = true,
wasDepthTestEnabled = true,
glDisable(GL_DEPTH_TEST);
wasVertexArraysEnabled = false;
if (!glIsEnabled(GL_VERTEX_ARRAY))
wasVertexArraysEnabled = false,
glEnable(GL_VERTEX_ARRAY);
}
void J2D::End() {
@@ -43,129 +49,136 @@ namespace JGL {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
if (wasDepthTest)
if (wasDepthTestEnabled)
glEnable(GL_DEPTH_TEST);
if (!wasVertexArraysEnabled)
glDisable(GL_VERTEX_ARRAY);
//std::cout << (int) glIsEnabled(GL_VERTEX_ARRAY) << std::endl;
}
void J2D::FillRect(const Color3 &color, const Vector2 &pos, const Vector2 &size) {
auto vp_pos = pos;
auto vp_size = size;
glBegin(GL_QUADS);
void J2D::FillRect(const Color3& 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);
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();
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) {
auto vp_pos = pos;
auto vp_size = size;
void J2D::OutlineRect(const Color3& 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);
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();
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) {
auto vp_a = A;
auto vp_b = B;
void J2D::DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness) {
Vector2 vertices[] = {A, B};
glLineWidth(thickness);
glBegin(GL_LINES);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_a.x, vp_a.y);
glVertex2f(vp_b.x, vp_b.y);
glEnd();
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 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) {
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::DrawPixel(const Color3 &color, float x, float y) {
void J2D::DrawPixel(const Color3& color, const Vector2& coordinates) {
Vector2 vertices[] = {coordinates};
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 1);
}
void J2D::DrawPixel(const Color3& color, float x, float y) {
DrawPixel(color, {x, y});
}
void J2D::DrawPixel(const Color3 &color, const Vector2 &coordinates) {
auto vp_pos = coordinates;
glBegin(GL_POINT);
glColor3f(color.r, color.g, color.b);
glVertex2f(vp_pos.x, vp_pos.y);
glEnd();
}
void J2D::OutlineCircle(const Color3 &color, const Vector2 &center, float radius, int subdivisions, float thickness) {
glBegin(GL_LINE_LOOP);
GLfloat angle;
glColor3f(color.r, color.g, color.b);
void J2D::OutlineCircle(const Color3& 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;
for (angle = 0.0f; angle < (2.f * M_PI); angle += step) {
GLfloat x = radius * sin(angle);
GLfloat y = radius * cos(angle);
x += center.x;
y += center.y;
glVertex2f(x, y);
x = radius * std::sin(angle) + center.x;
y = radius * std::cos(angle) + center.y;
vertices.emplace_back(x,y);
}
glEnd();
glLineWidth(thickness);
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 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) {
glBegin(GL_POLYGON);
GLfloat angle;
glColor3f(color.r, color.g, color.b);
void J2D::FillCircle(const Color3& 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) {
GLfloat x = radius * sin(angle);
GLfloat y = radius * cos(angle);
x += center.x;
y += center.y;
glVertex2f(x, y);
x = radius * sin(angle) + center.x;
y = radius * cos(angle) + center.y;
vertices.push_back({x, y});
}
glEnd();
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_POLYGON, 0, vertices.size());
}
void J2D::OutlineTriangle(const Color3 &color, const Triangle2D &tri) {
glBegin(GL_LINE_LOOP);
void J2D::OutlineTriangle(const Color3& 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);
glVertex2f(tri.A.x, tri.A.y);
glVertex2f(tri.B.x, tri.B.y);
glVertex2f(tri.C.x, tri.C.y);
glEnd();
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINE_LOOP, 0, 3);
}
void J2D::FillTriangle(const Color3 &color, const Triangle2D &tri) {
glBegin(GL_LINE_LOOP);
void J2D::FillTriangle(const Color3& 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);
glVertex2f(tri.A.x, tri.A.y);
glVertex2f(tri.B.x, tri.B.y);
glVertex2f(tri.C.x, tri.C.y);
glEnd();
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void J3D::Begin() {
glEnable(GL_TEXTURE_2D);
wasDepthTest = false;
wasDepthTestEnabled = false;
if (glIsEnabled(GL_DEPTH_TEST))
wasDepthTest = true,
wasDepthTestEnabled = true,
glDisable(GL_DEPTH_TEST);
wasVertexArraysEnabled = false;
if (!glIsEnabled(GL_VERTEX_ARRAY))
wasVertexArraysEnabled = false,
glEnable(GL_VERTEX_ARRAY);
}
void J3D::End() {
glDisable(GL_TEXTURE_2D);
if (wasDepthTest)
if (wasDepthTestEnabled)
glEnable(GL_DEPTH_TEST);
if (!wasVertexArraysEnabled)
glDisable(GL_VERTEX_ARRAY);
}
void J3D::DrawLine(const Color3 &color, const Vector3 &A, const Vector3 &B, float thickness) {
void J3D::DrawLine(const Color3& color, const Vector3& A, const Vector3& B, float thickness) {
Vector3 vertices[] = {A, B};
glLineWidth(thickness);
glBegin(GL_LINES);
glColor3f(color.r / 255.f, color.g / 255.f, color.b / 255.f);
glVertex3f(A.x, A.y, A.z);
glVertex3f(B.x, B.y, B.z);
glEnd();
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices);
glDrawArrays(GL_LINES, 0, 2);
}
}

View File

@@ -255,7 +255,7 @@ namespace JGL {
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glColor4f(currentColor[0], currentColor[1], currentColor[2], currentColor[3]); //Set draw color back to whatever it was before.
glPopMatrix();
}
}