Compare commits
7 Commits
36945a8a9d
...
75bda91302
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75bda91302 | ||
|
|
2c4392a51e | ||
|
|
521483f4e0 | ||
|
|
cda9950d33 | ||
|
|
2a2547f216 | ||
|
|
8a05052516 | ||
|
|
81bffb7511 |
@ -73,10 +73,8 @@ endif()
|
||||
# External Dep paths
|
||||
set(GLFW_PATH ../glfw/)
|
||||
set(GLAD_PATH ../glad/)
|
||||
set(STBIMAGE_PATH ../stbi_image)
|
||||
get_filename_component(GLFW_PATH ${GLFW_PATH} ABSOLUTE)
|
||||
get_filename_component(GLAD_PATH ${GLAD_PATH} ABSOLUTE)
|
||||
get_filename_component(STBIMAGE_PATH ${STBIMAGE_PATH} ABSOLUTE)
|
||||
|
||||
add_subdirectory(./engine)
|
||||
|
||||
|
||||
@ -9,11 +9,15 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include "StringUtils.hpp"
|
||||
#include "TypeTraits.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
typedef std::unordered_map<unsigned int, std::string> ErrorCodeMap;
|
||||
|
||||
template <class ErrorType=NoneType>
|
||||
class Error;
|
||||
|
||||
class ErrorBase {
|
||||
protected:
|
||||
|
||||
@ -132,40 +136,40 @@ class Error : public ErrorBase {
|
||||
using ErrorBase::type;
|
||||
};
|
||||
|
||||
class NBError : public Error<NBError> {
|
||||
protected:
|
||||
using Base = Error<NBError>;
|
||||
|
||||
template<>
|
||||
class Error<NoneType> : public Error<Error<NoneType>> {
|
||||
using Base = Error<Error<NoneType>>;
|
||||
|
||||
public:
|
||||
enum Codes : unsigned int {
|
||||
STANDARD, UNDEFINED, INDEX_ERROR
|
||||
};
|
||||
using Base::Base;
|
||||
|
||||
NBError(unsigned int code_=1) noexcept : Base(code_) {}
|
||||
NBError(const std::exception& err) : Base(
|
||||
STANDARD,
|
||||
Error(unsigned int code_=1) noexcept : Base(code_) {}
|
||||
|
||||
Error(const std::exception& err) : Base(
|
||||
Codes::STANDARD,
|
||||
std::string(err.what()),
|
||||
"std::exception",
|
||||
nullptr
|
||||
) {}
|
||||
|
||||
// fix
|
||||
NBError(std::string msg_) noexcept : Base(
|
||||
Error(std::string msg_) noexcept : Base(
|
||||
Codes::UNDEFINED,
|
||||
msg_
|
||||
) {}
|
||||
|
||||
inline static const std::string type = "nb::Error";
|
||||
inline static const ErrorCodeMap ErrorMessages = {
|
||||
{STANDARD, "std::exception"},
|
||||
{UNDEFINED, "Error"},
|
||||
{INDEX_ERROR, "Indexing error"}
|
||||
enum Codes : unsigned int {
|
||||
STANDARD, UNDEFINED
|
||||
};
|
||||
|
||||
using Base::what;
|
||||
using Base::code;
|
||||
using Base::msg;
|
||||
using Base::trace;
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
// template<typename... Args>
|
||||
// Error(Args...) -> Error<NoneType>;
|
||||
|
||||
} // namespace nb
|
||||
|
||||
#endif // _NB_ERRORS_IMPL
|
||||
@ -138,15 +138,6 @@ public:
|
||||
static_cast<LoggerType*>(this)->write_message(err.what(), lvl, file, line);
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
std::enable_if_t<std::is_integral_v<U>, void> write_message(
|
||||
const U& val,
|
||||
uint8_t lvl=0x00,
|
||||
std::string file="",
|
||||
unsigned int line=0
|
||||
) {
|
||||
static_cast<LoggerType*>(this)->write_message(std::to_string(val), lvl, file, line);
|
||||
}
|
||||
};
|
||||
|
||||
class DefaultDebugLogger : public DebugLogger<DefaultDebugLogger> {
|
||||
|
||||
@ -33,9 +33,6 @@ namespace detail {
|
||||
|
||||
struct NoneType{};
|
||||
|
||||
template<typename T, unsigned int Arg>
|
||||
using Const_t = std::integral_constant<T, Arg>;
|
||||
|
||||
template <
|
||||
template <class...> class Op,
|
||||
class... Args
|
||||
@ -112,41 +109,6 @@ ForEach(std::tuple<Pack...>& tup, Func f, const Args& arg) {
|
||||
ForEach<N+1, Func, Args, Pack...>(tup, f, arg);
|
||||
}
|
||||
|
||||
template<typename T, typename... Pack>
|
||||
struct Find;
|
||||
|
||||
template<typename T, typename U, typename... Pack>
|
||||
struct Find<T, U, Pack...> {
|
||||
protected:
|
||||
using here = Find<T, U>;
|
||||
static constexpr unsigned int downstream = std::conditional_t<
|
||||
here::value,
|
||||
Const_t<unsigned int, 0>,
|
||||
Find<T, Pack...>
|
||||
>::value;
|
||||
|
||||
public:
|
||||
static constexpr unsigned int value = std::conditional_t<
|
||||
here::value,
|
||||
Const_t<unsigned int, 1>,
|
||||
std::conditional_t<
|
||||
downstream,
|
||||
Const_t<unsigned int, 1+downstream>,
|
||||
Const_t<unsigned int, 0>
|
||||
>
|
||||
>::value;
|
||||
};
|
||||
|
||||
template<typename T, typename U>
|
||||
struct Find<T, U> {
|
||||
public:
|
||||
static constexpr unsigned int value = std::conditional_t<
|
||||
std::is_base_of<T, U>::value,
|
||||
std::integral_constant<unsigned int, 1>,
|
||||
std::integral_constant<unsigned int, 0>
|
||||
>::value;
|
||||
};
|
||||
|
||||
template <typename T, typename Pack1, typename... Pack>
|
||||
struct RefPackToPtrVec {
|
||||
RefPackToPtrVec(std::vector<T*> _vec, Pack1& p1, Pack&... packs)
|
||||
|
||||
@ -54,9 +54,7 @@ ByteVector concatVectorBytes(const std::vector<T>& vec1, const std::vector<S>& v
|
||||
template<typename T>
|
||||
std::vector<T> bytesToVector(const ByteVector& vec) {
|
||||
if (vec.size() % sizeof(T) != 0) {
|
||||
THROW(Error<NoneType>(
|
||||
std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">.")
|
||||
));
|
||||
THROW(std::runtime_error("Data size does not align to std::vector<" + std::string(typeid(T).name()) + ">."));
|
||||
}
|
||||
unsigned int num_elmts = vec.size() / sizeof(T);
|
||||
std::vector<T> ret(num_elmts);
|
||||
|
||||
@ -2,10 +2,15 @@
|
||||
|
||||
namespace nb {
|
||||
|
||||
const std::string Error<>::type = "nb::Error";
|
||||
const ErrorCodeMap Error<>::ErrorMessages = {
|
||||
{Error::Codes::STANDARD, "std::exception"},
|
||||
{Error::Codes::UNDEFINED, "Error"}
|
||||
};
|
||||
|
||||
//ErrorBase::
|
||||
|
||||
ErrorBase::ErrorBase(const std::exception& exception_) noexcept
|
||||
: ErrorBase(NBError(exception_)) {}
|
||||
|
||||
} // namespace nb
|
||||
|
||||
: ErrorBase(Error<NoneType>(exception_)) {}
|
||||
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
#include "GLLoad.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <NBCore/Errors.hpp>
|
||||
@ -16,113 +17,145 @@ 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>;
|
||||
using Base::Base;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
enum Codes : unsigned int {
|
||||
UNDEFINED,
|
||||
DATA_OVERFLOW,
|
||||
DATA_ERROR
|
||||
INVALID_BUFFER,
|
||||
HANDLE_OVERWRITE,
|
||||
};
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
|
||||
template <typename BufferType>
|
||||
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);
|
||||
Buffer() = default;
|
||||
Buffer(Buffer&& other) {
|
||||
*this = std::move(other);
|
||||
}
|
||||
Buffer& operator=(Buffer&& rhs) {
|
||||
auto targ_name = BufferTypes.at(Target);
|
||||
if (Target != rhs.Target) {
|
||||
THROW(BufferError(
|
||||
Codes::INVALID_BUFFER,
|
||||
"w/ ("+targ_name+") := ("+BufferTypes.at(rhs.Target)+")"
|
||||
));
|
||||
}
|
||||
//nb::logger.log(std::to_string(_id) + ":=" + std::to_string(rhs._id));
|
||||
if (_id) {
|
||||
THROW(BufferError(
|
||||
Codes::HANDLE_OVERWRITE,
|
||||
"w/ BufferType = " + targ_name
|
||||
));
|
||||
}
|
||||
_id = rhs._id;
|
||||
_usage = rhs._usage;
|
||||
_size = rhs._size;
|
||||
|
||||
rhs._id = 0;
|
||||
rhs._usage = GL_STATIC_DRAW;
|
||||
rhs._size = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
virtual void bind() const override {
|
||||
if (_id) {
|
||||
glBindBuffer(Target, _id);
|
||||
} else {
|
||||
THROW(OGLError(
|
||||
OGLError::Codes::INVALID_OBJECT,
|
||||
WARN(BufferError(
|
||||
Codes::INVALID_BUFFER,
|
||||
"w/ BufferType " + BufferTypes.at(Target)
|
||||
));
|
||||
), 0xFE);
|
||||
}
|
||||
}
|
||||
virtual void unbind() const override { glBindBuffer(Target, 0); }
|
||||
GLenum usage() const;
|
||||
size_t size() const;
|
||||
ByteVector data() const;
|
||||
virtual GLenum usage() const { return _usage; }
|
||||
virtual GLenum usage(GLenum usage_) {
|
||||
_usage = usage_;
|
||||
data(data(), _usage);
|
||||
return _usage;
|
||||
}
|
||||
virtual size_t size() const { return _size; }
|
||||
virtual ByteVector data() const {
|
||||
bind();
|
||||
ByteVector ret(_size);
|
||||
glGetBufferSubData(Target, 0, _size, ret.data());
|
||||
return ret;
|
||||
}
|
||||
virtual void data(
|
||||
const void* data_,
|
||||
GLsizei size_,
|
||||
GLenum usage_=GL_STATIC_DRAW
|
||||
) {
|
||||
declare();
|
||||
bind();
|
||||
glBufferData(Target, size_, data_, usage_);
|
||||
_size = size_;
|
||||
_usage = usage_;
|
||||
}
|
||||
virtual void data(const ByteVector& data_, GLenum usage_=GL_STATIC_DRAW) {
|
||||
data(data_.data(), data_.size(), usage_);
|
||||
|
||||
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 subdata(void* data_, GLsizeiptr size_, GLintptr offset_=0) {
|
||||
bind();
|
||||
if (offset_+size_ <= _size) {
|
||||
THROW(BufferError(BufferError::Codes::DATA_OVERFLOW));
|
||||
}
|
||||
glBufferSubData(Target, offset_, size_, data_);
|
||||
}
|
||||
virtual void subdata(ByteVector& data_, GLintptr offset_=0) {
|
||||
bind();
|
||||
size_t size_ = data_.size();
|
||||
if (offset_+size_ <= _size) {
|
||||
THROW(BufferError(BufferError::Codes::DATA_OVERFLOW));
|
||||
}
|
||||
glBufferSubData(Target, offset_, data_.size(), data_.data());
|
||||
}
|
||||
// virtual void clear() const { glClearBufferData(Target, ) }
|
||||
static const GLenum Target;
|
||||
|
||||
protected:
|
||||
using OpenGLObject::_id;
|
||||
GLenum _usage = GL_STATIC_DRAW;
|
||||
size_t _size;
|
||||
virtual GLuint declare() override {
|
||||
if (!_id) {
|
||||
glGenBuffers(1, &_id);
|
||||
}
|
||||
return _id;
|
||||
}
|
||||
virtual void remove() override {
|
||||
if (_id) {
|
||||
glDeleteBuffers(1, &_id);
|
||||
_id = 0;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/* template <typename BufferType>
|
||||
class ImmutableBuffer : public virtual Buffer {
|
||||
template<typename BufferType>
|
||||
const GLenum Buffer<BufferType>::Target = BufferType::Target;
|
||||
|
||||
template <typename BufferType>
|
||||
class ImmutableBuffer : public virtual Buffer<BufferType> {
|
||||
public:
|
||||
using Base = Buffer<BufferType>;
|
||||
using Base::Target;
|
||||
using Base::bind;
|
||||
|
||||
ImmutableBuffer() = default;
|
||||
ImmutableBuffer(
|
||||
size_t size_,
|
||||
GLenum usage_ = GL_STATIC_DRAW,
|
||||
GLbitfield flags_=0x0
|
||||
) : size(size_), Base::usage(usage_) {
|
||||
ImmutableBuffer(size_t size_, GLenum usage_ = GL_STATIC_DRAW, GLbitfield flags_=0x0) : size(size_), Base::usage(usage_) {
|
||||
glBufferStorage(Target, size, nullptr, flags_);
|
||||
}
|
||||
|
||||
@ -144,72 +177,63 @@ class ImmutableBuffer : public virtual Buffer {
|
||||
const size_t size;
|
||||
|
||||
protected:
|
||||
using Base::_id;
|
||||
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;
|
||||
template<bool Immutable=false>
|
||||
class ArrayBuffer : public Buffer<ArrayBuffer<false>> {
|
||||
using Base = Buffer<ArrayBuffer<false>>;
|
||||
using BufferType = Base;
|
||||
|
||||
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_) {}
|
||||
using Base::Base;
|
||||
using Base::data;
|
||||
ArrayBuffer(const ByteVector&, GLenum usage_=GL_STATIC_DRAW);
|
||||
|
||||
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);
|
||||
}
|
||||
static const GLenum Target = GL_ARRAY_BUFFER;
|
||||
|
||||
protected:
|
||||
using Base::_usage;
|
||||
};
|
||||
|
||||
template <>
|
||||
class ArrayBuffer<true>
|
||||
: public virtual ArrayBuffer<false>, public virtual ImmutableBuffer<ArrayBuffer<true>> {
|
||||
using Base = ArrayBuffer<false>;
|
||||
using BufferType = ImmutableBuffer<ArrayBuffer<true>>;
|
||||
using BufferType::BufferType;
|
||||
|
||||
|
||||
public:
|
||||
using Base::Target;
|
||||
|
||||
};
|
||||
|
||||
template<bool Immutable=false>
|
||||
class ElementBuffer : public Buffer<ElementBuffer<false>> {
|
||||
using Base = Buffer<ElementBuffer<false>>;
|
||||
using BufferType = Base;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
static const GLenum Target = GL_ELEMENT_ARRAY_BUFFER;
|
||||
|
||||
protected:
|
||||
|
||||
};
|
||||
|
||||
template <>
|
||||
class ElementBuffer<true>
|
||||
: public virtual ElementBuffer<false>, public virtual ImmutableBuffer<ElementBuffer<true>> {
|
||||
using Base = ElementBuffer<false>;
|
||||
using BufferType = ImmutableBuffer<ElementBuffer<true>>;
|
||||
|
||||
public:
|
||||
using BufferType::BufferType;
|
||||
using Base::Target;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -1,11 +1,7 @@
|
||||
find_package(OpenGL)
|
||||
add_subdirectory(${GLFW_PATH} ${GLFW_PATH}/build)
|
||||
|
||||
include_directories(
|
||||
${GLFW_PATH}/include
|
||||
${GLAD_PATH}/include
|
||||
${STBIMAGE_PATH}
|
||||
)
|
||||
include_directories(${GLFW_PATH}/include ${GLAD_PATH}/include)
|
||||
|
||||
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
|
||||
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
|
||||
@ -13,8 +9,6 @@ set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
|
||||
|
||||
toAbsolutePath(NB_GRAPHICS_SOURCE
|
||||
./src/Buffers.cpp
|
||||
./src/FrameBuffers.cpp
|
||||
./src/Image.cpp
|
||||
./src/OGLObjects.cpp
|
||||
./src/ProgramPipeline.cpp
|
||||
./src/Textures.cpp
|
||||
@ -26,9 +20,7 @@ toAbsolutePath(NB_GRAPHICS_INCLUDE
|
||||
./Buffers.hpp
|
||||
./Camera.hpp
|
||||
./Draw.hpp
|
||||
./FrameBuffers.hpp
|
||||
./GLLoad.hpp
|
||||
./Image.hpp
|
||||
./OGLObjects.hpp
|
||||
./ProgramPipeline.hpp
|
||||
./Textures.hpp
|
||||
|
||||
@ -1,236 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _NB_FRAMEBUFFER
|
||||
#define _NB_FRAMEBUFFER
|
||||
|
||||
#include "GLLoad.hpp"
|
||||
|
||||
#include <NBCore/Errors.hpp>
|
||||
|
||||
#include "Image.hpp"
|
||||
#include "OGLObjects.hpp"
|
||||
#include "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
|
||||
@ -1,216 +0,0 @@
|
||||
#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
|
||||
@ -15,7 +15,7 @@ class OGLError : public Error<OGLError> {
|
||||
using Base::Base;
|
||||
|
||||
enum Codes : unsigned int {
|
||||
UNDEFINED, HANGING_OBJECT, INVALID_OBJECT
|
||||
UNDEFINED, HANGING_OBJECT
|
||||
};
|
||||
|
||||
static const std::string type;
|
||||
@ -26,8 +26,6 @@ class OpenGLObject {
|
||||
public:
|
||||
OpenGLObject(const OpenGLObject&) = delete;
|
||||
OpenGLObject& operator=(const OpenGLObject&) = delete;
|
||||
OpenGLObject(OpenGLObject&&);
|
||||
OpenGLObject& operator=(OpenGLObject&&);
|
||||
virtual ~OpenGLObject() {};
|
||||
|
||||
virtual void bind() const = 0;
|
||||
@ -35,7 +33,6 @@ class OpenGLObject {
|
||||
GLuint id() const { return _id; }
|
||||
|
||||
protected:
|
||||
using Codes = OGLError::Codes;
|
||||
OpenGLObject() = default;
|
||||
|
||||
virtual GLuint declare() = 0;
|
||||
|
||||
@ -54,14 +54,12 @@ class Shader : public OpenGLObject {
|
||||
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;
|
||||
@ -70,22 +68,6 @@ class Shader : public OpenGLObject {
|
||||
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);
|
||||
|
||||
@ -3,150 +3,58 @@
|
||||
#define _NB_TEXTURES
|
||||
|
||||
#include "Buffers.hpp"
|
||||
#include "Image.hpp"
|
||||
#include "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)) {}
|
||||
template<bool Immutable=false>
|
||||
class TextureBuffer : public virtual Buffer<TextureBuffer<false>> {
|
||||
using Base = Buffer<TextureBuffer<false>>;
|
||||
using BufferType = Base;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
using Base::id;
|
||||
const GLenum Target;
|
||||
static const GLenum Target = GL_TEXTURE_BUFFER;
|
||||
|
||||
protected:
|
||||
|
||||
};
|
||||
|
||||
class Texture : public RenderTarget {
|
||||
protected:
|
||||
using Base = RenderTarget;
|
||||
using Base::_id;
|
||||
template <>
|
||||
class TextureBuffer<true>
|
||||
: public virtual TextureBuffer<false>, public virtual ImmutableBuffer<TextureBuffer<true>> {
|
||||
using Base = TextureBuffer<false>;
|
||||
using BufferType = ImmutableBuffer<TextureBuffer<true>>;
|
||||
|
||||
public:
|
||||
using BufferType::BufferType;
|
||||
using Base::Target;
|
||||
};
|
||||
|
||||
class Texture : public OpenGLObject {
|
||||
using Base = OpenGLObject;
|
||||
public:
|
||||
using Base::Base;
|
||||
using Base::id;
|
||||
|
||||
Texture(GLenum);
|
||||
virtual GLuint declare() override {
|
||||
if (!_id) {
|
||||
glGenTextures(1, &_id);
|
||||
}
|
||||
bind();
|
||||
return _id;
|
||||
}
|
||||
|
||||
virtual void bind() const override { glBindTexture(target, _id); }
|
||||
virtual void unbind() const override { glBindTexture(target, 0); }
|
||||
|
||||
const GLenum target;
|
||||
|
||||
friend Base;
|
||||
|
||||
protected:
|
||||
using Base::_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
|
||||
|
||||
@ -67,7 +67,7 @@ class VAOError : public Error<VAOError> {
|
||||
using Base::Base;
|
||||
|
||||
enum Codes : unsigned int {
|
||||
UNDEFINED, INVALID_ATTRIBUTE
|
||||
UNDEFINED, INVALID_ATTRIBUTE, INVALID_VAO
|
||||
};
|
||||
|
||||
static const std::string type;
|
||||
@ -90,10 +90,7 @@ class VAO : public OpenGLObject {
|
||||
if(_id) {
|
||||
glBindVertexArray(_id);
|
||||
} else {
|
||||
THROW(OGLError(
|
||||
OGLError::Codes::INVALID_OBJECT,
|
||||
"w/ VertexArrayObject"
|
||||
));
|
||||
THROW(VAOError(Codes::INVALID_VAO));
|
||||
}
|
||||
}
|
||||
virtual void unbind() const override { glBindVertexArray(0); }
|
||||
@ -122,29 +119,14 @@ class VAO : public OpenGLObject {
|
||||
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 VBO = ArrayBuffer<false>;
|
||||
using VertBufVec = SharedVector<VBO>;
|
||||
using EBO = ElementBuffer;
|
||||
using EBO = ElementBuffer<false>;
|
||||
|
||||
struct VertexData {
|
||||
std::shared_ptr<VBO> vbo;
|
||||
@ -154,70 +136,27 @@ struct VertexData {
|
||||
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...
|
||||
) {}
|
||||
VertexGroup(const VertexData&);
|
||||
VertexGroup(const VertexDataVec&);
|
||||
VertexGroup(const ByteVector&, const VertexAttributeList&);
|
||||
|
||||
void bind() const {
|
||||
_vao->bind();
|
||||
_ebo->bind();
|
||||
_vao->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;
|
||||
@ -228,10 +167,10 @@ class VertexGroup {
|
||||
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);
|
||||
|
||||
protected:
|
||||
VertexDataVec _vertex_data;
|
||||
std::shared_ptr<EBO> _ebo;
|
||||
std::shared_ptr<VAO> _vao;
|
||||
};
|
||||
|
||||
} // namespace nb
|
||||
|
||||
@ -12,92 +12,15 @@ const std::string BufferError::type = "nb::BufferError";
|
||||
const ErrorCodeMap BufferError::ErrorMessages = {
|
||||
{ BufferErrorCodes::UNDEFINED, "Error" },
|
||||
{ BufferErrorCodes::DATA_OVERFLOW, "Attempting buffer overflow" },
|
||||
{BufferErrorCodes::DATA_ERROR, "Invalid memory operation"}
|
||||
{ BufferErrorCodes::INVALID_BUFFER, "Attempting operation on invalid buffer" },
|
||||
{ BufferErrorCodes::HANDLE_OVERWRITE, "Attempting to overwrite active buffer"}
|
||||
|
||||
};
|
||||
|
||||
Buffer::Buffer(Buffer&& rval) : Target(rval.Target) {
|
||||
*this = std::move(rval);
|
||||
template<>
|
||||
ArrayBuffer<false>::ArrayBuffer(const ByteVector& data_, GLenum usage_)
|
||||
: BufferType() {
|
||||
data(data_, usage_);
|
||||
}
|
||||
|
||||
Buffer& Buffer::operator=(Buffer&& rhs) {
|
||||
if (Target != rhs.Target) {
|
||||
auto targ_name = BufferTypes.at(Target);
|
||||
THROW(OGLError(
|
||||
OGLError::Codes::INVALID_OBJECT,
|
||||
"w/ BufferType "+targ_name+" != BufferType "+BufferTypes.at(rhs.Target)
|
||||
));
|
||||
}
|
||||
OpenGLObject::operator=(std::move(rhs));
|
||||
_usage = rhs._usage;
|
||||
_size = rhs._size;
|
||||
|
||||
rhs._usage = GL_STATIC_DRAW;
|
||||
rhs._size = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
GLenum Buffer::usage() const { return _usage; }
|
||||
|
||||
size_t Buffer::size() const { return _size; }
|
||||
|
||||
ByteVector Buffer::data() const {
|
||||
bind();
|
||||
ByteVector ret(_size);
|
||||
glGetBufferSubData(Target, 0, _size, ret.data());
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Buffer::data(const void* data_, size_t size_, GLenum usage_) {
|
||||
declare();
|
||||
glBufferData(Target, size_, data_, usage_);
|
||||
_size = size_;
|
||||
_usage = usage_;
|
||||
}
|
||||
|
||||
void Buffer::data(const ByteVector& data_, GLenum usage_) {
|
||||
declare();
|
||||
data(data_.data(), data_.size(), usage_);
|
||||
}
|
||||
|
||||
void Buffer::subdata(const void* data_, size_t size_, GLintptr offset_) {
|
||||
bind();
|
||||
if (offset_+size_ <= _size) {
|
||||
THROW(BufferError(BufferError::Codes::DATA_OVERFLOW));
|
||||
}
|
||||
glBufferSubData(Target, offset_, size_, data_);
|
||||
}
|
||||
|
||||
void Buffer::subdata(const ByteVector& data_, GLintptr offset_) {
|
||||
bind();
|
||||
size_t size_ = data_.size();
|
||||
subdata(data_.data(), size_);
|
||||
}
|
||||
|
||||
ElementBuffer::ElementBuffer() : Buffer(GL_ELEMENT_ARRAY_BUFFER) {}
|
||||
|
||||
ByteVector ElementBuffer::data() const {
|
||||
return data();
|
||||
}
|
||||
|
||||
size_t ElementBuffer::size() const { return _size / _typeSize; }
|
||||
|
||||
size_t ElementBuffer::typeSize() const { return _typeSize; }
|
||||
|
||||
GLenum ElementBuffer::glslType() const { return _glslType; }
|
||||
|
||||
template void ElementBuffer::data<uint8_t>(const std::vector<uint8_t>& data, GLenum usage);
|
||||
template void ElementBuffer::data<uint16_t>(const std::vector<uint16_t>& data, GLenum usage);
|
||||
template void ElementBuffer::data<uint32_t>(const std::vector<uint32_t>& data, GLenum usage);
|
||||
template void ElementBuffer::data<uint8_t>(const uint8_t* const data, size_t size, GLenum usage);
|
||||
template void ElementBuffer::data<uint16_t>(const uint16_t* const data, size_t size, GLenum usage);
|
||||
template void ElementBuffer::data<uint32_t>(const uint32_t* const data, size_t size, GLenum usage);
|
||||
|
||||
template void ElementBuffer::subdata<uint8_t>(const uint8_t* const data, size_t size, size_t offset=0);
|
||||
template void ElementBuffer::subdata<uint16_t>(const uint16_t* const data, size_t size, size_t offset=0);
|
||||
template void ElementBuffer::subdata<uint32_t>(const uint32_t* const data, size_t size, size_t offset=0);
|
||||
template void ElementBuffer::subdata<uint8_t>(const std::vector<uint8_t>& data, size_t offset=0);
|
||||
template void ElementBuffer::subdata<uint16_t>(const std::vector<uint16_t>& data, size_t offset=0);
|
||||
template void ElementBuffer::subdata<uint32_t>(const std::vector<uint32_t>& data, size_t offset=0);
|
||||
|
||||
}
|
||||
@ -1,342 +0,0 @@
|
||||
#include "FrameBuffers.hpp"
|
||||
|
||||
namespace nb {
|
||||
|
||||
using FBOErrorCodes = FrameBufferError::Codes;
|
||||
const std::string FrameBufferError::type = "nb::FrameBufferError";
|
||||
const ErrorCodeMap FrameBufferError::ErrorMessages = {
|
||||
{FBOErrorCodes::UNDEFINED, "Error"},
|
||||
{FBOErrorCodes::INVALID_VALUE, "Invalid value"}
|
||||
};
|
||||
|
||||
GLuint FrameBufferBase::declare() {
|
||||
if (!_id) {
|
||||
glGenFramebuffers(1, &_id);
|
||||
}
|
||||
bind();
|
||||
return _id;
|
||||
}
|
||||
|
||||
void FrameBufferBase::remove() {
|
||||
if (_id) {
|
||||
unbind();
|
||||
glDeleteFramebuffers(1, &_id);
|
||||
_id=0;
|
||||
}
|
||||
}
|
||||
|
||||
FrameBufferBase::FrameBufferBase(GLenum target) : Target(target) {}
|
||||
|
||||
std::shared_ptr<RenderTarget> FrameBufferBase::detach(GLenum attch_) {
|
||||
bind();
|
||||
auto ret = _attachments.at(attch_);
|
||||
_attachments.erase(attch_);
|
||||
glFramebufferTexture(Target, attch_, 0, 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void FrameBufferBase::attach(
|
||||
GLenum attachment_,
|
||||
std::shared_ptr<Texture> texture_,
|
||||
unsigned int level_,
|
||||
unsigned int layer_
|
||||
) {
|
||||
declare();
|
||||
if (attachment_ == GL_NONE) {
|
||||
THROW(FrameBufferError(FrameBufferError::INVALID_VALUE,
|
||||
"Cannot attach object to `GL_NONE`"
|
||||
));
|
||||
}
|
||||
glFramebufferTextureLayer(
|
||||
Target,
|
||||
attachment_,
|
||||
texture_->id(),
|
||||
level_,
|
||||
layer_
|
||||
);
|
||||
_attachments[attachment_] = texture_;
|
||||
}
|
||||
|
||||
void FrameBufferBase::attach(
|
||||
GLenum attachment_,
|
||||
std::shared_ptr<Texture> texture_,
|
||||
unsigned int level_
|
||||
) {
|
||||
declare();
|
||||
if (attachment_ == GL_NONE) {
|
||||
THROW(FrameBufferError(FrameBufferError::INVALID_VALUE,
|
||||
"Cannot attach object to `GL_NONE`"
|
||||
));
|
||||
}
|
||||
if (texTypeIs2D(texture_->Target)) {
|
||||
glFramebufferTexture2D(
|
||||
Target,
|
||||
attachment_,
|
||||
texture_->Target,
|
||||
texture_->id(),
|
||||
level_
|
||||
);
|
||||
} else {
|
||||
glFramebufferTexture1D(
|
||||
Target,
|
||||
attachment_,
|
||||
texture_->Target,
|
||||
texture_->id(),
|
||||
level_
|
||||
);
|
||||
}
|
||||
_attachments[attachment_] = texture_;
|
||||
}
|
||||
|
||||
void FrameBufferBase::bind() const {
|
||||
if (_id) {
|
||||
glBindFramebuffer(Target, _id);
|
||||
} else {
|
||||
THROW(OGLError(
|
||||
OGLError::Codes::INVALID_OBJECT,
|
||||
"w/ FrameBuffer"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
void FrameBufferBase::unbind() const {
|
||||
glBindFramebuffer(Target, 0);
|
||||
}
|
||||
|
||||
GLenum FrameBufferBase::status() const {
|
||||
bind();
|
||||
return glCheckFramebufferStatus(Target);
|
||||
}
|
||||
|
||||
FrameBufferBase::AttachmentMap FrameBufferBase::getAllBuffers() const {
|
||||
return _attachments;
|
||||
}
|
||||
|
||||
std::shared_ptr<RenderTarget> FrameBufferBase::getBuffer(GLenum attch_) const {
|
||||
try {
|
||||
return _attachments.at(attch_);
|
||||
} catch (const std::out_of_range& e) {
|
||||
THROW(e);
|
||||
}
|
||||
}
|
||||
|
||||
// class ReadFrameBuffer
|
||||
|
||||
ReadFrameBuffer::ReadFrameBuffer()
|
||||
: _location(GL_NONE), FrameBufferBase(GL_READ_FRAMEBUFFER) {}
|
||||
|
||||
ReadFrameBuffer& ReadFrameBuffer::operator=(WriteFrameBuffer&& mv) {
|
||||
if (mv.id()) {
|
||||
_attachments = mv.getAllBuffers();
|
||||
mv.setWriteBuffer(GL_NONE);
|
||||
mv._attachments = {};
|
||||
mv._locations = {};
|
||||
FrameBufferBase::OpenGLObject::operator=(std::move(mv));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
ReadFrameBuffer& ReadFrameBuffer::operator=(ReadFrameBuffer&& mv) {
|
||||
if (mv.id()) {
|
||||
_attachments = mv._attachments;
|
||||
_location = mv._location;
|
||||
mv._attachments = {};
|
||||
mv._location = GL_NONE;
|
||||
FrameBufferBase::OpenGLObject::operator=(std::move(mv));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::shared_ptr<RenderTarget> ReadFrameBuffer::getReadBuffer() const {
|
||||
if (_location == GL_NONE) { return nullptr; }
|
||||
return getBuffer(_location);
|
||||
}
|
||||
|
||||
GLenum ReadFrameBuffer::getReadLocation() const { return _location; }
|
||||
|
||||
std::shared_ptr<RenderTarget> ReadFrameBuffer::setReadBuffer(GLenum attch_) {
|
||||
bind();
|
||||
if (attch_!=GL_NONE) {
|
||||
try {
|
||||
const auto& x = _attachments.at(attch_);
|
||||
} catch (const std::out_of_range& e) {
|
||||
THROW(FrameBufferError(FrameBufferError::INVALID_VALUE,
|
||||
"No buffer set at attachment"
|
||||
));
|
||||
}
|
||||
}
|
||||
_location = attch_;
|
||||
glReadBuffer(_location);
|
||||
return getReadBuffer();
|
||||
}
|
||||
|
||||
// class WriteFrameBuffer
|
||||
|
||||
WriteFrameBuffer::WriteFrameBuffer()
|
||||
: FrameBufferBase(GL_DRAW_FRAMEBUFFER) {}
|
||||
|
||||
WriteFrameBuffer& WriteFrameBuffer::operator=(ReadFrameBuffer&& rhs) {
|
||||
if (rhs.id()) {
|
||||
_attachments = rhs._attachments;
|
||||
rhs.setReadBuffer(GL_NONE);
|
||||
rhs._attachments = {};
|
||||
rhs._location = GL_NONE;
|
||||
FrameBufferBase::OpenGLObject::operator=(std::move(rhs));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
WriteFrameBuffer& WriteFrameBuffer::operator=(WriteFrameBuffer&& rhs) {
|
||||
if (rhs.id()) {
|
||||
_attachments = rhs._attachments;
|
||||
_locations = rhs._locations;
|
||||
rhs._attachments = {};
|
||||
rhs._locations = {};
|
||||
FrameBufferBase::OpenGLObject::operator=(std::move(rhs));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
using fRGBA = RGBA<float>;
|
||||
|
||||
std::vector<std::shared_ptr<Texture>> WriteFrameBuffer::getWriteBuffers() const {
|
||||
const size_t count = _locations.size();
|
||||
std::vector<std::shared_ptr<Texture>> ret(count);
|
||||
for (int i = 0; i < count; ++i) {
|
||||
ret[i] = std::static_pointer_cast<Texture>(getBuffer(_locations[i]));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<GLenum> WriteFrameBuffer::getWriteLocations() const {
|
||||
return _locations;
|
||||
}
|
||||
|
||||
WriteFrameBuffer::DrawTexVec WriteFrameBuffer::setWriteBuffers(const std::vector<GLenum>& attchs_) {
|
||||
bind();
|
||||
for (GLenum loc : attchs_) {
|
||||
if (getBuffer(loc)->Target == GL_RENDERBUFFER) {
|
||||
THROW(FrameBufferError(FrameBufferError::INVALID_VALUE,
|
||||
"Cannot write to renderbuffer [ @ Attachment = "+std::to_string(loc)+" ]"
|
||||
));
|
||||
}
|
||||
}
|
||||
_locations = attchs_;
|
||||
glDrawBuffers(_locations.size(), _locations.data());
|
||||
return getWriteBuffers();
|
||||
}
|
||||
|
||||
std::shared_ptr<Texture> WriteFrameBuffer::setWriteBuffer(GLenum attch_) {
|
||||
bind();
|
||||
if (attch_ == GL_NONE) {
|
||||
std::vector<GLenum> tmp(_locations.size(), GL_NONE);
|
||||
glDrawBuffers(tmp.size(), tmp.data());
|
||||
_locations = {};
|
||||
return nullptr;
|
||||
}
|
||||
setWriteBuffers({attch_});
|
||||
return std::static_pointer_cast<Texture>(getBuffer(attch_));
|
||||
}
|
||||
|
||||
fRGBA WriteFrameBuffer::clearColorValue() const {
|
||||
bind();
|
||||
float rgba[4];
|
||||
glGetFloatv(GL_COLOR_CLEAR_VALUE, rgba);
|
||||
return fRGBA{rgba[0], rgba[1], rgba[2], rgba[3]};
|
||||
}
|
||||
|
||||
fRGBA WriteFrameBuffer::clearColorValue(const fRGBA& color) {
|
||||
declare();
|
||||
glClearColor(color.r, color.g, color.b, color.a);
|
||||
return color;
|
||||
}
|
||||
|
||||
fRGBA WriteFrameBuffer::clearColorValue(float r, float g, float b, float a) {
|
||||
return clearColorValue({r, g,b , a});
|
||||
}
|
||||
|
||||
double WriteFrameBuffer::clearDepthValue() const {
|
||||
bind();
|
||||
double ret;
|
||||
glGetDoublev(GL_DEPTH_CLEAR_VALUE, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
double WriteFrameBuffer::clearDepthValue(double d_) {
|
||||
declare();
|
||||
glClearDepth(d_);
|
||||
return d_;
|
||||
}
|
||||
|
||||
GLint WriteFrameBuffer::clearStencilValue() const {
|
||||
bind();
|
||||
GLint ret;
|
||||
glGetIntegerv(GL_STENCIL_CLEAR_VALUE, &ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
GLint WriteFrameBuffer::clearStencilValue(GLint s_) {
|
||||
declare();
|
||||
glClearStencil(s_);
|
||||
return s_;
|
||||
}
|
||||
|
||||
void WriteFrameBuffer::clear(GLbitfield mask_) const {
|
||||
bind();
|
||||
glClear(mask_);
|
||||
}
|
||||
|
||||
void WriteFrameBuffer::clearDepth() const {
|
||||
clear(GL_DEPTH_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void WriteFrameBuffer::clearDepth(float val_) const {
|
||||
bind();
|
||||
glClearBufferfv(GL_DEPTH, 0, &val_);
|
||||
}
|
||||
|
||||
void WriteFrameBuffer::clearStencil() const {
|
||||
clear(GL_STENCIL_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void WriteFrameBuffer::clearStencil(GLint val_) const {
|
||||
bind();
|
||||
glClearBufferiv(GL_STENCIL, 0, &val_);
|
||||
}
|
||||
|
||||
void WriteFrameBuffer::clearDepthStencil() const {
|
||||
clear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void WriteFrameBuffer::clearDepthStencil(float d_, GLint s_) const {
|
||||
bind();
|
||||
glClearBufferfi(GL_DEPTH_STENCIL, 0, d_, s_);
|
||||
}
|
||||
|
||||
FrameBuffer::FrameBuffer()
|
||||
: FrameBufferBase(GL_FRAMEBUFFER) {}
|
||||
|
||||
FrameBuffer& FrameBuffer::operator=(FrameBuffer&& rhs) {
|
||||
if (rhs.id()) {
|
||||
_attachments = rhs._attachments;
|
||||
_location = rhs._location;
|
||||
_locations = rhs._locations;
|
||||
rhs._attachments = {};
|
||||
rhs._locations = {};
|
||||
rhs._location = GL_NONE;
|
||||
OpenGLObject::operator=(std::move(rhs));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
FrameBuffer& FrameBuffer::operator=(ReadFrameBuffer&& rhs) {
|
||||
ReadFrameBuffer::operator=(std::move(rhs));
|
||||
return *this;
|
||||
}
|
||||
|
||||
FrameBuffer& FrameBuffer::operator=(WriteFrameBuffer&& rhs) {
|
||||
WriteFrameBuffer::operator=(std::move(rhs));
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace nb
|
||||
@ -1,8 +0,0 @@
|
||||
#include "Image.hpp"
|
||||
|
||||
using ImageErrorCodes = nb::ImageError::Codes;
|
||||
const std::string nb::ImageError::type = "nb::ImageError";
|
||||
const nb::ErrorCodeMap nb::ImageError::ErrorMessages = {
|
||||
{ ImageErrorCodes::UNDEFINED, "Error" },
|
||||
{ImageErrorCodes::OUT_OF_BOUNDS, "Out of bounds"}
|
||||
};
|
||||
@ -5,19 +5,7 @@ namespace nb {
|
||||
const std::string OGLError::type = "nb::OGLError";
|
||||
const ErrorCodeMap OGLError::ErrorMessages = {
|
||||
{ OGLError::Codes::UNDEFINED, "Error" },
|
||||
{OGLError::Codes::HANGING_OBJECT, "Attempting to leave a hanging OpenGL object"},
|
||||
{OGLError::Codes::INVALID_OBJECT, "Attempting operation with invalid object"}
|
||||
{OGLError::Codes::HANGING_OBJECT, "Attempting to leave a hanging OpenGL object"}
|
||||
};
|
||||
|
||||
OpenGLObject::OpenGLObject(OpenGLObject&& rval) {
|
||||
*this = std::move(rval);
|
||||
}
|
||||
|
||||
OpenGLObject& OpenGLObject::operator=(OpenGLObject&& rhs) {
|
||||
if (_id) { THROW(OGLError(Codes::HANGING_OBJECT)); }
|
||||
_id = rhs._id;
|
||||
rhs._id = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace nb
|
||||
@ -15,29 +15,22 @@ const ErrorCodeMap ProgramError::ErrorMessages = {
|
||||
{ProgramErrCodes::LINKING_ERROR, "Linker error"}
|
||||
};
|
||||
|
||||
Shader::Shader(GLenum target_, const std::string& source_)
|
||||
: Shader(target_, std::vector<std::string>({source_})) {}
|
||||
|
||||
Shader::Shader(GLenum target_, const std::vector<std::string>& strings_)
|
||||
: target(target_), _sources(strings_) {
|
||||
Shader::Shader(GLenum target_, const std::string& source_) : target(target_) {
|
||||
declare();
|
||||
compile();
|
||||
const char* src_ptr= source_.data();
|
||||
glShaderSource(_id, 1, &src_ptr, NULL);
|
||||
glCompileShader(_id);
|
||||
_success = status(GL_COMPILE_STATUS);
|
||||
if (!_success) {
|
||||
WARN(log(), 0x0FE);
|
||||
}
|
||||
}
|
||||
|
||||
Shader::Shader(Shader&& cpy)
|
||||
: target(cpy.target), _success(cpy._success), _sources(cpy._sources) {
|
||||
Shader::Shader(Shader&& cpy) : target(cpy.target) {
|
||||
_id = cpy._id;
|
||||
|
||||
cpy._success = false;
|
||||
cpy._sources = {};
|
||||
cpy._id = 0;
|
||||
}
|
||||
|
||||
Shader::operator bool() { return success(); }
|
||||
|
||||
bool Shader::success() const {
|
||||
return _success;
|
||||
}
|
||||
Shader::operator bool() { return _success; }
|
||||
|
||||
GLint Shader::status(GLenum parameter) const {
|
||||
GLint param = 0;
|
||||
|
||||
@ -1,7 +1,10 @@
|
||||
#include "Textures.hpp"
|
||||
|
||||
namespace nb {
|
||||
namespace nb{
|
||||
|
||||
Texture::Texture(GLenum target_) : target(target_) {
|
||||
glGenTextures(1, &_id);
|
||||
}
|
||||
|
||||
Texture::Texture(GLenum target_) : Base(target_) {}
|
||||
|
||||
} // namespace nb
|
||||
@ -6,14 +6,8 @@ using VAOErrorCodes = VAOError::Codes;
|
||||
const std::string VAOError::type = "nb::VAOError";
|
||||
const ErrorCodeMap VAOError::ErrorMessages = {
|
||||
{ VAOErrorCodes::UNDEFINED, "Error" },
|
||||
{ VAOErrorCodes::INVALID_ATTRIBUTE, "Targeting invalid attribute"}
|
||||
};
|
||||
|
||||
using VertexCodes = VertexError::Codes;
|
||||
const std::string VertexError::type = "nb::VertexError";
|
||||
const ErrorCodeMap VertexError::ErrorMessages = {
|
||||
{ VertexError::UNDEFINED, "Error" },
|
||||
{ VertexError::MISALIGNED_DATA, "Data does not match vertex layout"}
|
||||
{ VAOErrorCodes::INVALID_ATTRIBUTE, "Targeting invalid attribute"},
|
||||
{ VAOErrorCodes::INVALID_VAO, "Targeting invalid VAO" }
|
||||
};
|
||||
|
||||
VAO::VAO(const VertexAttributePointerList& attr_ptrs) {
|
||||
@ -25,9 +19,12 @@ VAO::VAO(VAO&& other) {
|
||||
}
|
||||
|
||||
VAO& VAO::operator=(VAO&& rhs) {
|
||||
OpenGLObject::operator=(std::move(rhs));
|
||||
_id = rhs._id;
|
||||
_attrs = rhs._attrs;
|
||||
|
||||
rhs._id = 0;
|
||||
rhs._attrs = {};
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -118,17 +115,25 @@ VertexGroup& VertexGroup::operator=(VertexGroup&& rhs) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
void VertexGroup::draw() const {
|
||||
bind();
|
||||
glDrawElements(primitive, _ebo->size(), _ebo->glslType(), 0);
|
||||
VertexGroup::VertexGroup(const VertexDataVec& vertex_data_)
|
||||
: _vao(std::make_shared<VAO>()), _ebo(std::make_shared<EBO>()) {
|
||||
setBuffers(vertex_data_);
|
||||
}
|
||||
|
||||
VertexGroup::VertexGroup(const VertexData& vertex_data_)
|
||||
: VertexGroup(VertexDataVec({vertex_data_})) {}
|
||||
|
||||
VertexGroup::VertexGroup(
|
||||
const ByteVector& data_,
|
||||
const VertexAttributeList& attrs_
|
||||
) : VertexGroup({std::make_shared<VBO>(data_), attrs_}) {}
|
||||
|
||||
size_t VertexGroup::addBuffer(const VertexData& vertex_data_) {
|
||||
VertexData data = vertex_data_;
|
||||
_vertex_data.emplace_back(data);
|
||||
VertexAttributePointerList attrs = _vao->attributes();
|
||||
GLuint idx = attrs.size();
|
||||
for (const auto& attr : data.attrs) {
|
||||
for (auto attr : data.attrs) {
|
||||
attrs.emplace_back(
|
||||
VertexAttributePointer{attr, data.vbo->id(),idx}
|
||||
);
|
||||
@ -138,13 +143,6 @@ size_t VertexGroup::addBuffer(const VertexData& vertex_data_) {
|
||||
return _vertex_data.size();
|
||||
}
|
||||
|
||||
size_t VertexGroup::addBuffer(const ByteVector& data_, const VertexAttributeList& attrs_) {
|
||||
return addBuffer({
|
||||
std::make_shared<VBO>(data_),
|
||||
attrs_
|
||||
});
|
||||
}
|
||||
|
||||
VertexData VertexGroup::getBuffers(size_t idx) {
|
||||
return _vertex_data[idx];
|
||||
}
|
||||
@ -157,8 +155,8 @@ size_t VertexGroup::setBuffers(const VertexDataVec& vertex_data_) {
|
||||
_vertex_data = vertex_data_;
|
||||
VertexAttributePointerList attr_ptrs;
|
||||
GLuint idx = 0;
|
||||
for (const auto& data : _vertex_data) {
|
||||
for (const auto& attr : data.attrs) {
|
||||
for (auto data : _vertex_data) {
|
||||
for (auto attr : data.attrs) {
|
||||
attr_ptrs.emplace_back(
|
||||
VertexAttributePointer{attr, data.vbo->id(), idx}
|
||||
);
|
||||
@ -169,15 +167,9 @@ size_t VertexGroup::setBuffers(const VertexDataVec& vertex_data_) {
|
||||
return _vertex_data.size();
|
||||
}
|
||||
|
||||
VertexDataVec VertexGroup::dropBuffers() {
|
||||
auto ret = _vertex_data;
|
||||
setBuffers({});
|
||||
return ret;
|
||||
}
|
||||
|
||||
VertexData VertexGroup::dropBuffer(size_t idx) {
|
||||
if (idx >= _vertex_data.size()) {
|
||||
THROW(NBError(NBError::INDEX_ERROR));
|
||||
THROW(BufferError(BufferError::Codes::INVALID_BUFFER));
|
||||
}
|
||||
VertexDataVec tmp;
|
||||
VertexData ret;
|
||||
@ -236,16 +228,4 @@ void VertexGroup::enable() { _vao->enable(); }
|
||||
|
||||
void VertexGroup::disable() { _vao->disable(); }
|
||||
|
||||
std::shared_ptr<EBO> VertexGroup::indices() const { return _ebo; }
|
||||
|
||||
std::shared_ptr<EBO> VertexGroup::indices(const std::vector<uint32_t>& data) {
|
||||
_ebo->data(data);
|
||||
return _ebo;
|
||||
}
|
||||
|
||||
std::shared_ptr<EBO> VertexGroup::indices(std::shared_ptr<EBO> buffer) {
|
||||
_ebo = buffer;
|
||||
return _ebo;
|
||||
}
|
||||
|
||||
} // namespace nb
|
||||
|
||||
@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.26.0)
|
||||
if (NB_BUILD_TESTS)
|
||||
enable_testing()
|
||||
include(GoogleTest)
|
||||
|
||||
add_executable(TestWindow
|
||||
./TestWindow.cpp
|
||||
)
|
||||
@ -12,15 +11,4 @@ if (NB_BUILD_TESTS)
|
||||
NBGraphics
|
||||
)
|
||||
|
||||
add_executable(TestImages
|
||||
./testImages.cpp
|
||||
)
|
||||
target_link_libraries(TestImages
|
||||
NBCore
|
||||
NBGraphics
|
||||
GTest::gtest_main
|
||||
)
|
||||
|
||||
gtest_discover_tests(TestImages)
|
||||
|
||||
endif()
|
||||
@ -1,12 +1,7 @@
|
||||
#include "GLLoad.hpp"
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
|
||||
#include "Image.hpp"
|
||||
#include "ProgramPipeline.hpp"
|
||||
#include "VertexArray.hpp"
|
||||
#include "Window.hpp"
|
||||
#include "Textures.hpp"
|
||||
|
||||
int main() {
|
||||
nb::logger.log("Howdy!");
|
||||
@ -15,90 +10,54 @@ int main() {
|
||||
window.setWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
window.init();
|
||||
|
||||
nb::logger.log("Goob");
|
||||
|
||||
auto vert = std::make_shared<nb::Shader>(
|
||||
GL_VERTEX_SHADER,
|
||||
"#version 330 core\n"
|
||||
"layout (location = 0) in vec2 aPos;\n"
|
||||
"layout (location = 1) in vec2 tPos;\n"
|
||||
"out vec2 vPos;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);\n"
|
||||
" vPos = tPos;\n"
|
||||
"}\0"
|
||||
);
|
||||
|
||||
LOG(vert->log());
|
||||
LOG("Vertex Shader: " + vert->log());
|
||||
|
||||
auto frag = std::make_shared<nb::Shader>(
|
||||
GL_FRAGMENT_SHADER,
|
||||
"#version 330 core\n"
|
||||
"in vec2 vPos;\n"
|
||||
"out vec4 FragColor;\n"
|
||||
"uniform sampler2D tex;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" FragColor = texture(tex, vPos);\n"
|
||||
" FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
|
||||
"}\n\0"
|
||||
);
|
||||
|
||||
LOG(frag->log());
|
||||
LOG("Fragment shader: " + frag->log());
|
||||
|
||||
nb::ByteVector data = nb::vectorToBytes<float>({
|
||||
-0.5, -0.5, 0,0,
|
||||
-0.5, 0.5, 0,1,
|
||||
0.5, 0.5, 1,1,
|
||||
0.5, -0.5, 1,0
|
||||
-0.5, -0.5, 0.5, -0.5, 0.0, 0.5
|
||||
});
|
||||
|
||||
nb::Program prog({vert, frag});
|
||||
prog.bind();
|
||||
|
||||
std::vector<uint32_t> indxs = {0, 1, 2, 0, 2, 3};
|
||||
|
||||
nb::VertexGroup tri(data, {
|
||||
nb::VertexAttribute{
|
||||
2,
|
||||
GL_FLOAT,
|
||||
false,
|
||||
{0, 16}
|
||||
},
|
||||
nb::VertexAttribute{
|
||||
2,
|
||||
GL_FLOAT,
|
||||
false,
|
||||
{8, 16}
|
||||
{0, 8}
|
||||
}
|
||||
}, indxs);
|
||||
});
|
||||
tri.bind();
|
||||
|
||||
int width, height, numChannels;
|
||||
auto raw_img_data = stbi_load(
|
||||
"./awesomeface.png",
|
||||
&width,
|
||||
&height,
|
||||
&numChannels,
|
||||
0
|
||||
);
|
||||
LOG(numChannels);
|
||||
using RGBA = nb::Pixel<uint8_t, nb::Red, nb::Green, nb::Blue, nb::Alpha>;
|
||||
nb::ImageReference<RGBA> img(width, height, raw_img_data);
|
||||
auto tex = nb::ImageTexture<RGBA>();
|
||||
tex.parameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
tex.parameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
tex.setImage(img);
|
||||
tex.bind();
|
||||
tri.bind();
|
||||
LOG(img.at(256, 256).r);
|
||||
stbi_image_free(raw_img_data);
|
||||
|
||||
LOG(prog.log());
|
||||
|
||||
GLFWwindow* window_ptr = window.getWindow();
|
||||
while(!glfwWindowShouldClose(window_ptr)) {
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
tri.draw();
|
||||
glfwPollEvents();
|
||||
glfwSwapBuffers(window_ptr);
|
||||
}
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 58 KiB |
@ -1,156 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
|
||||
#include "Image.hpp"
|
||||
|
||||
TEST(TestImage, SinglePixel) {
|
||||
using RGB = nb::Pixel<uint8_t, nb::Red, nb::Green, nb::Blue>;
|
||||
RGB p1;
|
||||
p1.r = 0x00;
|
||||
p1.g = 0x0F;
|
||||
p1.b = 0xFF;
|
||||
ASSERT_EQ(p1.r, 0x00);
|
||||
ASSERT_EQ(p1.g, 0x0F);
|
||||
ASSERT_EQ(p1.b, 0xFF);
|
||||
RGB p2(p1);
|
||||
ASSERT_EQ(p2.r, 0x00);
|
||||
ASSERT_EQ(p2.g, 0x0F);
|
||||
ASSERT_EQ(p2.b, 0xFF);
|
||||
RGB p3(0xA, 0xB, 0xC);
|
||||
ASSERT_EQ(p3.r, 0xA);
|
||||
ASSERT_EQ(p3.g, 0xB);
|
||||
ASSERT_EQ(p3.b, 0xC);
|
||||
RGB p4 = p3;
|
||||
ASSERT_EQ(p4.r, 0xA);
|
||||
ASSERT_EQ(p4.g, 0xB);
|
||||
ASSERT_EQ(p4.b, 0xC);
|
||||
RGB p5(0xA0, 0xB0, 0xC0);
|
||||
ASSERT_EQ(p5.r, 0xA0);
|
||||
ASSERT_EQ(p5.g, 0xB0);
|
||||
ASSERT_EQ(p5.b, 0xC0);
|
||||
|
||||
using RBAG = nb::Pixel<float,
|
||||
nb::Red, nb::Blue, nb::Alpha, nb::Green
|
||||
>;
|
||||
float a=0.5, b=0.4, c=0.0, d=0.3;
|
||||
RBAG p6 = {a, b, c, d};
|
||||
ASSERT_EQ(p6.r, a);
|
||||
ASSERT_EQ(p6.b, b);
|
||||
ASSERT_EQ(p6.a, c);
|
||||
ASSERT_EQ(p6.g, d);
|
||||
RBAG p7 = p6;
|
||||
ASSERT_EQ(p7.r, a);
|
||||
ASSERT_EQ(p7.b, b);
|
||||
ASSERT_EQ(p7.a, c);
|
||||
ASSERT_EQ(p7.g, d);
|
||||
}
|
||||
|
||||
TEST(TestImage, PixelReference) {
|
||||
using RGB = nb::Pixel<uint8_t, nb::Red, nb::Green, nb::Blue>;
|
||||
using RGBRef = nb::PixelReference<RGB>;
|
||||
|
||||
using RBAG = nb::Pixel<float,
|
||||
nb::Red, nb::Blue, nb::Alpha, nb::Green
|
||||
>;
|
||||
using RBAGRef = nb::PixelReference<RBAG>;
|
||||
|
||||
RGB p1{255, 254, 253};
|
||||
RGBRef rp1(p1);
|
||||
ASSERT_EQ(rp1.r, p1.r);
|
||||
ASSERT_EQ(rp1.g, p1.g);
|
||||
ASSERT_EQ(rp1.b, p1.b);
|
||||
|
||||
RGB p2{0xa, 0xb, 0xc};
|
||||
rp1 = p2;
|
||||
ASSERT_EQ(p1.r, p2.r);
|
||||
ASSERT_EQ(p1.g, p2.g);
|
||||
ASSERT_EQ(p1.b, p2.b);
|
||||
|
||||
RGB p3 = rp1;
|
||||
ASSERT_EQ(p3.r, p1.r);
|
||||
ASSERT_EQ(p3.g, p1.g);
|
||||
ASSERT_EQ(p3.b, p1.b);
|
||||
|
||||
RGBRef rp2 = rp1;
|
||||
rp2 = RGB(2, 4, 8);
|
||||
ASSERT_EQ(p1.r, 2);
|
||||
ASSERT_EQ(p1.g, 4);
|
||||
ASSERT_EQ(p1.b, 8);
|
||||
|
||||
int val = 27;
|
||||
rp1.b = val;
|
||||
ASSERT_EQ(rp1.r, p1.r);
|
||||
ASSERT_EQ(rp1.g, p1.g);
|
||||
ASSERT_EQ(rp1.b, p1.b);
|
||||
ASSERT_EQ(p1.b, val);
|
||||
}
|
||||
|
||||
TEST(TestImage, SimpleImage) {
|
||||
unsigned char data[] = {
|
||||
0x00, 0x00,
|
||||
0x00, 0xFF,
|
||||
0xFF, 0x00,
|
||||
0xFF, 0xFF
|
||||
};
|
||||
using RG = nb::Pixel<uint8_t, nb::Red, nb::Blue>;
|
||||
nb::Image<RG> img(
|
||||
2, 2, data
|
||||
);
|
||||
for (int i = 0; i < sizeof(data); ++i) {
|
||||
ASSERT_EQ(img.data()[i], data[i]);
|
||||
}
|
||||
auto pixel = img.at(0, 1);
|
||||
ASSERT_EQ(pixel.r, data[4]);
|
||||
ASSERT_EQ(pixel.b, data[5]);
|
||||
|
||||
}
|
||||
|
||||
TEST(TestImage, LoadImage) {
|
||||
int width, height, numChannels;
|
||||
unsigned char* data = stbi_load(
|
||||
"./awesomeface.png",
|
||||
&width,
|
||||
&height,
|
||||
&numChannels,
|
||||
0
|
||||
);
|
||||
using RGBA = nb::Pixel<unsigned char, nb::Red, nb::Green, nb::Blue, nb::Alpha>;
|
||||
nb::Image<RGBA> img(
|
||||
width, height, data
|
||||
);
|
||||
for(int i = 0; i < height; ++i) {
|
||||
for (int j = 0; j < width; ++j) {
|
||||
auto pixel = img.at(j, i);
|
||||
ASSERT_EQ(pixel.r, data[4*(i*width+j)]);
|
||||
ASSERT_EQ(pixel.g, data[4*(i*width+j) + 1]);
|
||||
ASSERT_EQ(pixel.b, data[4*(i*width+j) + 2]);
|
||||
ASSERT_EQ(pixel.a, data[4*(i*width+j) + 3]);
|
||||
}
|
||||
}
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
TEST(TestImage, ImageBounds) {
|
||||
size_t len = 10;
|
||||
uint8_t val = 0xAA;
|
||||
std::vector<uint8_t> vec(2*len*len, val);
|
||||
using RB = nb::Pixel<uint8_t, nb::Red, nb::Blue>;
|
||||
nb::Image<RB> img(
|
||||
len, len, vec.data()
|
||||
);
|
||||
for (int i=0; i < 15; ++i) {
|
||||
if (i<len) {
|
||||
auto pixel = img.at(len/2, i);
|
||||
ASSERT_EQ(pixel.r, val);
|
||||
ASSERT_EQ(pixel.b, val);
|
||||
} else {
|
||||
ASSERT_THROW(img.at(len/2, i), nb::ImageError);
|
||||
ASSERT_THROW(img.at(i, len/2), nb::ImageError);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user