Uhhhhh big big changes
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
|
||||
namespace nb {
|
||||
|
||||
extern const std::unordered_map<GLenum, std::string> BufferTypes;
|
||||
extern const ConstantMap <GLenum, std::string> BufferTypes;
|
||||
|
||||
template<GLenum N>
|
||||
struct GLSLEnum;
|
||||
@@ -72,7 +72,7 @@ class Buffer : public OpenGLObject {
|
||||
Buffer(GLenum target) : Target(target) {}
|
||||
virtual GLuint declare() override {
|
||||
if (!_id) {
|
||||
glGenBuffers(1, &_id);
|
||||
OPENGL_CALL(glGenBuffers(1, &_id));
|
||||
}
|
||||
bind();
|
||||
return _id;
|
||||
@@ -80,7 +80,7 @@ class Buffer : public OpenGLObject {
|
||||
virtual void remove() override {
|
||||
if (_id) {
|
||||
unbind();
|
||||
glDeleteBuffers(1, &_id);
|
||||
OPENGL_CALL(glDeleteBuffers(1, &_id));
|
||||
_id = 0;
|
||||
}
|
||||
}
|
||||
@@ -93,15 +93,17 @@ class Buffer : public OpenGLObject {
|
||||
|
||||
virtual void bind() const override {
|
||||
if (_id) {
|
||||
glBindBuffer(Target, _id);
|
||||
OPENGL_CALL(glBindBuffer(Target, _id));
|
||||
} else {
|
||||
THROW(OGLError(
|
||||
OGLError::Codes::INVALID_OBJECT,
|
||||
"w/ BufferType " + BufferTypes.at(Target)
|
||||
THROW(OpenGLObjectError(
|
||||
OpenGLObjectError::Codes::INVALID_OBJECT,
|
||||
"w/ BufferType " + BufferTypes[Target]
|
||||
));
|
||||
}
|
||||
}
|
||||
virtual void unbind() const override { glBindBuffer(Target, 0); }
|
||||
virtual void unbind() const override {
|
||||
OPENGL_CALL(glBindBuffer(Target, 0));
|
||||
}
|
||||
GLenum usage() const;
|
||||
size_t size() const;
|
||||
ByteVector data() const;
|
||||
|
||||
@@ -9,13 +9,12 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <NBGraphics/Buffers.h>
|
||||
#include <NBGraphics/Shader.h>
|
||||
#include <NBGraphics/VAOManager.hpp>
|
||||
#include <NBGraphics/Buffers.hpp>
|
||||
#include <NBGraphics/VertexArray.hpp>
|
||||
|
||||
#define THROW_DRAW_ERROR(msg) throw DrawError(msg, __FILE__, __LINE__);
|
||||
|
||||
namespace NB{
|
||||
namespace nb{
|
||||
|
||||
class DrawError : public std::runtime_error {
|
||||
public:
|
||||
|
||||
@@ -29,19 +29,25 @@ class RenderBuffer : public RenderTarget {
|
||||
protected:
|
||||
GLuint declare() override {
|
||||
if (!_id) {
|
||||
glRenderbufferStorage(
|
||||
OPENGL_CALL(
|
||||
glGenRenderbuffers(1, &_id)
|
||||
);
|
||||
bind();
|
||||
OPENGL_CALL(glRenderbufferStorage(
|
||||
GL_RENDERBUFFER,
|
||||
format,
|
||||
width,
|
||||
height
|
||||
);
|
||||
));
|
||||
return _id;
|
||||
}
|
||||
bind();
|
||||
return _id;
|
||||
}
|
||||
void remove() override {
|
||||
if (_id) {
|
||||
unbind();
|
||||
glDeleteRenderbuffers(1, &_id);
|
||||
OPENGL_CALL(glDeleteRenderbuffers(1, &_id));
|
||||
_id = 0;
|
||||
}
|
||||
}
|
||||
@@ -50,11 +56,14 @@ class RenderBuffer : public RenderTarget {
|
||||
const unsigned int width;
|
||||
const unsigned int height;
|
||||
const GLenum format;
|
||||
template<typename PixelType>
|
||||
RenderBuffer(unsigned int width_, unsigned int height_) :
|
||||
RenderBuffer(
|
||||
GLenum format_,
|
||||
unsigned int width_,
|
||||
unsigned int height_
|
||||
) :
|
||||
format(format_),
|
||||
width(width_),
|
||||
height(height_),
|
||||
format(OGLPixelFormat<PixelType>::glFormat),
|
||||
RenderTarget(GL_RENDERBUFFER)
|
||||
{ declare(); }
|
||||
RenderBuffer(RenderBuffer&& rhs) :
|
||||
@@ -66,16 +75,16 @@ class RenderBuffer : public RenderTarget {
|
||||
RenderBuffer& operator=(RenderBuffer&&) = delete;
|
||||
void bind() const override {
|
||||
if (_id) {
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, _id);
|
||||
OPENGL_CALL(glBindRenderbuffer(GL_RENDERBUFFER, _id));
|
||||
} else {
|
||||
THROW(OGLError(
|
||||
OGLError::Codes::INVALID_OBJECT,
|
||||
THROW(OpenGLObjectError(
|
||||
OpenGLObjectError::Codes::INVALID_OBJECT,
|
||||
"w/ RenderBuffer"
|
||||
));
|
||||
}
|
||||
}
|
||||
void unbind() const override {
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
||||
OPENGL_CALL(glBindRenderbuffer(GL_RENDERBUFFER, 0));
|
||||
}
|
||||
|
||||
};
|
||||
@@ -85,8 +94,10 @@ class FrameBufferBase : public OpenGLObject {
|
||||
static bool texTypeIs2D(GLenum texType) {
|
||||
switch(texType) {
|
||||
case GL_TEXTURE_2D:
|
||||
default:
|
||||
return true;
|
||||
case GL_TEXTURE_1D:
|
||||
default:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -98,6 +109,19 @@ class FrameBufferBase : public OpenGLObject {
|
||||
GLuint declare() override;
|
||||
void remove() override;
|
||||
std::shared_ptr<RenderTarget> detach(GLenum);
|
||||
void attach(
|
||||
GLenum attachment,
|
||||
std::shared_ptr<RenderBuffer> renderbuffer
|
||||
);
|
||||
|
||||
public:
|
||||
const GLenum Target;
|
||||
void bind() const override;
|
||||
void unbind() const override;
|
||||
GLenum status() const;
|
||||
bool complete() const;
|
||||
AttachmentMap getAllBuffers() const;
|
||||
std::shared_ptr<RenderTarget> getBuffer(GLenum) const;
|
||||
void attach(
|
||||
GLenum attachment,
|
||||
std::shared_ptr<Texture> texture,
|
||||
@@ -109,32 +133,6 @@ class FrameBufferBase : public OpenGLObject {
|
||||
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;
|
||||
@@ -178,12 +176,6 @@ class WriteFrameBuffer : public virtual FrameBufferBase {
|
||||
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);
|
||||
|
||||
@@ -5,4 +5,8 @@
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
namespace nb {
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#endif
|
||||
@@ -134,40 +134,95 @@ class Image;
|
||||
|
||||
template<typename T, typename... Channels>
|
||||
class Image<Pixel<T, Channels...>> {
|
||||
private:
|
||||
Error<> sizeCompare(const Image& rhs) {
|
||||
const std::string errmsg = "Cannot assign images of different size";
|
||||
if (width != rhs.width) {
|
||||
return Error<>(
|
||||
Error<>::VALUE_ERROR,
|
||||
errmsg+"[ Width: "+std::to_string(width)+\
|
||||
" != "+std::to_string(rhs.width)+" ]"
|
||||
);
|
||||
}
|
||||
if (height != rhs.height) {
|
||||
return Error<>(
|
||||
Error<>::VALUE_ERROR,
|
||||
errmsg+"[ Width: "+std::to_string(width)+\
|
||||
" != "+std::to_string(rhs.width)+" ]"
|
||||
);
|
||||
}
|
||||
return Error<>("NoError");
|
||||
}
|
||||
|
||||
protected:
|
||||
using Codes = ImageError::Codes;
|
||||
bool _data_managed;
|
||||
T* _data;
|
||||
Image(unsigned int x, unsigned int y)
|
||||
: width(x), height(y), _data(nullptr) {}
|
||||
void clear() {
|
||||
if (_data_managed) {
|
||||
delete[] _data;
|
||||
_data_managed = false;
|
||||
}
|
||||
_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));
|
||||
static Image&& CreateImageStorage(
|
||||
unsigned int width,
|
||||
unsigned int height,
|
||||
T* data=nullptr
|
||||
) {
|
||||
unsigned int data_size = NumberChannels*width*height*sizeof(T);
|
||||
T* data_ptr = new T[data_size];
|
||||
auto& ret = Image(width, height, data_ptr);
|
||||
if (data) {
|
||||
std::memcpy(data_ptr, data, data_size);
|
||||
}
|
||||
ret._data_managed = true;
|
||||
return std::move(ret);
|
||||
}
|
||||
Image(unsigned int x, unsigned int y, T* data=nullptr)
|
||||
: width(x), height(y), _data(data), _data_managed(false) {}
|
||||
Image(const Image& cpy)
|
||||
: Image(cpy.width, cpy.height) {
|
||||
*this = cpy;
|
||||
}
|
||||
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(mv.width, mv.height) {
|
||||
*this = std::move(mv);
|
||||
}
|
||||
Image& operator=(const Image& cpy) {
|
||||
if (_data_managed) {
|
||||
THROW(Error<>(Error<>::OVERWRITE_ERROR));
|
||||
}
|
||||
const Error<> sizeErr = sizeCompare(cpy);
|
||||
if (sizeErr.code!=1) { THROW(sizeErr); }
|
||||
_data = cpy._data;
|
||||
return this;
|
||||
}
|
||||
Image& operator=(Image&& mv) {
|
||||
if (_data_managed) {
|
||||
THROW(Error<>(Error<>::OVERWRITE_ERROR));
|
||||
}
|
||||
const Error<> sizeErr = sizeCompare(mv);
|
||||
if (sizeErr.code!=1) { THROW(sizeErr); }
|
||||
_data = mv._data;
|
||||
_data_managed = mv._data_managed;
|
||||
mv._data_managed = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
~Image() {
|
||||
if (_data) {
|
||||
delete[] _data;
|
||||
}
|
||||
virtual ~Image() {
|
||||
clear();
|
||||
}
|
||||
|
||||
const T* data() const { return _data; }
|
||||
Image&& copy() const {
|
||||
return Image(width, height, _data);
|
||||
Image<PixelType>&& copy() const {
|
||||
return CreateImageStorage(width, height, _data);
|
||||
}
|
||||
PixelReference<PixelType> at(unsigned int x, unsigned int y) {
|
||||
if (!(x<width) || !(y<height)) {
|
||||
@@ -188,29 +243,6 @@ class Image<Pixel<T, Channels...>> {
|
||||
|
||||
};
|
||||
|
||||
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
|
||||
@@ -2,14 +2,40 @@
|
||||
#ifndef _NB_OGL_OBJECTS
|
||||
#define _NB_OGL_OBJECTS
|
||||
|
||||
#include <NBGraphics/GLLoad.hpp>
|
||||
|
||||
#include <NBCore/Errors.hpp>
|
||||
#include <NBGraphics/GLLoad.hpp>
|
||||
#include <NBCore/Utils.hpp>
|
||||
|
||||
namespace nb {
|
||||
|
||||
class OGLError : public Error<OGLError> {
|
||||
using Base = Error<OGLError>;
|
||||
typedef ConstantMap<GLenum, std::string> GLEnumTableType;
|
||||
|
||||
extern const GLEnumTableType GLenumTable;
|
||||
|
||||
class OpenGLError : public Error<OpenGLError> {
|
||||
using Base = Error<OpenGLError>;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
enum Codes : unsigned int {
|
||||
NO_ERROR = GL_NO_ERROR,
|
||||
INVALID_ENUM = GL_INVALID_ENUM,
|
||||
INVALID_VALUE = GL_INVALID_VALUE,
|
||||
INVALID_OPERATION = GL_INVALID_OPERATION,
|
||||
INVALID_FRAMEBUFFER_OPERATION = GL_INVALID_FRAMEBUFFER_OPERATION,
|
||||
OUT_OF_MEMORY = GL_OUT_OF_MEMORY,
|
||||
STACK_UNDERFLOW = GL_STACK_UNDERFLOW,
|
||||
STACK_OVERFLOW = GL_STACK_OVERFLOW
|
||||
};
|
||||
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
static OpenGLError status();
|
||||
};
|
||||
|
||||
class OpenGLObjectError : public Error<OpenGLObjectError> {
|
||||
using Base = Error<OpenGLObjectError>;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
@@ -35,7 +61,7 @@ class OpenGLObject {
|
||||
GLuint id() const { return _id; }
|
||||
|
||||
protected:
|
||||
using Codes = OGLError::Codes;
|
||||
using Codes = OpenGLObjectError::Codes;
|
||||
OpenGLObject() = default;
|
||||
|
||||
virtual GLuint declare() = 0;
|
||||
@@ -45,4 +71,43 @@ class OpenGLObject {
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
#ifndef OPENGL_CALL
|
||||
#ifdef _NB_MONITOR_OPENGL_CALLS
|
||||
namespace NB_OPENGL_CALL_MONITORING {
|
||||
template<typename Fn>
|
||||
inline auto run_expression(
|
||||
const std::string& file,
|
||||
unsigned int line,
|
||||
Fn fn
|
||||
) {
|
||||
auto check_ = [file, line]() {
|
||||
const auto no_err = nb::OpenGLError::NO_ERROR;
|
||||
auto stat = nb::OpenGLError::status();
|
||||
if (stat.code != no_err) {
|
||||
THROW_W_CODE_LOC(file, line, stat);
|
||||
}
|
||||
};
|
||||
if constexpr(std::is_same_v<std::invoke_result_t<Fn>, void>) {
|
||||
check_();
|
||||
fn();
|
||||
check_();
|
||||
} else {
|
||||
check_();
|
||||
auto ret = fn();
|
||||
check_();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
};
|
||||
#define OPENGL_CALL(expression) NB_OPENGL_CALL_MONITORING::run_expression(\
|
||||
__FILE__,\
|
||||
__LINE__,\
|
||||
[&]() {return expression;}\
|
||||
)
|
||||
#else
|
||||
#define OPENGL_CALL(x) x
|
||||
#endif // _NB_MONITOR_OPENGL_CALLS
|
||||
|
||||
#endif // OPENGL_CALL
|
||||
|
||||
#endif // _NB_OGL_OBJECTS
|
||||
@@ -78,8 +78,8 @@ class Shader : public OpenGLObject {
|
||||
for (int i=0; i < num_srcs; ++i) {
|
||||
src_ptrs[i] = _sources[i].data();
|
||||
}
|
||||
glShaderSource(_id, num_srcs, src_ptrs.data(), NULL);
|
||||
glCompileShader(_id);
|
||||
OPENGL_CALL(glShaderSource(_id, num_srcs, src_ptrs.data(), NULL));
|
||||
OPENGL_CALL(glCompileShader(_id));
|
||||
_success = status(GL_COMPILE_STATUS);
|
||||
if (!_success) {
|
||||
WARN(log(), 0x0FE);
|
||||
@@ -88,14 +88,14 @@ class Shader : public OpenGLObject {
|
||||
|
||||
virtual GLuint declare() override {
|
||||
if (!_id) {
|
||||
_id = _id = glCreateShader(target);
|
||||
_id = _id = OPENGL_CALL(glCreateShader(target));
|
||||
}
|
||||
return _id;
|
||||
}
|
||||
|
||||
virtual void remove() override {
|
||||
if (_id) {
|
||||
glDeleteShader(_id);
|
||||
OPENGL_CALL(glDeleteShader(_id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ class Program : public OpenGLObject {
|
||||
operator bool();
|
||||
~Program() { remove(); }
|
||||
virtual void bind() const override {
|
||||
glUseProgram(_id);
|
||||
OPENGL_CALL(glUseProgram(_id));
|
||||
}
|
||||
virtual void unbind() const override { /* TODO: Some warning of some kind perhaps*/ }
|
||||
GLint status(GLenum) const;
|
||||
@@ -125,13 +125,13 @@ class Program : public OpenGLObject {
|
||||
GLint _success;
|
||||
virtual GLuint declare() override {
|
||||
if (!_id) {
|
||||
_id = glCreateProgram();
|
||||
_id = OPENGL_CALL(glCreateProgram());
|
||||
}
|
||||
return _id;
|
||||
}
|
||||
virtual void remove() override {
|
||||
if (_id) {
|
||||
glDeleteProgram(_id);
|
||||
OPENGL_CALL(glDeleteProgram(_id));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include <NBGraphics/Image.hpp>
|
||||
#include <NBGraphics/OGLObjects.hpp>
|
||||
|
||||
/*! @file Textures.hpp */
|
||||
|
||||
namespace nb {
|
||||
|
||||
class TextureBuffer : public virtual Buffer {
|
||||
@@ -14,6 +16,9 @@ class TextureBuffer : public virtual Buffer {
|
||||
|
||||
};
|
||||
|
||||
/*!
|
||||
@brief An OpenGL object that may be rendered to within a Framebuffer.
|
||||
*/
|
||||
class RenderTarget : public OpenGLObject {
|
||||
protected:
|
||||
using Base = OpenGLObject;
|
||||
@@ -23,11 +28,13 @@ class RenderTarget : public OpenGLObject {
|
||||
: Target(rhs.Target), Base(std::move(rhs)) {}
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
using Base::id;
|
||||
const GLenum Target;
|
||||
};
|
||||
|
||||
/*!
|
||||
@brief An OpenGL Target object
|
||||
*/
|
||||
class Texture : public RenderTarget {
|
||||
protected:
|
||||
using Base = RenderTarget;
|
||||
@@ -35,7 +42,7 @@ class Texture : public RenderTarget {
|
||||
Texture(GLenum);
|
||||
virtual GLuint declare() override {
|
||||
if (!_id) {
|
||||
glGenTextures(1, &_id);
|
||||
OPENGL_CALL(glGenTextures(1, &_id));
|
||||
}
|
||||
bind();
|
||||
return _id;
|
||||
@@ -43,57 +50,69 @@ class Texture : public RenderTarget {
|
||||
virtual void remove() override {
|
||||
if (_id) {
|
||||
bind();
|
||||
glDeleteTextures(0, &_id);
|
||||
OPENGL_CALL(glDeleteTextures(0, &_id));
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
using Base::id;
|
||||
using Base::Target;
|
||||
|
||||
virtual void bind() const override {
|
||||
if (_id) {
|
||||
glBindTexture(Target, _id);
|
||||
OPENGL_CALL(glBindTexture(Target, _id));
|
||||
} else {
|
||||
THROW(OGLError(
|
||||
OGLError::Codes::INVALID_OBJECT,
|
||||
THROW(OpenGLObjectError(
|
||||
OpenGLObjectError::Codes::INVALID_OBJECT,
|
||||
"w/ Texture"
|
||||
));
|
||||
}
|
||||
}
|
||||
virtual void unbind() const override {
|
||||
glBindTexture(Target, 0);
|
||||
OPENGL_CALL(glBindTexture(Target, 0));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void parameter(GLenum param_, const T& val_);
|
||||
void parameter(GLenum param_, const float& val_) {
|
||||
declare();
|
||||
OPENGL_CALL(
|
||||
glTexParameterf(Target, param_, 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());
|
||||
OPENGL_CALL(
|
||||
glTexParameteri(Target, param_, val_)
|
||||
);
|
||||
}
|
||||
void parameter(GLenum param_, const std::vector<float>& val_) {
|
||||
declare();
|
||||
glTexParameterfv(_id, param_, val_.data());
|
||||
OPENGL_CALL(
|
||||
glTexParameterfv(Target, param_, val_.data())
|
||||
);
|
||||
}
|
||||
void parameter(GLenum param_, const std::vector<int>& val_) {
|
||||
declare();
|
||||
OPENGL_CALL(
|
||||
glTexParameteriv(Target, param_, val_.data())
|
||||
);
|
||||
}
|
||||
void parameter(GLenum param_, const std::vector<unsigned int>& val_) {
|
||||
declare();
|
||||
glTexParameterIuiv(_id, param_, val_.data());
|
||||
OPENGL_CALL(
|
||||
glTexParameterIuiv(Target, param_, val_.data())
|
||||
);
|
||||
}
|
||||
void generateMipmaps() const {
|
||||
bind();
|
||||
glGenerateMipmap(Target);
|
||||
OPENGL_CALL(
|
||||
glGenerateMipmap(Target)
|
||||
);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class ImageTexture;
|
||||
|
||||
template<typename T>
|
||||
struct OGLPixelFormat;
|
||||
|
||||
@@ -102,52 +121,110 @@ struct OGLPixelFormat<Pixel<T, Channels...>>;
|
||||
|
||||
template<>
|
||||
struct OGLPixelFormat<Pixel<uint8_t, Red, Green, Blue>> {
|
||||
static constexpr GLenum glFormat = GL_RGB;
|
||||
static constexpr GLenum glBase = GL_RGB;
|
||||
static constexpr GLenum glFormat = GL_RGB8UI;
|
||||
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 glBase = GL_RGBA;
|
||||
static constexpr GLenum glFormat = GL_RGBA8UI;
|
||||
static constexpr GLenum glData = GL_UNSIGNED_BYTE;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct OGLPixelFormat<Pixel<float, Red, Green>> {
|
||||
static constexpr GLenum glBase = GL_RG;
|
||||
static constexpr GLenum glFormat = GL_RG32F;
|
||||
static constexpr GLenum glData = GL_FLOAT;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class Texture2D;
|
||||
|
||||
template<typename T, typename... Channels>
|
||||
class ImageTexture<Pixel<T, Channels...>> : public Texture {
|
||||
class Texture2D<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) {
|
||||
Texture2D() : Texture(GL_TEXTURE_2D) {}
|
||||
Texture2D(Texture2D&& cpy) {
|
||||
*this = std::move(cpy);
|
||||
}
|
||||
ImageTexture& operator=(ImageTexture&& rhs) {
|
||||
Texture2D& operator=(Texture2D&& rhs) {
|
||||
return Texture::operator=(std::move(rhs));
|
||||
}
|
||||
void setImage(const Image<PixelType>& img, unsigned int layer) {
|
||||
void setLayer(const Image<PixelType>& img, unsigned int layer) {
|
||||
using Format = OGLPixelFormat<PixelType>;
|
||||
declare();
|
||||
glTexImage2D(
|
||||
auto data = img.data();
|
||||
OPENGL_CALL(glTexImage2D(
|
||||
Target,
|
||||
layer,
|
||||
Format::glFormat,
|
||||
img.width,
|
||||
img.height,
|
||||
0,
|
||||
Format::glFormat,
|
||||
Format::glBase,
|
||||
Format::glData,
|
||||
img.data()
|
||||
);
|
||||
data ? data : nullptr
|
||||
));
|
||||
}
|
||||
void setImage(const Image<PixelType>& img, bool generateMipmap=true) {
|
||||
setImage(img, (unsigned int)0);
|
||||
void setTexture(const Image<PixelType>& img, bool generateMipmap=true) {
|
||||
setLayer(img, 0);
|
||||
if (generateMipmap) { generateMipmaps(); }
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class Texture1D;
|
||||
|
||||
template<typename T, typename... Channels>
|
||||
class Texture1D<Pixel<T, Channels...>> : public Texture {
|
||||
protected:
|
||||
using Texture::Texture;
|
||||
using PixelType = Pixel<T, Channels...>;
|
||||
|
||||
public:
|
||||
using Texture::generateMipmaps;
|
||||
Texture1D() : Texture(GL_TEXTURE_1D) {}
|
||||
Texture1D(Texture1D&& cpy) {
|
||||
*this = std::move(cpy);
|
||||
}
|
||||
Texture1D& operator=(Texture1D&& rhs) {
|
||||
return Texture::operator=(std::move(rhs));
|
||||
}
|
||||
void setLayer(const Image<PixelType>& tex, unsigned int layer) {
|
||||
if (tex.height != 1) {
|
||||
THROW(Error<>(
|
||||
Error<>::VALUE_ERROR,
|
||||
"Image must be of height 1"
|
||||
));
|
||||
}
|
||||
using Format = OGLPixelFormat<PixelType>;
|
||||
declare();
|
||||
auto data = tex.data();
|
||||
OPENGL_CALL(glTexImage1D(
|
||||
Target,
|
||||
layer,
|
||||
Format::glFormat,
|
||||
tex.width,
|
||||
0,
|
||||
Format::glBase,
|
||||
Format::glData,
|
||||
data ? data : nullptr
|
||||
));
|
||||
}
|
||||
void setTexture(const Image<PixelType>& tex, bool generateMipmap=true) {
|
||||
setLayer(tex, 0);
|
||||
if (generateMipmap) { generateMipmaps(); }
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
#endif // _NB_TEXTURES
|
||||
@@ -88,15 +88,17 @@ class VAO : public OpenGLObject {
|
||||
|
||||
virtual void bind() const override {
|
||||
if(_id) {
|
||||
glBindVertexArray(_id);
|
||||
OPENGL_CALL(glBindVertexArray(_id));
|
||||
} else {
|
||||
THROW(OGLError(
|
||||
OGLError::Codes::INVALID_OBJECT,
|
||||
THROW(OpenGLObjectError(
|
||||
OpenGLObjectError::Codes::INVALID_OBJECT,
|
||||
"w/ VertexArrayObject"
|
||||
));
|
||||
}
|
||||
}
|
||||
virtual void unbind() const override { glBindVertexArray(0); }
|
||||
virtual void unbind() const override {
|
||||
OPENGL_CALL(glBindVertexArray(0));
|
||||
}
|
||||
|
||||
VertexAttributePointerList attributes() const;
|
||||
VertexAttributePointerList attributes(const VertexAttributePointerList&);
|
||||
@@ -114,13 +116,13 @@ class VAO : public OpenGLObject {
|
||||
if(_id) {
|
||||
disable();
|
||||
bind();
|
||||
glDeleteVertexArrays(1, &_id);
|
||||
OPENGL_CALL(glDeleteVertexArrays(1, &_id));
|
||||
_id = 0;
|
||||
}
|
||||
}
|
||||
virtual GLuint declare() override {
|
||||
if (!_id) {
|
||||
glGenVertexArrays(1, &_id);
|
||||
OPENGL_CALL(glGenVertexArrays(1, &_id));
|
||||
}
|
||||
bind();
|
||||
return _id;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#define _NB_WINDOW
|
||||
|
||||
#include <NBGraphics/GLLoad.hpp>
|
||||
#include <NBGraphics/OGLObjects.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <map>
|
||||
@@ -13,8 +14,8 @@
|
||||
|
||||
namespace nb {
|
||||
|
||||
class OpenGLError : public Error<OpenGLError> {
|
||||
using Base = Error<OpenGLError>;
|
||||
class GLFWError : public Error<GLFWError> {
|
||||
using Base = Error<GLFWError>;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
Reference in New Issue
Block a user