CGVIS Utils
Loading...
Searching...
No Matches
Camera.h
Go to the documentation of this file.
1#pragma once
2
3#define LNC_PI 3.14159265358979323846
4#define LNC_RADIANS(deg) (deg * LNC_PI / 180.0)
5
6#include "Vec3.h"
7#include "Mat4.h"
8
29class Camera {
30 Vec3 position;
31 Vec3 direction;
32 Vec3 right, up;
33 Vec3 worldUp;
34
35 float yaw{ -90.0f };
36 float pitch{ 0.0f };
37
38 float moveSpeed;
39 bool movingFront{ false };
40 bool movingBack{ false };
41 bool movingLeft{ false };
42 bool movingRight{ false };
43
44 float mouseSens;
45 float mouseX{ -1.0f }, mouseY{ -1.0f };
46 bool mouseEnabled{ false };
47
54 void updateDirection() {
55 float dirX = cos(LNC_RADIANS(yaw)) * cos(LNC_RADIANS(pitch));
56 float dirY = sin(LNC_RADIANS(pitch));
57 float dirZ = sin(LNC_RADIANS(yaw)) * cos(LNC_RADIANS(pitch));
58 direction = Vec3::normalize(Vec3(dirX, dirY, dirZ));
59 right = Vec3::normalize(Vec3::cross(direction, worldUp));
60 up = Vec3::normalize(Vec3::cross(right, direction));
61 }
62
63public:
64
72 Camera(Vec3 position, float moveSpeed = 0.015f, float mouseSens = 0.15f, Vec3 worldUp = Vec3(0.0f, 1.0f, 0.0f)) {
73 this->position = position;
74 this->moveSpeed = moveSpeed;
75 this->mouseSens = mouseSens;
76 this->worldUp = worldUp;
77 updateDirection();
78 }
79
82
83 void moveFront(bool b) { movingFront = b; }
84
86 void moveBack(bool b) { movingBack = b; }
87
89 void moveRight(bool b) { movingRight = b; }
90
92 void moveLeft(bool b) { movingLeft = b; }
94
97
98 void enableMouse() { mouseEnabled = true; }
99
107 mouseEnabled = false;
108 mouseX = mouseY = -1.0f;
109 }
110
120 void mouseMove(float xPosition, float yPosition) {
121 if (!mouseEnabled) {
122 return;
123 }
124 else if (mouseX == -1.0f && mouseY == -1.0f) {
125 mouseX = xPosition;
126 mouseY = yPosition;
127 return;
128 }
129
130 float xOffset = (xPosition - mouseX) * mouseSens;
131 float yOffset = (mouseY - yPosition) * mouseSens;
132
133 mouseX = xPosition;
134 mouseY = yPosition;
135
136 yaw += xOffset;
137 pitch += yOffset;
138
139 if (pitch > 89.0f) pitch = 89.0f;
140 else if (pitch < -89.0f) pitch = -89.0f;
141
142 updateDirection();
143 }
144
145
153 if (movingFront) position = position + (direction * moveSpeed);
154 if (movingBack) position = position - (direction * moveSpeed);
155 if (movingRight) position = position + (right * moveSpeed);
156 if (movingLeft) position = position - (right * moveSpeed);
157 }
158
164 return Mat4::lookAt(position, position + direction, up);
165 }
166};
#define LNC_RADIANS(deg)
Definition Camera.h:4
4×4 matrix template for 3D transformations, projections, and stereo sets.
Mat4t< float > Mat4
Single-precision 4×4 matrix.
Definition Mat4.h:567
3D vector template with arithmetic, geometry helpers, and RNG utilities.
Vec3t< float > Vec3
3D float vector (x,y,z).
Definition Vec3.h:424
void moveBack(bool b)
Toggle backward motion along the view direction.
Definition Camera.h:86
void mouseMove(float xPosition, float yPosition)
Update yaw/pitch from a new cursor position.
Definition Camera.h:120
void moveLeft(bool b)
Toggle motion to the left (strafe).
Definition Camera.h:92
Mat4 viewMatrix()
Compute the view (camera) matrix.
Definition Camera.h:163
void enableMouse()
Enable mouse-look processing in mouseMove().
Definition Camera.h:98
void disableMouse()
Disable mouse-look and reset the internal cursor cache.
Definition Camera.h:106
Camera(Vec3 position, float moveSpeed=0.015f, float mouseSens=0.15f, Vec3 worldUp=Vec3(0.0f, 1.0f, 0.0f))
Construct a camera at position with speeds/sensitivity and an up axis.
Definition Camera.h:72
void updatePosition()
Integrate position one step based on active movement flags.
Definition Camera.h:152
void moveFront(bool b)
Toggle forward motion along the view direction.
Definition Camera.h:83
void moveRight(bool b)
Toggle motion to the right (strafe).
Definition Camera.h:89
static Mat4t lookAt(const Vec3t< float > &vEye, const Vec3t< float > &vAt, const Vec3t< float > &vUp)
Definition Mat4.h:491
static Vec3t cross(const Vec3t &a, const Vec3t &b)
Definition Vec3.h:247
static Vec3t normalize(const Vec3t &a)
Definition Vec3.h:258