diff --git a/include/lol/BoundingBox.hpp b/include/lol/BoundingBox.hpp index fe7ff0e..f822b68 100644 --- a/include/lol/BoundingBox.hpp +++ b/include/lol/BoundingBox.hpp @@ -1,15 +1,20 @@ #pragma once -struct Rect +namespace lol { - float x, y; - float w, h; -}; -struct BoundingBox -{ - float x, y, z; - float w, h, d; -}; + struct Rect + { + float x, y; + float w, h; + }; -typedef BoundingBox BBox; \ No newline at end of file + struct BoundingBox + { + float x, y, z; + float w, h, d; + }; + + typedef BoundingBox BBox; + +} \ No newline at end of file diff --git a/include/lol/Camera.hpp b/include/lol/Camera.hpp index 210bd3c..a22ff41 100644 --- a/include/lol/Camera.hpp +++ b/include/lol/Camera.hpp @@ -4,64 +4,69 @@ #include "Transformable.hpp" #include "Drawable.hpp" -// TODO: Find better name -class CameraBase : public Transformable +namespace lol { -public: - // "Scaling" doesn't really makes sense for a camera - void GetScale() = delete; - void SetScale(const glm::vec3&) = delete; - void Scale(const glm::vec3&) = delete; - inline void LookAt(const glm::vec3& target) + // TODO: Find better name + class CameraBase : public Transformable { - transformation = glm::lookAt(position, target, glm::vec3(0.0f, 1.0f, 0.0f)); - } + public: + // "Scaling" doesn't really makes sense for a camera + void GetScale() = delete; + void SetScale(const glm::vec3&) = delete; + void Scale(const glm::vec3&) = delete; - inline const glm::mat4& GetView() const + inline void LookAt(const glm::vec3& target) + { + transformation = glm::lookAt(position, target, glm::vec3(0.0f, 1.0f, 0.0f)); + } + + inline const glm::mat4& GetView() const + { + return transformation; + } + + inline const glm::mat4& GetProjection() const + { + return projection; + } + + inline void Draw(const Drawable& drawable) const + { + drawable.Draw(*this); + } + + protected: + glm::mat4 projection; + }; + + + class Camera : public CameraBase { - return transformation; - } + public: + Camera(float fov = 90.0f, float aspect = 1.0f, float zNear = 0.01f, float zFar = 100.0f) + { + projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar); + } - inline const glm::mat4& GetProjection() const + inline void Update(float fov, float aspect, float zNear, float zFar) + { + projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar); + } + }; + + class OrthogonalCamera : public CameraBase { - return projection; - } + public: + OrthogonalCamera(float left = -1.0f, float right = 1.0f, float bottom = -1.0f, float top = 1.0f, float zNear = -100.0f, float zFar = 100.0f) + { + projection = glm::ortho(left, right, bottom, top, zNear, zFar); + } - inline void Draw(const Drawable& drawable) const - { - drawable.Draw(*this); - } + inline void Update(float left, float right, float bottom, float top, float zNear, float zFar) + { + projection = glm::ortho(left, right, bottom, top, zNear, zFar); + } + }; -protected: - glm::mat4 projection; -}; - - -class Camera : public CameraBase -{ -public: - Camera(float fov = 90.0f, float aspect = 1.0f, float zNear = 0.01f, float zFar = 100.0f) - { - projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar); - } - - inline void Update(float fov, float aspect, float zNear, float zFar) - { - projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar); - } -}; - -class OrthogonalCamera : public CameraBase -{ -public: - OrthogonalCamera(float left = -1.0f, float right = 1.0f, float bottom = -1.0f, float top = 1.0f, float zNear = -100.0f, float zFar = 100.0f) - { - projection = glm::ortho(left, right, bottom, top, zNear, zFar); - } - - inline void Update(float left, float right, float bottom, float top, float zNear, float zFar) - { - projection = glm::ortho(left, right, bottom, top, zNear, zFar); - } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/include/lol/Drawable.hpp b/include/lol/Drawable.hpp index 88f5540..77ace50 100644 --- a/include/lol/Drawable.hpp +++ b/include/lol/Drawable.hpp @@ -4,35 +4,40 @@ #include "VertexArrayObject.hpp" #include "Shader.hpp" -class CameraBase; - -enum class PrimitiveType +namespace lol { - Lines = GL_LINES, - LineStrip = GL_LINE_STRIP, - LineLoop = GL_LINE_LOOP, - Triangles = GL_TRIANGLES, - TriangleStrip = GL_TRIANGLE_STRIP, - TriangleFan = GL_TRIANGLE_FAN -}; + class CameraBase; -class Drawable -{ -public: - Drawable(const Drawable& other) = delete; - void operator=(const Drawable& other) = delete; + enum class PrimitiveType + { + Lines = GL_LINES, + LineStrip = GL_LINE_STRIP, + LineLoop = GL_LINE_LOOP, - virtual void PreRender(const CameraBase& camera) const { }; - void Draw(const CameraBase& camera) const; - void SetPrimitiveType(PrimitiveType type); + Triangles = GL_TRIANGLES, + TriangleStrip = GL_TRIANGLE_STRIP, + TriangleFan = GL_TRIANGLE_FAN + }; -protected: - Drawable() {} + class Drawable + { + public: + Drawable(const Drawable& other) = delete; + void operator=(const Drawable& other) = delete; -protected: - VertexArrayObject vao; - Shader shader; + virtual void PreRender(const CameraBase& camera) const { }; + void Draw(const CameraBase& camera) const; + void SetPrimitiveType(PrimitiveType type); - PrimitiveType type = PrimitiveType::Triangles; -}; \ No newline at end of file + protected: + Drawable() {} + + protected: + VertexArrayObject vao; + Shader shader; + + PrimitiveType type = PrimitiveType::Triangles; + }; + +} \ No newline at end of file diff --git a/include/lol/ObjectManager.hpp b/include/lol/ObjectManager.hpp index f8bf050..5441d02 100644 --- a/include/lol/ObjectManager.hpp +++ b/include/lol/ObjectManager.hpp @@ -3,69 +3,74 @@ #include #include -/** - * Some objects should only exist once but be available to multiple objects (e.g. multiple - * models sharing the same VAO and shaders. Any object can register objects here and other - * objects can then retrieve them if they have the ID. - * - * Objects are stored and returned as shared_ptr's, so even if a part of the program deletes - * an object from the manager, any part of the program that has the objects stored in them will - * not break and still work. But realistically this shouldn't happen in the first place. - * As a consequence, even if no objects are using an object stored in here it will continue to - * exist. - */ -template -class ObjectManager +namespace lol { -public: - static ObjectManager& GetInstance() - { - static ObjectManager instance; - return instance; - } - -public: - ObjectManager(const ObjectManager&) = delete; - void operator=(const ObjectManager&) = delete; /** - * Add new (existing) object to manager + * Some objects should only exist once but be available to multiple objects (e.g. multiple + * models sharing the same VAO and shaders. Any object can register objects here and other + * objects can then retrieve them if they have the ID. + * + * Objects are stored and returned as shared_ptr's, so even if a part of the program deletes + * an object from the manager, any part of the program that has the objects stored in them will + * not break and still work. But realistically this shouldn't happen in the first place. + * As a consequence, even if no objects are using an object stored in here it will continue to + * exist. */ - inline void Register(unsigned int id, std::shared_ptr obj) + template + class ObjectManager { - objects.insert(std::make_pair(id, obj)); - } + public: + static ObjectManager& GetInstance() + { + static ObjectManager instance; + return instance; + } - /** - * Remove object from manager - */ - inline void Delete(unsigned int id) - { - objects.erase(id); - } + public: + ObjectManager(const ObjectManager&) = delete; + void operator=(const ObjectManager&) = delete; - /** - * Retrieve object from manager - */ - inline std::shared_ptr Get(unsigned int id) - { - auto it = objects.find(id); + /** + * Add new (existing) object to manager + */ + inline void Register(unsigned int id, std::shared_ptr obj) + { + objects.insert(std::make_pair(id, obj)); + } - if (it == objects.end()) - return nullptr; + /** + * Remove object from manager + */ + inline void Delete(unsigned int id) + { + objects.erase(id); + } - return it->second; - } + /** + * Retrieve object from manager + */ + inline std::shared_ptr Get(unsigned int id) + { + auto it = objects.find(id); -private: - ObjectManager() {} + if (it == objects.end()) + return nullptr; -private: - std::map> objects; -}; + return it->second; + } -class AbstractShader; -class AbstractVertexArrayObject; + private: + ObjectManager() {} -typedef ObjectManager VAOManager; -typedef ObjectManager ShaderManager; \ No newline at end of file + private: + std::map> objects; + }; + + class AbstractShader; + class AbstractVertexArrayObject; + + typedef ObjectManager VAOManager; + typedef ObjectManager ShaderManager; + +} \ No newline at end of file diff --git a/include/lol/Shader.hpp b/include/lol/Shader.hpp index 1ae6010..2d8b0d7 100644 --- a/include/lol/Shader.hpp +++ b/include/lol/Shader.hpp @@ -1,39 +1,44 @@ #pragma once -#include - #include #include #include -class AbstractShader +#include + +namespace lol { - friend class ShaderFactory; -public: - AbstractShader(const std::string& vertexShader, const std::string& fragmentShader); - AbstractShader(const AbstractShader& other) = delete; - ~AbstractShader(); - - inline bool Good() { return id != 0; } - void Use(); - - void SetUniform(const std::string& name, const glm::mat4& value); - void SetUniform(const std::string& name, const glm::vec4& value); - -private: - unsigned int id; - - bool recording = false; -}; - -typedef std::shared_ptr Shader; - -class ShaderFactory -{ -public: - inline static Shader Produce(const std::string& vertexShader, const std::string& fragmentShader) + class AbstractShader { - return std::make_shared(vertexShader, fragmentShader); - } -}; \ No newline at end of file + friend class ShaderFactory; + + public: + AbstractShader(const std::string& vertexShader, const std::string& fragmentShader); + AbstractShader(const AbstractShader& other) = delete; + ~AbstractShader(); + + inline bool Good() { return id != 0; } + void Use(); + + void SetUniform(const std::string& name, const glm::mat4& value); + void SetUniform(const std::string& name, const glm::vec4& value); + + private: + unsigned int id; + + bool recording = false; + }; + + typedef std::shared_ptr Shader; + + class ShaderFactory + { + public: + inline static Shader Produce(const std::string& vertexShader, const std::string& fragmentShader) + { + return std::make_shared(vertexShader, fragmentShader); + } + }; + +} \ No newline at end of file diff --git a/include/lol/Transformable.hpp b/include/lol/Transformable.hpp index e01c86c..340cf9f 100644 --- a/include/lol/Transformable.hpp +++ b/include/lol/Transformable.hpp @@ -3,31 +3,36 @@ #include #include -class Transformable +namespace lol { -public: - Transformable(); - const glm::vec3& GetPosition() const; - void SetPosition(const glm::vec3& pos); - void Move(const glm::vec3& direction); + class Transformable + { + public: + Transformable(); - const glm::vec3 GetRotation() const; - const glm::quat& GetQuaternion() const; - void SetRotation(const glm::vec3& axis, float angle); - void SetRotation(const glm::vec3& eulerAngles); - void Rotate(const glm::vec3& axis, float angle); + const glm::vec3& GetPosition() const; + void SetPosition(const glm::vec3& pos); + void Move(const glm::vec3& direction); - const glm::vec3& GetScale() const; - void SetScale(const glm::vec3& scale); - void Scale(const glm::vec3& factor); + const glm::vec3 GetRotation() const; + const glm::quat& GetQuaternion() const; + void SetRotation(const glm::vec3& axis, float angle); + void SetRotation(const glm::vec3& eulerAngles); + void Rotate(const glm::vec3& axis, float angle); -private: - void CalculateTransformationMatrix(); + const glm::vec3& GetScale() const; + void SetScale(const glm::vec3& scale); + void Scale(const glm::vec3& factor); -protected: - glm::mat4 transformation; + private: + void CalculateTransformationMatrix(); - glm::vec3 position, scale; - glm::quat orientation; -}; \ No newline at end of file + protected: + glm::mat4 transformation; + + glm::vec3 position, scale; + glm::quat orientation; + }; + +} \ No newline at end of file diff --git a/include/lol/VertexArrayObject.hpp b/include/lol/VertexArrayObject.hpp index 3b644ca..4131451 100644 --- a/include/lol/VertexArrayObject.hpp +++ b/include/lol/VertexArrayObject.hpp @@ -3,56 +3,61 @@ #include #include -// struct representing an OpenGL attribute pointer -struct VertexAttribute +namespace lol { - int size; - unsigned int type; - bool normalized; - unsigned int stride; - const void* pointer; -}; -// Useful abbreviations -typedef std::vector VertexArray; -typedef std::vector IndexArray; -typedef std::vector Layout; - -// OpenGL Buffer usages (I turned them into an enum so it's easier to know what options exist) -enum class Usage -{ - Static, Stream, Dynamic -}; - -// VAO structure that sets up the buffers and deletes them at the end of the lifecycle -class AbstractVertexArrayObject -{ - friend class VAOFactory; - -public: - AbstractVertexArrayObject() = delete; - AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage); - ~AbstractVertexArrayObject(); - - void Render(unsigned int mode = 4); - -private: - unsigned int vao, vbo, ebo; - size_t indexCount; -}; - -// You cannot actually create this VAO, you are forced to use a shared pointer -// so the buffers dont get accidentally deleted while another obejct is potentially still using it. -// I find this to be very important since VAOs are supposed to be shared between copies of objects -// if they have the same model. -typedef std::shared_ptr VertexArrayObject; - -// Factory for creating said shared pointers. -class VAOFactory -{ -public: - static VertexArrayObject Produce(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static) + // struct representing an OpenGL attribute pointer + struct VertexAttribute { - return std::make_shared(vertices, indices, layout, usage); - } -}; \ No newline at end of file + int size; + unsigned int type; + bool normalized; + unsigned int stride; + const void* pointer; + }; + + // Useful abbreviations + typedef std::vector VertexArray; + typedef std::vector IndexArray; + typedef std::vector Layout; + + // OpenGL Buffer usages (I turned them into an enum so it's easier to know what options exist) + enum class Usage + { + Static, Stream, Dynamic + }; + + // VAO structure that sets up the buffers and deletes them at the end of the lifecycle + class AbstractVertexArrayObject + { + friend class VAOFactory; + + public: + AbstractVertexArrayObject() = delete; + AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage); + ~AbstractVertexArrayObject(); + + void Render(unsigned int mode = 4); + + private: + unsigned int vao, vbo, ebo; + size_t indexCount; + }; + + // You cannot actually create this VAO, you are forced to use a shared pointer + // so the buffers dont get accidentally deleted while another obejct is potentially still using it. + // I find this to be very important since VAOs are supposed to be shared between copies of objects + // if they have the same model. + typedef std::shared_ptr VertexArrayObject; + + // Factory for creating said shared pointers. + class VAOFactory + { + public: + static VertexArrayObject Produce(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static) + { + return std::make_shared(vertices, indices, layout, usage); + } + }; + +} \ No newline at end of file diff --git a/src/Drawable.cpp b/src/Drawable.cpp index eb75cc2..641a74a 100644 --- a/src/Drawable.cpp +++ b/src/Drawable.cpp @@ -1,13 +1,18 @@ #include -void Drawable::Draw(const CameraBase& camera) const +namespace lol { - shader->Use(); - PreRender(camera); - vao->Render(static_cast(type)); -} -void Drawable::SetPrimitiveType(PrimitiveType type) -{ - this->type = type; -} + void Drawable::Draw(const CameraBase& camera) const + { + shader->Use(); + PreRender(camera); + vao->Render(static_cast(type)); + } + + void Drawable::SetPrimitiveType(PrimitiveType type) + { + this->type = type; + } + +} \ No newline at end of file diff --git a/src/Shader.cpp b/src/Shader.cpp index 9808373..b883c1a 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -8,90 +8,95 @@ #define IMPLEMENT_UNIFORM_FUNCTION(type, func) \ inline -AbstractShader::AbstractShader(const std::string& vertexShader, const std::string& fragmentShader) : - id(0) +namespace lol { - GLint success; - GLchar infoLog[512]; - GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER); - const char* vertexShaderSource = vertexShader.c_str(); - glShaderSource(vertexShaderID, 1, &vertexShaderSource, NULL); - glCompileShader(vertexShaderID); - - glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &success); - if (!success) + AbstractShader::AbstractShader(const std::string& vertexShader, const std::string& fragmentShader) : + id(0) { - glGetShaderInfoLog(vertexShaderID, 512, NULL, infoLog); - std::cerr << "Vertex shader creation failed: \n" << infoLog << std::endl; + GLint success; + GLchar infoLog[512]; - glDeleteShader(vertexShaderID); - return; - } + GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER); + const char* vertexShaderSource = vertexShader.c_str(); + glShaderSource(vertexShaderID, 1, &vertexShaderSource, NULL); + glCompileShader(vertexShaderID); - GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); - const char* fragmentShaderSource = fragmentShader.c_str(); - glShaderSource(fragmentShaderID, 1, &fragmentShaderSource, NULL); - glCompileShader(fragmentShaderID); + glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(vertexShaderID, 512, NULL, infoLog); + std::cerr << "Vertex shader creation failed: \n" << infoLog << std::endl; - glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &success); - if (!success) - { - glGetShaderInfoLog(fragmentShaderID, 512, NULL, infoLog); - std::cerr << "Fragment shader creation failed: \n" << infoLog << std::endl; + glDeleteShader(vertexShaderID); + return; + } + + GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); + const char* fragmentShaderSource = fragmentShader.c_str(); + glShaderSource(fragmentShaderID, 1, &fragmentShaderSource, NULL); + glCompileShader(fragmentShaderID); + + glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(fragmentShaderID, 512, NULL, infoLog); + std::cerr << "Fragment shader creation failed: \n" << infoLog << std::endl; + + glDeleteShader(fragmentShaderID); + glDeleteShader(vertexShaderID); + return; + } + + id = glCreateProgram(); + glAttachShader(id, vertexShaderID); + glAttachShader(id, fragmentShaderID); + glLinkProgram(id); + + glGetProgramiv(id, GL_LINK_STATUS, &success); + if (!success) + { + glGetProgramInfoLog(fragmentShaderID, 512, NULL, infoLog); + std::cerr << "Shader program linking failed: \n" << infoLog << std::endl; + + glDeleteShader(fragmentShaderID); + glDeleteShader(vertexShaderID); + + id = 0; + + return; + } glDeleteShader(fragmentShaderID); glDeleteShader(vertexShaderID); - return; } - id = glCreateProgram(); - glAttachShader(id, vertexShaderID); - glAttachShader(id, fragmentShaderID); - glLinkProgram(id); - - glGetProgramiv(id, GL_LINK_STATUS, &success); - if (!success) + AbstractShader::~AbstractShader() { - glGetProgramInfoLog(fragmentShaderID, 512, NULL, infoLog); - std::cerr << "Shader program linking failed: \n" << infoLog << std::endl; - - glDeleteShader(fragmentShaderID); - glDeleteShader(vertexShaderID); - - id = 0; - - return; + glDeleteProgram(id); } - glDeleteShader(fragmentShaderID); - glDeleteShader(vertexShaderID); -} + void AbstractShader::Use() + { + glUseProgram(id); + } -AbstractShader::~AbstractShader() -{ - glDeleteProgram(id); -} + void AbstractShader::SetUniform(const std::string& name, const glm::mat4& value) + { + GLint location = glGetUniformLocation(id, name.c_str()); + if (location == -1) + return; -void AbstractShader::Use() -{ - glUseProgram(id); -} + glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value)); + } -void AbstractShader::SetUniform(const std::string& name, const glm::mat4& value) -{ - GLint location = glGetUniformLocation(id, name.c_str()); - if (location == -1) - return; + void AbstractShader::SetUniform(const std::string& name, const glm::vec4& value) + { + GLint location = glGetUniformLocation(id, name.c_str()); + if (location == -1) + return; - glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value)); -} + glUniform4fv(location, 1, glm::value_ptr(value)); + } -void AbstractShader::SetUniform(const std::string& name, const glm::vec4& value) -{ - GLint location = glGetUniformLocation(id, name.c_str()); - if (location == -1) - return; - - glUniform4fv(location, 1, glm::value_ptr(value)); } \ No newline at end of file diff --git a/src/Transformable.cpp b/src/Transformable.cpp index 563ce62..4b7e88d 100644 --- a/src/Transformable.cpp +++ b/src/Transformable.cpp @@ -1,82 +1,87 @@ #include -Transformable::Transformable() : - position(0.0f), scale(1.0f), orientation(glm::vec3(0.0, 0.0, 0.0)) +namespace lol { - CalculateTransformationMatrix(); -} -const glm::vec3& Transformable::GetPosition() const -{ - return position; -} + Transformable::Transformable() : + position(0.0f), scale(1.0f), orientation(glm::vec3(0.0, 0.0, 0.0)) + { + CalculateTransformationMatrix(); + } -void Transformable::SetPosition(const glm::vec3& pos) -{ - position = pos; - CalculateTransformationMatrix(); -} + const glm::vec3& Transformable::GetPosition() const + { + return position; + } -void Transformable::Move(const glm::vec3& direction) -{ - position += direction; - CalculateTransformationMatrix(); -} + void Transformable::SetPosition(const glm::vec3& pos) + { + position = pos; + CalculateTransformationMatrix(); + } -const glm::vec3 Transformable::GetRotation() const -{ - return glm::eulerAngles(orientation); -} + void Transformable::Move(const glm::vec3& direction) + { + position += direction; + CalculateTransformationMatrix(); + } -const glm::quat& Transformable::GetQuaternion() const -{ - return orientation; -} + const glm::vec3 Transformable::GetRotation() const + { + return glm::eulerAngles(orientation); + } -void Transformable::SetRotation(const glm::vec3& axis, float angle) -{ - orientation = glm::quat(glm::radians(angle), axis); - CalculateTransformationMatrix(); -} + const glm::quat& Transformable::GetQuaternion() const + { + return orientation; + } -void Transformable::SetRotation(const glm::vec3& eulerAngles) -{ - /*orientation = glm::quat(0.0f, 0.0f, 0.0f, 1.0f); - orientation = glm::rotate(orientation, eulerAngles.x, glm::vec3(1.0f, 0.0f, 0.0f)); - orientation = glm::rotate(orientation, eulerAngles.y, glm::vec3(0.0f, 1.0f, 0.0f)); - orientation = glm::rotate(orientation, eulerAngles.z, glm::vec3(0.0f, 0.0f, 1.0f));*/ + void Transformable::SetRotation(const glm::vec3& axis, float angle) + { + orientation = glm::quat(glm::radians(angle), axis); + CalculateTransformationMatrix(); + } - orientation = glm::quat(eulerAngles); - CalculateTransformationMatrix(); -} + void Transformable::SetRotation(const glm::vec3& eulerAngles) + { + /*orientation = glm::quat(0.0f, 0.0f, 0.0f, 1.0f); + orientation = glm::rotate(orientation, eulerAngles.x, glm::vec3(1.0f, 0.0f, 0.0f)); + orientation = glm::rotate(orientation, eulerAngles.y, glm::vec3(0.0f, 1.0f, 0.0f)); + orientation = glm::rotate(orientation, eulerAngles.z, glm::vec3(0.0f, 0.0f, 1.0f));*/ -void Transformable::Rotate(const glm::vec3& axis, float angle) -{ - orientation = glm::rotate(orientation, glm::radians(angle), axis); - CalculateTransformationMatrix(); -} + orientation = glm::quat(eulerAngles); + CalculateTransformationMatrix(); + } -const glm::vec3& Transformable::GetScale() const -{ - return scale; -} + void Transformable::Rotate(const glm::vec3& axis, float angle) + { + orientation = glm::rotate(orientation, glm::radians(angle), axis); + CalculateTransformationMatrix(); + } -void Transformable::SetScale(const glm::vec3& scale) -{ - this->scale = scale; - CalculateTransformationMatrix(); -} + const glm::vec3& Transformable::GetScale() const + { + return scale; + } -void Transformable::Scale(const glm::vec3& factor) -{ - this->scale *= scale; // I pray this is component-wise multiplication - CalculateTransformationMatrix(); -} + void Transformable::SetScale(const glm::vec3& scale) + { + this->scale = scale; + CalculateTransformationMatrix(); + } + + void Transformable::Scale(const glm::vec3& factor) + { + this->scale *= scale; // I pray this is component-wise multiplication + CalculateTransformationMatrix(); + } + + void Transformable::CalculateTransformationMatrix() + { + transformation = glm::mat4(1.0f); + transformation = glm::translate(transformation, position); + transformation *= glm::mat4(orientation); + transformation = glm::scale(transformation, scale); + } -void Transformable::CalculateTransformationMatrix() -{ - transformation = glm::mat4(1.0f); - transformation = glm::translate(transformation, position); - transformation *= glm::mat4(orientation); - transformation = glm::scale(transformation, scale); } diff --git a/src/VertexArrayObject.cpp b/src/VertexArrayObject.cpp index ec9a990..a10e250 100644 --- a/src/VertexArrayObject.cpp +++ b/src/VertexArrayObject.cpp @@ -3,58 +3,63 @@ #include #include -AbstractVertexArrayObject::~AbstractVertexArrayObject() +namespace lol { - glDeleteBuffers(1, &ebo); - glDeleteBuffers(1, &vbo); - glDeleteVertexArrays(1, &vao); -} -void AbstractVertexArrayObject::Render(GLenum mode) -{ - assert(vao != 0); - - glBindVertexArray(vao); - // GLenum result = glGetError(); - glDrawElements(mode, indexCount, GL_UNSIGNED_INT, 0); -} - -AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) : - vao(0), vbo(0), ebo(0), indexCount(indices.size()) -{ - glGenVertexArrays(1, &vao); - glBindVertexArray(vao); - glGenBuffers(1, &vbo); - glGenBuffers(1, &ebo); - - // Determing native OpenGL GLenum depending on specified usage - GLenum bufferUsage; - switch (usage) + AbstractVertexArrayObject::~AbstractVertexArrayObject() { - case Usage::Static: bufferUsage = GL_STATIC_DRAW; break; - case Usage::Dynamic: bufferUsage = GL_DYNAMIC_DRAW; break; - case Usage::Stream: bufferUsage = GL_STREAM_DRAW; break; - - default: // Forgot to add a usage case to this switch - assert("Unknown buffer usage" == ""); - break; + glDeleteBuffers(1, &ebo); + glDeleteBuffers(1, &vbo); + glDeleteVertexArrays(1, &vao); } - // Create VBO - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), (const void*)(vertices.data()), bufferUsage); - - // Create EBO - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), (const void*)(indices.data()), bufferUsage); - - // Set up pipeline layout - unsigned int index = 0; - for (const VertexAttribute& attribute : layout) + void AbstractVertexArrayObject::Render(GLenum mode) { - glVertexAttribPointer(index, attribute.size, attribute.type, attribute.normalized, attribute.stride, attribute.pointer); - glEnableVertexAttribArray(index); + assert(vao != 0); - index++; + glBindVertexArray(vao); + // GLenum result = glGetError(); + glDrawElements(mode, indexCount, GL_UNSIGNED_INT, 0); } -} + + AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) : + vao(0), vbo(0), ebo(0), indexCount(indices.size()) + { + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + glGenBuffers(1, &vbo); + glGenBuffers(1, &ebo); + + // Determing native OpenGL GLenum depending on specified usage + GLenum bufferUsage; + switch (usage) + { + case Usage::Static: bufferUsage = GL_STATIC_DRAW; break; + case Usage::Dynamic: bufferUsage = GL_DYNAMIC_DRAW; break; + case Usage::Stream: bufferUsage = GL_STREAM_DRAW; break; + + default: // Forgot to add a usage case to this switch + assert("Unknown buffer usage" == ""); + break; + } + + // Create VBO + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), (const void*)(vertices.data()), bufferUsage); + + // Create EBO + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), (const void*)(indices.data()), bufferUsage); + + // Set up pipeline layout + unsigned int index = 0; + for (const VertexAttribute& attribute : layout) + { + glVertexAttribPointer(index, attribute.size, attribute.type, attribute.normalized, attribute.stride, attribute.pointer); + glEnableVertexAttribArray(index); + + index++; + } + } + +} \ No newline at end of file