mirror of
https://github.com/brammie15/VoxelRenderer.git
synced 2025-12-15 09:41:48 +01:00
Quality of life changes for new semester. Breaks 'API'.
This commit is contained in:
15
LICENSE
15
LICENSE
@@ -32,18 +32,3 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
LearnOpenGL LICENSE
|
||||
|
||||
The header file camera.hpp found in the source directory is a modified
|
||||
version of camera.h found in the github repository for LearnOpenGL.
|
||||
https://github.com/JoeyDeVries/LearnOpenGL
|
||||
|
||||
From the about page at learnopengl.com:
|
||||
|
||||
All code samples, unless explicitly stated otherwise, are licensed under the public domain depicted by the terms of the CC0 1.0 Universal license as published by Creative Commons, either version 1.0 of the License, or (at your option) any later version.
|
||||
You can find a human-readable format of the license here:
|
||||
https://creativecommons.org/publicdomain/zero/1.0/
|
||||
and the full license here:
|
||||
https://creativecommons.org/publicdomain/zero/1.0/legalcode
|
||||
|
||||
85
README.rst
85
README.rst
@@ -86,87 +86,11 @@ Windows (cmake-gui)
|
||||
4. Click the generate button
|
||||
5. If your generator is an IDE such as Visual Studio, then open up the newly created .sln file and build ``ALL_BUILD``. After this you might want to set ``gloom`` as you StartUp Project.
|
||||
|
||||
Documentation
|
||||
=============
|
||||
|
||||
Examples
|
||||
========
|
||||
|
||||
Shader class
|
||||
------------
|
||||
|
||||
The class for loading shaders can be used as follows:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#include "shader.hpp"
|
||||
|
||||
// ...
|
||||
|
||||
Gloom::Shader shader;
|
||||
shader.attach("path_to_vertex_shader.vert");
|
||||
shader.attach("path_to_fragment_shader.frag");
|
||||
shader.link();
|
||||
|
||||
If all you are going to use are vertex and fragment shaders, then the piece of code above can be replaced by this:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
Gloom::Shader shader;
|
||||
shader.makeBasicShader("path_to_vertex_shader.vert",
|
||||
"path_to_fragment_shader.frag");
|
||||
|
||||
The shader program can be activated and deactivated as needed after you have linked the attached shaders. Below is an example from inside the rendering loop:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
// Activate shader program
|
||||
shader.activate();
|
||||
|
||||
// Perform draw calls using, for example, glDrawArrays
|
||||
|
||||
// Deactivate shader program
|
||||
shader.deactivate();
|
||||
|
||||
Remember to delete the shader program when exiting, e.g. ``shader.destroy();``.
|
||||
|
||||
|
||||
Loading images with stb
|
||||
-----------------------
|
||||
|
||||
The header-only library `stb`_ can be used to load images as follows:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
// ...
|
||||
|
||||
int width, height, channels;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
unsigned char *image = stbi_load("path_to_raster_image.jpg",
|
||||
&width,
|
||||
&height,
|
||||
&channels,
|
||||
STBI_rgb);
|
||||
|
||||
// ... do something with the image
|
||||
|
||||
stbi_image_free(image);
|
||||
|
||||
The first and second line includes the library and ensures that we are using the implementation provided by stb.
|
||||
|
||||
The ``stbi_set_flip_vertically_on_load()`` function will, when set to ``true``, flip the image vertically, so the first pixel corresponds to the lower left corner of the image. This is useful in, for example, OpenGL when texturing.
|
||||
|
||||
The fifth parameter of ``stbi_load()`` specifies the number of 8-bit components per pixel to use. It can be set to one of following four options:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
STBI_grey = 1
|
||||
STBI_grey_alpha = 2
|
||||
STBI_rgb = 3
|
||||
STBI_rgb_alpha = 4
|
||||
|
||||
Please have a look at the full documentation of ``stb_image.h`` at the `stb`_ GitHub page for more information.
|
||||
The full documentation can be found on the `repository wiki`_.
|
||||
Among other things, the wiki includes information on how to use the shader and camera class bundled with gloom.
|
||||
|
||||
|
||||
.. Links
|
||||
@@ -178,3 +102,4 @@ Please have a look at the full documentation of ``stb_image.h`` at the `stb`_ Gi
|
||||
.. _glm: https://github.com/g-truc/glm
|
||||
.. _stb: https://github.com/nothings/stb
|
||||
.. _CMake: https://cmake.org/
|
||||
.. _repository wiki: https://github.com/senbon/gloom/wiki
|
||||
|
||||
@@ -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
238
gloom/src/gloom/camera.hpp
Normal 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
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// Local headers
|
||||
#include "gloom.hpp"
|
||||
#include "gloom/gloom.hpp"
|
||||
#include "program.hpp"
|
||||
|
||||
// System headers
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user