diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index 9c79c36..10be1eb 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -12,7 +12,7 @@ #include "util/stb_image.h" // Continue: https://learnopengl.com/Getting-started/Camera -// Chapter: Look around +// Chapter: Camera class // // TODO: look at project->properties->VC++ Directories-> change everything to the Environment var // For the "real" Nebulix Engine setup everything so it can be compiled/used with VSCode/CLion and not just VS @@ -38,6 +38,10 @@ vec3 cameraUp = vec3(0.0f, 1.0f, 0.0f); float deltaTime = 0.0f; float lastFrameTime = 0.0f; +float pitch = 0.0f, yaw = -90.0f, fov = 45.0f; +float mouseLastX = WINDOW_WIDTH / 2, mouseLastY = WINDOW_HEIGHT / 2; + +bool firstMouseMove = true; void OnWindowResize(GLFWwindow* window, int width, int height) { @@ -66,10 +70,59 @@ void ProcessInput(GLFWwindow* window) cameraFront += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed * 0.25f; } -void Configure() +void OnMouseMove(GLFWwindow* window, double xpos, double ypos) { + if (firstMouseMove) + { + firstMouseMove = false; + mouseLastX = xpos; + mouseLastY = ypos; + return; + } + + float xoffset = xpos - mouseLastX; + float yoffset = mouseLastY - ypos; // reversed since y-coordinates range from bottom to top + mouseLastX = xpos; + mouseLastY = ypos; + + const float sensitivity = 0.1f; + xoffset *= sensitivity; + yoffset *= sensitivity; + + yaw += xoffset; + pitch += yoffset; + + if (pitch > 89.0f) + pitch = 89.0f; + if (pitch < -89.0f) + pitch = -89.0f; + + glm::vec3 direction; + direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); + direction.y = sin(glm::radians(pitch)); + direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); + cameraFront = glm::normalize(direction); +} + +void OnScroll(GLFWwindow* window, double xoffset, double yoffset) +{ + fov -= (float)yoffset; + if (fov < 1.0f) + fov = 1.0f; + if (fov > 90.0f) + fov = 90.0f; +} + +void Configure(GLFWwindow *window) +{ + OnWindowResize(window, WINDOW_WIDTH, WINDOW_HEIGHT); + glfwSetFramebufferSizeCallback(window, OnWindowResize); + glfwSetCursorPosCallback(window, OnMouseMove); + glfwSetScrollCallback(window, OnScroll); + stbi_set_flip_vertically_on_load(true); // because an image has 0 at top of y axis and opengl expects it to be on the botton, so if this is omitted, the image will be flipped glEnable(GL_DEPTH_TEST); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); } int main() @@ -95,12 +148,9 @@ int main() { std::cerr << "ERROR::GLAD::INITILISE"; return -1; - } + } - OnWindowResize(window, WINDOW_WIDTH, WINDOW_HEIGHT); - glfwSetFramebufferSizeCallback(window, OnWindowResize); - - Configure(); + Configure(window); float vertices[] = { // object coordinates, uv-coordinates -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, @@ -205,7 +255,6 @@ int main() Texture2D containerImage("images/container.jpg"); Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA); - mat4 projectionMatrix = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); // main loop while(!glfwWindowShouldClose(window)) @@ -220,6 +269,7 @@ int main() ProcessInput(window); mat4 viewMatrix = glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp); + mat4 projectionMatrix = glm::perspective(glm::radians(fov), 800.0f / 600.0f, 0.1f, 100.0f); shader->Use(); shader->SetInt("ourTexture1", 0);