massive rewrite
This commit is contained in:
parent
4c1c151422
commit
5811d3676a
|
@ -19,7 +19,7 @@ add_library(lol STATIC
|
||||||
"src/Shader.cpp"
|
"src/Shader.cpp"
|
||||||
"src/VertexArrayObject.cpp"
|
"src/VertexArrayObject.cpp"
|
||||||
"vendor/glad/src/glad.c"
|
"vendor/glad/src/glad.c"
|
||||||
"include/lol/Texture.hpp" "src/Texture.cpp" )
|
"include/lol/Texture.hpp" "src/Texture.cpp" "include/lol/util/Enums.hpp" "include/lol/Buffer.hpp" "src/Buffer.cpp" "include/lol/buffers/VertexBuffer.hpp" "src/buffers/VertexBuffer.cpp" "include/lol/buffers/ElementBuffer.hpp" "src/buffers/ElementBuffer.cpp")
|
||||||
|
|
||||||
target_include_directories(lol PUBLIC
|
target_include_directories(lol PUBLIC
|
||||||
${GLM_INCLUDE_DIRS}
|
${GLM_INCLUDE_DIRS}
|
||||||
|
|
32
include/lol/Buffer.hpp
Normal file
32
include/lol/Buffer.hpp
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <lol/util/NonCopyable.hpp>
|
||||||
|
#include <lol/util/Enums.hpp>
|
||||||
|
|
||||||
|
namespace lol
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @brief Represents a generic buffer object. The buffer is destroyed together with the object
|
||||||
|
*/
|
||||||
|
class Buffer : public NonCopyable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void* Map(Access access);
|
||||||
|
void Unmap();
|
||||||
|
|
||||||
|
void Bind();
|
||||||
|
void Unbind();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Buffer(BufferType target);
|
||||||
|
virtual ~Buffer();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
unsigned int id;
|
||||||
|
BufferType type;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -10,23 +10,14 @@ namespace lol
|
||||||
|
|
||||||
class CameraBase;
|
class CameraBase;
|
||||||
|
|
||||||
enum class PrimitiveType
|
|
||||||
{
|
|
||||||
Lines = GL_LINES,
|
|
||||||
LineStrip = GL_LINE_STRIP,
|
|
||||||
LineLoop = GL_LINE_LOOP,
|
|
||||||
|
|
||||||
Triangles = GL_TRIANGLES,
|
|
||||||
TriangleStrip = GL_TRIANGLE_STRIP,
|
|
||||||
TriangleFan = GL_TRIANGLE_FAN
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that can be displayed on a screen.
|
* A class that can be displayed on a screen.
|
||||||
*/
|
*/
|
||||||
class Drawable
|
class Drawable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
virtual ~Drawable() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Called immediately after the shader is bound, and before the VAO is drawn.
|
* @brief Called immediately after the shader is bound, and before the VAO is drawn.
|
||||||
*
|
*
|
||||||
|
@ -44,16 +35,16 @@ namespace lol
|
||||||
/**
|
/**
|
||||||
* @brief The VAO can be rendered as a mesh, a set of lines, loops, strips etc
|
* @brief The VAO can be rendered as a mesh, a set of lines, loops, strips etc
|
||||||
*/
|
*/
|
||||||
void SetPrimitiveType(PrimitiveType type);
|
void SetDrawMode(DrawMode type);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Drawable() {}
|
Drawable() {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
VertexArrayObject vao;
|
std::shared_ptr<VertexArray> vao;
|
||||||
Shader shader;
|
std::shared_ptr<Shader> shader;
|
||||||
|
|
||||||
PrimitiveType type = PrimitiveType::Triangles;
|
DrawMode type = DrawMode::Triangles;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
|
@ -11,13 +11,10 @@
|
||||||
|
|
||||||
namespace lol
|
namespace lol
|
||||||
{
|
{
|
||||||
class UniqueShader;
|
|
||||||
typedef std::shared_ptr<UniqueShader> Shader;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compiles shaders into a program and manages access to that program
|
* Compiles shaders into a program and manages access to that program
|
||||||
*/
|
*/
|
||||||
class UniqueShader : public NonCopyable
|
class Shader : public NonCopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
|
@ -26,21 +23,8 @@ namespace lol
|
||||||
* @param vertexShader Source code of the vertex shader
|
* @param vertexShader Source code of the vertex shader
|
||||||
* @param fragmentShader Source code of the fragment shader
|
* @param fragmentShader Source code of the fragment shader
|
||||||
*/
|
*/
|
||||||
UniqueShader(const std::string& vertexShader, const std::string& fragmentShader);
|
Shader(const std::string& vertexShader, const std::string& fragmentShader);
|
||||||
~UniqueShader();
|
~Shader();
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Creates a shareable UniqueShader. Note that they're SHAREable, not COPYable
|
|
||||||
*
|
|
||||||
* @param vertexShader Source code of the vertex shader
|
|
||||||
* @param fragmentShader Source code of the fragment shader
|
|
||||||
*
|
|
||||||
* @returns Shared pointer to a UniqueShader
|
|
||||||
*/
|
|
||||||
inline static Shader Share(const std::string& vertexShader, const std::string& fragmentShader)
|
|
||||||
{
|
|
||||||
return std::make_shared<UniqueShader>(vertexShader, fragmentShader);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Status of the program creation
|
* @brief Status of the program creation
|
||||||
|
@ -52,7 +36,8 @@ namespace lol
|
||||||
/**
|
/**
|
||||||
* @brief Bind this shader program
|
* @brief Bind this shader program
|
||||||
*/
|
*/
|
||||||
void Use();
|
void Bind();
|
||||||
|
void Unbind();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a 4x4 matrix uniform
|
* Set a 4x4 matrix uniform
|
||||||
|
@ -72,8 +57,6 @@ namespace lol
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
|
|
||||||
bool recording = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef ObjectManager<Shader> ShaderManager;
|
typedef ObjectManager<Shader> ShaderManager;
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
namespace lol
|
namespace lol
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
class UniqueTexture;
|
class UniqueTexture;
|
||||||
typedef std::shared_ptr<UniqueTexture> Texture;
|
typedef std::shared_ptr<UniqueTexture> Texture;
|
||||||
|
|
||||||
|
@ -12,6 +14,7 @@ namespace lol
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UniqueTexture();
|
UniqueTexture();
|
||||||
|
~UniqueTexture();
|
||||||
|
|
||||||
inline static Texture Share()
|
inline static Texture Share()
|
||||||
{
|
{
|
||||||
|
|
|
@ -17,6 +17,8 @@ namespace lol
|
||||||
*/
|
*/
|
||||||
Transformable();
|
Transformable();
|
||||||
|
|
||||||
|
virtual ~Transformable() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get position of the object
|
* @brief Get position of the object
|
||||||
*
|
*
|
||||||
|
|
|
@ -3,88 +3,40 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include <lol/util/NonCopyable.hpp>
|
#include <lol/buffers/VertexBuffer.hpp>
|
||||||
|
#include <lol/buffers/ElementBuffer.hpp>
|
||||||
#include <lol/util/ObjectManager.hpp>
|
#include <lol/util/ObjectManager.hpp>
|
||||||
|
|
||||||
namespace lol
|
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;
|
|
||||||
|
|
||||||
|
|
||||||
// OpenGL Buffer usages (I turned them into an enum so it's easier to know what options exist)
|
|
||||||
enum class Usage
|
|
||||||
{
|
|
||||||
Static, Stream, Dynamic
|
|
||||||
};
|
|
||||||
|
|
||||||
class UniqueVertexArrayObject;
|
|
||||||
// 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<UniqueVertexArrayObject> VertexArrayObject;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 UniqueVertexArrayObject : public NonCopyable
|
class VertexArray : public NonCopyable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Creates new VAO from a set of vertices and indices and a layout/usage description
|
* @brief Creates new VAO from a set of vertices and indices and a layout/usage description
|
||||||
*
|
|
||||||
* @param vertices The vertices to go into the array buffer
|
|
||||||
* @param indices The indices to go into the element array buffer
|
|
||||||
* @param layout The layout of the VAO
|
|
||||||
* @param usage Usage of the VAO (static, dynamic, stream)
|
|
||||||
*/
|
*/
|
||||||
UniqueVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static);
|
VertexArray();
|
||||||
~UniqueVertexArrayObject();
|
VertexArray(const std::shared_ptr<VertexBuffer>& vertexBuffer, const std::shared_ptr<ElementBuffer>& elementBuffer);
|
||||||
|
virtual ~VertexArray();
|
||||||
|
|
||||||
/**
|
void SetElementBuffer(const std::shared_ptr<ElementBuffer>& buffer);
|
||||||
* @brief Creates a shareable UniqueVertexArrayObject. Note that they're SHAREable, not COPYable
|
void SetVertexBuffer(const std::shared_ptr<VertexBuffer>& buffer);
|
||||||
*
|
|
||||||
* @param vertices The vertices to go into the array buffer
|
|
||||||
* @param indices The indices to go into the element array buffer
|
|
||||||
* @param layout The layout of the VAO
|
|
||||||
* @param usage Usage of the VAO (static, dynamic, stream)
|
|
||||||
*
|
|
||||||
* @returns Shared pointer to a UniqueVertexArrayObject
|
|
||||||
*/
|
|
||||||
inline static VertexArrayObject Share(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage = Usage::Static)
|
|
||||||
{
|
|
||||||
return std::make_shared<UniqueVertexArrayObject>(vertices, indices, layout, usage);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
void Bind();
|
||||||
* @brief Render the VAO
|
void Unbind();
|
||||||
*
|
|
||||||
* @param mode The render mode (lines, triangles, ...)
|
inline size_t GetIndexCount() { return elementBuffer->GetCount(); }
|
||||||
*/
|
|
||||||
void Render(unsigned int mode = 4);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int vao, vbo, ebo;
|
unsigned int id;
|
||||||
size_t indexCount;
|
std::shared_ptr<VertexBuffer> vertexBuffer;
|
||||||
|
std::shared_ptr<ElementBuffer> elementBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Object manager for managing said shared pointers
|
// Object manager for managing said shared pointers
|
||||||
typedef ObjectManager<VertexArrayObject> VAOManager;
|
typedef ObjectManager<VertexArray> VAOManager;
|
||||||
|
|
||||||
}
|
}
|
17
include/lol/buffers/ElementBuffer.hpp
Normal file
17
include/lol/buffers/ElementBuffer.hpp
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <lol/Buffer.hpp>
|
||||||
|
|
||||||
|
namespace lol
|
||||||
|
{
|
||||||
|
class ElementBuffer : public Buffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ElementBuffer(size_t count, unsigned int* elements, Usage usage = Usage::StaticDraw);
|
||||||
|
|
||||||
|
inline size_t GetCount() const { return count; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
size_t count;
|
||||||
|
};
|
||||||
|
}
|
46
include/lol/buffers/VertexBuffer.hpp
Normal file
46
include/lol/buffers/VertexBuffer.hpp
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <lol/Buffer.hpp>
|
||||||
|
|
||||||
|
namespace lol
|
||||||
|
{
|
||||||
|
struct VertexAttribute
|
||||||
|
{
|
||||||
|
int size;
|
||||||
|
Type type;
|
||||||
|
bool normalized;
|
||||||
|
int offset;
|
||||||
|
|
||||||
|
VertexAttribute(Type type, unsigned int count, bool normalized) :
|
||||||
|
type(type), size(count), normalized(normalized), offset(0)
|
||||||
|
{ }
|
||||||
|
};
|
||||||
|
|
||||||
|
class BufferLayout
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
BufferLayout(const std::initializer_list<VertexAttribute>& attributes);
|
||||||
|
|
||||||
|
inline const std::vector<VertexAttribute>& GetLayout() const { return layout; }
|
||||||
|
inline int GetStride() const { return stride; }
|
||||||
|
|
||||||
|
std::vector<VertexAttribute>::const_iterator begin() const { return layout.begin(); }
|
||||||
|
std::vector<VertexAttribute>::const_iterator end() const { return layout.end(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<VertexAttribute> layout;
|
||||||
|
int stride;
|
||||||
|
};
|
||||||
|
|
||||||
|
class VertexBuffer : public Buffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
VertexBuffer(size_t size, float* data, Usage usage = Usage::StaticDraw);
|
||||||
|
|
||||||
|
inline void SetLayout(const BufferLayout& layout) { this->layout = layout; };
|
||||||
|
inline const BufferLayout& GetLayout() { return layout; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
BufferLayout layout;
|
||||||
|
};
|
||||||
|
}
|
181
include/lol/util/Enums.hpp
Normal file
181
include/lol/util/Enums.hpp
Normal file
|
@ -0,0 +1,181 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <glad/glad.h>
|
||||||
|
|
||||||
|
#define NATIVE(x) static_cast<GLenum>(x)
|
||||||
|
|
||||||
|
namespace lol
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief OpenGL datatypes
|
||||||
|
*/
|
||||||
|
enum class Type : GLenum
|
||||||
|
{
|
||||||
|
Byte = GL_BYTE,
|
||||||
|
UByte = GL_UNSIGNED_BYTE,
|
||||||
|
Short = GL_UNSIGNED_SHORT,
|
||||||
|
Int = GL_INT,
|
||||||
|
UInt = GL_UNSIGNED_INT,
|
||||||
|
Half = GL_HALF_FLOAT,
|
||||||
|
Float = GL_FLOAT,
|
||||||
|
Double = GL_DOUBLE,
|
||||||
|
Fixed = GL_FIXED
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get size of OpenGL datatype in Bytes
|
||||||
|
*/
|
||||||
|
constexpr size_t SizeOf(Type type)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case Type::Byte: return sizeof(GLbyte);
|
||||||
|
case Type::UByte: return sizeof(GLubyte);
|
||||||
|
case Type::Short: return sizeof(GLshort);
|
||||||
|
case Type::Int: return sizeof(GLint);
|
||||||
|
case Type::UInt: return sizeof(GLuint);
|
||||||
|
case Type::Half: return sizeof(GLhalf);
|
||||||
|
case Type::Float: return sizeof(GLfloat);
|
||||||
|
case Type::Double: return sizeof(GLdouble);
|
||||||
|
case Type::Fixed: return sizeof(GLfixed);
|
||||||
|
|
||||||
|
default:
|
||||||
|
assert(false && "lol::SizeOf did not implement every datatype");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Datatypes in GLSL
|
||||||
|
*/
|
||||||
|
enum class GLSLType : GLenum
|
||||||
|
{
|
||||||
|
Float = GL_FLOAT, Float2 = GL_FLOAT_VEC2, Float3 = GL_FLOAT_VEC3, Float4 = GL_FLOAT_VEC4,
|
||||||
|
Double = GL_DOUBLE, Double2 = GL_DOUBLE_VEC2, Double3 = GL_DOUBLE_VEC3, Double4 = GL_DOUBLE_VEC4,
|
||||||
|
Int = GL_INT, Int2 = GL_INT_VEC2, Int3 = GL_INT_VEC3, Int4 = GL_INT_VEC4,
|
||||||
|
UInt = GL_UNSIGNED_INT, UInt2 = GL_UNSIGNED_INT_VEC2, UInt3 = GL_UNSIGNED_INT_VEC3, UInt4 = GL_UNSIGNED_INT_VEC4,
|
||||||
|
Bool = GL_BOOL, Bool2 = GL_BOOL_VEC2, Bool3 = GL_BOOL_VEC3, Bool4 = GL_BOOL_VEC4,
|
||||||
|
|
||||||
|
Mat2 = GL_FLOAT_MAT2, Mat3 = GL_FLOAT_MAT3, Mat4 = GL_FLOAT_MAT4,
|
||||||
|
Mat2x3 = GL_FLOAT_MAT2x3, Mat2x4 = GL_FLOAT_MAT2x4, Mat3x2 = GL_FLOAT_MAT3x2,
|
||||||
|
Mat3x4 = GL_FLOAT_MAT3x4, Mat4x2 = GL_FLOAT_MAT4x2, Mat4x3 = GL_FLOAT_MAT4x3,
|
||||||
|
|
||||||
|
DMat2 = GL_DOUBLE_MAT2, DMat3 = GL_DOUBLE_MAT3, DMat4 = GL_DOUBLE_MAT4,
|
||||||
|
DMat2x3 = GL_DOUBLE_MAT2x3, DMat2x4 = GL_DOUBLE_MAT2x4, DMat3x2 = GL_DOUBLE_MAT3x2,
|
||||||
|
DMat3x4 = GL_DOUBLE_MAT3x4, DMat4x2 = GL_DOUBLE_MAT4x2, DMat4x3 = GL_DOUBLE_MAT4x3,
|
||||||
|
|
||||||
|
Sampler1D = GL_SAMPLER_1D, Sampler2D = GL_SAMPLER_2D,
|
||||||
|
Sampler3D = GL_SAMPLER_3D, SamplerCube = GL_SAMPLER_CUBE,
|
||||||
|
Sampler1DShadow = GL_SAMPLER_1D_SHADOW, Sampler2DShadow = GL_SAMPLER_2D_SHADOW,
|
||||||
|
Sampler1DArray = GL_SAMPLER_1D_ARRAY, Sampler2DArray = GL_SAMPLER_2D_ARRAY,
|
||||||
|
Sampler1DArrayShadow = GL_SAMPLER_1D_ARRAY_SHADOW, Sampler2DArrayShadow = GL_SAMPLER_2D_ARRAY_SHADOW,
|
||||||
|
Sampler2DMS = GL_SAMPLER_2D_MULTISAMPLE, Sampler2DMSArray = GL_SAMPLER_2D_MULTISAMPLE_ARRAY,
|
||||||
|
SamplerCubeShadow = GL_SAMPLER_CUBE_SHADOW, SamplerBuffer = GL_SAMPLER_BUFFER,
|
||||||
|
Sampler2DRect = GL_SAMPLER_2D_RECT, Sampler2DRectShadow = GL_SAMPLER_2D_RECT_SHADOW,
|
||||||
|
|
||||||
|
ISampler1D = GL_INT_SAMPLER_1D, ISampler2D = GL_INT_SAMPLER_2D,
|
||||||
|
ISampler3D = GL_INT_SAMPLER_3D, ISamplerCube = GL_INT_SAMPLER_CUBE,
|
||||||
|
ISampler1DArray = GL_INT_SAMPLER_1D_ARRAY, ISampler2DArray = GL_INT_SAMPLER_2D_ARRAY,
|
||||||
|
ISampler2DMS = GL_INT_SAMPLER_2D_MULTISAMPLE, ISampler2DMSArray = GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY,
|
||||||
|
ISamplerBuffer = GL_INT_SAMPLER_BUFFER, ISampler2DRect = GL_INT_SAMPLER_2D_RECT,
|
||||||
|
|
||||||
|
USampler1D = GL_UNSIGNED_INT_SAMPLER_1D, USampler2D = GL_UNSIGNED_INT_SAMPLER_2D,
|
||||||
|
USampler3D = GL_UNSIGNED_INT_SAMPLER_3D, USamplerCube = GL_UNSIGNED_INT_SAMPLER_CUBE,
|
||||||
|
USampler1DArray = GL_UNSIGNED_INT_SAMPLER_1D_ARRAY, USampler2DArray = GL_UNSIGNED_INT_SAMPLER_2D_ARRAY,
|
||||||
|
USampler2DMS = GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE, USampler2DMSArray = GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY,
|
||||||
|
USamplerBuffer = GL_UNSIGNED_INT_SAMPLER_BUFFER, USampler2DRect = GL_UNSIGNED_INT_SAMPLER_2D_RECT,
|
||||||
|
|
||||||
|
Image1D = GL_IMAGE_1D, Image2D = GL_IMAGE_2D,
|
||||||
|
Image3D = GL_IMAGE_3D, Image2DRect = GL_IMAGE_2D_RECT,
|
||||||
|
ImageCube = GL_IMAGE_CUBE, ImageBuffer = GL_IMAGE_BUFFER,
|
||||||
|
Image1DArray = GL_IMAGE_1D_ARRAY, Image2DArray = GL_IMAGE_2D_ARRAY,
|
||||||
|
Image2DMS = GL_IMAGE_2D_MULTISAMPLE, Image2DMSArray = GL_IMAGE_2D_MULTISAMPLE_ARRAY,
|
||||||
|
|
||||||
|
IImage1D = GL_INT_IMAGE_1D, IImage2D = GL_INT_IMAGE_2D,
|
||||||
|
IImage3D = GL_INT_IMAGE_3D, IImage2DRect = GL_INT_IMAGE_2D_RECT,
|
||||||
|
IImageCube = GL_INT_IMAGE_CUBE, IImageBuffer = GL_INT_IMAGE_BUFFER,
|
||||||
|
IImage1DArray = GL_INT_IMAGE_1D_ARRAY, IImage2DArray = GL_INT_IMAGE_2D_ARRAY,
|
||||||
|
IImage2DMS = GL_INT_IMAGE_2D_MULTISAMPLE, IImage2DMSArray = GL_INT_IMAGE_2D_MULTISAMPLE_ARRAY,
|
||||||
|
|
||||||
|
UImage1D = GL_UNSIGNED_INT_IMAGE_1D, UImage2D = GL_UNSIGNED_INT_IMAGE_2D,
|
||||||
|
UImage3D = GL_UNSIGNED_INT_IMAGE_3D, UImage2DRect = GL_UNSIGNED_INT_IMAGE_2D_RECT,
|
||||||
|
UImageCube = GL_UNSIGNED_INT_IMAGE_CUBE, UImageBuffer = GL_UNSIGNED_INT_IMAGE_BUFFER,
|
||||||
|
UImage1DArray = GL_UNSIGNED_INT_IMAGE_1D_ARRAY, UImage2DArray = GL_UNSIGNED_INT_IMAGE_2D_ARRAY,
|
||||||
|
UImage2DMS = GL_UNSIGNED_INT_IMAGE_2D_MULTISAMPLE, UImage2DMSArray = GL_UNSIGNED_INT_IMAGE_2D_MULTISAMPLE_ARRAY,
|
||||||
|
|
||||||
|
AtomicUnit = GL_UNSIGNED_INT_ATOMIC_COUNTER
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief OpenGL buffer binding targets
|
||||||
|
*/
|
||||||
|
enum class BufferType : GLenum
|
||||||
|
{
|
||||||
|
Array = GL_ARRAY_BUFFER,
|
||||||
|
AtomicCounter = GL_ATOMIC_COUNTER_BUFFER,
|
||||||
|
CopyRead = GL_COPY_READ_BUFFER,
|
||||||
|
CopyWrite = GL_COPY_WRITE_BUFFER,
|
||||||
|
DispatchIndirect = GL_DISPATCH_INDIRECT_BUFFER,
|
||||||
|
DrawIndirect = GL_DRAW_INDIRECT_BUFFER,
|
||||||
|
ElementArray = GL_ELEMENT_ARRAY_BUFFER,
|
||||||
|
PixelPack = GL_PIXEL_PACK_BUFFER,
|
||||||
|
PixelUnpack = GL_PIXEL_UNPACK_BUFFER,
|
||||||
|
Query = GL_QUERY_BUFFER,
|
||||||
|
ShaderStorage = GL_SHADER_STORAGE_BUFFER,
|
||||||
|
Texture = GL_TEXTURE_BUFFER,
|
||||||
|
TransformFeedback = GL_TRANSFORM_FEEDBACK_BUFFER,
|
||||||
|
Uniform = GL_UNIFORM_BUFFER
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief OpenGL buffer usage types
|
||||||
|
*/
|
||||||
|
enum class Usage : GLenum
|
||||||
|
{
|
||||||
|
StaticDraw = GL_STATIC_DRAW,
|
||||||
|
StaticRead = GL_STATIC_READ,
|
||||||
|
StaticCopy = GL_STATIC_COPY,
|
||||||
|
|
||||||
|
StreamDraw = GL_STREAM_DRAW,
|
||||||
|
StreamRead = GL_STREAM_READ,
|
||||||
|
StreamCopy = GL_STREAM_COPY,
|
||||||
|
|
||||||
|
DynamicDraw = GL_DYNAMIC_DRAW,
|
||||||
|
DynamicRead = GL_DYNAMIC_READ,
|
||||||
|
DynamicCopy = GL_DYNAMIC_COPY
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief OpenGL glDraw* modes. Primitives OpenGL can render
|
||||||
|
*/
|
||||||
|
enum class DrawMode : GLenum
|
||||||
|
{
|
||||||
|
Points = GL_POINTS,
|
||||||
|
|
||||||
|
Lines = GL_LINES,
|
||||||
|
LineStrip = GL_LINE_STRIP,
|
||||||
|
LineLoop = GL_LINE_LOOP,
|
||||||
|
LineStripAdjacency = GL_LINE_STRIP_ADJACENCY,
|
||||||
|
LinesAdjacency = GL_LINES_ADJACENCY,
|
||||||
|
|
||||||
|
Triangles = GL_TRIANGLES,
|
||||||
|
TriangleStrip = GL_TRIANGLE_STRIP,
|
||||||
|
TriangleFan = GL_TRIANGLE_FAN,
|
||||||
|
TriangleStripAdjacency = GL_TRIANGLE_STRIP_ADJACENCY,
|
||||||
|
TrianglesAdjacency = GL_TRIANGLES_ADJACENCY,
|
||||||
|
|
||||||
|
Patches = GL_PATCHES
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief OpenGL buffer access modes
|
||||||
|
*/
|
||||||
|
enum class Access : GLenum
|
||||||
|
{
|
||||||
|
ReadOnly = GL_READ_ONLY,
|
||||||
|
WriteOnly = GL_WRITE_ONLY,
|
||||||
|
ReadWrite = GL_READ_WRITE
|
||||||
|
};
|
||||||
|
}
|
|
@ -30,7 +30,7 @@ namespace lol
|
||||||
/**
|
/**
|
||||||
* Add new (existing) object to manager
|
* Add new (existing) object to manager
|
||||||
*/
|
*/
|
||||||
inline void Register(unsigned int id, Type obj)
|
inline void Register(unsigned int id, const std::shared_ptr<Type>& obj)
|
||||||
{
|
{
|
||||||
objects.insert(std::make_pair(id, obj));
|
objects.insert(std::make_pair(id, obj));
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ namespace lol
|
||||||
/**
|
/**
|
||||||
* Retrieve object from manager
|
* Retrieve object from manager
|
||||||
*/
|
*/
|
||||||
inline Type Get(unsigned int id)
|
inline std::shared_ptr<Type> Get(unsigned int id)
|
||||||
{
|
{
|
||||||
auto it = objects.find(id);
|
auto it = objects.find(id);
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ namespace lol
|
||||||
ObjectManager() {}
|
ObjectManager() {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<unsigned int, Type> objects;
|
std::map<unsigned int, std::shared_ptr<Type>> objects;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
37
src/Buffer.cpp
Normal file
37
src/Buffer.cpp
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#include "..\include\lol\Buffer.hpp"
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace lol
|
||||||
|
{
|
||||||
|
Buffer::Buffer(BufferType type) :
|
||||||
|
id(0), type(type)
|
||||||
|
{
|
||||||
|
glGenBuffers(1, &id);
|
||||||
|
glBindBuffer(NATIVE(type), id);
|
||||||
|
}
|
||||||
|
|
||||||
|
Buffer::~Buffer()
|
||||||
|
{
|
||||||
|
glDeleteBuffers(1, &id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* Buffer::Map(Access access)
|
||||||
|
{
|
||||||
|
return glMapBuffer(NATIVE(type), NATIVE(access));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Buffer::Unmap()
|
||||||
|
{
|
||||||
|
glUnmapBuffer(NATIVE(type));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Buffer::Bind()
|
||||||
|
{
|
||||||
|
glBindBuffer(NATIVE(type), id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Buffer::Unbind()
|
||||||
|
{
|
||||||
|
glBindBuffer(NATIVE(type), 0);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,12 +5,14 @@ namespace lol
|
||||||
|
|
||||||
void Drawable::Draw(const CameraBase& camera) const
|
void Drawable::Draw(const CameraBase& camera) const
|
||||||
{
|
{
|
||||||
shader->Use();
|
shader->Bind();
|
||||||
|
vao->Bind();
|
||||||
PreRender(camera);
|
PreRender(camera);
|
||||||
vao->Render(static_cast<unsigned int>(type));
|
|
||||||
|
glDrawElements(NATIVE(type), vao->GetIndexCount(), GL_UNSIGNED_INT, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Drawable::SetPrimitiveType(PrimitiveType type)
|
void Drawable::SetDrawMode(DrawMode type)
|
||||||
{
|
{
|
||||||
this->type = type;
|
this->type = type;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ inline
|
||||||
namespace lol
|
namespace lol
|
||||||
{
|
{
|
||||||
|
|
||||||
UniqueShader::UniqueShader(const std::string& vertexShader, const std::string& fragmentShader) :
|
Shader::Shader(const std::string& vertexShader, const std::string& fragmentShader) :
|
||||||
id(0)
|
id(0)
|
||||||
{
|
{
|
||||||
GLint success;
|
GLint success;
|
||||||
|
@ -71,17 +71,22 @@ namespace lol
|
||||||
glDeleteShader(vertexShaderID);
|
glDeleteShader(vertexShaderID);
|
||||||
}
|
}
|
||||||
|
|
||||||
UniqueShader::~UniqueShader()
|
Shader::~Shader()
|
||||||
{
|
{
|
||||||
glDeleteProgram(id);
|
glDeleteProgram(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UniqueShader::Use()
|
void Shader::Bind()
|
||||||
{
|
{
|
||||||
glUseProgram(id);
|
glUseProgram(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UniqueShader::SetUniform(const std::string& name, const glm::mat4& value)
|
void Shader::Unbind()
|
||||||
|
{
|
||||||
|
glUseProgram(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shader::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)
|
||||||
|
@ -90,7 +95,7 @@ namespace lol
|
||||||
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value));
|
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void UniqueShader::SetUniform(const std::string& name, const glm::vec4& value)
|
void Shader::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)
|
||||||
|
|
|
@ -7,6 +7,14 @@ namespace lol
|
||||||
UniqueTexture::UniqueTexture() :
|
UniqueTexture::UniqueTexture() :
|
||||||
textureID(0)
|
textureID(0)
|
||||||
{
|
{
|
||||||
|
glGenTextures(1, &textureID);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||||
|
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_UNSIGNED_BYTE, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
UniqueTexture::~UniqueTexture()
|
||||||
|
{
|
||||||
|
glDeleteTextures(1, &textureID);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,59 +6,58 @@
|
||||||
namespace lol
|
namespace lol
|
||||||
{
|
{
|
||||||
|
|
||||||
UniqueVertexArrayObject::~UniqueVertexArrayObject()
|
VertexArray::VertexArray()
|
||||||
{
|
{
|
||||||
glDeleteBuffers(1, &ebo);
|
glGenVertexArrays(1, &id);
|
||||||
glDeleteBuffers(1, &vbo);
|
|
||||||
glDeleteVertexArrays(1, &vao);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UniqueVertexArrayObject::Render(GLenum mode)
|
VertexArray::VertexArray(const std::shared_ptr<VertexBuffer>& vertexBuffer, const std::shared_ptr<ElementBuffer>& elementBuffer) :
|
||||||
|
id(0)
|
||||||
{
|
{
|
||||||
assert(vao != 0);
|
glGenVertexArrays(1, &id);
|
||||||
|
|
||||||
glBindVertexArray(vao);
|
SetVertexBuffer(vertexBuffer);
|
||||||
// GLenum result = glGetError();
|
SetElementBuffer(elementBuffer);
|
||||||
glDrawElements(mode, indexCount, GL_UNSIGNED_INT, 0);
|
}
|
||||||
|
VertexArray::~VertexArray()
|
||||||
|
{
|
||||||
|
glDeleteVertexArrays(1, &id);
|
||||||
}
|
}
|
||||||
|
|
||||||
UniqueVertexArrayObject::UniqueVertexArrayObject(const VertexArray& vertices, const IndexArray& indices, const Layout& layout, Usage usage) :
|
void VertexArray::SetVertexBuffer(const std::shared_ptr<VertexBuffer>& buffer)
|
||||||
vao(0), vbo(0), ebo(0), indexCount(indices.size())
|
|
||||||
{
|
{
|
||||||
glGenVertexArrays(1, &vao);
|
glBindVertexArray(id);
|
||||||
glBindVertexArray(vao);
|
buffer->Bind();
|
||||||
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
|
// Set up pipeline layout
|
||||||
unsigned int index = 0;
|
unsigned int index = 0;
|
||||||
|
const BufferLayout& layout = buffer->GetLayout();
|
||||||
for (const VertexAttribute& attribute : layout)
|
for (const VertexAttribute& attribute : layout)
|
||||||
{
|
{
|
||||||
glVertexAttribPointer(index, attribute.size, attribute.type, attribute.normalized, attribute.stride, attribute.pointer);
|
|
||||||
glEnableVertexAttribArray(index);
|
glEnableVertexAttribArray(index);
|
||||||
|
glVertexAttribPointer(index, attribute.size, NATIVE(attribute.type), attribute.normalized, layout.GetStride(), (void*)(attribute.offset));
|
||||||
|
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vertexBuffer = buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexArray::SetElementBuffer(const std::shared_ptr<ElementBuffer>& buffer)
|
||||||
|
{
|
||||||
|
glBindVertexArray(id);
|
||||||
|
buffer->Bind();
|
||||||
|
|
||||||
|
elementBuffer = buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void VertexArray::Bind()
|
||||||
|
{
|
||||||
|
glBindVertexArray(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Unbind()
|
||||||
|
{
|
||||||
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
10
src/buffers/ElementBuffer.cpp
Normal file
10
src/buffers/ElementBuffer.cpp
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#include <lol/buffers/ElementBuffer.hpp>
|
||||||
|
|
||||||
|
namespace lol
|
||||||
|
{
|
||||||
|
ElementBuffer::ElementBuffer(size_t count, unsigned int* elements, Usage usage) :
|
||||||
|
Buffer(BufferType::ElementArray), count(count)
|
||||||
|
{
|
||||||
|
glBufferData(NATIVE(type), count * sizeof(unsigned int), elements, NATIVE(usage));
|
||||||
|
}
|
||||||
|
}
|
23
src/buffers/VertexBuffer.cpp
Normal file
23
src/buffers/VertexBuffer.cpp
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
#include <lol/buffers/VertexBuffer.hpp>
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace lol
|
||||||
|
{
|
||||||
|
BufferLayout::BufferLayout(const std::initializer_list<VertexAttribute>& attributes) :
|
||||||
|
layout(attributes), stride(0)
|
||||||
|
{
|
||||||
|
// Calculate stride and offsets of elements
|
||||||
|
for (VertexAttribute& attribute : layout)
|
||||||
|
{
|
||||||
|
attribute.offset += stride;
|
||||||
|
stride += (attribute.size * SizeOf(attribute.type));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
VertexBuffer::VertexBuffer(size_t size, float* data, Usage usage) :
|
||||||
|
Buffer(BufferType::Array), layout{}
|
||||||
|
{
|
||||||
|
glBufferData(NATIVE(type), size * sizeof(float), (void*)data, NATIVE(usage));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue