Add "ensureInitialize" function and now we can make empty objects

This commit is contained in:
Linloir 2022-12-13 09:43:54 +08:00
parent 38731299fd
commit 7ce0a4e27d
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
12 changed files with 177 additions and 101 deletions

1
.gitignore vendored
View File

@ -361,3 +361,4 @@ MigrationBackup/
# Fody - auto-generated XML schema # Fody - auto-generated XML schema
FodyWeavers.xsd FodyWeavers.xsd
/FinalProject/temp/shaders

View File

@ -1,32 +1,34 @@
#pragma once #pragma once
#include "ebo.h" #include "ebo.h"
#include "logger.h"
ElementBufferObject::ElementBufferObject() { ElementBufferObject::ElementBufferObject() {
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); _id = 0;
} }
ElementBufferObject::ElementBufferObject(const std::vector<unsigned int>& indices) : ElementBufferObject::ElementBufferObject(const std::vector<unsigned int>& indices) {
_indices(indices) {
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id);
OPENGL_EXTRA_FUNCTIONS->glBufferData( OPENGL_EXTRA_FUNCTIONS->glBufferData(
GL_ELEMENT_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER,
_indices.size() * sizeof(unsigned int), indices.size() * sizeof(unsigned int),
_indices.data(), indices.data(),
GL_STATIC_DRAW GL_STATIC_DRAW
); );
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
ElementBufferObject::ElementBufferObject(std::vector<unsigned int>&& indices) : void ElementBufferObject::setBuffer(const std::vector<unsigned int>& indices) {
_indices(std::move(indices)) { if (_id == 0) {
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); Logger::error("Try to bind buffer to an uninitialized element buffer object");
return;
}
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id);
OPENGL_EXTRA_FUNCTIONS->glBufferData( OPENGL_EXTRA_FUNCTIONS->glBufferData(
GL_ELEMENT_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER,
_indices.size() * sizeof(unsigned int), indices.size() * sizeof(unsigned int),
_indices.data(), indices.data(),
GL_STATIC_DRAW GL_STATIC_DRAW
); );
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

View File

@ -6,21 +6,49 @@
class ElementBufferObject class ElementBufferObject
{ {
public:
static ElementBufferObject empty() {
return ElementBufferObject();
}
private: private:
unsigned int _id = 0; unsigned int _id = 0;
std::vector<unsigned int> _indices;
private:
ElementBufferObject();
public: public:
ElementBufferObject();
ElementBufferObject(const std::vector<unsigned int>& indices); ElementBufferObject(const std::vector<unsigned int>& indices);
ElementBufferObject(std::vector<unsigned int>&& indices);
inline unsigned int id() const { return _id; } inline unsigned int id() const { return _id; }
inline std::vector<unsigned int> indices() const { return _indices; }
inline void dispose() const; inline void bind() const;
inline void unbind() const;
inline void dispose();
inline void ensureInitialized();
void setBuffer(const std::vector<unsigned int>& indices);
}; };
inline void ElementBufferObject::dispose() const { inline void ElementBufferObject::bind() const {
OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id); if (_id == 0) {
Logger::error("Binding an invalid ElementBufferObject");
return;
}
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id);
}
inline void ElementBufferObject::unbind() const {
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
inline void ElementBufferObject::dispose() {
OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id);
_id = 0;
}
inline void ElementBufferObject::ensureInitialized() {
if (_id == 0) {
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
}
} }

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "qdebug.h" #include <qdebug.h>
#include "logger.h" #include "logger.h"

View File

@ -30,8 +30,6 @@ SceneViewer::SceneViewer(QWidget* parent)
// Copy the shaders to the folder // Copy the shaders to the folder
QFile::copy(":/shaders/vertexshader.vs", "./temp/shaders/vertexshader.vs"); QFile::copy(":/shaders/vertexshader.vs", "./temp/shaders/vertexshader.vs");
QFile::copy(":/shaders/fragmentshader.fs", "./temp/shaders/fragmentshader.fs"); QFile::copy(":/shaders/fragmentshader.fs", "./temp/shaders/fragmentshader.fs");
} }
SceneViewer::~SceneViewer() { SceneViewer::~SceneViewer() {
@ -45,6 +43,17 @@ void SceneViewer::initializeGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Logger::info("Currently running on OpenGL version: " + std::string((const char*)glGetString(GL_VERSION))); Logger::info("Currently running on OpenGL version: " + std::string((const char*)glGetString(GL_VERSION)));
_vao.ensureInitialized();
Logger::info("Vertex Array Object initialized");
_shaderProgram.ensureInitialized();
Logger::info("Shader Program initialized");
VertexShader vertexShader("./temp/shaders/vertexshader.vs");
FragmentShader fragmentShader("./temp/shaders/fragmentshader.fs");
_shaderProgram.attachShader(vertexShader);
} }
void SceneViewer::resizeGL(int w, int h) { void SceneViewer::resizeGL(int w, int h) {
@ -67,7 +76,7 @@ void SceneViewer::paintGL() {
VertexShader vertexShader("./temp/shaders/vertexshader.vs"); VertexShader vertexShader("./temp/shaders/vertexshader.vs");
FragmentShader fragmentShader("./temp/shaders/fragmentshader.fs"); FragmentShader fragmentShader("./temp/shaders/fragmentshader.fs");
ShaderProgram shaderProgram(vertexShader, fragmentShader); ShaderProgram shaderProgram(vertexShader, fragmentShader);
shaderProgram.setActive(); shaderProgram.bind();
vao.bind(); vao.bind();
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawArrays(GL_TRIANGLES, 0, 3);
} }

View File

@ -6,6 +6,7 @@
#include <vector> #include <vector>
#include "shader.h"
#include "renderable.h" #include "renderable.h"
#include "vao.h" #include "vao.h"
#include "utils.h" #include "utils.h"
@ -16,6 +17,8 @@ class SceneViewer : public QOpenGLWidget, protected QOpenGLFunctions
private: private:
std::vector<Renderable> _objects; std::vector<Renderable> _objects;
ShaderProgram _shaderProgram = ShaderProgram::empty();
VertexArrayObject _vao = VertexArrayObject::empty();
public: public:
SceneViewer(QWidget* parent = 0); SceneViewer(QWidget* parent = 0);

View File

@ -102,7 +102,7 @@ void GeometryShader::compile(const std::string& source) {
} }
ShaderProgram::ShaderProgram() { ShaderProgram::ShaderProgram() {
_programId = OPENGL_EXTRA_FUNCTIONS->glCreateProgram(); _programId = 0;
} }
ShaderProgram::ShaderProgram(VertexShader vertexShader) { ShaderProgram::ShaderProgram(VertexShader vertexShader) {

View File

@ -8,7 +8,7 @@
class Shader { class Shader {
protected: protected:
unsigned int _shaderId = -1; unsigned int _shaderId = 0;
public: public:
Shader() {} Shader() {}
@ -27,7 +27,6 @@ inline void Shader::dispose() {
class VertexShader : public Shader { class VertexShader : public Shader {
public: public:
VertexShader() {}
VertexShader(const std::string& sourceFilePath); VertexShader(const std::string& sourceFilePath);
protected: protected:
@ -36,7 +35,6 @@ protected:
class FragmentShader : public Shader { class FragmentShader : public Shader {
public: public:
FragmentShader() {}
FragmentShader(const std::string& sourceFilePath); FragmentShader(const std::string& sourceFilePath);
protected: protected:
@ -45,7 +43,6 @@ protected:
class GeometryShader : public Shader { class GeometryShader : public Shader {
public: public:
GeometryShader() {}
GeometryShader(const std::string& sourceFilePath); GeometryShader(const std::string& sourceFilePath);
protected: protected:
@ -53,11 +50,18 @@ protected:
}; };
class ShaderProgram { class ShaderProgram {
public:
static ShaderProgram empty() {
return ShaderProgram();
}
private: private:
unsigned int _programId = 0; unsigned int _programId = 0;
public: private:
ShaderProgram(); ShaderProgram();
public:
ShaderProgram(VertexShader vertexShader); ShaderProgram(VertexShader vertexShader);
ShaderProgram(FragmentShader fragmentShader); ShaderProgram(FragmentShader fragmentShader);
ShaderProgram(GeometryShader geometryShader); ShaderProgram(GeometryShader geometryShader);
@ -67,19 +71,33 @@ public:
ShaderProgram(VertexShader vertexShader, FragmentShader fragmentShader, GeometryShader geometryShader); ShaderProgram(VertexShader vertexShader, FragmentShader fragmentShader, GeometryShader geometryShader);
public: public:
inline void setActive();
inline void setInactive();
inline unsigned int programId() const { return _programId; } inline unsigned int programId() const { return _programId; }
inline void attachShader(const Shader& shader) const;
inline void bind() const;
inline void unbind() const;
inline void dispose(); inline void dispose();
inline void ensureInitialized();
}; };
inline void ShaderProgram::setActive() { inline void ShaderProgram::attachShader(const Shader& shader) const {
if (_programId == 0) {
Logger::error("Attaching a shader to an invalid ShaderProgram");
return;
}
OPENGL_EXTRA_FUNCTIONS->glAttachShader(_programId, shader.shaderId());
}
inline void ShaderProgram::bind() const {
if (_programId == 0) {
Logger::error("Binding an invalid ShaderProgram");
return;
}
OPENGL_EXTRA_FUNCTIONS->glUseProgram(_programId); OPENGL_EXTRA_FUNCTIONS->glUseProgram(_programId);
} }
inline void ShaderProgram::setInactive() { inline void ShaderProgram::unbind() const {
OPENGL_EXTRA_FUNCTIONS->glUseProgram(0); OPENGL_EXTRA_FUNCTIONS->glUseProgram(0);
} }
@ -87,3 +105,9 @@ inline void ShaderProgram::dispose() {
OPENGL_EXTRA_FUNCTIONS->glDeleteProgram(_programId); OPENGL_EXTRA_FUNCTIONS->glDeleteProgram(_programId);
_programId = 0; _programId = 0;
} }
inline void ShaderProgram::ensureInitialized() {
if (_programId == 0) {
_programId = OPENGL_EXTRA_FUNCTIONS->glCreateProgram();
}
}

View File

@ -3,39 +3,20 @@
#include "vao.h" #include "vao.h"
VertexArrayObject::VertexArrayObject() { VertexArrayObject::VertexArrayObject() {
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id); _id = 0;
} }
VertexArrayObject::VertexArrayObject(const VertexBufferObject& vbo) : VertexArrayObject::VertexArrayObject(const VertexBufferObject& vbo) {
_vbo(vbo) {
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id); OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id()); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
} }
VertexArrayObject::VertexArrayObject(VertexBufferObject&& vbo) : VertexArrayObject::VertexArrayObject(const VertexBufferObject& vbo, const ElementBufferObject& ebo) {
_vbo(std::move(vbo)) {
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id); OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id()); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo.id());
}
VertexArrayObject::VertexArrayObject(const VertexBufferObject& vbo, const ElementBufferObject& ebo) :
_vbo(vbo), _ebo(ebo) {
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
VertexArrayObject::VertexArrayObject(VertexBufferObject&& vbo, ElementBufferObject&& ebo) :
_vbo(std::move(vbo)), _ebo(std::move(ebo)) {
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
} }

View File

@ -8,27 +8,30 @@
#include "logger.h" #include "logger.h"
class VertexArrayObject { class VertexArrayObject {
public:
static VertexArrayObject empty() {
return VertexArrayObject();
}
private: private:
unsigned int _id = 0; unsigned int _id = 0;
VertexBufferObject _vbo;
ElementBufferObject _ebo; private:
VertexArrayObject();
public: public:
VertexArrayObject();
VertexArrayObject(const VertexBufferObject& vbo); VertexArrayObject(const VertexBufferObject& vbo);
VertexArrayObject(VertexBufferObject&& vbo);
VertexArrayObject(const VertexBufferObject& vbo, const ElementBufferObject& ebo); VertexArrayObject(const VertexBufferObject& vbo, const ElementBufferObject& ebo);
VertexArrayObject(VertexBufferObject&& vbo, ElementBufferObject&& ebo);
inline unsigned int id() const { return _id; } inline unsigned int id() const { return _id; }
inline void bind() const; inline void bind() const;
inline void unbind() const; inline void unbind() const;
inline void dispose();
inline void ensureInitialized();
inline void bindVertexBufferObject(const VertexBufferObject& vbo); inline void bindVertexBufferObject(const VertexBufferObject& vbo);
inline void bindVertexBufferObject(VertexBufferObject&& vbo);
inline void bindElementBufferObject(const ElementBufferObject& ebo); inline void bindElementBufferObject(const ElementBufferObject& ebo);
inline void bindElementBufferObject(ElementBufferObject&& ebo);
inline void setVertexAttributePointer(unsigned int index, int size, unsigned int type, bool normalized, unsigned int stride, const void* pointer) const; inline void setVertexAttributePointer(unsigned int index, int size, unsigned int type, bool normalized, unsigned int stride, const void* pointer) const;
inline void enableVertexAttribute(unsigned int index) const; inline void enableVertexAttribute(unsigned int index) const;
@ -37,7 +40,7 @@ public:
inline void VertexArrayObject::bind() const { inline void VertexArrayObject::bind() const {
// Check is id is valid // Check is id is valid
if (_id == -1) { if (_id == 0) {
Logger::error("Binding an invalid VertexArrayObject"); Logger::error("Binding an invalid VertexArrayObject");
return; return;
} }
@ -48,31 +51,26 @@ inline void VertexArrayObject::unbind() const {
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
} }
inline void VertexArrayObject::bindVertexBufferObject(const VertexBufferObject& vbo) { inline void VertexArrayObject::dispose() {
_vbo = vbo; OPENGL_EXTRA_FUNCTIONS->glDeleteVertexArrays(1, &_id);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); _id = 0;
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
} }
inline void VertexArrayObject::bindVertexBufferObject(VertexBufferObject&& vbo) { inline void VertexArrayObject::ensureInitialized() {
_vbo = std::move(vbo); if (_id == 0) {
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
}
}
inline void VertexArrayObject::bindVertexBufferObject(const VertexBufferObject& vbo) {
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id()); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
} }
inline void VertexArrayObject::bindElementBufferObject(const ElementBufferObject& ebo) { inline void VertexArrayObject::bindElementBufferObject(const ElementBufferObject& ebo) {
_ebo = ebo;
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id()); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::bindElementBufferObject(ElementBufferObject&& ebo) {
_ebo = std::move(ebo);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
} }

View File

@ -1,32 +1,34 @@
#pragma once #pragma once
#include "vbo.h" #include "vbo.h"
#include "logger.h"
VertexBufferObject::VertexBufferObject() { VertexBufferObject::VertexBufferObject() {
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); _id = 0;
} }
VertexBufferObject::VertexBufferObject(const std::vector<Vertex>& vertices) : VertexBufferObject::VertexBufferObject(const std::vector<Vertex>& vertices) {
_vertices(vertices) {
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _id); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _id);
OPENGL_EXTRA_FUNCTIONS->glBufferData( OPENGL_EXTRA_FUNCTIONS->glBufferData(
GL_ARRAY_BUFFER, GL_ARRAY_BUFFER,
_vertices.size() * sizeof(Vertex), vertices.size() * sizeof(Vertex),
_vertices.data(), vertices.data(),
GL_STATIC_DRAW GL_STATIC_DRAW
); );
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
VertexBufferObject::VertexBufferObject(std::vector<Vertex>&& vertices) : void VertexBufferObject::setBuffer(const std::vector<Vertex>& vertices) {
_vertices(std::move(vertices)) { if (id == 0) {
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); Logger::error("Try to bind buffer to an uninitialized vertex buffer object");
return;
}
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _id); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _id);
OPENGL_EXTRA_FUNCTIONS->glBufferData( OPENGL_EXTRA_FUNCTIONS->glBufferData(
GL_ARRAY_BUFFER, GL_ARRAY_BUFFER,
_vertices.size() * sizeof(Vertex), vertices.size() * sizeof(Vertex),
_vertices.data(), vertices.data(),
GL_STATIC_DRAW GL_STATIC_DRAW
); );
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0);

View File

@ -6,21 +6,49 @@
#include "utils.h" #include "utils.h"
class VertexBufferObject { class VertexBufferObject {
public:
static VertexBufferObject empty() {
return VertexBufferObject();
}
private: private:
unsigned int _id = 0; unsigned int _id = 0;
std::vector<Vertex> _vertices;
private:
VertexBufferObject();
public: public:
VertexBufferObject();
VertexBufferObject(const std::vector<Vertex>& vertices); VertexBufferObject(const std::vector<Vertex>& vertices);
VertexBufferObject(std::vector<Vertex>&& vertices);
inline unsigned int id() const { return _id; } inline unsigned int id() const { return _id; }
inline std::vector<Vertex> vertices() const { return _vertices; }
inline void dispose() const; inline void bind() const;
inline void unbind() const;
inline void dispose();
inline void ensureInitialized();
void setBuffer(const std::vector<Vertex>& vertices);
}; };
inline void VertexBufferObject::dispose() const { inline void VertexBufferObject::bind() const {
OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id); if (_id == 0) {
Logger::error("Binding an invalid VertexBufferObject");
return;
}
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _id);
}
inline void VertexBufferObject::unbind() const {
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0);
}
inline void VertexBufferObject::dispose() {
OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id);
_id = 0;
}
inline void VertexBufferObject::ensureInitialized() {
if (_id == 0) {
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
}
} }