1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #pragma once #ifndef CAMERA_H #define CAMERA_H
#include "Math.h"
class Camera { public:
glm::vec3 Position; glm::vec3 Front; glm::vec3 Up; glm::vec3 Right; glm::vec3 WorldUp; float Fov; float Aspect; float Near; float Far; float Pitch; float Yaw;
Camera( glm::vec3 position = glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3 lookat = glm::vec3(0.0f, 0.0f, 0.0f), float fov = 60.0f, int w = 800, int h = 600, float n = 0.3f, float f = 100 ) : Position(position), WorldUp(up), Fov(glm::radians(fov)), Aspect((float)w / h), Pitch(0), Yaw(0), Near(n), Far(f) { Front = glm::normalize(lookat - Position); Right = glm::normalize(glm::cross(Front, WorldUp)); Up = glm::normalize(glm::cross(Right, Front));
glm::vec3 WorldFront(0, 0, -1); glm::vec3 FrontXZ = glm::normalize(glm::vec3(Front.x, 0, Front.z)); float yd = glm::dot(WorldFront, FrontXZ); float pd = glm::dot(Front, FrontXZ); if (yd > 1.0) yd = 1.0; if (yd < -1) yd = -1.0; if (pd > 1.0) pd = 1.0; if (pd < -1) pd = -1.0; Yaw = glm::degrees(acos(yd)); Pitch = glm::degrees(acos(pd)); }
glm::mat4 ViewMatrix() { return GetViewMatrix(Position, Front, Right, Up); }
glm::mat4 PerspectiveMatrix() { return GetPerspectiveMatrix(Fov, Aspect, Near, Far); }
void UpdateFov(float fov = 60.0f) { Fov = glm::radians(fov); }
void UpdateAspect(int w, int h) { Aspect = (float)w / h; }
void RotatePitch(float angle) { Pitch += angle; if (Pitch > 89.0) Pitch = 89.0; if (Pitch < -89.0) Pitch = -89.0; UpdateCameraVectors(); } void RotateYaw(float angle) { Yaw += angle; if (Yaw > 360) Yaw = 0; if (Yaw < 0) Yaw = 360; UpdateCameraVectors(); }
private: void UpdateCameraVectors() { glm::vec3 front; front.x = -sin(glm::radians(Yaw)) * cos(glm::radians(Pitch)); front.y = sin(glm::radians(Pitch)); front.z = -cos(glm::radians(Yaw)) * cos(glm::radians(Pitch)); Front = glm::normalize(front); Right = glm::normalize(glm::cross(Front, WorldUp)); Up = glm::normalize(glm::cross(Right, Front)); } };
#endif
|