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

@@ -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();
}
}