aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/camera.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/camera.hpp')
-rw-r--r--src/camera.hpp323
1 files changed, 163 insertions, 160 deletions
diff --git a/src/camera.hpp b/src/camera.hpp
index 59e6180..7c2ff8c 100644
--- a/src/camera.hpp
+++ b/src/camera.hpp
@@ -12,206 +12,209 @@
#include <glm/gtc/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp>
-class Camera
-{
- float fov;
- float znear, zfar;
+namespace vke {
- void updateViewMatrix()
+ class Camera
{
- glm::mat4 rotM = glm::mat4(1.0f);
- glm::mat4 transM;
+ float fov;
+ float znear, zfar;
- rotM = glm::rotate(rotM, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
- rotM = glm::rotate(rotM, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
- rotM = glm::rotate(rotM, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
+ void updateViewMatrix()
+ {
+ glm::mat4 rotM = glm::mat4(1.0f);
+ glm::mat4 transM;
- transM = glm::translate(glm::mat4(1.0f), position);
+ rotM = glm::rotate(rotM, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
+ rotM = glm::rotate(rotM, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
+ rotM = glm::rotate(rotM, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
- if (type == CameraType::firstperson)
- matrices.view = rotM * transM;
- else
- matrices.view = transM * rotM;
- }
-public:
- enum CameraType { lookat, firstperson };
- CameraType type = CameraType::lookat;
+ transM = glm::translate(glm::mat4(1.0f), position);
- glm::vec3 rotation = glm::vec3();
- glm::vec3 position = glm::vec3();
+ if (type == CameraType::firstperson)
+ matrices.view = rotM * transM;
+ else
+ matrices.view = transM * rotM;
+ }
+ public:
+ enum CameraType { lookat, firstperson };
+ CameraType type = CameraType::lookat;
- float rotationSpeed = 1.0f;
- float movementSpeed = 1.0f;
- float zoomSpeed = 0.5f;
+ glm::vec3 rotation = glm::vec3();
+ glm::vec3 position = glm::vec3();
- struct
- {
- glm::mat4 perspective;
- glm::mat4 view;
- } matrices;
+ float rotationSpeed = 1.0f;
+ float movementSpeed = 1.0f;
+ float zoomSpeed = 0.5f;
- struct {
- bool left = false;
- bool right = false;
- bool up = false;
- bool down = false;
- } keys;
+ struct
+ {
+ glm::mat4 perspective;
+ glm::mat4 view;
+ } matrices;
- bool moving()
- {
- return keys.left || keys.right || keys.up || keys.down;
- }
+ struct {
+ bool left = false;
+ bool right = false;
+ bool up = false;
+ bool down = false;
+ } keys;
- float getNearClip() {
- return znear;
- }
+ bool moving()
+ {
+ return keys.left || keys.right || keys.up || keys.down;
+ }
- float getFarClip() {
- return zfar;
- }
+ float getNearClip() {
+ return znear;
+ }
- glm::vec3 getViewDirection () {
- glm::vec3 camFront;
- camFront.x = -cos(glm::radians(rotation.x)) * sin(glm::radians(rotation.y));
- camFront.y = sin(glm::radians(rotation.x));
- camFront.z = cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y));
- camFront = glm::normalize(camFront);
- return camFront;
- }
+ float getFarClip() {
+ return zfar;
+ }
- void setPerspective(float fov, float aspect, float znear, float zfar)
- {
- this->fov = fov;
- this->znear = znear;
- this->zfar = zfar;
- matrices.perspective = glm::perspective(glm::radians(fov), aspect, znear, zfar);
- }
+ glm::vec3 getViewDirection () {
+ glm::vec3 camFront;
+ camFront.x = -cos(glm::radians(rotation.x)) * sin(glm::radians(rotation.y));
+ camFront.y = sin(glm::radians(rotation.x));
+ camFront.z = cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y));
+ camFront = glm::normalize(camFront);
+ return camFront;
+ }
- void updateAspectRatio(float aspect)
- {
- matrices.perspective = glm::perspective(glm::radians(fov), aspect, znear, zfar);
- }
+ void setPerspective(float fov, float aspect, float znear, float zfar)
+ {
+ this->fov = fov;
+ this->znear = znear;
+ this->zfar = zfar;
+ matrices.perspective = glm::perspective(glm::radians(fov), aspect, znear, zfar);
+ }
- void setPosition(glm::vec3 position)
- {
- this->position = position;
- updateViewMatrix();
- }
+ void updateAspectRatio(float aspect)
+ {
+ matrices.perspective = glm::perspective(glm::radians(fov), aspect, znear, zfar);
+ }
- void setRotation(glm::vec3 rotation)
- {
- this->rotation = rotation;
- updateViewMatrix();
- }
+ void setPosition(glm::vec3 position)
+ {
+ this->position = position;
+ updateViewMatrix();
+ }
- void rotate(glm::vec3 delta)
- {
- this->rotation += delta;
- updateViewMatrix();
- }
+ void setRotation(glm::vec3 rotation)
+ {
+ this->rotation = rotation;
+ updateViewMatrix();
+ }
- void setTranslation(glm::vec3 translation)
- {
- this->position = translation;
- updateViewMatrix();
- }
+ void rotate(glm::vec3 delta)
+ {
+ this->rotation += delta;
+ updateViewMatrix();
+ }
- void translate(glm::vec3 delta)
- {
- this->position += delta;
- updateViewMatrix();
- }
+ void setTranslation(glm::vec3 translation)
+ {
+ this->position = translation;
+ updateViewMatrix();
+ }
- void update(float deltaTime)
- {
- if (type == CameraType::firstperson)
+ void translate(glm::vec3 delta)
+ {
+ this->position += delta;
+ updateViewMatrix();
+ }
+
+ void update(float deltaTime)
{
- if (moving())
+ if (type == CameraType::firstperson)
{
- glm::vec3 camFront;
- camFront.x = -cos(glm::radians(rotation.x)) * sin(glm::radians(rotation.y));
- camFront.y = sin(glm::radians(rotation.x));
- camFront.z = cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y));
- camFront = glm::normalize(camFront);
+ if (moving())
+ {
+ glm::vec3 camFront;
+ camFront.x = -cos(glm::radians(rotation.x)) * sin(glm::radians(rotation.y));
+ camFront.y = sin(glm::radians(rotation.x));
+ camFront.z = cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y));
+ camFront = glm::normalize(camFront);
- float moveSpeed = deltaTime * movementSpeed;
+ float moveSpeed = deltaTime * movementSpeed;
- if (keys.up)
- position += camFront * moveSpeed;
- if (keys.down)
- position -= camFront * moveSpeed;
- if (keys.left)
- position -= glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * moveSpeed;
- if (keys.right)
- position += glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * moveSpeed;
+ if (keys.up)
+ position += camFront * moveSpeed;
+ if (keys.down)
+ position -= camFront * moveSpeed;
+ if (keys.left)
+ position -= glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * moveSpeed;
+ if (keys.right)
+ position += glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * moveSpeed;
- updateViewMatrix();
+ updateViewMatrix();
+ }
}
}
- }
- // Update camera passing separate axis data (gamepad)
- // Returns true if view or position has been changed
- bool updatePad(glm::vec2 axisLeft, glm::vec2 axisRight, float deltaTime)
- {
- bool retVal = false;
-
- if (type == CameraType::firstperson)
+ // Update camera passing separate axis data (gamepad)
+ // Returns true if view or position has been changed
+ bool updatePad(glm::vec2 axisLeft, glm::vec2 axisRight, float deltaTime)
{
- // Use the common console thumbstick layout
- // Left = view, right = move
+ bool retVal = false;
- const float deadZone = 0.0015f;
- const float range = 1.0f - deadZone;
+ if (type == CameraType::firstperson)
+ {
+ // Use the common console thumbstick layout
+ // Left = view, right = move
- glm::vec3 camFront;
- camFront.x = -cos(glm::radians(rotation.x)) * sin(glm::radians(rotation.y));
- camFront.y = sin(glm::radians(rotation.x));
- camFront.z = cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y));
- camFront = glm::normalize(camFront);
+ const float deadZone = 0.0015f;
+ const float range = 1.0f - deadZone;
- float moveSpeed = deltaTime * movementSpeed * 2.0f;
- float rotSpeed = deltaTime * rotationSpeed * 50.0f;
+ glm::vec3 camFront;
+ camFront.x = -cos(glm::radians(rotation.x)) * sin(glm::radians(rotation.y));
+ camFront.y = sin(glm::radians(rotation.x));
+ camFront.z = cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y));
+ camFront = glm::normalize(camFront);
- // Move
- if (fabsf(axisLeft.y) > deadZone)
- {
- float pos = (fabsf(axisLeft.y) - deadZone) / range;
- position -= camFront * pos * ((axisLeft.y < 0.0f) ? -1.0f : 1.0f) * moveSpeed;
- retVal = true;
+ float moveSpeed = deltaTime * movementSpeed * 2.0f;
+ float rotSpeed = deltaTime * rotationSpeed * 50.0f;
+
+ // Move
+ if (fabsf(axisLeft.y) > deadZone)
+ {
+ float pos = (fabsf(axisLeft.y) - deadZone) / range;
+ position -= camFront * pos * ((axisLeft.y < 0.0f) ? -1.0f : 1.0f) * moveSpeed;
+ retVal = true;
+ }
+ if (fabsf(axisLeft.x) > deadZone)
+ {
+ float pos = (fabsf(axisLeft.x) - deadZone) / range;
+ position += glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * pos * ((axisLeft.x < 0.0f) ? -1.0f : 1.0f) * moveSpeed;
+ retVal = true;
+ }
+
+ // Rotate
+ if (fabsf(axisRight.x) > deadZone)
+ {
+ float pos = (fabsf(axisRight.x) - deadZone) / range;
+ rotation.y += pos * ((axisRight.x < 0.0f) ? -1.0f : 1.0f) * rotSpeed;
+ retVal = true;
+ }
+ if (fabsf(axisRight.y) > deadZone)
+ {
+ float pos = (fabsf(axisRight.y) - deadZone) / range;
+ rotation.x -= pos * ((axisRight.y < 0.0f) ? -1.0f : 1.0f) * rotSpeed;
+ retVal = true;
+ }
}
- if (fabsf(axisLeft.x) > deadZone)
+ else
{
- float pos = (fabsf(axisLeft.x) - deadZone) / range;
- position += glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))) * pos * ((axisLeft.x < 0.0f) ? -1.0f : 1.0f) * moveSpeed;
- retVal = true;
+ // todo: move code from example base class for look-at
}
- // Rotate
- if (fabsf(axisRight.x) > deadZone)
+ if (retVal)
{
- float pos = (fabsf(axisRight.x) - deadZone) / range;
- rotation.y += pos * ((axisRight.x < 0.0f) ? -1.0f : 1.0f) * rotSpeed;
- retVal = true;
- }
- if (fabsf(axisRight.y) > deadZone)
- {
- float pos = (fabsf(axisRight.y) - deadZone) / range;
- rotation.x -= pos * ((axisRight.y < 0.0f) ? -1.0f : 1.0f) * rotSpeed;
- retVal = true;
+ updateViewMatrix();
}
- }
- else
- {
- // todo: move code from example base class for look-at
- }
- if (retVal)
- {
- updateViewMatrix();
+ return retVal;
}
- return retVal;
- }
-
-};
+ };
+}