Made NBGraphics installable

This commit is contained in:
NaifBanana
2026-08-20 08:14:29 -05:00
parent 3bad580793
commit bb2f046c90
26 changed files with 116 additions and 62 deletions
@@ -0,0 +1,217 @@
#pragma once
#ifndef _NB_BUFFER
#define _NB_BUFFER
#include <NBGraphics/GLLoad.hpp>
#include <string>
#include <NBCore/Errors.hpp>
#include <NBCore/Logger.hpp>
#include <NBCore/Utils.hpp>
#include <NBGraphics/OGLObjects.hpp>
namespace nb {
extern const std::unordered_map<GLenum, std::string> BufferTypes;
template<GLenum N>
struct GLSLEnum;
template<typename T>
struct GLSLType;
#define GLSLTypeTraits(type_, size_, value_) \
template<>\
struct GLSLEnum<value_> {\
using type = type_;\
static constexpr size_t size = size_;\
static constexpr GLenum value=value_;\
};\
template<>\
struct GLSLType<type_> {\
using type = type_;\
static constexpr size_t size = size_;\
static constexpr GLenum value=value_;\
};
#define GLSLTypeEquivalence(original, newtype) \
template<>\
struct GLSLType<newtype> : public GLSLType<original> {};
GLSLTypeTraits(uint8_t, 1, GL_UNSIGNED_BYTE);
GLSLTypeTraits(uint16_t, 2, GL_UNSIGNED_SHORT);
GLSLTypeTraits(uint32_t, 4, GL_UNSIGNED_INT);
GLSLTypeTraits(int8_t, 1, GL_BYTE);
GLSLTypeTraits(int16_t, 2, GL_SHORT);
GLSLTypeTraits(int32_t, 4, GL_INT);
GLSLTypeTraits(float, 4, GL_FLOAT);
GLSLTypeTraits(double, 8, GL_DOUBLE);
class BufferError : public Error<BufferError> {
protected:
using Base = Error<BufferError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED,
DATA_OVERFLOW,
DATA_ERROR
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
class Buffer : public OpenGLObject {
protected:
using Codes = BufferError::Codes;
using OpenGLObject::_id;
GLenum _usage = GL_STATIC_DRAW;
size_t _size;
Buffer(GLenum target) : Target(target) {}
virtual GLuint declare() override {
if (!_id) {
glGenBuffers(1, &_id);
}
bind();
return _id;
}
virtual void remove() override {
if (_id) {
unbind();
glDeleteBuffers(1, &_id);
_id = 0;
}
}
public:
const GLenum Target;
using OpenGLObject::OpenGLObject;
Buffer(Buffer&& other);
Buffer& operator=(Buffer&& rhs);
virtual void bind() const override {
if (_id) {
glBindBuffer(Target, _id);
} else {
THROW(OGLError(
OGLError::Codes::INVALID_OBJECT,
"w/ BufferType " + BufferTypes.at(Target)
));
}
}
virtual void unbind() const override { glBindBuffer(Target, 0); }
GLenum usage() const;
size_t size() const;
ByteVector data() const;
void data(const ByteVector& data, GLenum usage=GL_STATIC_DRAW);
void data(const void* data, size_t size, GLenum usage=GL_STATIC_DRAW);
void subdata(const void* data, size_t size, GLintptr offset=0);
void subdata(const ByteVector& data_, GLintptr offset=0);
// virtual void clear() const { glClearBufferData(Target, ) }
};
/* template <typename BufferType>
class ImmutableBuffer : public virtual Buffer {
public:
ImmutableBuffer() = default;
ImmutableBuffer(
size_t size_,
GLenum usage_ = GL_STATIC_DRAW,
GLbitfield flags_=0x0
) : size(size_), Base::usage(usage_) {
glBufferStorage(Target, size, nullptr, flags_);
}
std::weak_ptr<void> map(GLbitfield access_) {
if (_map && _mapAccess != access_) { unmap(); }
_mapAccess = access_;
if (!_map) {
bind();
glMapBuffer(Target, _mapAccess);
}
return _map;
}
void unmap() {
bind();
glUnmapBuffer(Target);
_map.reset();
}
const size_t size;
protected:
std::shared_ptr<void> _map = nullptr;
GLbitfield _mapAccess = 0;
}; */
class ArrayBuffer : public Buffer {
public:
template<typename T>
ArrayBuffer(const std::vector<T>& data, GLenum usage=GL_STATIC_DRAW)
: Buffer(GL_ARRAY_BUFFER) {
Buffer::data(vectorToBytes(data), usage);
}
};
class ElementBuffer : protected Buffer {
protected:
GLenum _glslType;
size_t _typeSize;
public:
using Buffer::Target;
using Buffer::bind;
using Buffer::usage;
using Buffer::unbind;
ElementBuffer();
template<typename T>
ElementBuffer(const T* const data_ptr, size_t size, GLenum usage=GL_STATIC_DRAW)
: Buffer(GL_ELEMENT_ARRAY_BUFFER) {
data(data_ptr, size, usage);
}
template<typename T>
ElementBuffer(const std::vector<T>& data_, GLenum usage_=GL_STATIC_DRAW)
: ElementBuffer(data_.data(), data_.size(), usage_) {}
size_t size() const;
size_t typeSize() const;
ByteVector data() const;
GLenum glslType() const;
template<typename T>
void data(const T* data_, size_t size_, GLenum usage_=GL_STATIC_DRAW) {
declare();
using TypeInfo = GLSLType<T>;
_typeSize = TypeInfo::size;
Buffer::data(data_, size_*_typeSize, usage_);
_glslType = TypeInfo::value;
}
template<typename T>
void data(const std::vector<T>& data_, GLenum usage_=GL_STATIC_DRAW) {
data(data_.data(), data_.size(), usage_);
}
template<typename T>
void subdata(const T* data, size_t size, size_t offset) {
using TypeInfo = GLSLType<T>;
if ( _glslType != TypeInfo::value ) {
THROW( BufferError(
Codes::DATA_ERROR,
"Check element buffer data types"
) );
}
Buffer::subdata(data, size*_typeSize, offset);
}
template<typename T>
void subdata(const std::vector<T>& data, size_t offset=0) {
subdata(data.data(), data.size(), offset);
}
};
} // namespace NB
#endif // _NB_BUFFER
@@ -0,0 +1,152 @@
#pragma once
#ifndef _NB_CAMERA
#define _NB_CAMERA
#include <NBGraphics/GLLoad.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
namespace nb {
class Camera {
public:
typedef glm::vec3 Vec3;
typedef glm::mat4 Mat4;
enum CameraTypeEnum : unsigned char {
None,
Orthographic,
Perspective
};
const CameraTypeEnum CameraType = CameraTypeEnum::None;
static Vec3 normCross(const Vec3&, const Vec3&);
static Vec3 transformVec(const Mat4&, const Vec3&);
Camera(
const Vec3& pos=Vec3(0.0f, 0.0f, -1.0f),
const Vec3& tar=Vec3(0.0f, 0.0f, 0.0f),
const Vec3& up =Vec3(0.0f, 1.0f, 0.0f)
);
Vec3 getPos() const;
Vec3 getTarget() const;
Vec3 getDirection() const;
Vec3 getWorldUp() const;
Vec3 getUp() const;
Vec3 getRight() const;
Mat4 getViewMatrix() const;
Mat4 getProjectionMatrix() const;
Mat4 getFlattenedMatrices() const;
bool isUpLocked() const;
void setProjection(const Mat4&);
void setPos(const Vec3&);
void setTarget(const Vec3&);
void setDirection(const Vec3&);
void setWorldUp(const Vec3&);
void lockToWorldUp(bool lock);
void resetUp();
void addPos(const Vec3&);
void panPos(const Vec3&);
void panPosRelative(const Vec3&);
void addTarget(const Vec3&);
void roll(float);
void pitch(float);
void yaw(float);
protected:
Vec3 _pos;
Vec3 _tar;
Vec3 _dir;
Vec3 _world_up;
Vec3 _up;
Vec3 _right;
Mat4 _view;
Mat4 _proj;
bool _lock_world_up = true;
void _calcLook();
};
class OrthographicCamera : public Camera {
public:
const CameraTypeEnum CameraType = CameraTypeEnum::Orthographic;
OrthographicCamera(
const Vec3& pos=Vec3(0.0f, 0.0f, -1.0f),
const Vec3& tar=Vec3(0.0f, 0.0f, 0.0f),
const Vec3& up =Vec3(0.0f, 1.0f, 0.0f),
float left = -1.0f,
float right = 1.0f,
float bottom = -1.0f,
float top = 1.0f,
float near = 0.001f,
float far = 1.0f
);
float getLeftPlane() const;
float getRightPlane() const;
float getBottomPlane() const;
float getTopPlane() const;
float getNearPlane() const;
float getFarPlane() const;
void setLeftPlane(float);
void setRightPlane(float);
void setBottomPlane(float);
void setTopPlane(float);
void setNearPlane(float);
void setFarPlane(float);
void setOrthographicProjection(float left=-1.0, float right=1.0, float bottom=-1.0, float top=1.0, float near=.001, float far=1.0);
void setViewingVolume(float width, float height, float depth);
protected:
float _leftPlane, _rightPlane, _topPlane, _bottomPlane, _nearPlane, _farPlane;
void _calcView();
using Camera::setProjection;
};
class PerspectiveCamera : public Camera {
public:
const CameraTypeEnum CameraType = CameraTypeEnum::Perspective;
PerspectiveCamera(
const Vec3& pos=Vec3(0.0f, 0.0f, -1.0f),
const Vec3& tar=Vec3(0.0f, 0.0f, 0.0f),
const Vec3& up =Vec3(0.0f, 1.0f, 0.0f),
float fov = 45.0f,
float aspectRatio = 1.0f,
float near = 0.001f,
float far = 1.0f
);
float getFOV() const;
float getAspectRatio() const;
float getNearPlane() const;
float getFarPlane() const;
void setFOV(float);
void setAspectRatio(float);
void setNearPlane(float);
void setFarPlane(float);
void setPerspectiveProjection(float fov=45, float aspectRatio=1.0f, float near=0.001f, float far=1.0f);
protected:
float _fov, _ratio, _nearPlane, _farPlane;
void _calcView();
using Camera::setProjection;
};
}
#endif // _NB_CAMERA
@@ -0,0 +1,84 @@
#pragma once
#ifndef _NB_DRAW
#define _NB_DRAW
#include <NBGraphics/GLLoad.hpp>
#include <exception>
#include <map>
#include <string>
#include <vector>
#include <NBGraphics/Buffers.h>
#include <NBGraphics/Shader.h>
#include <NBGraphics/VAOManager.hpp>
#define THROW_DRAW_ERROR(msg) throw DrawError(msg, __FILE__, __LINE__);
namespace NB{
class DrawError : public std::runtime_error {
public:
DrawError(const std::string&, const std::string& file="", int line=-1);
};
uint8_t VerticesFromPrimitives(GLenum, unsigned int num_prims=1);
uint8_t PrimitivesFromVertices(GLenum, unsigned int);
void copyVertexAttribute(const VertexAttribute&, VertexAttribute&);
class DrawBuffer {
public:
typedef std::shared_ptr<Buffer> BufferManagerPointer;
DrawBuffer(VertexAttributeList, GLenum prim_type=GL_TRIANGLES);
//DrawBuffer(std::initializer_list<VertexAttribute>, std::initializer_list<VertexAttributePointer>);
unsigned int vertSize() const;
const std::shared_ptr<ShaderProgram> shader() const;
unsigned int elementBufferID() const;
unsigned int vertexBufferID() const;
virtual void draw() const;
RawVec vertAttrData(unsigned int);
VertexAttributeList changeVertLayout(unsigned int, VertexAttributePointer);
RawVec vertAttrData(unsigned int, const RawVec&);
virtual void elementData(const std::vector<unsigned int>&);
virtual void elementData(unsigned int);
virtual void elementData(const std::vector<unsigned int>&, unsigned int offset);
virtual void vertexData(const RawVec&);
virtual void vertexData(unsigned int num_verts);
virtual void vertexData(const RawVec&, unsigned int offset);
BufferManagerPointer _vert_data;
BufferManagerPointer _elmt_data;
VAOManager _VAO;
const GLenum _gl_primitive_type=0;
virtual VertexAttributeList defaultVAO(VertexAttributeList);
unsigned int _num_verts, _num_prims;
protected:
};
class DrawInstanceBuffer : public DrawBuffer {
public:
DrawInstanceBuffer(VertexAttributeList, VertexAttributeList, GLenum primt_type=GL_TRIANGLES);
unsigned int instSize() const;
unsigned int instanceBufferID() const;
virtual void draw() const override;
VertexAttributeList changeInstanceLayout(unsigned int, VertexAttributePointer);
virtual void instanceData(const RawVec&);
virtual void instanceData(unsigned int);
virtual void instanceData(const RawVec&, unsigned int offset);
BufferManagerPointer _inst_data;
virtual VertexAttributeList defaultVAO(VertexAttributeList);
unsigned int _num_inst;
protected:
};
}
#endif
@@ -0,0 +1,236 @@
#pragma once
#ifndef _NB_FRAMEBUFFER
#define _NB_FRAMEBUFFER
#include <NBGraphics/GLLoad.hpp>
#include <NBCore/Errors.hpp>
#include <NBGraphics/Image.hpp>
#include <NBGraphics/OGLObjects.hpp>
#include <NBGraphics/Textures.hpp>
namespace nb {
class FrameBufferError : public Error<FrameBufferError> {
protected:
using Base = Error<FrameBufferError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED, INVALID_VALUE
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
class RenderBuffer : public RenderTarget {
protected:
GLuint declare() override {
if (!_id) {
glRenderbufferStorage(
GL_RENDERBUFFER,
format,
width,
height
);
}
return _id;
}
void remove() override {
if (_id) {
unbind();
glDeleteRenderbuffers(1, &_id);
_id = 0;
}
}
public:
const unsigned int width;
const unsigned int height;
const GLenum format;
template<typename PixelType>
RenderBuffer(unsigned int width_, unsigned int height_) :
width(width_),
height(height_),
format(OGLPixelFormat<PixelType>::glFormat),
RenderTarget(GL_RENDERBUFFER)
{ declare(); }
RenderBuffer(RenderBuffer&& rhs) :
width(rhs.width),
height(rhs.height),
format(rhs.format),
RenderTarget(std::move(rhs))
{}
RenderBuffer& operator=(RenderBuffer&&) = delete;
void bind() const override {
if (_id) {
glBindRenderbuffer(GL_RENDERBUFFER, _id);
} else {
THROW(OGLError(
OGLError::Codes::INVALID_OBJECT,
"w/ RenderBuffer"
));
}
}
void unbind() const override {
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}
};
class FrameBufferBase : public OpenGLObject {
private:
static bool texTypeIs2D(GLenum texType) {
switch(texType) {
case GL_TEXTURE_2D:
default:
return true;
break;
}
}
protected:
using AttachmentMap = std::unordered_map<GLenum, std::shared_ptr<RenderTarget>>;
AttachmentMap _attachments;
FrameBufferBase(GLenum target);
GLuint declare() override;
void remove() override;
std::shared_ptr<RenderTarget> detach(GLenum);
void attach(
GLenum attachment,
std::shared_ptr<Texture> texture,
unsigned int level=0
);
void attach(
GLenum attachment,
std::shared_ptr<Texture> texture,
unsigned int level,
unsigned int layer
);
void attach(
GLenum attachment,
std::shared_ptr<RenderBuffer> renderBuffer
) {
declare();
if (attachment == GL_NONE) {
THROW(FrameBufferError(FrameBufferError::INVALID_VALUE,
"Cannot attach RenderBuffer to `GL_NONE`"
));
}
glFramebufferRenderbuffer(
Target,
attachment,
GL_RENDERBUFFER,
renderBuffer->id()
);
_attachments[attachment] = renderBuffer;
}
public:
const GLenum Target;
void bind() const override;
void unbind() const override;
GLenum status() const;
AttachmentMap getAllBuffers() const;
std::shared_ptr<RenderTarget> getBuffer(GLenum) const;
};
class ReadFrameBuffer;
class WriteFrameBuffer;
class ReadFrameBuffer : public virtual FrameBufferBase {
friend WriteFrameBuffer;
protected:
using FrameBufferBase::_attachments;
GLenum _location;
public:
using FrameBufferBase::attach;
ReadFrameBuffer();
ReadFrameBuffer& operator=(WriteFrameBuffer&&);
ReadFrameBuffer& operator=(ReadFrameBuffer&&);
std::shared_ptr<RenderTarget> getReadBuffer() const;
GLenum getReadLocation() const;
std::shared_ptr<RenderTarget> setReadBuffer(GLenum attachment);
template<typename T, typename... Args>
std::shared_ptr<RenderTarget> setReadBuffer(
GLenum attachment,
std::shared_ptr<T> target,
Args... args
) {
declare();
FrameBufferBase::attach(attachment, target, args...);
setReadBuffer(attachment);
return getReadBuffer();
}
};
class WriteFrameBuffer : public virtual FrameBufferBase {
friend ReadFrameBuffer;
protected:
using DrawTexVec = std::vector<std::shared_ptr<Texture>>;
using FrameBufferBase::_attachments;
std::vector<GLenum> _locations;
public:
WriteFrameBuffer();
WriteFrameBuffer& operator=(ReadFrameBuffer&&);
WriteFrameBuffer& operator=(WriteFrameBuffer&&);
template<typename... T>
void attach(
GLenum attachment,
std::shared_ptr<Texture> texture,
T... args
) { FrameBufferBase::attach(attachment, texture, args...); }
DrawTexVec getWriteBuffers() const;
std::vector<GLenum> getWriteLocations() const;
DrawTexVec setWriteBuffers(const std::vector<GLenum>& attachments);
std::shared_ptr<Texture> setWriteBuffer(GLenum attachment);
template<typename... T>
std::shared_ptr<Texture> setWriteBuffer(
GLenum attachment,
std::shared_ptr<Texture> texture,
T... args
) {
attach(attachment, texture, args...);
setWriteBuffer(attachment);
return std::static_pointer_cast<Texture>(_attachments.at(attachment));
}
RGBA<float> clearColorValue() const;
RGBA<float> clearColorValue(const RGBA<float>&);
RGBA<float> clearColorValue(float r, float g, float b, float a);
double clearDepthValue() const;
double clearDepthValue(double);
GLint clearStencilValue() const;
GLint clearStencilValue(GLint);
void clear(GLbitfield mask=(
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT
)) const;
void clearColor() const;
void clearDepth() const;
void clearDepth(float) const;
void clearStencil() const;
void clearStencil(GLint) const;
void clearDepthStencil() const;
void clearDepthStencil(float, GLint) const;
};
class FrameBuffer : public ReadFrameBuffer, public WriteFrameBuffer {
protected:
using FrameBufferBase::_attachments;
using ReadFrameBuffer::_location;
using WriteFrameBuffer::_locations;
public:
FrameBuffer();
FrameBuffer& operator=(FrameBuffer&&);
FrameBuffer& operator=(ReadFrameBuffer&&);
FrameBuffer& operator=(WriteFrameBuffer&&);
};
} // namespace nb
#endif // _NB_FRAMEBUFFER
@@ -0,0 +1,8 @@
#pragma once
#ifndef _NB_OPENGL_LOADER
#define _NB_OPENGL_LOADER
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#endif
@@ -0,0 +1,216 @@
#pragma once
#ifndef _NB_IMAGE
#define _NB_IMAGE
#include <cstring>
#include <NBCore/Errors.hpp>
namespace nb {
template<typename T>
struct Color {};
struct Red : Color<Red> {};
struct Green : Color<Green> {};
struct Blue : Color<Blue> {};
struct Alpha : Color<Alpha> {};
struct Depth;
struct Stencil;
template<typename... T>
struct PixelChannel;
template<typename T>
struct PixelChannel<T, Red> {
using type=T;
T value;
T& r=value;
};
template<typename T>
struct PixelChannel<T, Green> {
using type=T;
T value;
T& g=value;
};
template<typename T>
struct PixelChannel<T, Blue> {
using type=T;
T value;
T& b=value;
};
template<typename T>
struct PixelChannel<T, Alpha> {
using type=T;
T value;
T& a=value;
};
template<typename T>
struct PixelChannel<T, Depth> {
using type=T;
T value;
T& z=value;
};
template<>
struct PixelChannel<uint8_t, Stencil> {
using type=uint8_t;
uint8_t value;
uint8_t& u=value;
};
template<typename T, typename... Channels>
struct Pixel : public PixelChannel<T, Channels>... {
Pixel() = default;
Pixel& operator=(const Pixel& rhs) {
((
[&](const typename PixelChannel<T, Channels>::type& val){
this->PixelChannel<T, Channels>::value = val;
}(rhs.PixelChannel<T, Channels>::value)
), ...);
return *this;
}
Pixel(const Pixel& cpy)
: Pixel(cpy.PixelChannel<T, Channels>::value...) {}
Pixel(typename PixelChannel<T, Channels>::type... vals)
: PixelChannel<T, Channels>{vals}... {}
};
template<typename Ref>
struct PixelReference;
template<typename T, typename... Channels>
struct PixelReference<Pixel<T, Channels...>>
: public Pixel<T&, Channels...> {
using PixelType = Pixel<T, Channels...>;
using Base = Pixel<T&, Channels...>;
using Base::Base;
PixelReference(PixelType& pixel)
: Base(pixel.PixelChannel<T, Channels>::value...) {}
operator PixelType() {
return PixelType(this->PixelChannel<T&, Channels>::value...);
}
PixelReference& operator=(const PixelType& pixel) {
((
[&](const typename PixelChannel<T, Channels>::type& val){
this->PixelChannel<T&, Channels>::value = val;
}(pixel.PixelChannel<T, Channels>::value)
), ...);
return *this;
}
};
template <typename T>
using RGBA = Pixel<T, Red, Green, Blue, Alpha>;
template <typename T>
using RGB = Pixel<T, Red, Green, Blue>;
class ImageError : public Error<ImageError> {
protected:
using Base = Error<ImageError>;
using Base::Base;
public:
enum Codes : unsigned int {
UNDEFINED, OUT_OF_BOUNDS
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
template<typename T>
class Image;
template<typename T, typename... Channels>
class Image<Pixel<T, Channels...>> {
protected:
using Codes = ImageError::Codes;
T* _data;
Image(unsigned int x, unsigned int y)
: width(x), height(y), _data(nullptr) {}
public:
using PixelType = Pixel<T, Channels...>;
static constexpr size_t NumberChannels = sizeof...(Channels);
const unsigned int width;
const unsigned int height;
Image(unsigned int x, unsigned int y, const T* data)
: Image(x, y) {
size_t data_size = width*height*NumberChannels;
_data = new T[data_size];
std::memcpy(_data, data, data_size*sizeof(T));
}
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
Image& operator=(Image&&) = delete;
Image(Image&& mv)
: width(mv.width), height(mv.height), _data(mv._data) {
mv._data = nullptr;
}
~Image() {
if (_data) {
delete[] _data;
}
}
const T* data() const { return _data; }
Image&& copy() const {
return Image(width, height, _data);
}
PixelReference<PixelType> at(unsigned int x, unsigned int y) {
if (!(x<width) || !(y<height)) {
std::string note = "w/ ("+std::to_string(x);
note += ", "+std::to_string(y) + ") >= (";
note += std::to_string(width)+", "+std::to_string(height);
note += ")";
THROW(ImageError(
Codes::OUT_OF_BOUNDS,
note
));
}
auto coord = y*height+x;
return PixelReference<PixelType>(
_data[coord*NumberChannels+Find<Channels, Channels...>::value-1]...
);
}
};
template<typename... T>
class ImageReference;
template<typename T, typename... Channels>
class ImageReference<Pixel<T, Channels...>> : public Image<Pixel<T, Channels...>> {
protected:
using Base = Image<Pixel<T, Channels...>>;
using Base::_data;
public:
using Base::width;
using Base::height;
using Base::copy;
ImageReference(unsigned int x, unsigned int y, T* data)
: Base(x, y) {
_data = data;
}
~ImageReference() noexcept {
Base::_data = nullptr;
}
};
} // namespace nb
#endif // _NB_IMAGE
@@ -0,0 +1,48 @@
#pragma once
#ifndef _NB_OGL_OBJECTS
#define _NB_OGL_OBJECTS
#include <NBGraphics/GLLoad.hpp>
#include <NBCore/Errors.hpp>
namespace nb {
class OGLError : public Error<OGLError> {
using Base = Error<OGLError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED, HANGING_OBJECT, INVALID_OBJECT
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
class OpenGLObject {
public:
OpenGLObject(const OpenGLObject&) = delete;
OpenGLObject& operator=(const OpenGLObject&) = delete;
OpenGLObject(OpenGLObject&&);
OpenGLObject& operator=(OpenGLObject&&);
virtual ~OpenGLObject() {};
virtual void bind() const = 0;
virtual void unbind() const = 0;
GLuint id() const { return _id; }
protected:
using Codes = OGLError::Codes;
OpenGLObject() = default;
virtual GLuint declare() = 0;
virtual void remove() = 0;
GLuint _id = 0;
};
} // namespace nb
#endif // _NB_OGL_OBJECTS
@@ -0,0 +1,141 @@
#pragma once
#ifndef _NB_SHADER
#define _NB_SHADER
#include <NBGraphics/GLLoad.hpp>
#include <string>
#include <NBCore/Errors.hpp>
#include <NBCore/Utils.hpp>
#include <NBGraphics/OGLObjects.hpp>
namespace nb {
class ShaderError : public Error<ShaderError> {
using Base = Error<ShaderError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
class ProgramError : public Error<ProgramError> {
using Base = Error<ProgramError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED, LINKING_ERROR
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
enum OpenGLProfiles {
Core,
Compatibility,
ES
};
class Shader : public OpenGLObject {
using Base = OpenGLObject;
public:
using Base::Base;
using Base::id;
Shader(GLenum, const std::string&);
Shader(GLenum, const std::vector<std::string>&);
Shader(Shader&&);
Shader& operator=(Shader&&) = delete;
~Shader() { remove(); }
operator bool();
virtual void bind() const override { /* TODO: Some warning of some kind perhaps*/ }
virtual void unbind() const override { /* TODO: Some warning of some kind perhaps*/ }
bool success() const;
GLint status(GLenum) const;
std::string log() const;
const GLenum target;
friend Base;
protected:
using Base::_id;
GLint _success;
std::vector<std::string> _sources;
virtual void compile() {
int num_srcs = _sources.size();
std::vector<char*> src_ptrs(num_srcs);
for (int i=0; i < num_srcs; ++i) {
src_ptrs[i] = _sources[i].data();
}
glShaderSource(_id, num_srcs, src_ptrs.data(), NULL);
glCompileShader(_id);
_success = status(GL_COMPILE_STATUS);
if (!_success) {
WARN(log(), 0x0FE);
}
}
virtual GLuint declare() override {
if (!_id) {
_id = _id = glCreateShader(target);
}
return _id;
}
virtual void remove() override {
if (_id) {
glDeleteShader(_id);
}
}
};
class Program : public OpenGLObject {
using Base = OpenGLObject;
public:
using Base::Base;
using Base::id;
Program(SharedVector<Shader>);
Program(Program&&);
Program& operator=(Program&&) = delete;
operator bool();
~Program() { remove(); }
virtual void bind() const override {
glUseProgram(_id);
}
virtual void unbind() const override { /* TODO: Some warning of some kind perhaps*/ }
GLint status(GLenum) const;
std::string log() const;
friend Base;
protected:
using Base::_id;
GLint _success;
virtual GLuint declare() override {
if (!_id) {
_id = glCreateProgram();
}
return _id;
}
virtual void remove() override {
if (_id) {
glDeleteProgram(_id);
}
}
};
}
#endif
@@ -0,0 +1,153 @@
#pragma once
#ifndef _NB_TEXTURES
#define _NB_TEXTURES
#include <NBGraphics/Buffers.hpp>
#include <NBGraphics/Image.hpp>
#include <NBGraphics/OGLObjects.hpp>
namespace nb {
class TextureBuffer : public virtual Buffer {
public:
TextureBuffer() : Buffer(GL_TEXTURE_BUFFER) {}
};
class RenderTarget : public OpenGLObject {
protected:
using Base = OpenGLObject;
using Base::_id;
RenderTarget(GLenum target) : Target(target) {}
RenderTarget(RenderTarget&& rhs)
: Target(rhs.Target), Base(std::move(rhs)) {}
public:
using Base::Base;
using Base::id;
const GLenum Target;
};
class Texture : public RenderTarget {
protected:
using Base = RenderTarget;
using Base::_id;
Texture(GLenum);
virtual GLuint declare() override {
if (!_id) {
glGenTextures(1, &_id);
}
bind();
return _id;
}
virtual void remove() override {
if (_id) {
bind();
glDeleteTextures(0, &_id);
}
}
public:
using Base::Base;
using Base::id;
using Base::Target;
virtual void bind() const override {
if (_id) {
glBindTexture(Target, _id);
} else {
THROW(OGLError(
OGLError::Codes::INVALID_OBJECT,
"w/ Texture"
));
}
}
virtual void unbind() const override {
glBindTexture(Target, 0);
}
template <typename T>
void parameter(GLenum param_, const T& val_);
void parameter(GLenum param_, const int& val_) {
declare();
glTexParameteri(_id, param_, val_);
}
void parameter(GLenum param_, const std::vector<int>& val_) {
declare();
glTexParameteriv(_id, param_, val_.data());
}
void parameter(GLenum param_, const std::vector<float>& val_) {
declare();
glTexParameterfv(_id, param_, val_.data());
}
void parameter(GLenum param_, const std::vector<unsigned int>& val_) {
declare();
glTexParameterIuiv(_id, param_, val_.data());
}
void generateMipmaps() const {
bind();
glGenerateMipmap(Target);
}
};
template<typename T>
class ImageTexture;
template<typename T>
struct OGLPixelFormat;
template<typename T, typename... Channels>
struct OGLPixelFormat<Pixel<T, Channels...>>;
template<>
struct OGLPixelFormat<Pixel<uint8_t, Red, Green, Blue>> {
static constexpr GLenum glFormat = GL_RGB;
static constexpr GLenum glData = GL_UNSIGNED_BYTE;
};
template<>
struct OGLPixelFormat<Pixel<uint8_t, Red, Green, Blue, Alpha>> {
static constexpr GLenum glFormat = GL_RGBA;
static constexpr GLenum glData = GL_UNSIGNED_BYTE;
};
template<typename T, typename... Channels>
class ImageTexture<Pixel<T, Channels...>> : public Texture {
protected:
using Texture::Texture;
using PixelType = Pixel<T, Channels...>;
public:
using Texture::generateMipmaps;
ImageTexture() : Texture(GL_TEXTURE_2D) {}
ImageTexture(ImageTexture&& cpy) {
*this = std::move(cpy);
}
ImageTexture& operator=(ImageTexture&& rhs) {
return Texture::operator=(std::move(rhs));
}
void setImage(const Image<PixelType>& img, unsigned int layer) {
using Format = OGLPixelFormat<PixelType>;
declare();
glTexImage2D(
Target,
layer,
Format::glFormat,
img.width,
img.height,
0,
Format::glFormat,
Format::glData,
img.data()
);
}
void setImage(const Image<PixelType>& img, bool generateMipmap=true) {
setImage(img, (unsigned int)0);
if (generateMipmap) { generateMipmaps(); }
}
};
} // namespace nb
#endif // _NB_TEXTURES
@@ -0,0 +1,238 @@
#pragma once
#ifndef _NB_VERTEX_ARRAY
#define _NB_VERTEX_ARRAY
#include <NBGraphics/GLLoad.hpp>
#include <NBCore/Errors.hpp>
#include <NBGraphics/Buffers.hpp>
#include <NBGraphics/OGLObjects.hpp>
namespace nb {
static uint8_t GLSLTypeSize(GLenum type) {
switch(type) {
case GL_SHORT:
case GL_UNSIGNED_SHORT:
case GL_HALF_FLOAT:
return 2;
break;
case GL_INT:
case GL_UNSIGNED_INT:
case GL_FLOAT:
return 4;
break;
case GL_DOUBLE:
return 8;
break;
default:
return 1;
break;
}
}
struct VertexAttributeLayout {
int32_t offset = 0;
GLsizei stride = 0;
GLuint divisor = 0;
};
struct VertexAttribute {
GLint GLSLSize;
GLenum GLSLType;
GLboolean GLSLNormalization;
VertexAttributeLayout layout;
};
struct VertexAttributePointer {
VertexAttribute attribute;
GLuint buffer;
GLuint index;
};
static bool isIndexInVertexAttribute(int i, const VertexAttribute& va) {
if ((i -= va.layout.offset) < 0) { return false; }
return (i%va.layout.stride) < GLSLTypeSize(va.GLSLType) * va.GLSLSize;
}
typedef std::vector<VertexAttribute> VertexAttributeList;
typedef std::vector<VertexAttributePointer> VertexAttributePointerList;
class VAOError : public Error<VAOError> {
using Base = Error<VAOError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED, INVALID_ATTRIBUTE
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
class VAO : public OpenGLObject {
using Base = OpenGLObject;
using Codes = VAOError::Codes;
public:
using Base::Base;
VAO() = default;
VAO(const VertexAttributePointerList&);
VAO(VAO&&);
VAO& operator=(VAO&&);
virtual void bind() const override {
if(_id) {
glBindVertexArray(_id);
} else {
THROW(OGLError(
OGLError::Codes::INVALID_OBJECT,
"w/ VertexArrayObject"
));
}
}
virtual void unbind() const override { glBindVertexArray(0); }
VertexAttributePointerList attributes() const;
VertexAttributePointerList attributes(const VertexAttributePointerList&);
VertexAttributePointer attr(GLuint) const;
void enable() const;
void enable(GLuint) const;
void disable() const;
void disable(GLuint) const;
protected:
using Base::_id;
VertexAttributePointerList _attrs;
virtual void remove() override {
if(_id) {
disable();
bind();
glDeleteVertexArrays(1, &_id);
_id = 0;
}
}
virtual GLuint declare() override {
if (!_id) {
glGenVertexArrays(1, &_id);
}
bind();
return _id;
}
};
class VertexError : public Error<VertexError> {
protected:
using Base = Error<VertexError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED, MISALIGNED_DATA
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
using VBO = ArrayBuffer;
using VertBufVec = SharedVector<VBO>;
using EBO = ElementBuffer;
struct VertexData {
std::shared_ptr<VBO> vbo;
VertexAttributeList attrs;
};
using VertexDataVec = std::vector<VertexData>;
class VertexGroup {
protected:
using Codes = VertexError::Codes;
VertexDataVec _vertex_data;
std::shared_ptr<EBO> _ebo;
std::shared_ptr<VAO> _vao;
GLenum _primitive;
public:
GLenum primitive=GL_TRIANGLES;
VertexGroup(VertexGroup&&);
VertexGroup& operator=(VertexGroup&&);
VertexGroup(
const VertexDataVec& vertex_data,
const std::shared_ptr<EBO>& ebo
)
: _vao(std::make_shared<VAO>()), _ebo(ebo) {
setBuffers(vertex_data);
}
template<typename... T>
VertexGroup(
const VertexDataVec& vertex_data,
T... args
)
: VertexGroup(vertex_data, std::make_shared<EBO>(args...)) {
setBuffers(vertex_data);
}
template<typename... T>
VertexGroup(
const VertexData& vertex_data,
T... args
)
: VertexGroup(VertexDataVec{vertex_data}, args...) {}
template<typename... T>
VertexGroup(
const ByteVector& vertex_data,
const VertexAttributeList& vertex_specification,
T... args
) : VertexGroup({
std::make_shared<VBO>(vertex_data),
vertex_specification},
args...
) {}
void bind() const {
_vao->bind();
_ebo->bind();
}
void unbind() const {
_vao->unbind();
_ebo->unbind();
}
void draw() const;
VertexDataVec getBuffers();
VertexData getBuffers(size_t);
size_t setBuffers(const VertexDataVec&);
size_t addBuffer(const VertexData&);
size_t addBuffer(const ByteVector& data, const VertexAttributeList& attrs);
VertexDataVec dropBuffers();
VertexData dropBuffer(size_t);
VertexAttributePointer attribute(size_t) const;
void enableAttr(GLuint);
void disableAttr(GLuint);
void enableBuffer(size_t);
void disableBuffer(size_t);
void enable();
void disable();
std::shared_ptr<EBO> indices() const;
std::shared_ptr<EBO> indices(const std::vector<unsigned int>& data);
std::shared_ptr<EBO> indices(std::shared_ptr<EBO> buffer);
};
} // namespace nb
#endif // _NB_VERTEX_ARRAY
@@ -0,0 +1,87 @@
#pragma once
#ifndef _NB_WINDOW
#define _NB_WINDOW
#include <NBGraphics/GLLoad.hpp>
#include <array>
#include <map>
#include <string>
#include <NBCore/Errors.hpp>
#include <NBCore/Utils.hpp>
namespace nb {
class OpenGLError : public Error<OpenGLError> {
using Base = Error<OpenGLError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED, INIT_FAILED, GLFW_INTIALIZED, GLAD_FAILED
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
class WindowError : public Error<WindowError> {
using Base = Error<WindowError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED, INITIALIZED_WINDOW, NO_GLFW, INIT_FAILED
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
class Window {
public:
static bool StrictInitialization;
static int getGLFWHint(int);
static int setGLFWHint(int, int);
static Window* getCurrent();
Window(const std::array<uint16_t, 2>, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
Window(const uint16_t, const uint16_t, const char*, GLFWmonitor* initMonitor=NULL, GLFWwindow* initWindow=NULL);
~Window();
int getWindowHint(int) const;
GLFWwindow* getWindow() const;
std::array<uint16_t, 2> getSize() const;
float getAspectRatio() const;
std::string getName() const;
int init();
int setWindowHint(int, int);
void setCurrent();
void resize(const std::array<uint16_t, 2>);
void resize(const uint16_t x, const uint16_t y);
protected:
static std::map<GLFWwindow*, Window*> WindowContexts;
static std::map<int, int> GLFWHints;
static Window* _current;
static bool _glfw_init;
static bool checkKillGLFW();
std::map<int, int> windowHints;
std::array<uint16_t, 2> windowSize;
float _aspect_ratio;
std::string windowName;
GLFWwindow* window;
GLFWmonitor* monitor;
GLFWwindow* shareWindow;
bool _init = false;
int gladResponse = -1;
};
};
#endif