Added VAO and some stuff

This commit is contained in:
NaifBanana 2026-03-21 22:14:31 -05:00
parent 9ec4a11abf
commit fe8d4b8e13
2 changed files with 62 additions and 11 deletions

View File

@ -2,6 +2,8 @@
#ifndef _NB_CORE_TYPES #ifndef _NB_CORE_TYPES
#define _NB_CORE_TYPES #define _NB_CORE_TYPES
#include <vector>
namespace nb { namespace nb {
template<typename T> template<typename T>
@ -17,5 +19,7 @@ T swapEndian(const T& val) {
return ret; return ret;
} }
using ByteVector = std::vector<uint8_t>;
} // namespace nb } // namespace nb
#endif // _NB_CORE_TYPES #endif // _NB_CORE_TYPES

View File

@ -10,6 +10,7 @@
#include <vector> #include <vector>
#include "NBCore/Errors.hpp" #include "NBCore/Errors.hpp"
#include "NBCore/Types.hpp"
namespace nb { namespace nb {
@ -101,6 +102,8 @@ public:
template <typename ObjectType> template <typename ObjectType>
class OpenGLObject { class OpenGLObject {
public: public:
OpenGLObject(OpenGLObject&&) = default;
OpenGLObject& operator=(OpenGLObject&&) = default;
~OpenGLObject() { remove(); } ~OpenGLObject() { remove(); }
virtual void bind() const = 0; virtual void bind() const = 0;
@ -110,27 +113,71 @@ public:
protected: protected:
OpenGLObject() = default; OpenGLObject() = default;
OpenGLObject(const OpenGLObject& rhs) = delete; OpenGLObject(const OpenGLObject&) = delete;
OpenGLObject& operator=(const OpenGLObject& rhs) = delete; OpenGLObject& operator=(const OpenGLObject&) = delete;
virtual void remove() const = 0; virtual void remove() const = 0;
GLuint _id; GLuint _id;
}; };
template <typename BufferType> class VAO : public virtual OpenGLObject<VAO> {
class Buffer : OpenGLObject<Buffer<BufferType>> {
public: public:
using Base = OpenGLObject<Buffer>; using Base = OpenGLObject<VAO>;
using Base::Base; using Base::Base;
using BufferType::BufferType;
using BufferType::GLTarget;
void bind() const { glBindBuffer(GLTarget, _id); } VAO() { glGenVertexArrays(1, &_id); }
void unbind() const { glBindBuffer(GLTarget, 0); }
void bind() const { glBindVertexArray(_id); }
void unbind() const { glBindVertexArray(0); }
protected: protected:
using Base::_id; using Base::_id;
const
void remove() { glDeleteVertexArrays(1, &_id); }
};
template <typename BufferType>
class Buffer : public virtual OpenGLObject<Buffer<BufferType>> {
public:
using Base = OpenGLObject<Buffer>;
using Base::Base;
Buffer(GLenum usage_ = GL_STATIC_DRAW) : usage(usage_) { glGenBuffers(1, &_id); }
void bind() const { glBindBuffer(GLTarget, _id); }
void unbind() const { glBindBuffer(GLTarget, 0); }
void data(void* data_, size_t size) const { glBufferData(GLTarget, size, data_, usage); }
void data(nb::ByteVector& data_) const { glBufferData(GLTarget, data_.size(), data_.data(), usage); }
void invalidate() const { glInvalidateBufferData(_id); }
const GLenum GLTarget = BufferType::GLTarget;
GLenum usage;
protected:
using Base::_id;
void remove() { glDeleteBuffers(1, &_id); }
};
class VertexBuffer : public virtual Buffer<VertexBuffer> {
public:
static const GLenum GLTarget = GL_ARRAY_BUFFER;
protected:
};
class ElementBuffer : public virtual Buffer<ElementBuffer> {
public:
static const GLenum GLTarget = GL_ELEMENT_ARRAY_BUFFER;
protected:
}; };