added namespace

This commit is contained in:
Lauchmelder 2021-12-23 00:51:59 +01:00
parent 8c789f57cb
commit de7cbdabbd
11 changed files with 484 additions and 429 deletions

View file

@ -1,15 +1,20 @@
#pragma once #pragma once
struct Rect namespace lol
{ {
struct Rect
{
float x, y; float x, y;
float w, h; float w, h;
}; };
struct BoundingBox struct BoundingBox
{ {
float x, y, z; float x, y, z;
float w, h, d; float w, h, d;
}; };
typedef BoundingBox BBox; typedef BoundingBox BBox;
}

View file

@ -4,10 +4,13 @@
#include "Transformable.hpp" #include "Transformable.hpp"
#include "Drawable.hpp" #include "Drawable.hpp"
// TODO: Find better name namespace lol
class CameraBase : public Transformable
{ {
public:
// TODO: Find better name
class CameraBase : public Transformable
{
public:
// "Scaling" doesn't really makes sense for a camera // "Scaling" doesn't really makes sense for a camera
void GetScale() = delete; void GetScale() = delete;
void SetScale(const glm::vec3&) = delete; void SetScale(const glm::vec3&) = delete;
@ -33,14 +36,14 @@ public:
drawable.Draw(*this); drawable.Draw(*this);
} }
protected: protected:
glm::mat4 projection; glm::mat4 projection;
}; };
class Camera : public CameraBase class Camera : public CameraBase
{ {
public: public:
Camera(float fov = 90.0f, float aspect = 1.0f, float zNear = 0.01f, float zFar = 100.0f) 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); projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar);
@ -50,11 +53,11 @@ public:
{ {
projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar); projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar);
} }
}; };
class OrthogonalCamera : public CameraBase class OrthogonalCamera : public CameraBase
{ {
public: 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) 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); projection = glm::ortho(left, right, bottom, top, zNear, zFar);
@ -64,4 +67,6 @@ public:
{ {
projection = glm::ortho(left, right, bottom, top, zNear, zFar); projection = glm::ortho(left, right, bottom, top, zNear, zFar);
} }
}; };
}

View file

@ -4,10 +4,13 @@
#include "VertexArrayObject.hpp" #include "VertexArrayObject.hpp"
#include "Shader.hpp" #include "Shader.hpp"
class CameraBase; namespace lol
enum class PrimitiveType
{ {
class CameraBase;
enum class PrimitiveType
{
Lines = GL_LINES, Lines = GL_LINES,
LineStrip = GL_LINE_STRIP, LineStrip = GL_LINE_STRIP,
LineLoop = GL_LINE_LOOP, LineLoop = GL_LINE_LOOP,
@ -15,11 +18,11 @@ enum class PrimitiveType
Triangles = GL_TRIANGLES, Triangles = GL_TRIANGLES,
TriangleStrip = GL_TRIANGLE_STRIP, TriangleStrip = GL_TRIANGLE_STRIP,
TriangleFan = GL_TRIANGLE_FAN TriangleFan = GL_TRIANGLE_FAN
}; };
class Drawable class Drawable
{ {
public: public:
Drawable(const Drawable& other) = delete; Drawable(const Drawable& other) = delete;
void operator=(const Drawable& other) = delete; void operator=(const Drawable& other) = delete;
@ -27,12 +30,14 @@ public:
void Draw(const CameraBase& camera) const; void Draw(const CameraBase& camera) const;
void SetPrimitiveType(PrimitiveType type); void SetPrimitiveType(PrimitiveType type);
protected: protected:
Drawable() {} Drawable() {}
protected: protected:
VertexArrayObject vao; VertexArrayObject vao;
Shader shader; Shader shader;
PrimitiveType type = PrimitiveType::Triangles; PrimitiveType type = PrimitiveType::Triangles;
}; };
}

View file

@ -3,7 +3,10 @@
#include <map> #include <map>
#include <memory> #include <memory>
/** namespace lol
{
/**
* Some objects should only exist once but be available to multiple objects (e.g. multiple * 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 * 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 can then retrieve them if they have the ID.
@ -14,17 +17,17 @@
* As a consequence, even if no objects are using an object stored in here it will continue to * As a consequence, even if no objects are using an object stored in here it will continue to
* exist. * exist.
*/ */
template<typename Type> template<typename Type>
class ObjectManager class ObjectManager
{ {
public: public:
static ObjectManager<Type>& GetInstance() static ObjectManager<Type>& GetInstance()
{ {
static ObjectManager<Type> instance; static ObjectManager<Type> instance;
return instance; return instance;
} }
public: public:
ObjectManager(const ObjectManager<Type>&) = delete; ObjectManager(const ObjectManager<Type>&) = delete;
void operator=(const ObjectManager<Type>&) = delete; void operator=(const ObjectManager<Type>&) = delete;
@ -57,15 +60,17 @@ public:
return it->second; return it->second;
} }
private: private:
ObjectManager() {} ObjectManager() {}
private: private:
std::map<unsigned int, std::shared_ptr<Type>> objects; std::map<unsigned int, std::shared_ptr<Type>> objects;
}; };
class AbstractShader; class AbstractShader;
class AbstractVertexArrayObject; class AbstractVertexArrayObject;
typedef ObjectManager<AbstractVertexArrayObject> VAOManager; typedef ObjectManager<AbstractVertexArrayObject> VAOManager;
typedef ObjectManager<AbstractShader> ShaderManager; typedef ObjectManager<AbstractShader> ShaderManager;
}

View file

@ -1,16 +1,19 @@
#pragma once #pragma once
#include <glm/glm.hpp>
#include <string> #include <string>
#include <memory> #include <memory>
#include <functional> #include <functional>
class AbstractShader #include <glm/glm.hpp>
namespace lol
{ {
class AbstractShader
{
friend class ShaderFactory; friend class ShaderFactory;
public: public:
AbstractShader(const std::string& vertexShader, const std::string& fragmentShader); AbstractShader(const std::string& vertexShader, const std::string& fragmentShader);
AbstractShader(const AbstractShader& other) = delete; AbstractShader(const AbstractShader& other) = delete;
~AbstractShader(); ~AbstractShader();
@ -21,19 +24,21 @@ public:
void SetUniform(const std::string& name, const glm::mat4& value); void SetUniform(const std::string& name, const glm::mat4& value);
void SetUniform(const std::string& name, const glm::vec4& value); void SetUniform(const std::string& name, const glm::vec4& value);
private: private:
unsigned int id; unsigned int id;
bool recording = false; bool recording = false;
}; };
typedef std::shared_ptr<AbstractShader> Shader; typedef std::shared_ptr<AbstractShader> Shader;
class ShaderFactory class ShaderFactory
{ {
public: public:
inline static Shader Produce(const std::string& vertexShader, const std::string& fragmentShader) inline static Shader Produce(const std::string& vertexShader, const std::string& fragmentShader)
{ {
return std::make_shared<AbstractShader>(vertexShader, fragmentShader); return std::make_shared<AbstractShader>(vertexShader, fragmentShader);
} }
}; };
}

View file

@ -3,9 +3,12 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp> #include <glm/gtx/quaternion.hpp>
class Transformable namespace lol
{ {
public:
class Transformable
{
public:
Transformable(); Transformable();
const glm::vec3& GetPosition() const; const glm::vec3& GetPosition() const;
@ -22,12 +25,14 @@ public:
void SetScale(const glm::vec3& scale); void SetScale(const glm::vec3& scale);
void Scale(const glm::vec3& factor); void Scale(const glm::vec3& factor);
private: private:
void CalculateTransformationMatrix(); void CalculateTransformationMatrix();
protected: protected:
glm::mat4 transformation; glm::mat4 transformation;
glm::vec3 position, scale; glm::vec3 position, scale;
glm::quat orientation; glm::quat orientation;
}; };
}

View file

@ -3,56 +3,61 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
// struct representing an OpenGL attribute pointer namespace lol
struct VertexAttribute
{ {
// struct representing an OpenGL attribute pointer
struct VertexAttribute
{
int size; int size;
unsigned int type; unsigned int type;
bool normalized; bool normalized;
unsigned int stride; unsigned int stride;
const void* pointer; const void* pointer;
}; };
// Useful abbreviations // Useful abbreviations
typedef std::vector<float> VertexArray; typedef std::vector<float> VertexArray;
typedef std::vector<unsigned int> IndexArray; typedef std::vector<unsigned int> IndexArray;
typedef std::vector<VertexAttribute> Layout; typedef std::vector<VertexAttribute> Layout;
// OpenGL Buffer usages (I turned them into an enum so it's easier to know what options exist) // OpenGL Buffer usages (I turned them into an enum so it's easier to know what options exist)
enum class Usage enum class Usage
{ {
Static, Stream, Dynamic Static, Stream, Dynamic
}; };
// VAO structure that sets up the buffers and deletes them at the end of the lifecycle // VAO structure that sets up the buffers and deletes them at the end of the lifecycle
class AbstractVertexArrayObject class AbstractVertexArrayObject
{ {
friend class VAOFactory; friend class VAOFactory;
public: public:
AbstractVertexArrayObject() = delete; AbstractVertexArrayObject() = delete;
AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage); AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage);
~AbstractVertexArrayObject(); ~AbstractVertexArrayObject();
void Render(unsigned int mode = 4); void Render(unsigned int mode = 4);
private: private:
unsigned int vao, vbo, ebo; unsigned int vao, vbo, ebo;
size_t indexCount; size_t indexCount;
}; };
// You cannot actually create this VAO, you are forced to use a shared pointer // 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. // 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 // I find this to be very important since VAOs are supposed to be shared between copies of objects
// if they have the same model. // if they have the same model.
typedef std::shared_ptr<AbstractVertexArrayObject> VertexArrayObject; typedef std::shared_ptr<AbstractVertexArrayObject> VertexArrayObject;
// Factory for creating said shared pointers. // Factory for creating said shared pointers.
class VAOFactory class VAOFactory
{ {
public: public:
static VertexArrayObject Produce(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static) static VertexArrayObject Produce(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static)
{ {
return std::make_shared<AbstractVertexArrayObject>(vertices, indices, layout, usage); return std::make_shared<AbstractVertexArrayObject>(vertices, indices, layout, usage);
} }
}; };
}

View file

@ -1,13 +1,18 @@
#include <lol/Drawable.hpp> #include <lol/Drawable.hpp>
void Drawable::Draw(const CameraBase& camera) const namespace lol
{ {
void Drawable::Draw(const CameraBase& camera) const
{
shader->Use(); shader->Use();
PreRender(camera); PreRender(camera);
vao->Render(static_cast<unsigned int>(type)); vao->Render(static_cast<unsigned int>(type));
} }
void Drawable::SetPrimitiveType(PrimitiveType type) void Drawable::SetPrimitiveType(PrimitiveType type)
{ {
this->type = type; this->type = type;
}
} }

View file

@ -8,9 +8,12 @@
#define IMPLEMENT_UNIFORM_FUNCTION(type, func) \ #define IMPLEMENT_UNIFORM_FUNCTION(type, func) \
inline inline
AbstractShader::AbstractShader(const std::string& vertexShader, const std::string& fragmentShader) : namespace lol
id(0)
{ {
AbstractShader::AbstractShader(const std::string& vertexShader, const std::string& fragmentShader) :
id(0)
{
GLint success; GLint success;
GLchar infoLog[512]; GLchar infoLog[512];
@ -66,32 +69,34 @@ AbstractShader::AbstractShader(const std::string& vertexShader, const std::strin
glDeleteShader(fragmentShaderID); glDeleteShader(fragmentShaderID);
glDeleteShader(vertexShaderID); glDeleteShader(vertexShaderID);
} }
AbstractShader::~AbstractShader() AbstractShader::~AbstractShader()
{ {
glDeleteProgram(id); glDeleteProgram(id);
} }
void AbstractShader::Use() void AbstractShader::Use()
{ {
glUseProgram(id); glUseProgram(id);
} }
void AbstractShader::SetUniform(const std::string& name, const glm::mat4& value) void AbstractShader::SetUniform(const std::string& name, const glm::mat4& value)
{ {
GLint location = glGetUniformLocation(id, name.c_str()); GLint location = glGetUniformLocation(id, name.c_str());
if (location == -1) if (location == -1)
return; return;
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value)); glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value));
} }
void AbstractShader::SetUniform(const std::string& name, const glm::vec4& value) void AbstractShader::SetUniform(const std::string& name, const glm::vec4& value)
{ {
GLint location = glGetUniformLocation(id, name.c_str()); GLint location = glGetUniformLocation(id, name.c_str());
if (location == -1) if (location == -1)
return; return;
glUniform4fv(location, 1, glm::value_ptr(value)); glUniform4fv(location, 1, glm::value_ptr(value));
}
} }

View file

@ -1,46 +1,49 @@
#include <lol/Transformable.hpp> #include <lol/Transformable.hpp>
Transformable::Transformable() : namespace lol
{
Transformable::Transformable() :
position(0.0f), scale(1.0f), orientation(glm::vec3(0.0, 0.0, 0.0)) position(0.0f), scale(1.0f), orientation(glm::vec3(0.0, 0.0, 0.0))
{ {
CalculateTransformationMatrix(); CalculateTransformationMatrix();
} }
const glm::vec3& Transformable::GetPosition() const const glm::vec3& Transformable::GetPosition() const
{ {
return position; return position;
} }
void Transformable::SetPosition(const glm::vec3& pos) void Transformable::SetPosition(const glm::vec3& pos)
{ {
position = pos; position = pos;
CalculateTransformationMatrix(); CalculateTransformationMatrix();
} }
void Transformable::Move(const glm::vec3& direction) void Transformable::Move(const glm::vec3& direction)
{ {
position += direction; position += direction;
CalculateTransformationMatrix(); CalculateTransformationMatrix();
} }
const glm::vec3 Transformable::GetRotation() const const glm::vec3 Transformable::GetRotation() const
{ {
return glm::eulerAngles(orientation); return glm::eulerAngles(orientation);
} }
const glm::quat& Transformable::GetQuaternion() const const glm::quat& Transformable::GetQuaternion() const
{ {
return orientation; return orientation;
} }
void Transformable::SetRotation(const glm::vec3& axis, float angle) void Transformable::SetRotation(const glm::vec3& axis, float angle)
{ {
orientation = glm::quat(glm::radians(angle), axis); orientation = glm::quat(glm::radians(angle), axis);
CalculateTransformationMatrix(); CalculateTransformationMatrix();
} }
void Transformable::SetRotation(const glm::vec3& eulerAngles) void Transformable::SetRotation(const glm::vec3& eulerAngles)
{ {
/*orientation = glm::quat(0.0f, 0.0f, 0.0f, 1.0f); /*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.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.y, glm::vec3(0.0f, 1.0f, 0.0f));
@ -48,35 +51,37 @@ void Transformable::SetRotation(const glm::vec3& eulerAngles)
orientation = glm::quat(eulerAngles); orientation = glm::quat(eulerAngles);
CalculateTransformationMatrix(); CalculateTransformationMatrix();
} }
void Transformable::Rotate(const glm::vec3& axis, float angle) void Transformable::Rotate(const glm::vec3& axis, float angle)
{ {
orientation = glm::rotate(orientation, glm::radians(angle), axis); orientation = glm::rotate(orientation, glm::radians(angle), axis);
CalculateTransformationMatrix(); CalculateTransformationMatrix();
} }
const glm::vec3& Transformable::GetScale() const const glm::vec3& Transformable::GetScale() const
{ {
return scale; return scale;
} }
void Transformable::SetScale(const glm::vec3& scale) void Transformable::SetScale(const glm::vec3& scale)
{ {
this->scale = scale; this->scale = scale;
CalculateTransformationMatrix(); CalculateTransformationMatrix();
} }
void Transformable::Scale(const glm::vec3& factor) void Transformable::Scale(const glm::vec3& factor)
{ {
this->scale *= scale; // I pray this is component-wise multiplication this->scale *= scale; // I pray this is component-wise multiplication
CalculateTransformationMatrix(); CalculateTransformationMatrix();
} }
void Transformable::CalculateTransformationMatrix() void Transformable::CalculateTransformationMatrix()
{ {
transformation = glm::mat4(1.0f); transformation = glm::mat4(1.0f);
transformation = glm::translate(transformation, position); transformation = glm::translate(transformation, position);
transformation *= glm::mat4(orientation); transformation *= glm::mat4(orientation);
transformation = glm::scale(transformation, scale); transformation = glm::scale(transformation, scale);
}
} }

View file

@ -3,25 +3,28 @@
#include <assert.h> #include <assert.h>
#include <glad/glad.h> #include <glad/glad.h>
AbstractVertexArrayObject::~AbstractVertexArrayObject() namespace lol
{ {
AbstractVertexArrayObject::~AbstractVertexArrayObject()
{
glDeleteBuffers(1, &ebo); glDeleteBuffers(1, &ebo);
glDeleteBuffers(1, &vbo); glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao); glDeleteVertexArrays(1, &vao);
} }
void AbstractVertexArrayObject::Render(GLenum mode) void AbstractVertexArrayObject::Render(GLenum mode)
{ {
assert(vao != 0); assert(vao != 0);
glBindVertexArray(vao); glBindVertexArray(vao);
// GLenum result = glGetError(); // GLenum result = glGetError();
glDrawElements(mode, indexCount, GL_UNSIGNED_INT, 0); glDrawElements(mode, indexCount, GL_UNSIGNED_INT, 0);
} }
AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) : AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) :
vao(0), vbo(0), ebo(0), indexCount(indices.size()) vao(0), vbo(0), ebo(0), indexCount(indices.size())
{ {
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &vao);
glBindVertexArray(vao); glBindVertexArray(vao);
glGenBuffers(1, &vbo); glGenBuffers(1, &vbo);
@ -57,4 +60,6 @@ AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices
index++; index++;
} }
}
} }