Minor name changes and cleanup in main.cpp and camera.hpp.

This commit is contained in:
Aleksander Rognhaugen
2016-09-20 21:00:49 +02:00
parent fd306e2e7d
commit 2ecd0b9910
6 changed files with 44 additions and 83 deletions

View File

@@ -14,54 +14,22 @@ namespace Gloom
class Camera class Camera
{ {
public: public:
Camera(GLfloat aspect, // aspect = mWidth / mHeight Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 2.0f),
glm::vec3 position = glm::vec3(0.0f, 0.0f, 2.0f), GLfloat movementSpeed = 5.0f,
GLfloat fov = 45.0f, GLfloat mouseSensitivity = 0.005f)
GLfloat nearClip = 0.1f,
GLfloat farClip = 1000.0f,
GLfloat movementSpeed = 5.0f,
GLfloat mouseSensitivity = 0.005f)
{ {
cAspect = aspect;
cPosition = position; cPosition = position;
cFov = fov;
cNearClip = nearClip;
cFarClip = farClip;
cMovementSpeed = movementSpeed; cMovementSpeed = movementSpeed;
cMouseSensitivity = mouseSensitivity; 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 // Set up the initial view matrix
updateViewMatrix(); updateViewMatrix();
} }
// Public member functions // Public member functions
/* Getter for the model, view, and projection matrices */ /* Getter for the view matrix */
void getMVP(glm::mat4 &proj, glm::mat4 &view, glm::mat4 &model) glm::mat4 getViewMatrix() { return matView; }
{
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 */ /* Handle keyboard inputs from a callback mechanism */
@@ -222,16 +190,9 @@ namespace Gloom
// Camera settings // Camera settings
GLfloat cMovementSpeed; GLfloat cMovementSpeed;
GLfloat cMouseSensitivity; GLfloat cMouseSensitivity;
GLfloat cFov;
GLfloat cAspect;
GLfloat cNearClip;
GLfloat cFarClip;
// Transformation matrices // View matrix
glm::mat4 matProj;
glm::mat4 matView; glm::mat4 matView;
glm::mat4 matModel;
}; };
} }

View File

@@ -10,10 +10,10 @@
#include <string> #include <string>
// Constants // Constants
const int mWidth = 1024; const int windowWidth = 1024;
const int mHeight = 768; const int windowHeight = 768;
const std::string mTitle = "OpenGL"; const std::string windowTitle = "OpenGL";
const GLint mResizable = GL_FALSE; const GLint windowResizable = GL_FALSE;
const int mSamples = 4; const int windowSamples = 4;
#endif #endif

View File

@@ -30,10 +30,11 @@ namespace Gloom
{ {
// Load GLSL Shader from source // Load GLSL Shader from source
std::ifstream fd(filename.c_str()); std::ifstream fd(filename.c_str());
if(fd.fail()) { if (fd.fail())
fprintf(stderr, {
fprintf(stderr,
"Something went wrong when attaching the Shader file at \"%s\".\n" "Something went wrong when attaching the Shader file at \"%s\".\n"
"The file may not exist or is currently inaccessible.\n", "The file may not exist or is currently inaccessible.\n",
filename.c_str()); filename.c_str());
return; return;
} }

View File

@@ -10,7 +10,7 @@
#include <cstdlib> #include <cstdlib>
// A callback which allows GLFW to report errors whenever they occur. // A callback which allows GLFW to report errors whenever they occur
static void glfwErrorCallback(int error, const char *description) static void glfwErrorCallback(int error, const char *description)
{ {
fprintf(stderr, "GLFW returned an error:\n\t%s (%i)\n", description, error); fprintf(stderr, "GLFW returned an error:\n\t%s (%i)\n", description, error);
@@ -30,24 +30,23 @@ GLFWwindow* initialise()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// Enable the GLFW runtime error callback function defined previously. // Enable the GLFW runtime error callback function defined previously.
glfwSetErrorCallback(glfwErrorCallback); glfwSetErrorCallback(glfwErrorCallback);
// Set additional window options // Set additional window options
glfwWindowHint(GLFW_RESIZABLE, mResizable); glfwWindowHint(GLFW_RESIZABLE, windowResizable);
glfwWindowHint(GLFW_SAMPLES, mSamples); // MSAA glfwWindowHint(GLFW_SAMPLES, windowSamples); // MSAA
// Create window using GLFW // Create window using GLFW
GLFWwindow* mWindow = glfwCreateWindow(mWidth, GLFWwindow* window = glfwCreateWindow(windowWidth,
mHeight, windowHeight,
mTitle.c_str(), windowTitle.c_str(),
nullptr, nullptr,
nullptr); nullptr);
// Ensure the window is set up correctly // Ensure the window is set up correctly
if (!mWindow) if (!window)
{ {
fprintf(stderr, "Could not open GLFW window\n"); fprintf(stderr, "Could not open GLFW window\n");
glfwTerminate(); glfwTerminate();
@@ -55,7 +54,7 @@ GLFWwindow* initialise()
} }
// Let the window be the current OpenGL context and initialise glad // Let the window be the current OpenGL context and initialise glad
glfwMakeContextCurrent(mWindow); glfwMakeContextCurrent(window);
gladLoadGL(); gladLoadGL();
// Print various OpenGL information to stdout // Print various OpenGL information to stdout
@@ -64,17 +63,17 @@ GLFWwindow* initialise()
printf("OpenGL\t %s\n", glGetString(GL_VERSION)); printf("OpenGL\t %s\n", glGetString(GL_VERSION));
printf("GLSL\t %s\n\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); printf("GLSL\t %s\n\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
return mWindow; return window;
} }
int main(int argc, char* argb[]) int main(int argc, char* argb[])
{ {
// Initialise window using GLFW // Initialise window using GLFW
GLFWwindow* mWindow = initialise(); GLFWwindow* window = initialise();
// Run an OpenGL application using this window // Run an OpenGL application using this window
runProgram(mWindow); runProgram(window);
// Terminate GLFW (no need to call glfwDestroyWindow) // Terminate GLFW (no need to call glfwDestroyWindow)
glfwTerminate(); glfwTerminate();

View File

@@ -3,36 +3,36 @@
#include "gloom/gloom.hpp" #include "gloom/gloom.hpp"
void runProgram(GLFWwindow* mWindow) void runProgram(GLFWwindow* window)
{ {
// Set GLFW callback mechanism(s) // Set GLFW callback mechanism(s)
glfwSetKeyCallback(mWindow, keyboardCallback); glfwSetKeyCallback(window, keyboardCallback);
// Enable depth (Z) buffer (accept "closest" fragment) // Enable depth (Z) buffer (accept "closest" fragment)
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
// Configure miscellaneous OpenGL settings // Configure miscellaneous OpenGL settings
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
// Set default colour after clearing the colour buffer // Set default colour after clearing the colour buffer
glClearColor(0.3f, 0.3f, 0.4f, 1.0f); glClearColor(0.3f, 0.3f, 0.4f, 1.0f);
// Set up your scene here (create Vertex Array Objects, etc) // Set up your scene here (create Vertex Array Objects, etc.)
// Rendering Loop // Rendering Loop
while (!glfwWindowShouldClose(mWindow)) while (!glfwWindowShouldClose(window))
{ {
// Clear colour and depth buffers // Clear colour and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw your scene here
// Handle other events // Handle other events
glfwPollEvents(); glfwPollEvents();
// Draw your scene here
// Flip buffers // Flip buffers
glfwSwapBuffers(mWindow); glfwSwapBuffers(window);
} }
} }

View File

@@ -10,7 +10,7 @@
// Main OpenGL program // Main OpenGL program
void runProgram(GLFWwindow* mWindow); void runProgram(GLFWwindow* window);
// GLFW callback mechanisms // GLFW callback mechanisms
@@ -28,29 +28,29 @@ inline void printGLError() {
switch(errorID) { switch(errorID) {
case GL_INVALID_ENUM: case GL_INVALID_ENUM:
errorString = "GL_INVALID_ENUM"; errorString = "GL_INVALID_ENUM";
break; break;
case GL_INVALID_OPERATION: case GL_INVALID_OPERATION:
errorString = "GL_INVALID_OPERATION"; errorString = "GL_INVALID_OPERATION";
break; break;
case GL_INVALID_FRAMEBUFFER_OPERATION: case GL_INVALID_FRAMEBUFFER_OPERATION:
errorString = "GL_INVALID_FRAMEBUFFER_OPERATION"; errorString = "GL_INVALID_FRAMEBUFFER_OPERATION";
break; break;
case GL_OUT_OF_MEMORY: case GL_OUT_OF_MEMORY:
errorString = "GL_OUT_OF_MEMORY"; errorString = "GL_OUT_OF_MEMORY";
break; break;
case GL_STACK_UNDERFLOW: case GL_STACK_UNDERFLOW:
errorString = "GL_STACK_UNDERFLOW"; errorString = "GL_STACK_UNDERFLOW";
break; break;
case GL_STACK_OVERFLOW: case GL_STACK_OVERFLOW:
errorString = "GL_STACK_OVERFLOW"; errorString = "GL_STACK_OVERFLOW";
break; break;
default: default:
errorString = "[Unknown error ID]"; errorString = "[Unknown error ID]";
break; break;
} }
fprintf(stderr, "An OpenGL error occurred (%i): %s.\n", fprintf(stderr, "An OpenGL error occurred (%i): %s.\n",
errorID, errorString.c_str()); errorID, errorString.c_str());
} }
} }