From ce5c4d4eb1d2f295f13f5852a3db6af57b636517 Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 23 Oct 2024 14:16:33 -0400 Subject: [PATCH] Sending up work --- include/JGL/JGL.h | 16 +++++-- main.cpp | 16 ++++++- src/JGL.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 134 insertions(+), 14 deletions(-) diff --git a/include/JGL/JGL.h b/include/JGL/JGL.h index ac75c3f..030aa54 100644 --- a/include/JGL/JGL.h +++ b/include/JGL/JGL.h @@ -307,23 +307,29 @@ namespace JGL { /// Drawing functions for primitive 3D Shapes. namespace J3D { - void Init(const Vector2& window_size, float fov, float far_plane); + void Init(const Vector2 &window_size, float fov, float far_plane); + void ChangeFOV(float fov); + void ChangeFarPlane(float far_plane); + void Begin(); + void End(); - void SetMatrix(const std::vector& matrix, const Vector2& window_size); + + void SetMatrix(const std::vector &matrix, const Vector2 &window_size); /// Draws a line in 3D space. /// @param color /// @param A /// @param B /// @param thickness - void DrawLine(const Color4& color, const Vector3& A, const Vector3& B, float thickness = 1.f); + void DrawLine(const Color4 &color, const Vector3 &A, const Vector3 &B, float thickness = 1.f); + void WireframeIcosahedron(const Color4 &color, const Vector3 &position, float radius, float thickness = 1.f); - void WireframeSphere(const Color4& color, const Vector3& position, float radius, float thickness = 1.f, unsigned int subdivisions = 10); - void WireframeSphere(const Color4& color, const Sphere& sphere, float thickness = 1.f, unsigned int subdivisions = 10); + void WireframeSphere(const Color4& color, const Vector3& position, float radius, float thickness = 1.f, uint sectors = 10, uint stacks = 10); + void WireframeSphere(const Color4& color, const Sphere& sphere, float thickness = 1.f, uint sectors = 10, uint stacks = 10); void WireframeIcosphere(const Color4& color, const Vector3& position, float radius, float thickness = 1.f, unsigned int subdivisions = 10); void WireframeIcosphere(const Color4& color, const Sphere& sphere, float thickness = 1.f, unsigned int subdivisions = 10); void WireframeCubesphere(const Color4& color, const Vector3& position, float radius, float thickness = 1.f, unsigned int subdivisions = 10); diff --git a/main.cpp b/main.cpp index 0034bbb..157f3d8 100644 --- a/main.cpp +++ b/main.cpp @@ -166,7 +166,8 @@ 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, true); - J3D::WireframeSphere(Colors::Green, {0,0,1.f}, 0.25f, 25, 1.f); + J3D::WireframeSphere(Colors::Green, {0,0,0.5f}, 0.25f, 1.f, 10, 10); + //J3D::WireframeIcosahedron(Colors::Green, {0,0,0.5f}, 0.125f, 1.f); J3D::End(); J2D::Begin(j2d_render_target, true); @@ -220,6 +221,19 @@ public: } void OnRefresh(float elapsed) override { + if (isKeyDown(Keys::RightArrow)) + camera->angle.y += 45.f * elapsed; + if (isKeyDown(Keys::LeftArrow)) + camera->angle.y -= 45.f * elapsed; + if (isKeyDown(Keys::UpArrow)) + camera->angle.x -= 45.f * elapsed; + if (isKeyDown(Keys::DownArrow)) + camera->angle.x += 45.f * elapsed; + if (isKeyDown(Keys::Space)) + camera->position.y += 1.f * elapsed; + if (isKeyDown(Keys::LeftShift)) + camera->position.y -= 1.f * elapsed; + auto mouse = GetMouseCoordinates(); a.Update(mouse); b.Update(mouse); diff --git a/src/JGL.cpp b/src/JGL.cpp index dedb0fb..f9507f1 100644 --- a/src/JGL.cpp +++ b/src/JGL.cpp @@ -6,6 +6,7 @@ #include #include #include +#include "JGL/types/VRamList.h" JGL::RenderTarget* render_target = nullptr; GLfloat oldColor[4] = {0, 0, 0, 1}; @@ -1061,15 +1062,15 @@ namespace JGL { glColor4fv(baseColor); } - void J3D::WireframeSphere(const Color4& color, const Vector3& position, float radius, float thickness, unsigned int subdivisions) { + void J3D::WireframeSphere(const Color4& color, const Vector3& position, float radius, float thickness, uint sectors, uint stacks) { if (!inJ3D) Logger::Error("Drawing J3D element before J3D begin."); glLineWidth(thickness); glColor4ubv(color.ptr()); - unsigned int lats = subdivisions; - unsigned int longs = subdivisions; + unsigned int lats = sectors; + unsigned int longs = stacks; float r = radius; std::vector vertices((lats + 1) * (longs + 1)); @@ -1099,19 +1100,118 @@ namespace JGL { 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()); + glDrawArrays(GL_LINES, 0, vertices.size()); glColor4fv(baseColor); - } void J3D::WireframeIcosphere(const Color4 &color, const Vector3& position, float radius, float thickness, unsigned int subdivisions) { - const float h_angle = Math::Pi / 180 * 72; // 72 degree = 360 / 5; - const float v_angle = Math::Atan(1.f / 2.f); + // TODO: Revise this once J3ML::Geometry::Icosahedron is implemented. + const float h_angle = J3ML::Math::Pi / 180.f * 72.f; // 72 degree = 360 / 5; + const float v_angle = J3ML::Math::Atan(1.0f / 2.f); // elevation = 26.565; - std::vector vertices(12*3); + std::vector vertices; // array of 12 vertices (x,y,z) + unsigned int index = 0; // indices + float z, xy; // coords + float hAngle1 = -Math::Pi / 2.f - h_angle / 2.f; // start from -126 at 1st row + float hAngle2 = -Math::Pi / 2.f; // start from -90 deg at 2nd row + + // the first top vertex at (0,0,r) + vertices[0] = position + Vector3{0,0,radius}; + + + + // compute 10 vertices at 1st and 2nd rows + for (int i = 1; i <= 5; ++i) + { + + z = radius * Math::Sin(v_angle); // elevation + xy = radius * Math::Cos(v_angle); // length on XY plane + + vertices[index++] = position + Vector3{xy * Math::Cos(hAngle1), xy * Math::Sin(hAngle1), z} ; + vertices[index++] = position + Vector3{xy * Math::Cos(hAngle2), xy * Math::Sin(hAngle2), -z}; + + // next horizontal angles + hAngle1 += h_angle; + hAngle2 += h_angle; + } + + // the last bottom vertex at (0, 0, -r) + vertices[11] = position + Vector3{0,0, -radius}; + + //glLineWidth(thickness); + //glColor4ubv(color.ptr()); + //glBindBuffer(GL_ARRAY_BUFFER, vertices.size()); + //glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data()); + //glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()); + //glColor4fv(baseColor); + + std::vector tmpVertices; + std::vector tmpIndices; + std::vector newVs((subdivisions+1) * (subdivisions+2) / 2 * 3); + const Vector3 v1, v2, v3; + Vector3 newV1, newV2, newV3; + + int i, j, k; + index = 0; // unsigned int + float a; // lerp alpha + unsigned int i1, i2; // indices + + // copy prev arrays + tmpVertices = vertices; + //tmpIndices = indices; + + // iterate each triangle of icosahedron + for (i = 0; i < tmpIndices.size(); i++) + { + //v1 = tmpVertices[tmpIndices[i]]; + + } + } + + void J3D::WireframeIcosahedron(const Color4 &color, const Vector3 &position, float radius, float thickness) { + // TODO: Revise this once J3ML::Geometry::Icosahedron is implemented. + const float h_angle = J3ML::Math::Pi / 180.f * 72.f; // 72 degree = 360 / 5; + const float v_angle = J3ML::Math::Atan(1.0f / 2.f); // elevation = 26.565; + + std::array vertices; // array of 12 vertices (x,y,z) + int index = 0; // indices + float z, xy; // coords + float hAngle1 = -Math::Pi / 2.f - h_angle / 2.f; // start from -126 at 1st row + float hAngle2 = -Math::Pi / 2.f; // start from -90 deg at 2nd row + + // the first top vertex at (0,0,r) + vertices[0] = position + Vector3{0,0,radius}; + + + + // compute 10 vertices at 1st and 2nd rows + for (int i = 1; i <= 5; ++i) + { + + z = radius * Math::Sin(v_angle); // elevation + xy = radius * Math::Cos(v_angle); // length on XY plane + + vertices[index++] = position + Vector3{xy * Math::Cos(hAngle1), xy * Math::Sin(hAngle1), z} ; + vertices[index++] = position + Vector3{xy * Math::Cos(hAngle2), xy * Math::Sin(hAngle2), -z}; + + // next horizontal angles + hAngle1 += h_angle; + hAngle2 += h_angle; + } + + // the last bottom vertex at (0, 0, -r) + vertices[11] = position + Vector3{0,0, -radius}; + + glLineWidth(thickness); + glColor4ubv(color.ptr()); + //glBindBuffer(GL_ARRAY_BUFFER, vertices.size()); + glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices.data()); + glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()); + glColor4fv(baseColor); } #pragma endregion