ruch kamerą, wielkość i obrót planety

This commit is contained in:
K4RP4T 2024-01-18 20:18:10 +01:00
parent 1c4579f1d6
commit 15c62bc5ce

View File

@ -34,9 +34,14 @@ Core::Shader_Loader shaderLoader;
Core::RenderContext sphereContext; Core::RenderContext sphereContext;
glm::vec3 sunPosition = glm::vec3(12.f, 0.f, 12.f); glm::vec3 sunPos = glm::vec3(12.f, 0.f, 12.f);
float sunSize = 0.04f;
glm::vec3 cameraPos = glm::vec3(-1.f, 0.f, 0.f); glm::vec3 planetPos = glm::vec3(0.f, 0.f, 0.f);
float planetSize = 0.005f;
float planetRot = 0.f;
glm::vec3 cameraPos = glm::vec3(-2.f, 0.f, 0.f);
glm::vec3 cameraDir = glm::vec3(1.f, 0.f, 0.f); glm::vec3 cameraDir = glm::vec3(1.f, 0.f, 0.f);
GLuint VAO, VBO; GLuint VAO, VBO;
@ -143,7 +148,7 @@ void drawObjectTexture(Core::RenderContext& context, glm::mat4 modelMatrix, GLui
glm::mat4 transformation = viewProjectionMatrix * modelMatrix; glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(programTex, "transformation"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(programTex, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(programTex, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); glUniformMatrix4fv(glGetUniformLocation(programTex, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniform3f(glGetUniformLocation(programTex, "lightPos"), sunPosition.x, sunPosition.y, sunPosition.z); glUniform3f(glGetUniformLocation(programTex, "lightPos"), sunPos.x, sunPos.y, sunPos.z);
glUniform3f(glGetUniformLocation(programTex, "lightColor"), lightColor.x, lightColor.y, lightColor.z); glUniform3f(glGetUniformLocation(programTex, "lightColor"), lightColor.x, lightColor.y, lightColor.z);
Core::DrawContext(context); Core::DrawContext(context);
glUseProgram(0); glUseProgram(0);
@ -156,7 +161,7 @@ void drawSun(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint texture
glm::mat4 transformation = viewProjectionMatrix * modelMatrix; glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(programSun, "transformation"), 1, GL_FALSE, (float*)&transformation); glUniformMatrix4fv(glGetUniformLocation(programSun, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(programSun, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix); glUniformMatrix4fv(glGetUniformLocation(programSun, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniform3f(glGetUniformLocation(programSun, "lightPos"), sunPosition.x, sunPosition.y, sunPosition.z); glUniform3f(glGetUniformLocation(programSun, "lightPos"), sunPos.x, sunPos.y, sunPos.z);
Core::DrawContext(context); Core::DrawContext(context);
glUseProgram(0); glUseProgram(0);
} }
@ -195,17 +200,16 @@ void renderScene(GLFWwindow* window)
{ {
glClearColor(0.0f, 0.0f, 0.15f, 1.0f); glClearColor(0.0f, 0.0f, 0.15f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 transformation;
float time = glfwGetTime(); float time = glfwGetTime();
glm::mat4 sunScale = glm::scale(glm::vec3(0.04f)); glm::mat4 sunScale = glm::scale(glm::vec3(sunSize));
glm::mat4 sunTranslate = glm::translate(sunPosition); glm::mat4 sunTranslate = glm::translate(sunPos);
drawSun(sphereContext, sunTranslate * sunScale, texture::sun); drawSun(sphereContext, sunTranslate * sunScale, texture::sun);
glm::mat4 planetScale = glm::scale(glm::vec3(0.005f)); // model jest bardzo duży, dlatego taka niska skala glm::mat4 planetScale = glm::scale(glm::vec3(planetSize));
glm::mat4 planetRotate = glm::rotate(time * 0.0416f, glm::vec3(0, 1, 0)); // 1 godzina mija w 1 sekundę, obrót z zachodu na wschód glm::mat4 planetRotate = glm::rotate(time * planetRot, glm::vec3(0, 1, 0));
glm::mat4 planetTranslate = glm::translate(glm::vec3(0, 0, 0)); // zostaje na domyślnej pozycji glm::mat4 planetTranslate = glm::translate(planetPos);
drawObjectTexture(sphereContext, planetTranslate * planetRotate * planetScale, texture::earth); drawObjectTexture(sphereContext, planetTranslate * planetRotate * planetScale, texture::earth);
@ -261,28 +265,45 @@ void shutdown(GLFWwindow* window)
//obsluga wejscia //obsluga wejscia
void processInput(GLFWwindow* window) void processInput(GLFWwindow* window)
{ {
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f, 1.f, 0.f))); //wyjście
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
float angleSpeed = 0.002f;
float moveSpeed = 0.004f;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) //ruch kamerą
cameraPos += cameraDir * moveSpeed; glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f, 1.f, 0.f)));
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) float cameraSpeed = 0.01f;
cameraPos -= cameraDir * moveSpeed;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS && glm::length(planetPos - cameraPos) > 0.01f * 150)
cameraPos += cameraDir * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS && glm::length(planetPos - cameraPos) < 0.01f * 400)
cameraPos -= cameraDir * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cameraPos += cameraSide * moveSpeed; cameraPos += cameraSide * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cameraPos -= cameraSide * moveSpeed; cameraPos -= cameraSide * cameraSpeed;
cameraDir = glm::normalize(-cameraPos);
//wielkość planety
float sizeSpeed = 0.00005f;
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS && planetSize < 0.01f)
planetSize += sizeSpeed;
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS && planetSize > 0.001f)
planetSize -= sizeSpeed;
//obrót planety
float rotationSpeed = 0.0025f;
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS && planetRot < 1.f)
planetRot += rotationSpeed;
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS && planetRot > -1.f)
planetRot -= rotationSpeed;
//if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) //if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
// cameraDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(cameraDir, 0)); // cameraDir = glm::vec3(glm::eulerAngleY(angleSpeed) * glm::vec4(cameraDir, 0));
//if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) //if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
// cameraDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(cameraDir, 0)); // cameraDir = glm::vec3(glm::eulerAngleY(-angleSpeed) * glm::vec4(cameraDir, 0));
cameraDir = glm::normalize(-cameraPos);
} }
// funkcja jest glowna petla // funkcja jest glowna petla