From bebee39768c5932fef4a92bbdebc83c9b4a4caff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?W=C5=82adys=C5=82aw=20Kuczerenko?= Date: Fri, 28 Jun 2024 15:44:20 +0200 Subject: [PATCH] Rewrite spheres render logic --- zadanie-5/main.cpp | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/zadanie-5/main.cpp b/zadanie-5/main.cpp index dc73219..69cf478 100644 --- a/zadanie-5/main.cpp +++ b/zadanie-5/main.cpp @@ -21,6 +21,8 @@ const float initialSpeed = 5.0f; const float initialAngle = 45.0f; const float ballRadius = 0.5; +const int numberOfPointsOnCircle = 120; + const int windowWidth = 1920; const int windowHeight = 800; @@ -395,31 +397,30 @@ void rungeKuttaStep(Ball &ball, float dt) { } void setupSphereBuffer() { - int slices = 20; - int stacks = 20; - std::vector vertices; - for (int i = 0; i <= stacks; ++i) { - float V = i / (float) stacks; - float phi = V * M_PI; + std::vector vertices; - for (int j = 0; j <= slices; ++j) { - float U = j / (float) slices; - float theta = U * (M_PI * 2); + float angle = 360.0f / numberOfPointsOnCircle; - float x = cosf(theta) * sinf(phi); - float y = cosf(phi); - float z = sinf(theta) * sinf(phi); + int triangleCount = numberOfPointsOnCircle - 2; - vertices.push_back(x * ballRadius); - vertices.push_back(y * ballRadius); - vertices.push_back(z * ballRadius); + std::vector temp; + // positions + for (int i = 0; i < numberOfPointsOnCircle; i++) + { + float currentAngle = angle * i; + float x = ballRadius * cos(glm::radians(currentAngle)); + float y = ballRadius * sin(glm::radians(currentAngle)); + float z = 0.0f; - // Dodanie normalnych wektorów - vertices.push_back(x); - vertices.push_back(y); - vertices.push_back(z); - } + temp.push_back(glm::vec3(x, y, z)); + } + + for (int i = 0; i < triangleCount; i++) + { + vertices.push_back(temp[0]); + vertices.push_back(temp[i + 1]); + vertices.push_back(temp[i + 2]); } glGenVertexArrays(1, &VAO_SPHERES); @@ -428,7 +429,7 @@ void setupSphereBuffer() { glBindVertexArray(VAO_SPHERES); glBindBuffer(GL_ARRAY_BUFFER, VBO_SPHERES); - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW); // Współrzędne wierzchołków glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);