Mandrill 2025.6.0
Loading...
Searching...
No Matches
Camera.h
1#pragma once
2
3#include "Common.h"
4
5#include "Buffer.h"
6#include "Descriptor.h"
7#include "Device.h"
8#include "Swapchain.h"
9
10namespace Mandrill
11{
12 struct CameraMatrices {
13 glm::mat4 view;
14 glm::mat4 view_inv;
15 glm::mat4 proj;
16 glm::mat4 proj_inv;
17 };
18
22 class Camera
23 {
24 public:
25 MANDRILL_NON_COPYABLE(Camera)
26
27
33 MANDRILL_API Camera(ptr<Device> pDevice, GLFWwindow* pWindow, ptr<Swapchain> pSwapchain);
34
38 MANDRILL_API ~Camera();
39
44 MANDRILL_API void updateAspectRatio();
45
52 MANDRILL_API void update(float delta, glm::vec2 cursorDelta);
53
58 MANDRILL_API bool isMouseCaptured() const
59 {
60 return mMouseCaptured;
61 }
62
68 MANDRILL_API void captureMouse(bool capture)
69 {
70 mMouseCaptured = capture;
71 }
72
77 MANDRILL_API bool toggleMouseCapture()
78 {
79 mMouseCaptured = !mMouseCaptured;
80 return mMouseCaptured;
81 }
82
87 MANDRILL_API glm::vec3 getPosition() const
88 {
89 return mPosition;
90 }
91
97 MANDRILL_API void setPosition(glm::vec3 pos)
98 {
99 mPosition = pos;
100 }
101
106 MANDRILL_API glm::vec3 getDirection() const
107 {
108 return mDirection;
109 }
110
116 MANDRILL_API void setDirection(glm::vec3 dir)
117 {
118 mDirection = dir;
119 }
120
126 MANDRILL_API void setTarget(glm::vec3 target)
127 {
128 mDirection = glm::normalize(target - mPosition);
129 }
130
136 MANDRILL_API void setUp(glm::vec3 up)
137 {
138 mUp = up;
139 }
140
146 MANDRILL_API void setFov(float fov)
147 {
148 mFov = fov;
149 }
150
156 MANDRILL_API void setMoveSpeed(float speed)
157 {
158 mMoveSpeed = speed;
159 }
160
165 MANDRILL_API glm::mat4 getViewMatrix() const
166 {
167 CameraMatrices* matrices =
168 static_cast<CameraMatrices*>(mpUniforms->getHostMap()) + mpSwapchain->getInFlightIndex();
169 return matrices->view;
170 }
171
176 MANDRILL_API glm::mat4 getProjectionMatrix() const
177 {
178 CameraMatrices* matrices =
179 static_cast<CameraMatrices*>(mpUniforms->getHostMap()) + mpSwapchain->getInFlightIndex();
180 return matrices->proj;
181 }
182
187 MANDRILL_API ptr<Descriptor> getDescriptor() const
188 {
189 return mpDescriptor;
190 }
191
197 VkWriteDescriptorSet getWriteDescriptor(uint32_t binding) const
198 {
199 VkWriteDescriptorSet write = {
200 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
201 .dstBinding = binding,
202 .dstArrayElement = 0,
203 .descriptorCount = 1,
204 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
205 .pBufferInfo = &mBufferInfo,
206 };
207
208 return write;
209 }
210
211 private:
212 ptr<Device> mpDevice;
213 GLFWwindow* mpWindow;
214 ptr<Swapchain> mpSwapchain;
215
216 bool mMouseCaptured = false;
217
218 float mAspect;
219 float mNear, mFar;
220 float mFov;
221 glm::vec3 mPosition;
222 glm::vec3 mDirection;
223 glm::vec3 mUp;
224 float mMoveSpeed;
225
226 ptr<Buffer> mpUniforms;
227 ptr<Descriptor> mpDescriptor;
228 VkDescriptorSetLayout mLayout;
229 VkDescriptorBufferInfo mBufferInfo;
230 };
231} // namespace Mandrill
Camera class for managing camera properties and transformations in a 3D scene.
Definition Camera.h:23
MANDRILL_API glm::vec3 getPosition() const
Get the position of the camera.
Definition Camera.h:87
MANDRILL_API void setUp(glm::vec3 up)
Set the up direction of the camera.
Definition Camera.h:136
MANDRILL_API void setPosition(glm::vec3 pos)
Set the position of the camera.
Definition Camera.h:97
MANDRILL_API void updateAspectRatio()
Update the aspect ratio that is used for the camera matrix. Call this if the window size changes.
Definition Camera.cpp:68
MANDRILL_API bool toggleMouseCapture()
Toggle mouse capture state, without specifying the new state.
Definition Camera.h:77
VkWriteDescriptorSet getWriteDescriptor(uint32_t binding) const
Get the write descriptor of the camera. Use this with when using push descriptors.
Definition Camera.h:197
MANDRILL_API bool isMouseCaptured() const
Check if camera has captured the mouse movements.
Definition Camera.h:58
MANDRILL_API glm::vec3 getDirection() const
Get the direction of the camera.
Definition Camera.h:106
MANDRILL_API glm::mat4 getProjectionMatrix() const
Get the projection matrix of the camera.
Definition Camera.h:176
MANDRILL_API void setMoveSpeed(float speed)
Set the movement speed of the camera.
Definition Camera.h:156
MANDRILL_API glm::mat4 getViewMatrix() const
Get the view matrix of the camera.
Definition Camera.h:165
MANDRILL_API ~Camera()
Destructor of camera.
Definition Camera.cpp:62
MANDRILL_API void setFov(float fov)
Set the field of view of the camera.
Definition Camera.h:146
MANDRILL_API ptr< Descriptor > getDescriptor() const
Get the descriptor of the camera, containing the view and projection matrices, and their invertations...
Definition Camera.h:187
MANDRILL_API void captureMouse(bool capture)
Set the capture state of the mouse.
Definition Camera.h:68
MANDRILL_API void update(float delta, glm::vec2 cursorDelta)
Update function to handle camera movements. Call this each app update.
Definition Camera.cpp:75
MANDRILL_API void setDirection(glm::vec3 dir)
Set the direction of the camera.
Definition Camera.h:116
MANDRILL_API void setTarget(glm::vec3 target)
Set the target the camera should look towards.
Definition Camera.h:126