Compare commits
5 Commits
3cdac91721
...
84061f9577
Author | SHA1 | Date |
---|---|---|
Kamil Ryżek | 84061f9577 | |
Kamil Ryżek | 9884610edc | |
Kamil Ryżek | 82a1d0ce8a | |
Kamil Ryżek | 83173bfbdd | |
Kamil Ryżek | 476427bce3 |
|
@ -46,6 +46,8 @@ namespace texture {
|
|||
|
||||
GLuint grid;
|
||||
|
||||
GLuint gameOverSprite;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -68,13 +70,16 @@ Core::RenderContext cylinderContext;
|
|||
glm::vec3 cameraPos = glm::vec3(-4.f, 0, 0);
|
||||
glm::vec3 cameraDir = glm::vec3(1.f, 0, 0.f);
|
||||
|
||||
glm::vec3 spaceshipPos = glm::vec3(-4.f, 0, 0);
|
||||
glm::vec3 spaceshipPos = glm::vec3(-22.f, 0, 0);
|
||||
glm::vec3 spaceshipDir = glm::vec3(1.f, 0.f, 0.f);
|
||||
GLuint VAO, VBO;
|
||||
|
||||
float aspectRatio = 1.f;
|
||||
unsigned int textureID;
|
||||
|
||||
bool gameOver = false;
|
||||
bool gameWon = false;
|
||||
|
||||
const int maxSize = 30;
|
||||
int numberOfCollectedCoins = 0;
|
||||
|
||||
|
@ -92,6 +97,35 @@ float zCordinatesOfCoin[maxSize] = {
|
|||
float xCordinatesOfCollectedCoin[maxSize] = {};
|
||||
float zCordinatesOfCollectedCoin[maxSize] = {};
|
||||
|
||||
float angleSpeed = 0.005f;
|
||||
float moveSpeed = 0.0035f;
|
||||
|
||||
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 colorBuffers[2];
|
||||
unsigned int pingpongFBO[2];
|
||||
|
@ -401,6 +435,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) {
|
||||
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>
|
||||
|
@ -411,7 +446,8 @@ void renderPlanet(float time, float orbitRadius, float orbitRotationSpeed, float
|
|||
}
|
||||
|
||||
void renderPlanets() {
|
||||
float time = glfwGetTime();
|
||||
float time = 0;
|
||||
//float time = glfwGetTime();
|
||||
|
||||
float mercuryOrbitRadius = 1.3f;
|
||||
float venusOrbitRadius = 2.0f;
|
||||
|
@ -472,8 +508,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)
|
||||
{
|
||||
//float time = 0;
|
||||
float time = glfwGetTime();
|
||||
glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
|
@ -489,7 +559,27 @@ void renderScene(GLFWwindow* window)
|
|||
|
||||
|
||||
|
||||
renderPlanets();
|
||||
for (auto& planet : planets) {
|
||||
|
||||
glm::mat4 modelMatrix = glm::rotate(glm::mat4(1.0f), 5*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(2*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));
|
||||
}
|
||||
|
||||
renderCoins();
|
||||
// obliczanie orientacji statku
|
||||
|
@ -517,6 +607,8 @@ void renderScene(GLFWwindow* window)
|
|||
|
||||
|
||||
glUseProgram(0);
|
||||
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
|
@ -625,6 +717,21 @@ void init(GLFWwindow* window)
|
|||
glUseProgram(programBloom);
|
||||
glUniform1i(glGetUniformLocation(programBloom, "scene"), 0);
|
||||
glUniform1i(glGetUniformLocation(programBloom, "bloomBlur"), 1);
|
||||
initializePlanets();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void gameOverScreen(GLFWwindow* window) {
|
||||
if (gameWon) {
|
||||
std::cout << "Congratulations!" << std::endl;
|
||||
}
|
||||
else if (gameOver) {
|
||||
std::cout << "GAME OVER. Number of collected coins: " << numberOfCollectedCoins << std::endl;
|
||||
}
|
||||
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
||||
moveSpeed = 0;
|
||||
angleSpeed = 0;
|
||||
}
|
||||
|
||||
void shutdown(GLFWwindow* window)
|
||||
|
@ -637,8 +744,8 @@ void processInput(GLFWwindow* window)
|
|||
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);
|
||||
|
||||
float angleSpeed = 0.005f;
|
||||
float moveSpeed = 0.0025f;
|
||||
//float angleSpeed = 0.005f;
|
||||
//float moveSpeed = 0.0025f;
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
|
@ -658,16 +765,63 @@ void processInput(GLFWwindow* window)
|
|||
spaceshipDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(spaceshipDir, 0));
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
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;
|
||||
cameraDir = spaceshipDir;
|
||||
}
|
||||
|
||||
void renderLoop(GLFWwindow* window) {
|
||||
|
||||
float timeOfLastUpdate = glfwGetTime();
|
||||
int loopCount = 0;
|
||||
int interval = 2500;
|
||||
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
processInput(window);
|
||||
renderScene(window);
|
||||
|
||||
int index = 0;
|
||||
|
||||
if (numberOfCollectedCoins == 30) {
|
||||
gameWon = true;
|
||||
}
|
||||
|
||||
for (auto& planet : planets) {
|
||||
|
||||
if (checkCollision(spaceshipPos, planet.currentPos, planet.modelScale)) {
|
||||
gameOver = true;
|
||||
//std::cout << "Kolizja statku z planeta " << index << std::endl;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
for (auto& satellite : satellites) {
|
||||
|
||||
if (checkCollision(spaceshipPos, satellite.currentPos, satellite.modelScale)) {
|
||||
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;
|
||||
std::cout << "Coins collected " << numberOfCollectedCoins << std::endl;
|
||||
loopCount = 0;
|
||||
timeOfLastUpdate = glfwGetTime();
|
||||
}
|
||||
|
||||
if (gameOver || gameWon) {
|
||||
gameOverScreen(window);
|
||||
}
|
||||
|
||||
glfwPollEvents();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue