akwk/zadanie-2_new/main.cpp

226 lines
6.6 KiB
C++
Raw Normal View History

2024-06-06 23:28:15 +02:00
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cmath>
#include <vector>
#include <iostream>
2024-06-09 23:18:46 +02:00
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
2024-06-06 23:28:15 +02:00
const int windowWidth = 1200;
const int windowHeight = 800;
struct Ball {
float x, y, z;
float vx, vy, vz;
float r, g, b;
};
// Ustawienia rzutu ukośnego dla każdej kuli (kąt startowy, prędkość początkowa)
std::vector<Ball> balls;
2024-06-09 23:18:46 +02:00
GLuint shaderProgram;
GLuint VAO, VBO;
2024-06-06 23:28:15 +02:00
void initializeBalls() {
for (int i = 0; i < 10; ++i) {
float angle = 15 + i * 6; // Kąty między 15 a 75 stopni
float speed = 10 + i; // Prędkości początkowe
Ball ball = {
.x = 0.0f,
.y = 0.0f,
.z = 0.0f,
.vx = static_cast<float>(speed * cos(angle * M_PI / 180.0f)),
.vy = static_cast<float>(speed * sin(angle * M_PI / 180.0f)),
.vz = 0.0f,
.r = static_cast<float>(rand()) / RAND_MAX,
.g = static_cast<float>(rand()) / RAND_MAX,
.b = static_cast<float>(rand()) / RAND_MAX
};
balls.push_back(ball);
}
}
2024-06-09 23:18:46 +02:00
void compileShaders() {
Shader shader("circle_vs.glsl", "circle_fs.glsl");
shaderProgram = shader.programID();
}
2024-06-06 23:28:15 +02:00
// Funkcja do integracji Rungego-Kutty
void rungeKuttaStep(Ball &ball, float dt) {
float k1vx = -0.1f * ball.vx;
float k1vy = -9.8f - 0.1f * ball.vy;
float k2vx = -0.1f * (ball.vx + 0.5f * dt * k1vx);
float k2vy = -9.8f - 0.1f * (ball.vy + 0.5f * dt * k1vy);
float k3vx = -0.1f * (ball.vx + 0.5f * dt * k2vx);
float k3vy = -9.8f - 0.1f * (ball.vy + 0.5f * dt * k2vy);
float k4vx = -0.1f * (ball.vx + dt * k3vx);
float k4vy = -9.8f - 0.1f * (ball.vy + dt * k3vy);
ball.vx += (dt / 6.0f) * (k1vx + 2.0f * k2vx + 2.0f * k3vx + k4vx);
ball.vy += (dt / 6.0f) * (k1vy + 2.0f * k2vy + 2.0f * k3vy + k4vy);
ball.x += ball.vx * dt;
ball.y += ball.vy * dt;
}
2024-06-09 23:18:46 +02:00
void setupSphereBuffers() {
float radius = 0.5f;
int slices = 20;
int stacks = 20;
std::vector<float> vertices;
2024-06-06 23:28:15 +02:00
for (int i = 0; i <= stacks; ++i) {
float V = i / (float) stacks;
float phi = V * M_PI;
for (int j = 0; j <= slices; ++j) {
float U = j / (float) slices;
float theta = U * (M_PI * 2);
float x = cosf(theta) * sinf(phi);
float y = cosf(phi);
float z = sinf(theta) * sinf(phi);
2024-06-09 23:18:46 +02:00
vertices.push_back(x * radius);
vertices.push_back(y * radius);
vertices.push_back(z * radius);
}
}
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);
2024-06-06 23:28:15 +02:00
}
}
2024-06-09 23:18:46 +02:00
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
GLuint EBO;
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
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);
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);
glBindVertexArray(0);
}
void drawSphere() {
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 20 * 20 * 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
2024-06-06 23:28:15 +02:00
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2024-06-09 23:18:46 +02:00
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));
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));
2024-06-06 23:28:15 +02:00
for (const auto &ball : balls) {
2024-06-09 23:18:46 +02:00
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");
glUniform3f(colorLoc, ball.r, ball.g, ball.b);
drawSphere();
2024-06-06 23:28:15 +02:00
}
glfwSwapBuffers(glfwGetCurrentContext());
}
void update() {
float dt = 0.01f;
for (auto &ball : balls) {
rungeKuttaStep(ball, dt);
}
}
void setupOpenGL() {
2024-06-09 23:18:46 +02:00
compileShaders();
setupSphereBuffers();
2024-06-06 23:28:15 +02:00
glEnable(GL_DEPTH_TEST);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS) {
switch (key) {
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GLFW_TRUE);
break;
}
}
}
int main() {
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return -1;
}
2024-06-09 23:18:46 +02:00
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
2024-06-06 23:28:15 +02:00
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "Animacja 10 kul - Rzut ukośny", nullptr, nullptr);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyCallback); // Rejestracja callbacku klawiatury
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
return -1;
}
initializeBalls();
setupOpenGL();
while (!glfwWindowShouldClose(window)) {
display();
update();
glfwPollEvents();
}
2024-06-09 23:18:46 +02:00
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
2024-06-06 23:28:15 +02:00
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}