Quality of life changes for new semester. Breaks 'API'.

This commit is contained in:
Aleksander Rognhaugen
2016-06-27 12:22:12 +02:00
parent b30dc7ef2f
commit 080537f909
8 changed files with 248 additions and 270 deletions

View File

@@ -1,171 +0,0 @@
#ifndef CAMERA_HPP
#define CAMERA_HPP
#pragma once
// System headers
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
namespace Gloom
{
enum CameraDirection {
FORWARD, BACKWARD, LEFT, RIGHT
};
class Camera
{
public:
Camera(GLfloat aspect, // aspect = mWidth / mHeight
GLfloat fov = 45.0f,
GLfloat nearClip = 0.1f,
GLfloat farClip = 1000.0f,
glm::vec3 position = glm::vec3(0.0f, 0.0f, 2.0f),
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f),
GLfloat yaw = -90.0f,
GLfloat pitch = 0.0f,
GLfloat movementSpeed = 3.0f,
GLfloat mouseSensitivity = 0.15f)
{
cAspect = aspect;
cFov = fov;
cNearClip = nearClip;
cFarClip = farClip;
cPosition = position;
cWorldUp = up;
cFront = front;
cYaw = yaw;
cPitch = pitch;
cMovementSpeed = movementSpeed;
cMouseSensitivity = mouseSensitivity;
update();
}
// Public member functions
/* Getter for the model, view, projection matrices */
void getMVP(glm::mat4 &proj, glm::mat4 &view, glm::mat4 &model)
{
proj = getProjectionMatrix();
view = getViewMatrix();
model = glm::mat4(1.0f);
}
/* Getter for the projection matrix */
glm::mat4 getProjectionMatrix()
{
return glm::perspective(cFov, cAspect, cNearClip, cFarClip);
}
/* Getter for the view matrix */
glm::mat4 getViewMatrix()
{
return glm::lookAt(cPosition, cPosition + cFront, cUp);
}
/* Process keyboard inputs
`deltaTime` is the time between the current and last frame */
void processKeyboard(Gloom::CameraDirection dir,
GLfloat deltaTime)
{
// Trick to balance PC speed with movement
GLfloat velocity = cMovementSpeed * deltaTime;
// Alter position in the appropriate direction
if (dir == Gloom::FORWARD)
{
cPosition += cFront * velocity;
}
if (dir == Gloom::BACKWARD)
{
cPosition -= cFront * velocity;
}
if (dir == Gloom::LEFT)
{
cPosition -= cRight * velocity;
}
if (dir == Gloom::RIGHT)
{
cPosition += cRight * velocity;
}
}
/* Process mouse movements */
void processMouse(GLfloat xoffset, GLfloat yoffset)
{
// Smoothing
xoffset *= cMouseSensitivity;
yoffset *= cMouseSensitivity;
// Update Euler angles
cYaw += xoffset;
cPitch += yoffset;
// Constrain pitch so the screen doesn't flip
if (cPitch > 89.0f)
{
cPitch = 89.0f;
}
if (cPitch < -89.0f)
{
cPitch = -89.0f;
}
// Update cFront, cRight, and cUp using the new yaw and pitch
update();
}
private:
// Disable copying and assignment
Camera(Camera const &) = delete;
Camera & operator =(Camera const &) = delete;
// Private member function
/* Computes the cFront, cRight, and cUp vector given updated
Euler angles */
void update()
{
// Calculate cFront
glm::vec3 front;
front.x = cos(glm::radians(cYaw)) * cos(glm::radians(cPitch));
front.y = sin(glm::radians(cPitch));
front.z = sin(glm::radians(cYaw)) * cos(glm::radians(cPitch));
cFront = glm::normalize(front);
// Re-calculate cRight and cUp
cRight = glm::normalize(glm::cross(cFront, cWorldUp));
cUp = glm::normalize(glm::cross(cRight, cFront));
}
// Private member variables
// Camera variables
glm::vec3 cPosition;
glm::vec3 cFront;
glm::vec3 cUp;
glm::vec3 cRight;
glm::vec3 cWorldUp;
// Euler angles
GLfloat cYaw;
GLfloat cPitch;
// Camera settings
GLfloat cMovementSpeed;
GLfloat cMouseSensitivity;
GLfloat cFov;
GLfloat cAspect;
GLfloat cNearClip;
GLfloat cFarClip;
};
}
#endif

238
gloom/src/gloom/camera.hpp Normal file
View File

@@ -0,0 +1,238 @@
#ifndef CAMERA_HPP
#define CAMERA_HPP
#pragma once
// System headers
#include <glad/glad.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
namespace Gloom
{
class Camera
{
public:
Camera(GLfloat aspect, // aspect = mWidth / mHeight
glm::vec3 position = glm::vec3(0.0f, 0.0f, 2.0f),
GLfloat fov = 45.0f,
GLfloat nearClip = 0.1f,
GLfloat farClip = 1000.0f,
GLfloat movementSpeed = 5.0f,
GLfloat mouseSensitivity = 0.005f)
{
cAspect = aspect;
cPosition = position;
cFov = fov;
cNearClip = nearClip;
cFarClip = farClip;
cMovementSpeed = movementSpeed;
cMouseSensitivity = mouseSensitivity;
// Set up default model and projection matrices
matProj = glm::perspective(cFov, cAspect, cNearClip, cFarClip);
matModel = glm::mat4(1.0f);
// Set up the initial view matrix
updateViewMatrix();
}
// Public member functions
/* Getter for the model, view, and projection matrices */
void getMVP(glm::mat4 &proj, glm::mat4 &view, glm::mat4 &model)
{
proj = getProjectionMatrix();
view = getViewMatrix();
model = getModelMatrix();
}
/* Getters for transformation matrices */
glm::mat4 getProjectionMatrix() { return matProj; }
glm::mat4 getViewMatrix() { return matView; }
glm::mat4 getModelMatrix() { return matModel; }
/* Setter for new aspect ratio */
void setAspect(GLfloat aspect) {
cAspect = aspect;
// Update perspective matrix
matProj = glm::perspective(cFov, cAspect, cNearClip, cFarClip);
}
/* Handle keyboard inputs from a callback mechanism */
void handleKeyboardInputs(int key, int action)
{
// Keep track of pressed/released buttons
if (key >= 0 && key < 512)
{
if (action == GLFW_PRESS)
{
keysInUse[key] = true;
}
else if (action == GLFW_RELEASE)
{
keysInUse[key] = false;
}
}
}
/* Handle mouse button inputs from a callback mechanism */
void handleMouseButtonInputs(int button, int action)
{
// Ensure that the camera only rotates when the left mouse button is
// pressed
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
isMousePressed = true;
}
else
{
isMousePressed = false;
resetMouse = true;
}
}
/* Handle cursor position from a callback mechanism */
void handleCursorPosInput(double xpos, double ypos)
{
// Do nothing if the left mouse button is not pressed
if (isMousePressed == false)
return;
// There should be no movement when the mouse button is released
if (resetMouse)
{
lastXPos = xpos;
lastYPos = ypos;
resetMouse = false;
}
// Keep track of pitch and yaw for the current frame
fYaw = xpos - lastXPos;
fPitch = ypos - lastYPos;
// Update last known cursor position
lastXPos = xpos;
lastYPos = ypos;
}
/* Update the camera position and view matrix
`deltaTime` is the time between the current and last frame */
void updateCamera(GLfloat deltaTime)
{
// Extract movement information from the view matrix
glm::vec3 dirX(matView[0][0], matView[1][0], matView[2][0]);
glm::vec3 dirY(matView[0][1], matView[1][1], matView[2][1]);
glm::vec3 dirZ(matView[0][2], matView[1][2], matView[2][2]);
// Alter position in the appropriate direction
glm::vec3 fMovement(0.0f, 0.0f, 0.0f);
if (keysInUse[GLFW_KEY_W]) // forward
fMovement -= dirZ;
if (keysInUse[GLFW_KEY_S]) // backward
fMovement += dirZ;
if (keysInUse[GLFW_KEY_A]) // left
fMovement -= dirX;
if (keysInUse[GLFW_KEY_D]) // right
fMovement += dirX;
if (keysInUse[GLFW_KEY_E]) // vertical up
fMovement += dirY;
if (keysInUse[GLFW_KEY_Q]) // vertical down
fMovement -= dirY;
// Trick to balance PC speed with movement
GLfloat velocity = cMovementSpeed * deltaTime;
// Update camera position using the appropriate velocity
cPosition += fMovement * velocity;
// Update the view matrix based on the new information
updateViewMatrix();
}
private:
// Disable copying and assignment
Camera(Camera const &) = delete;
Camera & operator =(Camera const &) = delete;
// Private member function
/* Update the view matrix based on the current information */
void updateViewMatrix()
{
// Adjust cursor movement using the specified sensitivity
fPitch *= cMouseSensitivity;
fYaw *= cMouseSensitivity;
// Create quaternions given the current pitch and yaw
glm::quat qPitch = glm::quat(glm::vec3(fPitch, 0.0f, 0.0f));
glm::quat qYaw = glm::quat(glm::vec3(0.0f, fYaw, 0.0f));
// Reset pitch and yaw values for the current rotation
fPitch = 0.0f;
fYaw = 0.0f;
// Update camera quaternion and normalise
cQuaternion = qYaw * qPitch * cQuaternion;
cQuaternion = glm::normalize(cQuaternion);
// Build rotation matrix using the camera quaternion
glm::mat4 matRotation = glm::mat4_cast(cQuaternion);
// Build translation matrix
glm::mat4 matTranslate = glm::translate(glm::mat4(1.0f), -cPosition);
// Update view matrix
matView = matRotation * matTranslate;
}
// Private member variables
// Camera quaternion and frame pitch and yaw
glm::quat cQuaternion;
GLfloat fPitch = 0.0f;
GLfloat fYaw = 0.0f;
// Camera position
glm::vec3 cPosition;
// Variables used for bookkeeping
GLboolean resetMouse = true;
GLboolean isMousePressed = false;
GLboolean keysInUse[512];
// Last cursor position
GLfloat lastXPos = 0.0f;
GLfloat lastYPos = 0.0f;
// Camera settings
GLfloat cMovementSpeed;
GLfloat cMouseSensitivity;
GLfloat cFov;
GLfloat cAspect;
GLfloat cNearClip;
GLfloat cFarClip;
// Transformation matrices
glm::mat4 matProj;
glm::mat4 matView;
glm::mat4 matModel;
};
}
#endif

View File

@@ -117,6 +117,8 @@ namespace Gloom
if (ext == "comp") return glCreateShader(GL_COMPUTE_SHADER);
else if (ext == "frag") return glCreateShader(GL_FRAGMENT_SHADER);
else if (ext == "geom") return glCreateShader(GL_GEOMETRY_SHADER);
else if (ext == "tcs") return glCreateShader(GL_TESS_CONTROL_SHADER);
else if (ext == "tes") return glCreateShader(GL_TESS_EVALUATION_SHADER);
else if (ext == "vert") return glCreateShader(GL_VERTEX_SHADER);
else return false;
}

View File

@@ -1,5 +1,5 @@
// Local headers
#include "gloom.hpp"
#include "gloom/gloom.hpp"
#include "program.hpp"
// System headers

View File

@@ -1,12 +1,11 @@
// Local headers
#include "program.hpp"
#include "gloom.hpp"
#include "shader.hpp"
#include "gloom/gloom.hpp"
void runProgram(GLFWwindow* mWindow)
{
// Set GLFW callback mechanisms
// Set GLFW callback mechanism(s)
glfwSetKeyCallback(mWindow, keyboardCallback);
// Enable depth (Z) buffer (accept "closest" fragment)