Arbitrary starting camera position

This commit is contained in:
Jack Christensen 2023-11-30 18:40:28 -05:00
parent 679589554e
commit fb891fa274
4 changed files with 32 additions and 16 deletions

View File

@ -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

View File

@ -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; }
//
// 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);

View File

@ -48,8 +48,8 @@ namespace scene
float angle = glm::pi<float>() / 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<Primitive> primitiveData;
std::vector<Light> lightData;

View File

@ -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;