dodanie struktury planety, przepisanie renderu jako pętla po planets, wstępne collision detection

This commit is contained in:
Kamil Ryżek 2024-02-29 20:50:27 +01:00
parent 0a5a4c43b2
commit 476427bce3

View File

@ -73,6 +73,20 @@ float aspectRatio = 1.f;
unsigned int textureID;
struct Planet {
glm::vec3 currentPos;
glm::vec3 modelScale;
float rotationSpeed; // ruch obrotowy
float orbitSpeed; // ruch obiegowy
float orbitRadius;
GLuint texture;
GLuint normalTexture;
};
std::vector<Planet> planets;
glm::mat4 createCameraMatrix()
{
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir,glm::vec3(0.f,1.f,0.f)));
@ -191,6 +205,8 @@ 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³a s³oñca
* glm::translate(glm::vec3(orbitRadius, 0, 0)) // translacja na odp. odleg³oœæ
@ -201,7 +217,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;
@ -262,8 +279,41 @@ 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 });
}
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);
@ -274,8 +324,30 @@ void renderScene(GLFWwindow* window)
drawSun(sphereContext, glm::mat4(), texture::sun);
//rendering all the planets
renderPlanets();
//renderPlanets();
/*glm::mat4 modelMatrix = glm::eulerAngleY(time / planet.rotationSpeed)
* glm::translate(glm::vec3(planet.orbitRadius, 0, 0))
* glm::eulerAngleY(time)
* glm::scale(planet.modelScale);*/
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));
}
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);
// obliczanie orientacji statku
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
@ -393,6 +465,8 @@ void init(GLFWwindow* window)
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
//skybox end
initializePlanets();
}
void shutdown(GLFWwindow* window)
@ -427,6 +501,9 @@ 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;
@ -434,12 +511,35 @@ void processInput(GLFWwindow* window)
void renderLoop(GLFWwindow* window) {
double timeOfLastUpdate = glfwGetTime();
int loopCount = 0;
int interval = 2500;
while (!glfwWindowShouldClose(window))
{
processInput(window);
renderScene(window);
for (auto& planet : planets) {
if (checkCollision(spaceshipPos, planet.currentPos, planet.modelScale)) {
//placeholder
std::cout << "Kolizja statku z planeta " << planet.texture << std::endl;
}
}
loopCount++;
if (loopCount >= interval) {
printPlanetPos();
std::cout << "Pozycja statku " << glm::to_string(spaceshipPos) << std::endl;
loopCount = 0;
timeOfLastUpdate = glfwGetTime();
}
glfwPollEvents();
}
}