Added Namespace: charcoal

This commit is contained in:
elipzer 2018-09-12 17:03:46 -04:00
parent 3205680062
commit 77e8b0de5e
41 changed files with 1322 additions and 1227 deletions

View File

@ -4,9 +4,11 @@
#include "Util.h"
Application::Application(int width, int height)
: m_screen_size(width, height)
namespace charcoal
{
Application::Application(int width, int height)
: m_screen_size(width, height)
{
if (width < 0 || height < 0)
throw EXCEPTION("Invalid screen dimensions");
@ -44,10 +46,10 @@ Application::Application(int width, int height)
glfwSetKeyCallback(m_p_window, &GLFWInputManager::key_callback);
m_fps.prepare();
}
}
int Application::run()
{
int Application::run()
{
try
{
init();
@ -72,9 +74,10 @@ int Application::run()
glfwTerminate();
throw e;
}
}
}
void Application::base_close()
{
void Application::base_close()
{
close();
}
}

View File

@ -9,11 +9,13 @@
// TODO: Close without rendering next frame.
using namespace glm;
class Application
namespace charcoal
{
public:
using namespace glm;
class Application
{
public:
Application(int width = -1, int height = -1);
virtual ~Application() {}
@ -25,7 +27,7 @@ public:
const FPS& get_fps() const { return m_fps; }
protected:
protected:
// Called on initialization of the application (called by base_init)
virtual void init() = 0;
@ -45,7 +47,7 @@ protected:
GLFWInputManager m_glfw_input_manager;
FPS m_fps;
private:
private:
void base_close();
};
};
}

View File

@ -7,15 +7,12 @@
#include "Exception.h"
namespace
namespace charcoal
{
struct EmptyElement {};
}
template <typename VertexType, typename IndexType, int element_buffer_count = 0, typename Renderable = Renderable<VertexType, IndexType> >
class Batch
{
public:
template <typename VertexType, typename IndexType, int element_buffer_count = 0, typename Renderable = Renderable<VertexType, IndexType> >
class Batch
{
public:
typedef GLsizei SizeType;
Batch(
@ -103,7 +100,7 @@ public:
SizeType get_element_render_count() const { return m_element_render_count; }
protected:
protected:
virtual void setup_element_buffers() {}
virtual void setup_vao() = 0;
@ -116,8 +113,9 @@ protected:
GLuint m_vertex_vbo;
std::vector<GLuint> m_element_buffers;
private:
private:
GLuint m_vao;
GLuint m_index_vbo;
GLenum m_gl_index_type;
};
};
}

View File

@ -4,11 +4,13 @@
#include "Poseable.h"
using namespace glm;
class Camera
namespace charcoal
{
public:
using namespace glm;
class Camera
{
public:
Camera() : m_view_matrix(1.0f), m_projection_matrix(1.0f) {}
mat4 get_world_to_view_matrix() const { return m_projection_matrix * m_view_matrix; }
@ -17,7 +19,8 @@ public:
const mat4& get_projection_matrix() const { return m_projection_matrix; }
protected:
protected:
mat4 m_view_matrix;
mat4 m_projection_matrix;
};
};
}

View File

@ -2,46 +2,50 @@
#include "Exception.h"
Camera2D::Camera2D(const vec2& size, const vec2& position)
: Camera2D(vec3(size.x, size.y, 2.0f), vec3(position.x, position.y, 0.0f)) {}
Camera2D::Camera2D(const vec3& size, const vec3& position)
namespace charcoal
{
Camera2D::Camera2D(const vec2& size, const vec2& position)
: Camera2D(vec3(size.x, size.y, 2.0f), vec3(position.x, position.y, 0.0f))
{}
Camera2D::Camera2D(const vec3& size, const vec3& position)
{
update_size(size);
update_position(position);
}
}
void Camera2D::update_size(const vec3& size)
{
void Camera2D::update_size(const vec3& size)
{
m_size = size;
update_scale();
}
}
void Camera2D::update_position(const vec3& position)
{
void Camera2D::update_position(const vec3& position)
{
m_position = position;
}
}
void Camera2D::translate(const vec2& translation)
{
void Camera2D::translate(const vec2& translation)
{
translate(vec3(translation.x, translation.y, 0.0f));
}
}
void Camera2D::translate(const vec3& translation)
{
void Camera2D::translate(const vec3& translation)
{
m_position += translation;
}
}
void Camera2D::prerender()
{
void Camera2D::prerender()
{
m_view_matrix[3][0] = -m_position.x;
m_view_matrix[3][1] = -m_position.y;
m_view_matrix[3][2] = -m_position.z;
}
}
void Camera2D::update_scale()
{
void Camera2D::update_scale()
{
m_projection_matrix[0][0] = 2.0f / m_size.x;
m_projection_matrix[1][1] = 2.0f / m_size.y;
m_projection_matrix[2][2] = -2.0f / m_size.z;
}
}

View File

@ -4,11 +4,13 @@
#include <glm/glm.hpp>
using namespace glm;
class Camera2D : public Camera
namespace charcoal
{
public:
using namespace glm;
class Camera2D : public Camera
{
public:
Camera2D(const vec2& size, const vec2& position = vec2(0.0f, 0.0f));
Camera2D(const vec3& size = vec3(2.0f, 2.0f, 2.0f), const vec3& position = vec3(0.0f, 0.0f, 0.0f));
@ -23,9 +25,10 @@ public:
const vec3& get_size() const { return m_size; }
const vec3& get_position() const { return m_position; }
private:
private:
void update_scale();
vec3 m_size;
vec3 m_position;
};
};
}

View File

@ -2,7 +2,9 @@
#include <glm/gtc/matrix_transform.hpp>
Camera3D::Camera3D(
namespace charcoal
{
Camera3D::Camera3D(
float fov_y,
float aspect_ratio,
float znear,
@ -11,25 +13,25 @@ Camera3D::Camera3D(
const vec3& forward,
const vec3& up,
const vec3& right
)
{
)
{
update_frustum(fov_y, aspect_ratio, znear, zfar);
update_position(position);
update_orientation(forward, up, right);
}
}
void Camera3D::update_frustum(float fov_y, float aspect_ratio, float znear, float zfar)
{
void Camera3D::update_frustum(float fov_y, float aspect_ratio, float znear, float zfar)
{
m_projection_matrix = glm::perspective(fov_y, aspect_ratio, znear, zfar);
}
}
void Camera3D::update_position(const vec3& position)
{
void Camera3D::update_position(const vec3& position)
{
m_position = position;
}
}
void Camera3D::update_orientation(const vec3& forward, const vec3& up, const vec3& right)
{
void Camera3D::update_orientation(const vec3& forward, const vec3& up, const vec3& right)
{
vec3 real_forward;
vec3 real_up;
vec3 real_right;
@ -45,30 +47,30 @@ void Camera3D::update_orientation(const vec3& forward, const vec3& up, const vec
m_forward = vec4(real_forward, 0.0f);
m_up = vec4(real_up, 0.0f);
m_right = vec4(real_right, 0.0f);
}
}
void Camera3D::direct_update_orientation(const vec3& forward, const vec3& up, const vec3& right)
{
void Camera3D::direct_update_orientation(const vec3& forward, const vec3& up, const vec3& right)
{
m_forward = vec4(forward, 0.0f);
m_up = vec4(up, 0.0f);
m_right = vec4(right, 0.0f);
}
}
void Camera3D::translate(const vec3& translation)
{
void Camera3D::translate(const vec3& translation)
{
m_position += translation;
}
}
void Camera3D::rotate(const vec3& axis, float angle)
{
void Camera3D::rotate(const vec3& axis, float angle)
{
mat4 rotation = glm::rotate(mat4(1.0f), angle, axis);
m_forward = rotation * m_forward;
m_up = rotation * m_up;
m_right = rotation * m_right;
}
}
void Camera3D::prerender()
{
void Camera3D::prerender()
{
// This works but it mirrors the scene left and right?
m_view_matrix = mat4(
vec4(m_right.x, m_up.x, -m_forward.x, 0.0f),
@ -77,4 +79,5 @@ void Camera3D::prerender()
vec4(-glm::dot(vec3(m_right), m_position), -glm::dot(vec3(m_up), m_position), glm::dot(vec3(m_forward), m_position), 1.0f)
);
}
}

View File

@ -4,11 +4,13 @@
#include <glm/glm.hpp>
using namespace glm;
class Camera3D : public Camera
namespace charcoal
{
public:
using namespace glm;
class Camera3D : public Camera
{
public:
Camera3D(
float fov_y,
float aspect_ratio,
@ -39,9 +41,10 @@ public:
vec3 get_up() const { return vec3(m_up); }
vec3 get_right() const { return vec3(m_right); }
private:
private:
vec3 m_position;
vec4 m_forward;
vec4 m_up;
vec4 m_right;
};
};
}

View File

@ -2,8 +2,10 @@
#include "stdafx.h"
enum DrawMode : GLenum
namespace charcoal
{
enum DrawMode : GLenum
{
DRAW_POINTS = GL_POINTS,
DRAW_LINE_STRIP = GL_LINE_STRIP,
DRAW_LINE_LOOP = GL_LINE_LOOP,
@ -16,4 +18,5 @@ enum DrawMode : GLenum
DRAW_TRIANGLE_STRIP_ADJACENCY = GL_TRIANGLE_STRIP_ADJACENCY,
DRAW_TRIANGLES_ADJACENCY = GL_TRIANGLES_ADJACENCY,
DRAW_PATCHES = GL_PATCHES,
};
};
}

View File

@ -5,9 +5,11 @@
// TODO: This MUST be changed to something less generic
#define EXCEPTION(message) Exception(message, __FILE__, __LINE__)
class Exception
namespace charcoal
{
public:
class Exception
{
public:
Exception(const std::string& message, const std::string& file_name, int line)
: m_message(message), m_file_name(file_name), m_line(line)
{}
@ -16,8 +18,9 @@ public:
const std::string& get_file_name() { return m_file_name; }
int get_line() { return m_line; }
private:
private:
std::string m_message;
std::string m_file_name;
int m_line;
};
};
}

View File

@ -1,48 +1,51 @@
#include "FPS.h"
FPS::FPS(unsigned short frames /* = 2 */) :
m_iter(frames), m_frames(frames)
namespace charcoal
{
FPS::FPS(unsigned short frames /* = 2 */) :
m_iter(frames), m_frames(frames)
{
if (frames < 2)
throw "Frames is too small";
m_clocks = new clock_t[m_frames];
}
}
FPS::~FPS()
{
FPS::~FPS()
{
if (m_clocks)
delete[] m_clocks;
}
}
const clock_t& FPS::get_clock() const
{
const clock_t& FPS::get_clock() const
{
return m_clocks[m_iter];
}
}
float FPS::get_spf() const
{
float FPS::get_spf() const
{
clock_t diff = (m_clocks[m_iter] - m_clocks[(m_iter + 1) % m_frames]) / (m_frames - 1);
return ((float)diff) / CLOCKS_PER_SEC;
}
}
float FPS::get_fps() const
{
float FPS::get_fps() const
{
clock_t diff = (m_clocks[m_iter] - m_clocks[(m_iter + 1) % m_frames]) / (m_frames - 1);
return ((float)CLOCKS_PER_SEC) / diff;
}
}
void FPS::prepare()
{
void FPS::prepare()
{
clock_t c = clock();
for (unsigned short i = 0; i < m_frames; ++i)
m_clocks[i] = c;
}
}
float FPS::mark()
{
float FPS::mark()
{
(++m_iter) %= m_frames;
m_clocks[m_iter] = clock();
clock_t diff = (m_clocks[m_iter] - m_clocks[(m_iter > 0 ? m_iter - 1 : m_frames - 1)]);
return ((float)diff) / CLOCKS_PER_SEC;
}
}

View File

@ -2,16 +2,18 @@
#include <ctime>
//A class to handle the FPS of the game
//Calculations are done per the number of frames specified
//Frames must be a value from 2 - 255
//Recommended numbers for frames is 2 (Instant), 5 (Slightly Smoothed), 60 (Smoothed)
//Max number for frames = 255
//Lower frames = jumpy, instant updates
//Higher frames = smoother, flowing updates
class FPS final
namespace charcoal
{
public:
//A class to handle the FPS of the game
//Calculations are done per the number of frames specified
//Frames must be a value from 2 - 255
//Recommended numbers for frames is 2 (Instant), 5 (Slightly Smoothed), 60 (Smoothed)
//Max number for frames = 255
//Lower frames = jumpy, instant updates
//Higher frames = smoother, flowing updates
class FPS final
{
public:
FPS(unsigned short frames = 2);
~FPS();
@ -35,10 +37,11 @@ public:
//(m_clock - m_prevClock) / CLOCKS_PER_SEC
float mark();
private:
private:
//m_iter marks the place of the next mark
unsigned short m_iter;
unsigned short m_frames;
clock_t* m_clocks = nullptr;
};
};
}

View File

@ -2,23 +2,25 @@
#include "Exception.h"
std::map<GLFWwindow*, GLFWInputManager*> GLFWInputManager::s_glfw_windows;
GLFWInputManager::~GLFWInputManager()
namespace charcoal
{
std::map<GLFWwindow*, GLFWInputManager*> GLFWInputManager::s_glfw_windows;
GLFWInputManager::~GLFWInputManager()
{
s_glfw_windows.erase(m_p_window);
}
}
void GLFWInputManager::init(GLFWwindow* p_window)
{
void GLFWInputManager::init(GLFWwindow* p_window)
{
if (m_p_window != nullptr)
throw EXCEPTION("GLFWInputManager Already Initialized.");
m_p_window = p_window;
s_glfw_windows.insert(std::map<GLFWwindow*, GLFWInputManager*>::value_type(m_p_window, this));
}
}
void GLFWInputManager::key_callback(GLFWwindow* p_window, int key, int scancode, int action, int mods)
{
void GLFWInputManager::key_callback(GLFWwindow* p_window, int key, int scancode, int action, int mods)
{
std::map<GLFWwindow*, GLFWInputManager*>::iterator iter = s_glfw_windows.find(p_window);
if (iter == s_glfw_windows.end())
return; // Ignore Unknown Windows
@ -27,8 +29,9 @@ void GLFWInputManager::key_callback(GLFWwindow* p_window, int key, int scancode,
input_manager.key_down(key);
else if (action == GLFW_RELEASE)
input_manager.key_up(key);
else if (action == GLFW_REPEAT) { /* Ignored */}
else if (action == GLFW_REPEAT) { /* Ignored */ }
else
throw EXCEPTION("Invalid GLFW Key Action: " + std::to_string(action) + " (" + std::to_string(scancode) + ")");
}
}

View File

@ -6,16 +6,19 @@
#include <map>
class GLFWInputManager : public InputManager
namespace charcoal
{
public:
class GLFWInputManager : public InputManager
{
public:
~GLFWInputManager();
void init(GLFWwindow* p_window);
static void key_callback(GLFWwindow* p_window, int key, int scancode, int action, int mods);
private:
private:
GLFWwindow* m_p_window = nullptr;
static std::map<GLFWwindow*, GLFWInputManager*> s_glfw_windows;
};
};
}

View File

@ -1,81 +1,83 @@
#include "InputManager.h"
InputManager::InputManager() {}
InputManager::~InputManager() {}
void InputManager::mark()
namespace charcoal
{
InputManager::InputManager() {}
InputManager::~InputManager() {}
void InputManager::mark()
{
m_keys_pressed.clear();
m_mouse_delta = { 0, 0 };
m_scroll_distance = 0;
}
}
void InputManager::key_down(KeyCode key_code)
{
void InputManager::key_down(KeyCode key_code)
{
if (!m_keys_down[key_code])
{
m_keys_down[key_code] = true;
m_keys_pressed[key_code] = true;
}
}
}
void InputManager::key_up(KeyCode key_code)
{
void InputManager::key_up(KeyCode key_code)
{
m_keys_down[key_code] = false;
m_keys_pressed[key_code] = false;
}
}
void InputManager::mouse_move(const ivec2& position)
{
void InputManager::mouse_move(const ivec2& position)
{
m_mouse_delta += position - m_mouse_position;
m_mouse_position = position;
}
}
void InputManager::mouse_scroll(int distance)
{
void InputManager::mouse_scroll(int distance)
{
m_scroll_distance += distance;
}
}
bool InputManager::is_key_down(KeyCode key_code) const
{
bool InputManager::is_key_down(KeyCode key_code) const
{
auto iter = m_keys_down.find(key_code);
if (iter == m_keys_down.end())
return false;
else
return iter->second;
}
}
bool InputManager::is_key_pressed(KeyCode key_code) const
{
bool InputManager::is_key_pressed(KeyCode key_code) const
{
auto iter = m_keys_pressed.find(key_code);
if (iter == m_keys_pressed.end())
return false;
else
return iter->second;
}
}
bool InputManager::is_key_released(KeyCode key_code) const
{
bool InputManager::is_key_released(KeyCode key_code) const
{
auto iter = m_keys_pressed.find(key_code);
if (iter == m_keys_pressed.end())
return false;
else
return !iter->second;
}
}
const ivec2& InputManager::get_mouse_position() const
{
const ivec2& InputManager::get_mouse_position() const
{
return m_mouse_position;
}
}
const ivec2& InputManager::get_mouse_delta() const
{
const ivec2& InputManager::get_mouse_delta() const
{
return m_mouse_delta;
}
}
const int& InputManager::get_scroll_distance() const
{
const int& InputManager::get_scroll_distance() const
{
return m_scroll_distance;
}
}

View File

@ -44,11 +44,13 @@
#define K_Y 0x59
#define K_Z 0x5A
using namespace glm;
class InputManager
namespace charcoal
{
public:
using namespace glm;
class InputManager
{
public:
typedef int KeyCode;
InputManager();
@ -64,13 +66,13 @@ public:
const ivec2& get_mouse_delta() const;
const int& get_scroll_distance() const;
protected:
protected:
void key_down(KeyCode key_code);
void key_up(KeyCode key_code);
void mouse_move(const ivec2& position);
void mouse_scroll(int distance);
private:
private:
//Keys pressed since the last frame
//The existance of a value in this array means that the key has changed to that state
//since the last mark() call.
@ -90,4 +92,5 @@ private:
int m_scroll_distance;
//TODO: Controller Movement
};
};
}

View File

@ -1,8 +1,10 @@
#pragma once
template <typename VertexType, typename IndexType>
struct Mesh
namespace charcoal
{
template <typename VertexType, typename IndexType>
struct Mesh
{
typedef VertexType VertexType;
typedef IndexType IndexType;
@ -10,4 +12,5 @@ struct Mesh
unsigned int vertex_count = 0;
IndexType* indices = nullptr;
unsigned int index_count = 0;
};
};
}

View File

@ -8,16 +8,18 @@
#include "Mesh.h"
template <typename VertexType, typename IndexType>
class MeshFactory
namespace charcoal
{
public:
template <typename VertexType, typename IndexType>
class MeshFactory
{
public:
typedef Mesh<VertexType, IndexType> MeshType;
private:
private:
MeshFactory() {} // Prevent instanciation of this static class
public:
public:
typedef unsigned int SizeType;
virtual ~MeshFactory()
@ -358,7 +360,7 @@ public:
}
}
protected:
protected:
static MeshType* create_mesh(unsigned int vertex_count, unsigned int index_count)
{
MeshType* mesh = new MeshType();
@ -370,9 +372,10 @@ protected:
return mesh;
}
private:
private:
static std::vector<MeshType*> m_meshes;
};
};
template <typename VertexType, typename IndexType>
std::vector<Mesh<VertexType, IndexType>*> MeshFactory<VertexType, IndexType>::m_meshes = std::vector<Mesh<VertexType, IndexType>*>();
template <typename VertexType, typename IndexType>
std::vector<Mesh<VertexType, IndexType>*> MeshFactory<VertexType, IndexType>::m_meshes = std::vector<Mesh<VertexType, IndexType>*>();
}

View File

@ -6,6 +6,8 @@
#include "MySimple3DScene.h"
#include "MySimpleCubeScene.h"
using namespace charcoal;
class MyApplication :
public Application
{

View File

@ -4,6 +4,8 @@
#include "MyBasicShaderProgram.h"
using namespace charcoal;
class MyBasicScene : public Scene
{
public:

View File

@ -5,6 +5,8 @@
#include "Mesh.h"
#include "Renderable.h"
using namespace charcoal;
class MyBasicShaderProgram : public ShaderProgram
{
public:

View File

@ -6,6 +6,8 @@
#include "MySimpleShaderProgram.h"
using namespace charcoal;
class MyBatch : public Batch<MySimpleShaderProgram::Vertex, MySimpleShaderProgram::Index, 2>
{
public:

View File

@ -56,7 +56,7 @@ void MySimple2DScene::update(float delta_time, clock_t clock)
else
brightness = (float)(intervals - c) / half_interval;
radians = (float)egm::TAU * c / intervals;
radians = (float)TAU * c / intervals;
{
MySimpleShaderProgram::Color& color = m_batch.get_color(0);

View File

@ -7,6 +7,8 @@
#include "MyBatch.h"
#include "MySimpleShaderProgram.h"
using namespace charcoal;
class MySimple2DScene : public Scene
{
public:

View File

@ -20,7 +20,7 @@ MySimple3DScene::MySimple3DScene(Application& application)
MySimpleShaderProgram::Vertex(2.0f, -1.0f, 0.0f)
), DrawMode::DRAW_TRIANGLES),
m_batch(&m_shape, 2),
m_camera((float)egm::TAU_1_4, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, -5.0f))
m_camera((float)TAU_1_4, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, -5.0f))
{}
MySimple3DScene::~MySimple3DScene()
@ -56,7 +56,7 @@ void MySimple3DScene::update(float delta_time, clock_t clock)
else
brightness = (float)(intervals - c) / half_interval;
radians = (float)egm::TAU * c / intervals;
radians = (float)TAU * c / intervals;
{
MySimpleShaderProgram::Color& color = m_batch.get_color(0);

View File

@ -7,6 +7,8 @@
#include "MyBatch.h"
#include "MySimpleShaderProgram.h"
using namespace charcoal;
class MySimple3DScene : public Scene
{
public:

View File

@ -19,7 +19,7 @@ MySimpleCubeScene::MySimpleCubeScene(Application& application)
MySimpleShaderProgram::Vertex( 1.0f, -1.0f, 1.0f)
), DrawMode::DRAW_TRIANGLES),
m_batch(&m_shape, 1),
m_camera((float)egm::TAU_1_4, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, -5.0f))
m_camera((float)TAU_1_4, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, -5.0f))
{}
MySimpleCubeScene::~MySimpleCubeScene()
@ -55,7 +55,7 @@ void MySimpleCubeScene::update(float delta_time, clock_t clock)
else
brightness = (float)(intervals - c) / half_interval;
radians = (float)egm::TAU * c / intervals;
radians = (float)TAU * c / intervals;
{
MySimpleShaderProgram::Color& color = m_batch.get_color(0);
@ -65,7 +65,7 @@ void MySimpleCubeScene::update(float delta_time, clock_t clock)
color.a = 1.0f;
Poseable& pose = m_batch.get_pose(0);
pose.rotate(glm::normalize(vec3(1.0f, 1.0f, 0.0f)), (float)egm::TAU_1_2 * delta_time);
pose.rotate(glm::normalize(vec3(1.0f, 1.0f, 0.0f)), (float)TAU_1_2 * delta_time);
pose.update_position(vec3(3 * (float)cos(radians), 0.0f, 0.0f));
}
@ -83,7 +83,7 @@ void MySimpleCubeScene::update(float delta_time, clock_t clock)
if (m_input_manager.is_key_down(GLFW_KEY_C)) camera_rotation -= 1;
m_camera.translate(camera_translation * delta_time);
m_camera.rotate(vec3(0.0f, 1.0f, 0.0f), camera_rotation * (float)egm::TAU_1_8 * delta_time);
m_camera.rotate(vec3(0.0f, 1.0f, 0.0f), camera_rotation * (float)TAU_1_8 * delta_time);
}
void MySimpleCubeScene::prerender()

View File

@ -7,6 +7,8 @@
#include "MyBatch.h"
#include "MySimpleShaderProgram.h"
using namespace charcoal;
class MySimpleCubeScene : public Scene
{
public:

View File

@ -5,6 +5,8 @@
#include "Mesh.h"
#include "Renderable.h"
using namespace charcoal;
class MySimpleShaderProgram : public ShaderProgram
{
public:

View File

@ -146,24 +146,15 @@
<ClInclude Include="ShaderProgram.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
<ClInclude Include="DrawMode.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
<ClInclude Include="Shader.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
<ClInclude Include="Mesh.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
<ClInclude Include="Camera.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
<ClInclude Include="Batch.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
<ClInclude Include="Renderable.h">
<Filter>Header Files\Engine\Rendering</Filter>
</ClInclude>
<ClInclude Include="MyBatch.h">
<Filter>Header Files\Example\Rendering</Filter>
</ClInclude>
@ -200,6 +191,15 @@
<ClInclude Include="MySimpleCubeScene.h">
<Filter>Header Files\Example\Application</Filter>
</ClInclude>
<ClInclude Include="DrawMode.h">
<Filter>Header Files\Engine</Filter>
</ClInclude>
<ClInclude Include="Renderable.h">
<Filter>Header Files\Engine</Filter>
</ClInclude>
<ClInclude Include="Mesh.h">
<Filter>Header Files\Engine</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="MySimpleVS.glsl">

View File

@ -2,28 +2,31 @@
#include <glm/gtc/matrix_transform.hpp>
Poseable::Poseable(
namespace charcoal
{
Poseable::Poseable(
const vec3& position,
const vec3& forward,
const vec3& up,
const vec3& right
)
)
: m_orientation_matrix(
vec4(right, 0.0f),
vec4(up, 0.0f),
vec4(forward, 0.0f),
vec4(position, 1.0f)
) {}
)
{}
void Poseable::update_position(const vec3& position)
{
void Poseable::update_position(const vec3& position)
{
m_orientation_matrix[3][0] = position.x;
m_orientation_matrix[3][1] = position.y;
m_orientation_matrix[3][2] = position.z;
}
}
void Poseable::update_orientation(const vec3& forward, const vec3& up, const vec3& right)
{
void Poseable::update_orientation(const vec3& forward, const vec3& up, const vec3& right)
{
m_orientation_matrix[0][0] = right.x;
m_orientation_matrix[0][1] = right.y;
m_orientation_matrix[0][2] = right.z;
@ -35,19 +38,20 @@ void Poseable::update_orientation(const vec3& forward, const vec3& up, const vec
m_orientation_matrix[2][0] = forward.x;
m_orientation_matrix[2][1] = forward.y;
m_orientation_matrix[2][2] = forward.z;
}
}
void Poseable::translate(const vec3& translation)
{
void Poseable::translate(const vec3& translation)
{
m_orientation_matrix[3][0] += translation.x;
m_orientation_matrix[3][1] += translation.y;
m_orientation_matrix[3][2] += translation.z;
}
}
void Poseable::rotate(const vec3& axis, float angle)
{
void Poseable::rotate(const vec3& axis, float angle)
{
mat4 rotation_matrix = glm::rotate(glm::mat4(1.0f), angle, axis);
m_orientation_matrix[0] = rotation_matrix * m_orientation_matrix[0];
m_orientation_matrix[1] = rotation_matrix * m_orientation_matrix[1];
m_orientation_matrix[2] = rotation_matrix * m_orientation_matrix[2];
}
}

View File

@ -2,11 +2,13 @@
#include <glm/glm.hpp>
using namespace glm;
class Poseable
namespace charcoal
{
public:
using namespace glm;
class Poseable
{
public:
// Assumes that forward, up, and right are orthogonal and normalized
Poseable(
const vec3& position = vec3(0.0f, 0.0f, 0.0f),
@ -24,6 +26,7 @@ public:
const mat4& get_orientation_matrix() const { return m_orientation_matrix; }
protected:
protected:
mat4 m_orientation_matrix;
};
};
}

View File

@ -8,22 +8,26 @@
#include "DrawMode.h"
#include "Batch.h"
template <typename VertexType, typename IndexType>
class Renderable final
namespace charcoal
{
public:
template <typename VertexType, typename IndexType>
class Renderable final
{
public:
typedef VertexType VertexType;
typedef IndexType IndexType;
typedef Mesh<VertexType, IndexType> MeshType;
Renderable(const MeshType* mesh, const DrawMode& draw_mode)
: m_p_mesh(mesh), m_draw_mode(draw_mode) {}
: m_p_mesh(mesh), m_draw_mode(draw_mode)
{}
const MeshType* get_mesh() const { return m_p_mesh; }
const DrawMode& get_draw_mode() const { return m_draw_mode; }
private:
private:
const MeshType* m_p_mesh = nullptr;
DrawMode m_draw_mode;
};
};
}

View File

@ -6,14 +6,17 @@
#include "Application.h"
class Scene
namespace charcoal
{
public:
class Scene
{
public:
Scene(Application& application)
: m_screen_size(application.get_screen_size()),
m_input_manager(application.get_input_manager()),
m_fps(application.get_fps()) {};
virtual ~Scene() { };
m_fps(application.get_fps())
{};
virtual ~Scene() {};
// Called when the scene is ready to be initialized
virtual void init() = 0;
@ -35,9 +38,9 @@ public:
// Called when the frame is being rendered
virtual void render() = 0;
protected:
protected:
const ivec2& m_screen_size;
const GLFWInputManager& m_input_manager;
const FPS& m_fps;
};
};
}

View File

@ -2,8 +2,10 @@
#include "Exception.h"
Shader::Shader(const std::string& source, ShaderType type)
namespace charcoal
{
Shader::Shader(const std::string& source, ShaderType type)
{
GLenum gl_shader_type;
switch (type)
{
@ -41,11 +43,12 @@ Shader::Shader(const std::string& source, ShaderType type)
m_shader = 0;
throw EXCEPTION("Error compiling shader.");
}
}
}
Shader::~Shader() {}
Shader::~Shader() {}
GLuint Shader::get_shader() const
{
GLuint Shader::get_shader() const
{
return m_shader;
}
}

View File

@ -4,21 +4,23 @@
#include <string>
enum ShaderType
namespace charcoal
{
enum ShaderType
{
VERTEX_SHADER,
FRAGMENT_SHADER
};
};
class Shader
{
public:
class Shader
{
public:
Shader(const std::string& source, ShaderType type);
~Shader();
GLuint get_shader() const;
private:
private:
GLuint m_shader;
};
};
}

View File

@ -1,31 +1,34 @@
#include "ShaderProgram.h"
ShaderProgram::ShaderProgram()
namespace charcoal
{
ShaderProgram::ShaderProgram()
: m_program(glCreateProgram())
{}
{}
ShaderProgram::~ShaderProgram()
{
ShaderProgram::~ShaderProgram()
{
glDeleteProgram(m_program);
}
}
void ShaderProgram::attach_shader(const Shader& shader)
{
void ShaderProgram::attach_shader(const Shader& shader)
{
glAttachShader(m_program, shader.get_shader());
}
}
void ShaderProgram::link()
{
void ShaderProgram::link()
{
glLinkProgram(m_program);
// TODO: Error handling
}
}
void ShaderProgram::use() const
{
void ShaderProgram::use() const
{
glUseProgram(m_program);
}
}
GLuint ShaderProgram::get_program() const
{
GLuint ShaderProgram::get_program() const
{
return m_program;
}
}

View File

@ -6,9 +6,11 @@
#include "Shader.h"
class ShaderProgram
namespace charcoal
{
public:
class ShaderProgram
{
public:
ShaderProgram();
virtual ~ShaderProgram();
@ -16,10 +18,11 @@ public:
GLuint get_program() const;
protected:
protected:
void attach_shader(const Shader& shader);
void link();
private:
private:
GLuint m_program = 0;
};
};
}

View File

@ -9,8 +9,10 @@
#include "Exception.h"
std::string Util::load_file(const std::string& path)
namespace charcoal
{
std::string Util::load_file(const std::string& path)
{
std::ifstream input_stream;
try
{
@ -33,51 +35,52 @@ std::string Util::load_file(const std::string& path)
{
throw EXCEPTION("Unable to access file: " + path);
}
}
}
void Util::set_console_position(short x, short y)
{
void Util::set_console_position(short x, short y)
{
COORD pos = { x, y };
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(output, pos);
}
}
void Util::print_matrix(const mat4& matrix)
{
void Util::print_matrix(const mat4& matrix)
{
std::cout << std::showpos << std::scientific << std::setprecision(5)
<< "[ " << matrix[0][0] << " " << matrix[1][0] << " " << matrix[2][0] << " " << matrix[3][0] << " ]" << std::endl
<< "[ " << matrix[0][1] << " " << matrix[1][1] << " " << matrix[2][1] << " " << matrix[3][1] << " ]" << std::endl
<< "[ " << matrix[0][2] << " " << matrix[1][2] << " " << matrix[2][2] << " " << matrix[3][2] << " ]" << std::endl
<< "[ " << matrix[0][3] << " " << matrix[1][3] << " " << matrix[2][3] << " " << matrix[3][3] << " ]" << std::endl;
}
}
void Util::print_vec(const vec2& v)
{
void Util::print_vec(const vec2& v)
{
std::cout << std::showpos << std::fixed << std::setprecision(5)
<< "[" << v.x << "]" << std::endl
<< "[" << v.y << "]" << std::endl;
}
}
void Util::print_vec(const vec3& v)
{
void Util::print_vec(const vec3& v)
{
std::cout << std::showpos << std::fixed << std::setprecision(5)
<< "[" << v.x << "]" << std::endl
<< "[" << v.y << "]" << std::endl
<< "[" << v.z << "]" << std::endl;
}
}
void Util::print_vec(const vec4& v)
{
void Util::print_vec(const vec4& v)
{
std::cout << std::showpos << std::fixed << std::setprecision(5)
<< "[" << v.x << "]" << std::endl
<< "[" << v.y << "]" << std::endl
<< "[" << v.z << "]" << std::endl
<< "[" << v.w << "]" << std::endl;
}
}
void Util::_check_gl_err(const char* file_name, int line)
{
void Util::_check_gl_err(const char* file_name, int line)
{
GLenum gl_err = glGetError();
if (gl_err != GL_NO_ERROR)
throw Exception(("Caught OpenGL Error: " + std::string((const char*)gluErrorString(gl_err)) + " (" + std::to_string(gl_err) + ")").c_str(), file_name, line);
}
}

View File

@ -5,15 +5,17 @@
#include <glm/glm.hpp>
using namespace glm;
#define DISPLAY_DIGITS 5
#define CHECK_GL_ERR() Util::_check_gl_err(__FILE__, __LINE__)
class Util
namespace charcoal
{
public:
using namespace glm;
class Util
{
public:
static std::string load_file(const std::string& path);
static void set_console_position(short x, short y);
static void print_matrix(const mat4& matrix);
@ -23,4 +25,5 @@ public:
// Use the CHECK_GL_ERR macro unless you know what you are doing with this function
static void _check_gl_err(const char* file_name, int line);
};
};
}

View File

@ -3,7 +3,7 @@
#define DEG_TO_RAD(x) x * egm::TAU_1_360;
#define RAD_TO_DEG(x)
namespace egm
namespace charcoal
{
// 2PI = TAU: 6.2831853071795864769252867665590