Zadanie 2 stable version
This commit is contained in:
parent
dcd8ec7ace
commit
9ad27248c3
@ -1,6 +1,9 @@
|
||||
#version 330 core
|
||||
in vec3 fragColor;
|
||||
out vec4 color;
|
||||
void main() {
|
||||
color = vec4(fragColor, 1.0);
|
||||
}
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 color;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(color, 1.0);
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
#version 330 core
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in vec3 color;
|
||||
out vec3 fragColor;
|
||||
layout(location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
void main() {
|
||||
fragColor = color;
|
||||
gl_Position = projection * view * model * vec4(position, 1.0);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||
}
|
||||
|
@ -3,6 +3,10 @@
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#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 windowHeight = 800;
|
||||
@ -15,6 +19,8 @@ struct Ball {
|
||||
|
||||
// Ustawienia rzutu ukośnego dla każdej kuli (kąt startowy, prędkość początkowa)
|
||||
std::vector<Ball> balls;
|
||||
GLuint shaderProgram;
|
||||
GLuint VAO, VBO;
|
||||
|
||||
void initializeBalls() {
|
||||
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
|
||||
void rungeKuttaStep(Ball &ball, float dt) {
|
||||
float k1vx = -0.1f * ball.vx;
|
||||
@ -52,12 +63,16 @@ void rungeKuttaStep(Ball &ball, float 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) {
|
||||
float V = i / (float) stacks;
|
||||
float phi = V * M_PI;
|
||||
|
||||
glBegin(GL_TRIANGLE_STRIP);
|
||||
for (int j = 0; j <= slices; ++j) {
|
||||
float U = j / (float) slices;
|
||||
float theta = U * (M_PI * 2);
|
||||
@ -66,24 +81,74 @@ void drawSphere(float radius, int slices, int stacks) {
|
||||
float y = cosf(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() {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
gluLookAt(30, 0, 40, 15, 0, 0, 0, 1, 0);
|
||||
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));
|
||||
|
||||
for (const auto &ball : balls) {
|
||||
glPushMatrix();
|
||||
glTranslatef(ball.x, ball.y, ball.z);
|
||||
glColor3f(ball.r, ball.g, ball.b);
|
||||
drawSphere(0.5, 20, 20); // Zastąpienie glutSolidSphere funkcją drawSphere
|
||||
glPopMatrix();
|
||||
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();
|
||||
}
|
||||
|
||||
glfwSwapBuffers(glfwGetCurrentContext());
|
||||
@ -93,18 +158,15 @@ void update() {
|
||||
float dt = 0.01f;
|
||||
for (auto &ball : balls) {
|
||||
rungeKuttaStep(ball, dt);
|
||||
// Diagnostyka aktualizacji pozycji
|
||||
std::cout << "Ball position: (" << ball.x << ", " << ball.y << ", " << ball.z << ")\n";
|
||||
}
|
||||
}
|
||||
|
||||
void setupOpenGL() {
|
||||
compileShaders();
|
||||
setupSphereBuffers();
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
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) {
|
||||
@ -113,7 +175,6 @@ void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods
|
||||
case GLFW_KEY_ESCAPE:
|
||||
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
||||
break;
|
||||
// Dodaj inne przypadki obsługi klawiszy tutaj
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -124,6 +185,11 @@ int main() {
|
||||
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);
|
||||
if (!window) {
|
||||
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||
@ -149,6 +215,10 @@ int main() {
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteProgram(shaderProgram);
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user