Zadanie 2 stable version

This commit is contained in:
Władysław Kuczerenko 2024-06-09 23:31:10 +02:00
parent 9ad27248c3
commit 2513b172c7

View File

@ -28,7 +28,7 @@ void initializeBalls() {
float speed = 10 + i; // Prędkości początkowe float speed = 10 + i; // Prędkości początkowe
Ball ball = { Ball ball = {
.x = 0.0f, .x = 0.0f,
.y = 0.0f, .y = i * 2.0f, // Ustawienie kul w pionie
.z = 0.0f, .z = 0.0f,
.vx = static_cast<float>(speed * cos(angle * M_PI / 180.0f)), .vx = static_cast<float>(speed * cos(angle * M_PI / 180.0f)),
.vy = static_cast<float>(speed * sin(angle * M_PI / 180.0f)), .vy = static_cast<float>(speed * sin(angle * M_PI / 180.0f)),
@ -87,44 +87,24 @@ void setupSphereBuffers() {
} }
} }
std::vector<unsigned int> indices;
for (int i = 0; i < stacks; ++i) {
for (int j = 0; j < slices; ++j) {
int first = (i * (slices + 1)) + j;
int second = first + slices + 1;
indices.push_back(first);
indices.push_back(second);
indices.push_back(first + 1);
indices.push_back(second);
indices.push_back(second + 1);
indices.push_back(first + 1);
}
}
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
GLuint EBO;
glGenBuffers(1, &EBO);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
} }
void drawSphere() { void drawSphere() {
glBindVertexArray(VAO); glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 20 * 20 * 6, GL_UNSIGNED_INT, 0); glDrawArrays(GL_TRIANGLE_STRIP, 0, (20 + 1) * (20 + 1));
glBindVertexArray(0); glBindVertexArray(0);
} }
@ -132,23 +112,25 @@ void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
glm::mat4 view = glm::lookAt(glm::vec3(30.0f, 0.0f, 40.0f), glm::vec3(15.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); // Oddal kamerę zmieniając parametr eye w glm::lookAt
glm::mat4 view = glm::lookAt(glm::vec3(0, 20, 50), glm::vec3(0, 10, 0), glm::vec3(0, 1, 0));
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)windowWidth / (float)windowHeight, 0.1f, 100.0f); glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)windowWidth / (float)windowHeight, 0.1f, 100.0f);
unsigned int viewLoc = glGetUniformLocation(shaderProgram, "view");
unsigned int projectionLoc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
for (const auto &ball : balls) { for (const auto &ball : balls) {
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(ball.x, ball.y, ball.z)); glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(ball.x, ball.y, ball.z));
unsigned int modelLoc = glGetUniformLocation(shaderProgram, "model");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
unsigned int colorLoc = glGetUniformLocation(shaderProgram, "color"); GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
GLint colorLoc = glGetUniformLocation(shaderProgram, "color");
glUniform3f(colorLoc, ball.r, ball.g, ball.b); glUniform3f(colorLoc, ball.r, ball.g, ball.b);
drawSphere(); drawSphere(); // Zastąpienie glutSolidSphere funkcją drawSphere
} }
glfwSwapBuffers(glfwGetCurrentContext()); glfwSwapBuffers(glfwGetCurrentContext());