unlogic
Loading...
Searching...
No Matches
Camera.h
Go to the documentation of this file.
1//
2// Created by Nathan on 11/22/2024.
3//
4
5#ifndef CAMERA_H
6#define CAMERA_H
7
8#include <numbers>
9#include <glm/glm.hpp>
10#include <glm/ext.hpp>
11
12namespace unlogic
13{
14 class Camera
15 {
16 alignas(4) float dpi = 1.f;
17 alignas(4) float fov = 45.f;
18
22 alignas(16) glm::vec3 location = {0.f, 0.f, 20.0f};
23 alignas(8) glm::vec2 window = {0.f, 0.f};
24
25 alignas(16) glm::mat4 model = glm::mat4(1.f);
26 alignas(16) glm::mat4 view = glm::mat4(1.f);
27 alignas(16) glm::mat4 projection = glm::mat4(1.f);
28
29 protected:
31 {
32 this->location.z = glm::clamp(this->location.z, 5.f, 100.f);
33
34 // Plots come out of function in world space already.
35 this->model = glm::mat4(1.f);
36
37 this->view = glm::lookAt(this->location, {this->location.x, this->location.y, 0.f}, {0.f, 1.f, 0.f});
38
39 if (this->window.y != 0)
40 {
41 this->projection = glm::perspective(glm::radians(this->fov), this->window.x / this->window.y, 0.1f, 100.f);
42 }
43 }
44
45 public:
46 void SetDPI(float new_dpi)
47 {
48 this->dpi = new_dpi;
49 this->CalculateMVP();
50 }
51
52 void SetWindowSize(glm::vec2 const &new_window)
53 {
54 this->window = new_window;
55 this->CalculateMVP();
56 }
57
58 [[nodiscard]] glm::vec2 GetWindowSize() const
59 {
60 return this->window;
61 }
62
63 void TranslatePixel(glm::vec2 const &distance, float d = 0.f)
64 {
65 auto world_translation = glm::vec3((distance / this->window) * this->WorldDimensionsAtDepth(d), 0.f) * -1.f;
66 this->location = glm::translate(glm::mat4(1.f), world_translation) * glm::vec4(this->location, 1.f);
67 this->CalculateMVP();
68 }
69
70 void TranslateWorld(glm::vec3 const &translation)
71 {
72 this->location = glm::translate(glm::mat4(1.f), translation) * glm::vec4(this->location, 1.f);
73 this->CalculateMVP();
74 }
75
76 [[nodiscard]] glm::vec2 WorldDimensionsAtDepth(float d = 0.f) const
77 {
78 float distance = this->location.z + d;
79 float alpha = (180.f - this->fov) / 2;
80 float base = (distance / std::tan(alpha * (std::numbers::pi / 180.f))) * 2;
81
82 return {
83 base * (this->window.x / this->window.y),
84 base,
85 };
86 }
87
88 [[nodiscard]] glm::vec3 ScreenToWorldCoordinate(glm::vec3 const &screen) const {}
89
90 [[nodiscard]] glm::vec3 WorldToScreenCoordinate(glm::vec3 const &world) const {}
91 };
92} // namespace unlogic
93
94#endif // CAMERA_H
void SetWindowSize(glm::vec2 const &new_window)
Definition Camera.h:52
glm::vec3 ScreenToWorldCoordinate(glm::vec3 const &screen) const
Definition Camera.h:88
glm::vec2 WorldDimensionsAtDepth(float d=0.f) const
Definition Camera.h:76
void SetDPI(float new_dpi)
Definition Camera.h:46
void TranslateWorld(glm::vec3 const &translation)
Definition Camera.h:70
void TranslatePixel(glm::vec2 const &distance, float d=0.f)
Definition Camera.h:63
void CalculateMVP()
Definition Camera.h:30
glm::vec2 GetWindowSize() const
Definition Camera.h:58
glm::vec3 WorldToScreenCoordinate(glm::vec3 const &world) const
Definition Camera.h:90