I’m building a video game engine using C++ as the coding language and Vulkan for…
- User
I’m building a video game engine using C++ as the coding language and Vulkan for graphics. I am trying to set up a generic renderer using Vulkan that is flexible and will render objects based on a vector that is supplied to it. The renderer will also handle the creation of the window using GLFW and use GLM for all relevant math calls. I am using the ASSIMP library to load 3d models and animations.
Here is some of the code:
Camera.h:
#pragma once
#include <glm/glm.hpp>
class Camera { public: Camera(); ~Camera();
void Initialize(float aspectRatio); void Shutdown(); void SetPosition(const glm::vec3& position); void SetRotation(const glm::vec3& rotation); const glm::mat4& GetViewMatrix() const; const glm::mat4& GetProjectionMatrix() const; void UpdateAspectRatio(float aspectRatio);private: glm::vec3 position; glm::vec3 rotation; glm::mat4 viewMatrix; glm::mat4 projectionMatrix;
void UpdateViewMatrix();};
Camera.cpp:
#include "Camera.h" #include <glm/gtc/matrix_transform.hpp>
Camera::Camera() : position(0.0f), rotation(0.0f), viewMatrix(1.0f), projectionMatrix(1.0f) { }
Camera::~Camera() { Shutdown(); }
void Camera::Initialize(float aspectRatio) { projectionMatrix = glm::perspective(glm::radians(45.0f), aspectRatio, 0.1f, 100.0f); SetPosition(glm::vec3(0.0f, 0.0f, 3.0f)); SetRotation(glm::vec3(0.0f, 0.0f, 0.0f)); }
void Camera::Shutdown() { }
void Camera::UpdateAspectRatio(float aspectRatio) { projectionMatrix = glm::perspective(glm::radians(45.0f), aspectRatio, 0.1f, 100.0f); }
void Camera::SetPosition(const glm::vec3& position) { this->position = position; UpdateViewMatrix(); }
void Camera::SetRotation(const glm::vec3& rotation) { this->rotation = rotation; UpdateViewMatrix(); }
const glm::mat4& Camera::GetViewMatrix() const { return viewMatrix; }
const glm::mat4& Camera::GetProjectionMatrix() const { return projectionMatrix; }
void Camera::UpdateViewMatrix() { glm::mat4 rotMatrix = glm::mat4(1.0f); rotMatrix = glm::rotate(rotMatrix, rotation.x, glm::vec3(1.0f, 0.0f, 0.0f)); rotMatrix = glm::rotate(rotMatrix, rotation.y, glm::vec3(0.0f, 1.0f, 0.0f)); rotMatrix = glm::rotate(rotMatrix, rotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
glm::vec3 lookAt = glm::vec3(rotMatrix * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)); viewMatrix = glm::lookAt(position, position + lookAt, glm::vec3(0.0f, 1.0f, 0.0f));}
Can you convert the Camera class to utilise quaternions?
- Assistant
Certainly! Here's the modified version of the Camera class that uses quaternions for rotation:
#include "Camera.h" #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/quaternion.hpp> Camera::Camera() : position(0.0f), rotation(1.0f), viewMatrix(1.0f), projectionMatrix(1.0f) { } Camera::~Camera() { Shutdown(); } void Camera::Initialize(float aspectRatio) { projectionMatrix = glm::perspective(glm::radians(45.0f), aspectRatio, 0.1f, 100.0f); SetPosition(glm::vec3(0.0f, 0.0f, 3.0f)); SetRotation(glm::vec3(0.0f, 0.0f, 0.0f)); } void Camera::Shutdown() { } void Camera::UpdateAspectRatio(float aspectRatio) { projectionMatrix = glm::perspective(glm::radians(45.0f), aspectRatio, 0.1f, 100.0f); } void Camera::SetPosition(const glm::vec3& position) { this->position = position; UpdateViewMatrix(); } void Camera::SetRotation(const glm::vec3& rotation) { glm::quat quaternion = glm::quat(glm::radians(rotation)); this->rotation = glm::normalize(quaternion); UpdateViewMatrix(); } const glm::mat4& Camera::GetViewMatrix() const { return viewMatrix; } const glm::mat4& Camera::GetProjectionMatrix() const { return projectionMatrix; } void Camera::UpdateViewMatrix() { glm::mat4 rotMatrix = glm::toMat4(rotation); glm::vec3 lookAt = glm::vec3(rotMatrix * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)); viewMatrix = glm::lookAt(position, position + lookAt, glm::vec3(0.0f, 1.0f, 0.0f)); }