Rewrite spheres render logic

This commit is contained in:
Władysław Kuczerenko 2024-06-28 15:44:20 +02:00
parent 118e8d574d
commit bebee39768

View File

@ -21,6 +21,8 @@ const float initialSpeed = 5.0f;
const float initialAngle = 45.0f; const float initialAngle = 45.0f;
const float ballRadius = 0.5; const float ballRadius = 0.5;
const int numberOfPointsOnCircle = 120;
const int windowWidth = 1920; const int windowWidth = 1920;
const int windowHeight = 800; const int windowHeight = 800;
@ -395,31 +397,30 @@ void rungeKuttaStep(Ball &ball, float dt) {
} }
void setupSphereBuffer() { void setupSphereBuffer() {
int slices = 20;
int stacks = 20;
std::vector<float> vertices;
for (int i = 0; i <= stacks; ++i) { std::vector<glm::vec3> vertices;
float V = i / (float) stacks;
float phi = V * M_PI;
for (int j = 0; j <= slices; ++j) { float angle = 360.0f / numberOfPointsOnCircle;
float U = j / (float) slices;
float theta = U * (M_PI * 2);
float x = cosf(theta) * sinf(phi); int triangleCount = numberOfPointsOnCircle - 2;
float y = cosf(phi);
float z = sinf(theta) * sinf(phi);
vertices.push_back(x * ballRadius); std::vector<glm::vec3> temp;
vertices.push_back(y * ballRadius); // positions
vertices.push_back(z * ballRadius); 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 temp.push_back(glm::vec3(x, y, z));
vertices.push_back(x); }
vertices.push_back(y);
vertices.push_back(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); glGenVertexArrays(1, &VAO_SPHERES);
@ -428,7 +429,7 @@ void setupSphereBuffer() {
glBindVertexArray(VAO_SPHERES); glBindVertexArray(VAO_SPHERES);
glBindBuffer(GL_ARRAY_BUFFER, VBO_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 // Współrzędne wierzchołków
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);