added namespace
This commit is contained in:
parent
8c789f57cb
commit
de7cbdabbd
|
@ -1,15 +1,20 @@
|
|||
#pragma once
|
||||
|
||||
struct Rect
|
||||
namespace lol
|
||||
{
|
||||
|
||||
struct Rect
|
||||
{
|
||||
float x, y;
|
||||
float w, h;
|
||||
};
|
||||
};
|
||||
|
||||
struct BoundingBox
|
||||
{
|
||||
struct BoundingBox
|
||||
{
|
||||
float x, y, z;
|
||||
float w, h, d;
|
||||
};
|
||||
};
|
||||
|
||||
typedef BoundingBox BBox;
|
||||
typedef BoundingBox BBox;
|
||||
|
||||
}
|
|
@ -4,10 +4,13 @@
|
|||
#include "Transformable.hpp"
|
||||
#include "Drawable.hpp"
|
||||
|
||||
// TODO: Find better name
|
||||
class CameraBase : public Transformable
|
||||
namespace lol
|
||||
{
|
||||
public:
|
||||
|
||||
// TODO: Find better name
|
||||
class CameraBase : public Transformable
|
||||
{
|
||||
public:
|
||||
// "Scaling" doesn't really makes sense for a camera
|
||||
void GetScale() = delete;
|
||||
void SetScale(const glm::vec3&) = delete;
|
||||
|
@ -33,14 +36,14 @@ public:
|
|||
drawable.Draw(*this);
|
||||
}
|
||||
|
||||
protected:
|
||||
protected:
|
||||
glm::mat4 projection;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
class Camera : public CameraBase
|
||||
{
|
||||
public:
|
||||
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);
|
||||
|
@ -50,11 +53,11 @@ public:
|
|||
{
|
||||
projection = glm::perspective(glm::radians(fov), aspect, zNear, zFar);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
class OrthogonalCamera : public CameraBase
|
||||
{
|
||||
public:
|
||||
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);
|
||||
|
@ -64,4 +67,6 @@ public:
|
|||
{
|
||||
projection = glm::ortho(left, right, bottom, top, zNear, zFar);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
}
|
|
@ -4,10 +4,13 @@
|
|||
#include "VertexArrayObject.hpp"
|
||||
#include "Shader.hpp"
|
||||
|
||||
class CameraBase;
|
||||
|
||||
enum class PrimitiveType
|
||||
namespace lol
|
||||
{
|
||||
|
||||
class CameraBase;
|
||||
|
||||
enum class PrimitiveType
|
||||
{
|
||||
Lines = GL_LINES,
|
||||
LineStrip = GL_LINE_STRIP,
|
||||
LineLoop = GL_LINE_LOOP,
|
||||
|
@ -15,11 +18,11 @@ enum class PrimitiveType
|
|||
Triangles = GL_TRIANGLES,
|
||||
TriangleStrip = GL_TRIANGLE_STRIP,
|
||||
TriangleFan = GL_TRIANGLE_FAN
|
||||
};
|
||||
};
|
||||
|
||||
class Drawable
|
||||
{
|
||||
public:
|
||||
class Drawable
|
||||
{
|
||||
public:
|
||||
Drawable(const Drawable& other) = delete;
|
||||
void operator=(const Drawable& other) = delete;
|
||||
|
||||
|
@ -27,12 +30,14 @@ public:
|
|||
void Draw(const CameraBase& camera) const;
|
||||
void SetPrimitiveType(PrimitiveType type);
|
||||
|
||||
protected:
|
||||
protected:
|
||||
Drawable() {}
|
||||
|
||||
protected:
|
||||
protected:
|
||||
VertexArrayObject vao;
|
||||
Shader shader;
|
||||
|
||||
PrimitiveType type = PrimitiveType::Triangles;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
|
@ -3,7 +3,10 @@
|
|||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
/**
|
||||
namespace lol
|
||||
{
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
@ -14,17 +17,17 @@
|
|||
* As a consequence, even if no objects are using an object stored in here it will continue to
|
||||
* exist.
|
||||
*/
|
||||
template<typename Type>
|
||||
class ObjectManager
|
||||
{
|
||||
public:
|
||||
template<typename Type>
|
||||
class ObjectManager
|
||||
{
|
||||
public:
|
||||
static ObjectManager<Type>& GetInstance()
|
||||
{
|
||||
static ObjectManager<Type> instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
public:
|
||||
public:
|
||||
ObjectManager(const ObjectManager<Type>&) = delete;
|
||||
void operator=(const ObjectManager<Type>&) = delete;
|
||||
|
||||
|
@ -57,15 +60,17 @@ public:
|
|||
return it->second;
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
ObjectManager() {}
|
||||
|
||||
private:
|
||||
private:
|
||||
std::map<unsigned int, std::shared_ptr<Type>> objects;
|
||||
};
|
||||
};
|
||||
|
||||
class AbstractShader;
|
||||
class AbstractVertexArrayObject;
|
||||
class AbstractShader;
|
||||
class AbstractVertexArrayObject;
|
||||
|
||||
typedef ObjectManager<AbstractVertexArrayObject> VAOManager;
|
||||
typedef ObjectManager<AbstractShader> ShaderManager;
|
||||
typedef ObjectManager<AbstractVertexArrayObject> VAOManager;
|
||||
typedef ObjectManager<AbstractShader> ShaderManager;
|
||||
|
||||
}
|
|
@ -1,16 +1,19 @@
|
|||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
class AbstractShader
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
namespace lol
|
||||
{
|
||||
|
||||
class AbstractShader
|
||||
{
|
||||
friend class ShaderFactory;
|
||||
|
||||
public:
|
||||
public:
|
||||
AbstractShader(const std::string& vertexShader, const std::string& fragmentShader);
|
||||
AbstractShader(const AbstractShader& other) = delete;
|
||||
~AbstractShader();
|
||||
|
@ -21,19 +24,21 @@ public:
|
|||
void SetUniform(const std::string& name, const glm::mat4& value);
|
||||
void SetUniform(const std::string& name, const glm::vec4& value);
|
||||
|
||||
private:
|
||||
private:
|
||||
unsigned int id;
|
||||
|
||||
bool recording = false;
|
||||
};
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<AbstractShader> Shader;
|
||||
typedef std::shared_ptr<AbstractShader> Shader;
|
||||
|
||||
class ShaderFactory
|
||||
{
|
||||
public:
|
||||
class ShaderFactory
|
||||
{
|
||||
public:
|
||||
inline static Shader Produce(const std::string& vertexShader, const std::string& fragmentShader)
|
||||
{
|
||||
return std::make_shared<AbstractShader>(vertexShader, fragmentShader);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
}
|
|
@ -3,9 +3,12 @@
|
|||
#include <glm/glm.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
|
||||
class Transformable
|
||||
namespace lol
|
||||
{
|
||||
public:
|
||||
|
||||
class Transformable
|
||||
{
|
||||
public:
|
||||
Transformable();
|
||||
|
||||
const glm::vec3& GetPosition() const;
|
||||
|
@ -22,12 +25,14 @@ public:
|
|||
void SetScale(const glm::vec3& scale);
|
||||
void Scale(const glm::vec3& factor);
|
||||
|
||||
private:
|
||||
private:
|
||||
void CalculateTransformationMatrix();
|
||||
|
||||
protected:
|
||||
protected:
|
||||
glm::mat4 transformation;
|
||||
|
||||
glm::vec3 position, scale;
|
||||
glm::quat orientation;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
|
@ -3,56 +3,61 @@
|
|||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
// struct representing an OpenGL attribute pointer
|
||||
struct VertexAttribute
|
||||
namespace lol
|
||||
{
|
||||
|
||||
// struct representing an OpenGL attribute pointer
|
||||
struct VertexAttribute
|
||||
{
|
||||
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;
|
||||
// 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
|
||||
{
|
||||
// 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
|
||||
{
|
||||
// VAO structure that sets up the buffers and deletes them at the end of the lifecycle
|
||||
class AbstractVertexArrayObject
|
||||
{
|
||||
friend class VAOFactory;
|
||||
|
||||
public:
|
||||
public:
|
||||
AbstractVertexArrayObject() = delete;
|
||||
AbstractVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage);
|
||||
~AbstractVertexArrayObject();
|
||||
|
||||
void Render(unsigned int mode = 4);
|
||||
|
||||
private:
|
||||
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;
|
||||
// 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:
|
||||
// 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);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
}
|
|
@ -1,13 +1,18 @@
|
|||
#include <lol/Drawable.hpp>
|
||||
|
||||
void Drawable::Draw(const CameraBase& camera) const
|
||||
namespace lol
|
||||
{
|
||||
|
||||
void Drawable::Draw(const CameraBase& camera) const
|
||||
{
|
||||
shader->Use();
|
||||
PreRender(camera);
|
||||
vao->Render(static_cast<unsigned int>(type));
|
||||
}
|
||||
}
|
||||
|
||||
void Drawable::SetPrimitiveType(PrimitiveType type)
|
||||
{
|
||||
void Drawable::SetPrimitiveType(PrimitiveType type)
|
||||
{
|
||||
this->type = type;
|
||||
}
|
||||
|
||||
}
|
|
@ -8,9 +8,12 @@
|
|||
#define IMPLEMENT_UNIFORM_FUNCTION(type, func) \
|
||||
inline
|
||||
|
||||
AbstractShader::AbstractShader(const std::string& vertexShader, const std::string& fragmentShader) :
|
||||
id(0)
|
||||
namespace lol
|
||||
{
|
||||
|
||||
AbstractShader::AbstractShader(const std::string& vertexShader, const std::string& fragmentShader) :
|
||||
id(0)
|
||||
{
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
|
||||
|
@ -66,32 +69,34 @@ AbstractShader::AbstractShader(const std::string& vertexShader, const std::strin
|
|||
|
||||
glDeleteShader(fragmentShaderID);
|
||||
glDeleteShader(vertexShaderID);
|
||||
}
|
||||
}
|
||||
|
||||
AbstractShader::~AbstractShader()
|
||||
{
|
||||
AbstractShader::~AbstractShader()
|
||||
{
|
||||
glDeleteProgram(id);
|
||||
}
|
||||
}
|
||||
|
||||
void AbstractShader::Use()
|
||||
{
|
||||
void AbstractShader::Use()
|
||||
{
|
||||
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());
|
||||
if (location == -1)
|
||||
return;
|
||||
|
||||
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());
|
||||
if (location == -1)
|
||||
return;
|
||||
|
||||
glUniform4fv(location, 1, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,46 +1,49 @@
|
|||
#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))
|
||||
{
|
||||
{
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
const glm::vec3& Transformable::GetPosition() const
|
||||
{
|
||||
const glm::vec3& Transformable::GetPosition() const
|
||||
{
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
void Transformable::SetPosition(const glm::vec3& pos)
|
||||
{
|
||||
void Transformable::SetPosition(const glm::vec3& pos)
|
||||
{
|
||||
position = pos;
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
void Transformable::Move(const glm::vec3& direction)
|
||||
{
|
||||
void Transformable::Move(const glm::vec3& direction)
|
||||
{
|
||||
position += direction;
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
const glm::vec3 Transformable::GetRotation() const
|
||||
{
|
||||
const glm::vec3 Transformable::GetRotation() const
|
||||
{
|
||||
return glm::eulerAngles(orientation);
|
||||
}
|
||||
}
|
||||
|
||||
const glm::quat& Transformable::GetQuaternion() const
|
||||
{
|
||||
const glm::quat& Transformable::GetQuaternion() const
|
||||
{
|
||||
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);
|
||||
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::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));
|
||||
|
@ -48,35 +51,37 @@ void Transformable::SetRotation(const glm::vec3& eulerAngles)
|
|||
|
||||
orientation = glm::quat(eulerAngles);
|
||||
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);
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
const glm::vec3& Transformable::GetScale() const
|
||||
{
|
||||
const glm::vec3& Transformable::GetScale() const
|
||||
{
|
||||
return scale;
|
||||
}
|
||||
}
|
||||
|
||||
void Transformable::SetScale(const glm::vec3& scale)
|
||||
{
|
||||
void Transformable::SetScale(const glm::vec3& scale)
|
||||
{
|
||||
this->scale = scale;
|
||||
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
|
||||
CalculateTransformationMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
void Transformable::CalculateTransformationMatrix()
|
||||
{
|
||||
void Transformable::CalculateTransformationMatrix()
|
||||
{
|
||||
transformation = glm::mat4(1.0f);
|
||||
transformation = glm::translate(transformation, position);
|
||||
transformation *= glm::mat4(orientation);
|
||||
transformation = glm::scale(transformation, scale);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,25 +3,28 @@
|
|||
#include <assert.h>
|
||||
#include <glad/glad.h>
|
||||
|
||||
AbstractVertexArrayObject::~AbstractVertexArrayObject()
|
||||
namespace lol
|
||||
{
|
||||
|
||||
AbstractVertexArrayObject::~AbstractVertexArrayObject()
|
||||
{
|
||||
glDeleteBuffers(1, &ebo);
|
||||
glDeleteBuffers(1, &vbo);
|
||||
glDeleteVertexArrays(1, &vao);
|
||||
}
|
||||
}
|
||||
|
||||
void AbstractVertexArrayObject::Render(GLenum mode)
|
||||
{
|
||||
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) :
|
||||
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);
|
||||
|
@ -57,4 +60,6 @@ AbstractVertexArrayObject::AbstractVertexArrayObject(const VertexArray& vertices
|
|||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue