From 59c4aafb60891b229cb5f09739c464c777059f97 Mon Sep 17 00:00:00 2001 From: Joey de Vries Date: Tue, 12 May 2020 15:15:57 +0200 Subject: [PATCH] Add camera exercise solutions to repo. --- .../7.5.camera_exercise1/camera_exercise1.cpp | 20 +++++++++++ .../7.5.camera_exercise2/camera_exercise2.cpp | 36 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/1.getting_started/7.5.camera_exercise1/camera_exercise1.cpp create mode 100644 src/1.getting_started/7.5.camera_exercise2/camera_exercise2.cpp diff --git a/src/1.getting_started/7.5.camera_exercise1/camera_exercise1.cpp b/src/1.getting_started/7.5.camera_exercise1/camera_exercise1.cpp new file mode 100644 index 0000000..c1f203a --- /dev/null +++ b/src/1.getting_started/7.5.camera_exercise1/camera_exercise1.cpp @@ -0,0 +1,20 @@ +// This function is found in the camera class. What we basically do is keep the y position value at 0.0f to force our +// user to stick to the ground. + +[...] +// processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems) +void ProcessKeyboard(Camera_Movement direction, float deltaTime) +{ + float velocity = MovementSpeed * deltaTime; + if (direction == FORWARD) + Position += Front * velocity; + if (direction == BACKWARD) + Position -= Front * velocity; + if (direction == LEFT) + Position -= Right * velocity; + if (direction == RIGHT) + Position += Right * velocity; + // make sure the user stays at the ground level + Position.y = 0.0f; // <-- this one-liner keeps the user at the ground level (xz plane) +} +[...] \ No newline at end of file diff --git a/src/1.getting_started/7.5.camera_exercise2/camera_exercise2.cpp b/src/1.getting_started/7.5.camera_exercise2/camera_exercise2.cpp new file mode 100644 index 0000000..a8768e8 --- /dev/null +++ b/src/1.getting_started/7.5.camera_exercise2/camera_exercise2.cpp @@ -0,0 +1,36 @@ +// Custom implementation of the LookAt function +glm::mat4 calculate_lookAt_matrix(glm::vec3 position, glm::vec3 target, glm::vec3 worldUp) +{ + // 1. Position = known + // 2. Calculate cameraDirection + glm::vec3 zaxis = glm::normalize(position - target); + // 3. Get positive right axis vector + glm::vec3 xaxis = glm::normalize(glm::cross(glm::normalize(worldUp), zaxis)); + // 4. Calculate camera up vector + glm::vec3 yaxis = glm::cross(zaxis, xaxis); + + // Create translation and rotation matrix + // In glm we access elements as mat[col][row] due to column-major layout + glm::mat4 translation = glm::mat4(1.0f); // Identity matrix by default + translation[3][0] = -position.x; // Third column, first row + translation[3][1] = -position.y; + translation[3][2] = -position.z; + glm::mat4 rotation = glm::mat4(1.0f); + rotation[0][0] = xaxis.x; // First column, first row + rotation[1][0] = xaxis.y; + rotation[2][0] = xaxis.z; + rotation[0][1] = yaxis.x; // First column, second row + rotation[1][1] = yaxis.y; + rotation[2][1] = yaxis.z; + rotation[0][2] = zaxis.x; // First column, third row + rotation[1][2] = zaxis.y; + rotation[2][2] = zaxis.z; + + // Return lookAt matrix as combination of translation and rotation matrix + return rotation * translation; // Remember to read from right to left (first translation then rotation) +} + + +// Don't forget to replace glm::lookAt with your own version +// view = glm::lookAt(glm::vec3(camX, 0.0f, camZ), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); +view = calculate_lookAt_matrix(glm::vec3(camX, 0.0f, camZ), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); \ No newline at end of file