Zadanie 2 stable version

This commit is contained in:
Władysław Kuczerenko 2024-06-09 23:18:46 +02:00
parent dcd8ec7ace
commit 9ad27248c3
3 changed files with 104 additions and 31 deletions

View File

@ -1,6 +1,9 @@
#version 330 core #version 330 core
in vec3 fragColor; out vec4 FragColor;
out vec4 color;
void main() { uniform vec3 color;
color = vec4(fragColor, 1.0);
void main()
{
FragColor = vec4(color, 1.0);
} }

View File

@ -1,11 +1,11 @@
#version 330 core #version 330 core
layout(location = 0) in vec3 position; layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 color;
out vec3 fragColor;
uniform mat4 model; uniform mat4 model;
uniform mat4 view; uniform mat4 view;
uniform mat4 projection; uniform mat4 projection;
void main() {
fragColor = color; void main()
gl_Position = projection * view * model * vec4(position, 1.0); {
gl_Position = projection * view * model * vec4(aPos, 1.0);
} }

View File

@ -3,6 +3,10 @@
#include <cmath> #include <cmath>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
const int windowWidth = 1200; const int windowWidth = 1200;
const int windowHeight = 800; const int windowHeight = 800;
@ -15,6 +19,8 @@ struct Ball {
// Ustawienia rzutu ukośnego dla każdej kuli (kąt startowy, prędkość początkowa) // Ustawienia rzutu ukośnego dla każdej kuli (kąt startowy, prędkość początkowa)
std::vector<Ball> balls; std::vector<Ball> balls;
GLuint shaderProgram;
GLuint VAO, VBO;
void initializeBalls() { void initializeBalls() {
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
@ -35,6 +41,11 @@ void initializeBalls() {
} }
} }
void compileShaders() {
Shader shader("circle_vs.glsl", "circle_fs.glsl");
shaderProgram = shader.programID();
}
// Funkcja do integracji Rungego-Kutty // Funkcja do integracji Rungego-Kutty
void rungeKuttaStep(Ball &ball, float dt) { void rungeKuttaStep(Ball &ball, float dt) {
float k1vx = -0.1f * ball.vx; float k1vx = -0.1f * ball.vx;
@ -52,12 +63,16 @@ void rungeKuttaStep(Ball &ball, float dt) {
ball.y += ball.vy * dt; ball.y += ball.vy * dt;
} }
void drawSphere(float radius, int slices, int stacks) { void setupSphereBuffers() {
float radius = 0.5f;
int slices = 20;
int stacks = 20;
std::vector<float> vertices;
for (int i = 0; i <= stacks; ++i) { for (int i = 0; i <= stacks; ++i) {
float V = i / (float) stacks; float V = i / (float) stacks;
float phi = V * M_PI; float phi = V * M_PI;
glBegin(GL_TRIANGLE_STRIP);
for (int j = 0; j <= slices; ++j) { for (int j = 0; j <= slices; ++j) {
float U = j / (float) slices; float U = j / (float) slices;
float theta = U * (M_PI * 2); float theta = U * (M_PI * 2);
@ -66,24 +81,74 @@ void drawSphere(float radius, int slices, int stacks) {
float y = cosf(phi); float y = cosf(phi);
float z = sinf(theta) * sinf(phi); float z = sinf(theta) * sinf(phi);
glVertex3f(x * radius, y * radius, z * radius); vertices.push_back(x * radius);
vertices.push_back(y * radius);
vertices.push_back(z * radius);
} }
glEnd();
} }
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);
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);
} }
void display() { void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); glUseProgram(shaderProgram);
glLoadIdentity();
gluLookAt(30, 0, 40, 15, 0, 0, 0, 1, 0); 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));
for (const auto &ball : balls) { for (const auto &ball : balls) {
glPushMatrix(); glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(ball.x, ball.y, ball.z));
glTranslatef(ball.x, ball.y, ball.z); unsigned int modelLoc = glGetUniformLocation(shaderProgram, "model");
glColor3f(ball.r, ball.g, ball.b); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
drawSphere(0.5, 20, 20); // Zastąpienie glutSolidSphere funkcją drawSphere
glPopMatrix(); unsigned int colorLoc = glGetUniformLocation(shaderProgram, "color");
glUniform3f(colorLoc, ball.r, ball.g, ball.b);
drawSphere();
} }
glfwSwapBuffers(glfwGetCurrentContext()); glfwSwapBuffers(glfwGetCurrentContext());
@ -93,18 +158,15 @@ void update() {
float dt = 0.01f; float dt = 0.01f;
for (auto &ball : balls) { for (auto &ball : balls) {
rungeKuttaStep(ball, dt); rungeKuttaStep(ball, dt);
// Diagnostyka aktualizacji pozycji
std::cout << "Ball position: (" << ball.x << ", " << ball.y << ", " << ball.z << ")\n";
} }
} }
void setupOpenGL() { void setupOpenGL() {
compileShaders();
setupSphereBuffers();
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)windowWidth / (double)windowHeight, 0.1, 100.0);
} }
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
@ -113,7 +175,6 @@ void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GLFW_TRUE); glfwSetWindowShouldClose(window, GLFW_TRUE);
break; break;
// Dodaj inne przypadki obsługi klawiszy tutaj
} }
} }
} }
@ -124,6 +185,11 @@ int main() {
return -1; return -1;
} }
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "Animacja 10 kul - Rzut ukośny", nullptr, nullptr); GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "Animacja 10 kul - Rzut ukośny", nullptr, nullptr);
if (!window) { if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl; std::cerr << "Failed to create GLFW window" << std::endl;
@ -149,6 +215,10 @@ int main() {
glfwPollEvents(); glfwPollEvents();
} }
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();
return 0; return 0;