merge collision do master

This commit is contained in:
Kamil Ryżek 2024-03-01 17:41:22 +01:00
commit 9884610edc

View File

@ -46,6 +46,8 @@ namespace texture {
GLuint grid; GLuint grid;
GLuint gameOverSprite;
} }
@ -92,6 +94,35 @@ float zCordinatesOfCoin[maxSize] = {
float xCordinatesOfCollectedCoin[maxSize] = {}; float xCordinatesOfCollectedCoin[maxSize] = {};
float zCordinatesOfCollectedCoin[maxSize] = {}; float zCordinatesOfCollectedCoin[maxSize] = {};
float angleSpeed = 0.005f;
float moveSpeed = 0.0025f;
struct Planet {
glm::vec3 currentPos;
glm::vec3 modelScale;
float rotationSpeed; // ruch obrotowy
float orbitSpeed; // ruch obiegowy
float orbitRadius;
GLuint texture;
GLuint normalTexture;
};
struct Satellite {
glm::vec3 currentPos;
glm::vec3 modelScale;
float rotationSpeed; // ruch obrotowy
float orbitSpeed; // ruch obiegowy
float orbitRadius;
GLuint texture;
GLuint normalTexture;
Planet parentPlanet;
};
std::vector<Planet> planets;
std::vector<Satellite> satellites;
unsigned int hdrFBO; unsigned int hdrFBO;
unsigned int colorBuffers[2]; unsigned int colorBuffers[2];
unsigned int pingpongFBO[2]; unsigned int pingpongFBO[2];
@ -401,6 +432,7 @@ void drawSpaceShip(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint t
} }
void renderPlanet(float time, float orbitRadius, float orbitRotationSpeed, float selfRotationSpeed, float scale, GLuint planetTexture, GLuint normalTexture) { void renderPlanet(float time, float orbitRadius, float orbitRotationSpeed, float selfRotationSpeed, float scale, GLuint planetTexture, GLuint normalTexture) {
glm::mat4 planetTransform = glm::rotate(glm::mat4(1.0f), time * orbitRotationSpeed, glm::vec3(0, 1, 0)) // orbitowanie dooko<6B>a s<>o<EFBFBD>ca glm::mat4 planetTransform = glm::rotate(glm::mat4(1.0f), time * orbitRotationSpeed, glm::vec3(0, 1, 0)) // orbitowanie dooko<6B>a s<>o<EFBFBD>ca
* glm::translate(glm::vec3(orbitRadius, 0, 0)) // translacja na odp. odleg<65>o<EFBFBD><6F> * glm::translate(glm::vec3(orbitRadius, 0, 0)) // translacja na odp. odleg<65>o<EFBFBD><6F>
@ -411,7 +443,8 @@ void renderPlanet(float time, float orbitRadius, float orbitRotationSpeed, float
} }
void renderPlanets() { void renderPlanets() {
float time = glfwGetTime(); float time = 0;
//float time = glfwGetTime();
float mercuryOrbitRadius = 1.3f; float mercuryOrbitRadius = 1.3f;
float venusOrbitRadius = 2.0f; float venusOrbitRadius = 2.0f;
@ -472,8 +505,42 @@ void renderPlanets() {
} }
void initializePlanets() {
planets.push_back(Planet{ glm::vec3(2.0f, 0, 0), glm::vec3(0.11f), 1 / 59.0f, 1.0f / 2, 2.0f, texture::mercury, texture::mercuryNormal });
planets.push_back(Planet{ glm::vec3(3.5f, 0, 0), glm::vec3(0.29f), 1 / 243.0f, 1.0f / 5, 3.5f, texture::venus, texture::venusNormal });
planets.push_back(Planet{ glm::vec3(5.0f, 0, 0), glm::vec3(0.3f), 1.f, 1.0f / 6, 5.0f, texture::earth, texture::earthNormal });
planets.push_back(Planet{ glm::vec3(7.5f, 0, 0), glm::vec3(0.2f), 1.f, 1.0f / 7, 7.5f, texture::mars, texture::marsNormal });
planets.push_back(Planet{ glm::vec3(10.f, 0, 0), glm::vec3(1.f), 2.4f, 1.0f / 9, 10.f, texture::jupiter, texture::jupiterNormal });
planets.push_back(Planet{ glm::vec3(12.5f, 0, 0), glm::vec3(0.7f), 2.4f, 1.0f / 10, 12.5f, texture::saturn, texture::saturnNormal });
planets.push_back(Planet{ glm::vec3(15.f, 0, 0), glm::vec3(1.f), 1.7f, 1.0f / 15, 15.f, texture::uranus, texture::uranusNormal });
planets.push_back(Planet{ glm::vec3(20.f, 0, 0), glm::vec3(1.f), 1.2f, 1.0f / 24, 20.f, texture::neptune, texture::neptuneNormal });
planets.push_back(Planet{ glm::vec3(0, 0, 0), glm::vec3(1.f), 0.f, 0.f, 0.f, texture::sun, texture::sun });
satellites.push_back(Satellite{ glm::vec3(5.0f, 0, 0), glm::vec3(0.055f), 1.f / 2, 1.0f / 2, 0.8f, texture::moon, texture::asteroidNormal, planets[2] });
}
void printPlanetPos() {
int index = 0;
for (auto& planet : planets) {
std::cout << index << " " << glm::to_string(planet.currentPos) << std::endl;
++index;
}
}
bool checkCollision(glm::vec3 spaceshipPos, glm::vec3 planetPos, glm::vec3 planetScale) {
float planetRadius = planetScale.x;
float shipSize = 0.5f;
float distanceSquared = glm::distance2(spaceshipPos, planetPos);
float distance = glm::length(planetPos - spaceshipPos);
float minDistance = planetRadius + shipSize;
return distanceSquared <= (planetRadius + shipSize) * (planetRadius + shipSize);
}
void renderScene(GLFWwindow* window) void renderScene(GLFWwindow* window)
{ {
//float time = 0;
float time = glfwGetTime();
glClearColor(0.0f, 0.3f, 0.3f, 1.0f); glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -487,9 +554,43 @@ void renderScene(GLFWwindow* window)
drawSun(sphereContext, glm::mat4(), texture::sun); drawSun(sphereContext, glm::mat4(), texture::sun);
//rendering all the planets
//renderPlanets();
/*glm::mat4 modelMatrix = glm::eulerAngleY(time / planet.rotationSpeed)
* glm::translate(glm::vec3(planet.orbitRadius, 0, 0))
* glm::eulerAngleY(time)
* glm::scale(planet.modelScale);*/
renderPlanets(); for (auto& planet : planets) {
glm::mat4 modelMatrix = glm::rotate(glm::mat4(1.0f), time * planet.orbitSpeed, glm::vec3(0, 1, 0)) // orbitowanie dooko³a s³oñca
* glm::translate(glm::vec3(planet.orbitRadius, 0, 0)) // translacja na odp. odleg³oœæ
* glm::rotate(glm::mat4(1.0f), time * planet.rotationSpeed, glm::vec3(0, 1, 0)) //obrót planety wokó³ w³asnej osi
* glm::scale(glm::vec3(planet.modelScale)); //skalowanie planety
drawObjectTexture(sphereContext, modelMatrix, planet.texture, planet.normalTexture);
planet.currentPos = glm::vec3(modelMatrix * glm::vec4(0, 0, 0, 1.0f));
}
for (auto& satellite : satellites) {
glm::mat4 modelMatrix = glm::eulerAngleY(time * satellite.parentPlanet.orbitSpeed)
* glm::translate(glm::vec3(satellite.parentPlanet.orbitRadius, 0, 0))
* glm::eulerAngleY(time * satellite.rotationSpeed)
* glm::translate(glm::vec3(satellite.orbitRadius, 0, 0))
* glm::scale(glm::vec3(satellite.modelScale));
drawObjectTexture(sphereContext, modelMatrix, satellite.texture, satellite.normalTexture);
satellite.currentPos = glm::vec3(modelMatrix * glm::vec4(0, 0, 0, 1.0f));
}
/*drawObjectTexture(sphereContext,
glm::eulerAngleY(time * planets[2].orbitSpeed) * glm::translate(glm::vec3(planets[2].orbitRadius, 0, 0)) * glm::eulerAngleY(time * 0.5f) * glm::translate(glm::vec3(0.8f, 0, 0)) * glm::scale(glm::vec3(0.055f)),
texture::moon,
texture::asteroidNormal);*/
//renderPlanets();
renderCoins(); renderCoins();
// obliczanie orientacji statku // obliczanie orientacji statku
@ -517,6 +618,8 @@ void renderScene(GLFWwindow* window)
glUseProgram(0); glUseProgram(0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
void framebuffer_size_callback(GLFWwindow* window, int width, int height) void framebuffer_size_callback(GLFWwindow* window, int width, int height)
@ -625,6 +728,14 @@ void init(GLFWwindow* window)
glUseProgram(programBloom); glUseProgram(programBloom);
glUniform1i(glGetUniformLocation(programBloom, "scene"), 0); glUniform1i(glGetUniformLocation(programBloom, "scene"), 0);
glUniform1i(glGetUniformLocation(programBloom, "bloomBlur"), 1); glUniform1i(glGetUniformLocation(programBloom, "bloomBlur"), 1);
initializePlanets();
}
void gameOverScreen(GLFWwindow* window) {
moveSpeed = 0;
angleSpeed = 0;
} }
void shutdown(GLFWwindow* window) void shutdown(GLFWwindow* window)
@ -637,8 +748,8 @@ void processInput(GLFWwindow* window)
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f))); glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 spaceshipUp = glm::vec3(0.f, 1.f, 0.f); glm::vec3 spaceshipUp = glm::vec3(0.f, 1.f, 0.f);
float angleSpeed = 0.005f; //float angleSpeed = 0.005f;
float moveSpeed = 0.0025f; //float moveSpeed = 0.0025f;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
} }
@ -658,16 +769,59 @@ void processInput(GLFWwindow* window)
spaceshipDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceshipDir, 0)); spaceshipDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceshipDir, 0));
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
spaceshipDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(spaceshipDir, 0)); spaceshipDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(spaceshipDir, 0));
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
spaceshipPos += spaceshipDir * 5*moveSpeed;
cameraPos = spaceshipPos - 1.5 * spaceshipDir + glm::vec3(0, 1, 0) * 0.5f; cameraPos = spaceshipPos - 1.5 * spaceshipDir + glm::vec3(0, 1, 0) * 0.5f;
cameraDir = spaceshipDir; cameraDir = spaceshipDir;
} }
void renderLoop(GLFWwindow* window) { void renderLoop(GLFWwindow* window) {
float timeOfLastUpdate = glfwGetTime();
int loopCount = 0;
int interval = 2500;
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
processInput(window); processInput(window);
renderScene(window); renderScene(window);
int index = 0;
for (auto& planet : planets) {
if (checkCollision(spaceshipPos, planet.currentPos, planet.modelScale)) {
//placeholder
gameOver = true;
std::cout << "Kolizja statku z planeta " << index << std::endl;
}
++index;
}
for (auto& satellite : satellites) {
if (checkCollision(spaceshipPos, satellite.currentPos, satellite.modelScale)) {
//placeholder
gameOver = true;
std::cout << "Kolizja statku z satelita " << index << std::endl;
}
++index;
}
loopCount++;
if (loopCount >= interval) {
printPlanetPos();
std::cout << "Pozycja statku " << glm::to_string(spaceshipPos) << std::endl;
loopCount = 0;
timeOfLastUpdate = glfwGetTime();
}
if (gameOver) {
gameOverScreen(window);
}
glfwPollEvents(); glfwPollEvents();
} }
} }