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
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;
struct BoundingBox
{
float x, y, z;
float w, h, d;
};
typedef BoundingBox BBox;
}

View file

@ -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);
}
};
}

View file

@ -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;
};
protected:
Drawable() {}
protected:
VertexArrayObject vao;
Shader shader;
PrimitiveType type = PrimitiveType::Triangles;
};
}

View file

@ -3,69 +3,74 @@
#include <map>
#include <memory>
/**
* 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<typename Type>
class ObjectManager
namespace lol
{
public:
static ObjectManager<Type>& GetInstance()
{
static ObjectManager<Type> instance;
return instance;
}
public:
ObjectManager(const ObjectManager<Type>&) = delete;
void operator=(const ObjectManager<Type>&) = 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<Type> obj)
template<typename Type>
class ObjectManager
{
objects.insert(std::make_pair(id, obj));
}
public:
static ObjectManager<Type>& GetInstance()
{
static ObjectManager<Type> instance;
return instance;
}
/**
* Remove object from manager
*/
inline void Delete(unsigned int id)
{
objects.erase(id);
}
public:
ObjectManager(const ObjectManager<Type>&) = delete;
void operator=(const ObjectManager<Type>&) = delete;
/**
* Retrieve object from manager
*/
inline std::shared_ptr<Type> Get(unsigned int id)
{
auto it = objects.find(id);
/**
* Add new (existing) object to manager
*/
inline void Register(unsigned int id, std::shared_ptr<Type> 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<Type> Get(unsigned int id)
{
auto it = objects.find(id);
private:
ObjectManager() {}
if (it == objects.end())
return nullptr;
private:
std::map<unsigned int, std::shared_ptr<Type>> objects;
};
return it->second;
}
class AbstractShader;
class AbstractVertexArrayObject;
private:
ObjectManager() {}
typedef ObjectManager<AbstractVertexArrayObject> VAOManager;
typedef ObjectManager<AbstractShader> ShaderManager;
private:
std::map<unsigned int, std::shared_ptr<Type>> objects;
};
class AbstractShader;
class AbstractVertexArrayObject;
typedef ObjectManager<AbstractVertexArrayObject> VAOManager;
typedef ObjectManager<AbstractShader> ShaderManager;
}

View file

@ -1,39 +1,44 @@
#pragma once
#include <glm/glm.hpp>
#include <string>
#include <memory>
#include <functional>
class AbstractShader
#include <glm/glm.hpp>
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<AbstractShader> Shader;
class ShaderFactory
{
public:
inline static Shader Produce(const std::string& vertexShader, const std::string& fragmentShader)
class AbstractShader
{
return std::make_shared<AbstractShader>(vertexShader, fragmentShader);
}
};
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<AbstractShader> Shader;
class ShaderFactory
{
public:
inline static Shader Produce(const std::string& vertexShader, const std::string& fragmentShader)
{
return std::make_shared<AbstractShader>(vertexShader, fragmentShader);
}
};
}

View file

@ -3,31 +3,36 @@
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
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;
};
protected:
glm::mat4 transformation;
glm::vec3 position, scale;
glm::quat orientation;
};
}

View file

@ -3,56 +3,61 @@
#include <vector>
#include <memory>
// 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<float> VertexArray;
typedef std::vector<unsigned int> IndexArray;
typedef std::vector<VertexAttribute> 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<AbstractVertexArrayObject> 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<AbstractVertexArrayObject>(vertices, indices, layout, usage);
}
};
int size;
unsigned int type;
bool normalized;
unsigned int stride;
const void* pointer;
};
// Useful abbreviations
typedef std::vector<float> VertexArray;
typedef std::vector<unsigned int> IndexArray;
typedef std::vector<VertexAttribute> 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<AbstractVertexArrayObject> 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<AbstractVertexArrayObject>(vertices, indices, layout, usage);
}
};
}