Added vertex buffers/data handling and shader/program handling
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <NBCore/Errors.hpp>
|
||||
#include <NBCore/Logger.hpp>
|
||||
@@ -16,69 +15,20 @@
|
||||
|
||||
namespace nb {
|
||||
|
||||
static uint8_t GLSLTypeSize(GLenum type) {
|
||||
switch(type) {
|
||||
case GL_SHORT:
|
||||
case GL_UNSIGNED_SHORT:
|
||||
case GL_HALF_FLOAT:
|
||||
return 2;
|
||||
break;
|
||||
case GL_INT:
|
||||
case GL_UNSIGNED_INT:
|
||||
case GL_FLOAT:
|
||||
return 4;
|
||||
break;
|
||||
case GL_DOUBLE:
|
||||
return 8;
|
||||
break;
|
||||
default:
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ByteVector vectorToRaw(const std::vector<T>& vec) {
|
||||
unsigned int num_bytes = vec.size() * sizeof(T);
|
||||
ByteVector ret(num_bytes);
|
||||
memcpy(ret.data(), vec.data(), num_bytes);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T, typename S>
|
||||
ByteVector concatVectorsToRaw(const std::vector<T>& vec1, const std::vector<S>& vec2) {
|
||||
ByteVector vec1_raw = vectorToRaw<T>(vec1);
|
||||
ByteVector vec2_raw = vectorToRaw<S>(vec2);
|
||||
unsigned int vec1_raw_size = vec1_raw.size();
|
||||
unsigned int vec2_raw_size = vec2_raw.size();
|
||||
ByteVector ret(vec1_raw_size + vec2_raw_size);
|
||||
memcpy(ret.data(), vec1_raw.data(), vec1_raw_size);
|
||||
memcpy(ret.data() + vec1_raw_size, vec2_raw.data(), vec2_raw_size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::vector<T> rawToVector(const ByteVector& vec) {
|
||||
if (vec.size() % sizeof(T) != 0) {
|
||||
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);
|
||||
memcpy(ret.data(), vec.data(), vec.size());
|
||||
|
||||
return ret;
|
||||
}
|
||||
extern const std::unordered_map<GLenum, std::string> BufferTypes;
|
||||
|
||||
class BufferError : public Error<BufferError> {
|
||||
protected:
|
||||
using Base = Error<BufferError>;
|
||||
|
||||
public:
|
||||
using Base::Base;
|
||||
|
||||
enum Codes : unsigned int {
|
||||
UNDEFINED, DATA_OVERFLOW, INVALID_BUFFER, HANDLE_OVERWRITE
|
||||
};
|
||||
|
||||
public:
|
||||
enum Codes : unsigned int {
|
||||
UNDEFINED,
|
||||
DATA_OVERFLOW,
|
||||
INVALID_BUFFER,
|
||||
HANDLE_OVERWRITE,
|
||||
};
|
||||
static const std::string type;
|
||||
static const ErrorCodeMap ErrorMessages;
|
||||
};
|
||||
@@ -89,13 +39,25 @@ class Buffer : public OpenGLObject {
|
||||
|
||||
public:
|
||||
using OpenGLObject::OpenGLObject;
|
||||
|
||||
Buffer() = default;
|
||||
Buffer(Buffer&& other) {
|
||||
*this = std::move(other);
|
||||
}
|
||||
Buffer& operator=(Buffer&& rhs) {
|
||||
if (_id) { THROW(BufferError(Codes::HANDLE_OVERWRITE)); }
|
||||
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;
|
||||
@@ -109,8 +71,12 @@ class Buffer : public OpenGLObject {
|
||||
virtual void bind() const override {
|
||||
if (_id) {
|
||||
glBindBuffer(Target, _id);
|
||||
} else {
|
||||
THROW(BufferError(
|
||||
Codes::INVALID_BUFFER,
|
||||
"w/ BufferType " + BufferTypes.at(Target)
|
||||
));
|
||||
}
|
||||
THROW(BufferError(Codes::INVALID_BUFFER));
|
||||
}
|
||||
virtual void unbind() const override { glBindBuffer(Target, 0); }
|
||||
virtual GLenum usage() const { return _usage; }
|
||||
@@ -240,13 +206,14 @@ class ArrayBuffer<true>
|
||||
using BufferType = ImmutableBuffer<ArrayBuffer<true>>;
|
||||
using BufferType::BufferType;
|
||||
|
||||
|
||||
public:
|
||||
using Base::Target;
|
||||
|
||||
};
|
||||
|
||||
template<bool Immutable=false>
|
||||
class ElementBuffer : public virtual Buffer<ElementBuffer<false>> {
|
||||
class ElementBuffer : public Buffer<ElementBuffer<false>> {
|
||||
using Base = Buffer<ElementBuffer<false>>;
|
||||
using BufferType = Base;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user