Batching for WireframeSphere.
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m48s

This commit is contained in:
2024-11-12 22:49:27 -05:00
parent f0e2cd151f
commit 627a047a9b
3 changed files with 40 additions and 29 deletions

View File

@@ -305,7 +305,6 @@ namespace JGL {
/// TODO Implement the following. These ones are going to be extremely annoying.
void FillPolygon(const Color4& color, const std::vector<Vector2>& points);
void OutlineEllipse(const Color4& color, const Vector2& position, float radius_x, float radius_y, float thickness = 1, int subdivisions = 8);
void FillEllipse(const Color4& color, const Vector2& position, float radius_x, float radius_y, int subdivisions = 8);
}
@@ -330,7 +329,7 @@ namespace JGL {
void End();
void SetMatrix(const std::vector<GLfloat> &matrix, const Vector2 &window_size);
void SetMatrix(const std::vector<GLfloat>& matrix, const Vector2& window_size);
/// Draws a line in 3D space.
/// @param color A 3-or-4 channel color value. @see class Color3, class Color4
@@ -383,6 +382,16 @@ namespace JGL {
/// @param stacks The amount of longitudinal subdivisions to perform when computing the sphere.
void WireframeSphere(const Color4& color, const Sphere& sphere, float thickness = 1.f, uint sectors = 10, uint stacks = 10);
/// Draws outlines of multiple spheres in 3D space.
/// @param color A 3-or-4 channel color value. @see class Color3, class Color4
/// @param spheres The mathematically-defined sphere objects to be rendered.
/// @param sphere_count The number of spheres.
/// @param thickness The line-width to draw the Icosahedron outline with.
/// @param sectors The amount of latitudinal subdivisions to perform when computing the sphere.
/// @param stacks The amount of longitudinal subdivisions to perform when computing the sphere.
/// @note the "Position" of the spheres is expected to be in world space.
void BatchWireframeSphere(const Color4& color, const Sphere* spheres, const size_t& sphere_count, float thickness = 1.f, uint sectors = 10, uint stacks = 10);
/// Draws the outline of an Icosphere in 3D space.
/// @note An Icosphere is an approximation of a sphere that is generated by recursively subdividing an Icosahedron.
/// @param color A 3-or-4 channel color value. @see class Color3, class Color4

View File

@@ -162,8 +162,9 @@ public:
J3D::DrawLine(Colors::Red, {-0.33,-0.125,1}, {-1,-0.125,1});
J3D::DrawLine(Colors::Red, {-0.33,-0.125,1}, {-0.33,0.25,1});
J3D::DrawString(Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f}, 1.f, 32, FreeSans, textAngle * 100, true);
//J3D::FillSphere(Colors::Green, {0,0,0.5f}, 0.25f, 10, 10);
//J3D::WireframeSphere(Colors::Green, {0,0,0.5f}, 0.25f, 1, 128, 128);
Sphere sphere[5] = {{{0,0, 0.5f}, 0.0125}, {{0,0, 0.5f}, 0.025},{{0,0, 0.5f}, 0.05},{{0,0, 0.5f}, 0.075},{{0,0, 0.5f}, 0.1}};
J3D::BatchWireframeSphere(Colors::Green, sphere, 5, 1, 24, 24);
//J3D::FillAABB(Colors::Whites::AliceBlue, {0,0,0.5f}, {0.1f, 0.05f, 0.1f});
//J3D::WireframeAABB(Colors::Gray, {0,0,0.5f}, {0.11f, 0.06f, 0.11f});

View File

@@ -1070,27 +1070,22 @@ namespace JGL {
glColor4fv(baseColor);
}
void J3D::WireframeSphere(const Color4& color, const Vector3& position, float radius, float thickness, uint sectors, uint stacks) {
void J3D::BatchWireframeSphere(const Color4& color, const Sphere* spheres, const size_t& sphere_count, float thickness, uint sectors, uint stacks) {
if (!inJ3D)
Logger::Error("Drawing J3D element before J3D begin.");
glLineWidth(thickness);
glColor4ubv(color.ptr());
unsigned int lats = sectors;
unsigned int longs = stacks;
float r = radius;
std::vector<Vector3> vertices((lats + 1) * (longs + 1));
// Create one sphere with a radius of 1 about 0, 0.
float r = 1;
std::vector<Vector3> vertices((sectors + 1) * (stacks + 1));
int index = 0;
for (int i = 0; i <= lats; i++) {
float lat = M_PI * (-0.5 + (float) i / lats);
for (int i = 0; i <= sectors; i++) {
float lat = M_PI * (-0.5 + (float) i / sectors);
float z = J3ML::Math::Sin(lat);
float zr = J3ML::Math::Cos(lat);
for (int j = 0; j <= longs; j++) {
float lng = 2 * J3ML::Math::Pi * (float) (j - 1) / longs;
for (int j = 0; j <= stacks; j++) {
float lng = 2 * J3ML::Math::Pi * (float) (j - 1) / stacks;
float x = J3ML::Math::Cos(lng);
float y = J3ML::Math::Sin(lng);
@@ -1098,24 +1093,35 @@ namespace JGL {
float pos_y = r * y * zr;
float pos_z = r * z;
pos_x += position.x;
pos_y += position.y;
pos_z += position.z;
vertices[index++] = Vector3(pos_x, pos_y, pos_z);
}
}
glLineWidth(thickness);
glColor4ubv(color.ptr());
//glBindBuffer(GL_ARRAY_BUFFER, vertices.size());
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data());
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
// Render each sphere in the batch at their given position and radius.
for(size_t i = 0; i < sphere_count; i++) {
glPushMatrix();
glTranslatef(spheres[i].Position.x, spheres[i].Position.y, spheres[i].Position.z);
glScalef(spheres[i].Radius, spheres[i].Radius, spheres[i].Radius);
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
glPopMatrix();
}
glColor4fv(baseColor);
}
void J3D::WireframeSphere(const Color4& color, const Vector3& position, float radius, float thickness, uint sectors, uint stacks) {
Sphere sphere = {position, radius};
BatchWireframeSphere(color, &sphere, 1, thickness, sectors, stacks);
}
void J3D::WireframeIcosphere(const Color4 &color, const Vector3& position, float radius, float thickness,
void J3D::WireframeSphere(const Color4& color, const Sphere& sphere, float thickness, uint sectors, uint stacks) {
BatchWireframeSphere(color, &sphere, 1, thickness, sectors, stacks);
}
void J3D::WireframeIcosphere(const Color4& color, const Vector3& position, float radius, float thickness,
unsigned int subdivisions) {
if (!inJ3D)
@@ -1361,10 +1367,6 @@ namespace JGL {
FillAABB(color, aabb.Centroid(), aabb.Size());
}
void J3D::WireframeSphere(const Color4& color, const Sphere& sphere, float thickness, uint sectors, uint stacks) {
WireframeSphere(color, sphere.Centroid(), sphere.Radius, thickness, sectors, stacks);
}
void J3D::FillSphere(const Color4 &color, const Vector3& position, float radius, uint sectors, uint stacks) {
if (!inJ3D)
Logger::Error("Drawing J3D element before J3D begin.");
@@ -1468,6 +1470,5 @@ namespace JGL {
// Have to manually draw the last segment of the curve.
DrawLine(color, last, first, thickness);
}
#pragma endregion
}