From b428efe1a36d57b34075da389ed577828bd717ba Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Mon, 24 Jun 2024 15:30:14 +0200 Subject: [PATCH] smooth camera travel --- src/main.c | 76 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/src/main.c b/src/main.c index 069ce03..840af98 100644 --- a/src/main.c +++ b/src/main.c @@ -45,14 +45,36 @@ int framesSinceUpdate = 0; GLfloat step = 0.0f; const GLfloat pi = 3.14159f; -vec3 cameraPosition = {0.0f, 3.0f, 5.5f}; +vec3 cameraPosition = {0.0f, 2.0f, 1.0f}; vec3 objectPosition = {0.0f, 0.0f, 0.0f}; + +vec3 cameraTravelPosition = {0.0f, 3.0f, 5.5f}; +bool cameraTraveling = true; + GLfloat radius = 1.0f; mat4 viewingTransformation; // Define a global scene graph root node SceneNode* rootNode; +void handleCameraTravel() { + if (cameraTraveling) { + vec3 delta; + + vec3Subtract(&delta, &cameraTravelPosition, &cameraPosition); + + if (vec3Length(&delta) < 0.0001) cameraTraveling = false; + + vec3Multiply(&delta, &delta, 0.1); + vec3Add(&cameraPosition, &cameraPosition, &delta); + } +} + +void travelCamera(vec3 newPosition) { + cameraTravelPosition = newPosition; + cameraTraveling = true; +} + /** * Input handler for camera movement. * */ @@ -60,40 +82,42 @@ void handleInputs(double deltaTime) { assert(window != NULL); if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) { - cameraPosition = (vec3){0.0f, 1.7f, 2.4f}; + travelCamera((vec3){0.0f, 1.7f, 2.4f}); } if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) { - cameraPosition = (vec3){3.3f, 3.4f, -11.0f}; + travelCamera((vec3){3.3f, 3.4f, -11.0f}); } if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) { - cameraPosition = (vec3){-3.0f, 2.9f, -7.5f}; + travelCamera((vec3){-3.0f, 2.9f, -7.5f}); } if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS) { - cameraPosition = (vec3){-0.6f, 2.1f, -4.5f}; + travelCamera((vec3){-0.6f, 2.1f, -4.5f}); } if (glfwGetKey(window, GLFW_KEY_5) == GLFW_PRESS) { - cameraPosition = (vec3){-10.6f, 22.1f, -4.5f}; + travelCamera((vec3){-10.6f, 22.1f, -4.5f}); } if (glfwGetKey(window, GLFW_KEY_6) == GLFW_PRESS) { - cameraPosition = (vec3){3.574698f, 0.966039f, 3.852249f}; + travelCamera((vec3){3.574698f, 0.966039f, 3.852249f}); } - if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { - cameraPosition.z += deltaTime * 10; - } - if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { - cameraPosition.z -= deltaTime * 10; - } - if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { - cameraPosition.x += deltaTime * 10; - } - if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { - cameraPosition.x -= deltaTime * 10; - } - if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS) { - cameraPosition.y += deltaTime * 10; - } - if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) { - cameraPosition.y -= deltaTime * 10; + if (!cameraTraveling) { + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { + cameraPosition.z += deltaTime * 10; + } + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { + cameraPosition.z -= deltaTime * 10; + } + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { + cameraPosition.x += deltaTime * 10; + } + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { + cameraPosition.x -= deltaTime * 10; + } + if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS) { + cameraPosition.y += deltaTime * 10; + } + if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) { + cameraPosition.y -= deltaTime * 10; + } } if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) { @@ -251,7 +275,7 @@ void init(void) { void updateStats() { printf("\rFPS: %.1f", framesSinceUpdate / timeSinceUpdate); - printf(" - Camera Position: [%f, %f, %f]", cameraPosition.x, cameraPosition.y, cameraPosition.z); + printf(" - Camera Position: [%f, %f, %f] - Traveling: %s ", cameraPosition.x, cameraPosition.y, cameraPosition.z, cameraTraveling ? "true" : "false"); fflush(stdout); } @@ -260,6 +284,8 @@ void updateStats() { */ void draw(void) { + handleCameraTravel(); + // FPS Counter framesSinceUpdate++; double deltaTime = glfwGetTime();