From fb891fa274e5b4b3ca04235ba9984e09379a2ecd Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 30 Nov 2023 18:40:28 -0500 Subject: [PATCH] Arbitrary starting camera position --- imgui.ini | 6 +++--- include/Camera.h | 26 ++++++++++++++++++-------- src/Application.cpp | 4 ++-- src/Camera.cpp | 12 +++++++++--- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/imgui.ini b/imgui.ini index d30a096..3276003 100644 --- a/imgui.ini +++ b/imgui.ini @@ -16,7 +16,7 @@ DockId=0x00000001,0 [Window][Object Controls] Pos=1532,124 -Size=388,956 +Size=388,937 Collapsed=0 DockId=0x00000004,0 @@ -26,7 +26,7 @@ Collapsed=0 [Window][DockSpace Demo] Pos=0,0 -Size=1920,1080 +Size=1920,1061 Collapsed=0 [Window][Dear ImGui Demo] @@ -35,7 +35,7 @@ Size=1553,966 Collapsed=0 [Docking][Data] -DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,19 Size=1920,1061 Split=Y +DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,19 Size=1920,1042 Split=Y DockNode ID=0x00000001 Parent=0x3BC79352 SizeRef=1904,103 Selected=0x8CCBC963 DockNode ID=0x00000002 Parent=0x3BC79352 SizeRef=1904,956 Split=X DockNode ID=0x00000003 Parent=0x00000002 SizeRef=1530,956 CentralNode=1 diff --git a/include/Camera.h b/include/Camera.h index 25e738a..05f9e08 100644 --- a/include/Camera.h +++ b/include/Camera.h @@ -5,10 +5,13 @@ class Camera { private: - glm::vec3 m_position = { 0.f, 0.f, 1.f }; + glm::vec3 m_position = { -1.f, 1.f, 1.f }; glm::vec3 m_front = { 0.f, 0.f, 0.f }; - glm::vec3 m_up = { 0.f, 1.f, 0.f }; - glm::vec3 m_right = { 1.f, 0.f, 0.f }; + glm::vec3 m_right = glm::normalize(glm::cross(m_position - m_front, glm::vec3(0.f, 1.f, 0.f))); + glm::vec3 m_up = glm::normalize(glm::cross(m_position - m_front, m_position - m_right)); + + float m_yaw = -44.977; // atan2(m_position.x, m_position.z) assuming m_position = {-1, 1, 1} + float m_pitch = 35.264; // asin(m_position.y) assuming m_position = {-1, 1, 1} public: // Since all variables are dependent on each other I've decided to @@ -17,12 +20,19 @@ public: // glm::vec3& front() { return m_front; } // glm::vec3& up() { return m_up; } // glm::vec3& right() { return m_right; } - - const glm::vec3& position() const { return m_position; } - const glm::vec3& front() const { return m_front; } - const glm::vec3& up() const { return m_up; } + // + // float& yaw() { return m_yaw; } + // float& pitch() { return m_pitch; } + + const glm::vec3& position() const { return m_position; } + const glm::vec3& front() const { return m_front; } + const glm::vec3& up() const { return m_up; } const glm::vec3& right() const { return m_right; } - + + const float& yaw() const { return m_yaw; } + const float pitch() const { return m_pitch; } + + void pan(float dist_x, float dist_y); void orbit(float yaw, float pitch); void zoom(float dist); diff --git a/src/Application.cpp b/src/Application.cpp index 8553484..dfc1d0e 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -48,8 +48,8 @@ namespace scene float angle = glm::pi() / 2.0f; - float yaw = -90.f; - float pitch = 0.f; + float yaw = camera.yaw(); // Initial yaw for position (-1, 1, 1) + float pitch = camera.pitch(); // Initial pitch for position (-1, 1, 1) std::vector primitiveData; std::vector lightData; diff --git a/src/Camera.cpp b/src/Camera.cpp index b0ce3be..c4af49f 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -1,6 +1,7 @@ #include "Camera.h" -void Camera::pan(float dist_x, float dist_y) { +void Camera::pan(float dist_x, float dist_y) +{ m_front += m_right * dist_x; m_position += m_right * dist_x; @@ -8,7 +9,11 @@ void Camera::pan(float dist_x, float dist_y) { m_position += m_up * dist_y; } -void Camera::orbit(float yaw, float pitch) { +void Camera::orbit(float yaw, float pitch) +{ + m_yaw = yaw; + m_pitch = pitch; + glm::vec3 direction; direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); @@ -23,7 +28,8 @@ void Camera::orbit(float yaw, float pitch) { m_up = glm::cross(m_right, glm::normalize(m_front - m_position)); } -void Camera::zoom(float dist) { +void Camera::zoom(float dist) +{ m_position += (m_position - m_front) * dist; if (glm::length(m_position - m_front) > 100.f) { m_position = glm::normalize(m_position) * 100.f + m_front;