diff --git a/FinalProject/ebo.cpp b/FinalProject/ebo.cpp index 6f70f09..9996aaf 100644 --- a/FinalProject/ebo.cpp +++ b/FinalProject/ebo.cpp @@ -1 +1,38 @@ #pragma once + +#include "ebo.h" +#include "utils.h" + +ElementBufferObject::ElementBufferObject() { + OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); +} + +ElementBufferObject::ElementBufferObject(const std::vector& indices) : + _indices(indices) { + OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id); + OPENGL_EXTRA_FUNCTIONS->glBufferData( + GL_ELEMENT_ARRAY_BUFFER, + _indices.size() * sizeof(unsigned int), + _indices.data(), + GL_STATIC_DRAW + ); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} + +ElementBufferObject::ElementBufferObject(std::vector&& indices) : + _indices(std::move(indices)) { + OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id); + OPENGL_EXTRA_FUNCTIONS->glBufferData( + GL_ELEMENT_ARRAY_BUFFER, + _indices.size() * sizeof(unsigned int), + _indices.data(), + GL_STATIC_DRAW + ); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} + +inline void ElementBufferObject::dispose() const { + OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id); +} diff --git a/FinalProject/ebo.h b/FinalProject/ebo.h index 539ac14..5e847b1 100644 --- a/FinalProject/ebo.h +++ b/FinalProject/ebo.h @@ -5,13 +5,16 @@ class ElementBufferObject { private: - unsigned int _id; + unsigned int _id = 0; + std::vector _indices; public: - ElementBufferObject(std::vector indices); + ElementBufferObject(); + ElementBufferObject(const std::vector& indices); + ElementBufferObject(std::vector&& indices); inline unsigned int id() const { return _id; } - - inline void bind() const; - inline void unbind() const; + inline std::vector indices() const { return _indices; } + + inline void dispose() const; }; \ No newline at end of file diff --git a/FinalProject/model.cpp b/FinalProject/model.cpp index e64d2e2..38ccca0 100644 --- a/FinalProject/model.cpp +++ b/FinalProject/model.cpp @@ -20,7 +20,7 @@ void Model::loadModel(std::string path) { if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { Logger::error("Failed to load model: " + std::string(importer.GetErrorString())); - _status = ERROR; + _status = ERR; return; } _directory = path.substr(0, path.find_last_of('/')); diff --git a/FinalProject/model.h b/FinalProject/model.h index fbc8ff4..2e319d2 100644 --- a/FinalProject/model.h +++ b/FinalProject/model.h @@ -11,7 +11,7 @@ class Model { public: - enum MODELSTATUS { LOADING, LOADED, ERROR}; + enum MODELSTATUS { LOADING, LOADED, ERR }; private: std::vector _meshes; @@ -24,7 +24,7 @@ public: ~Model(); public: - inline MODELSTATUS isReady() const { return _status; } + inline MODELSTATUS status() const { return _status; } private: void loadModel(std::string path); diff --git a/FinalProject/texture.cpp b/FinalProject/texture.cpp index 8d166d5..b9ba746 100644 --- a/FinalProject/texture.cpp +++ b/FinalProject/texture.cpp @@ -1,6 +1,50 @@ #pragma once #include +#include +#include #include "texture.h" +#include "logger.h" +#include "utils.h" +QOpenGLContext* sharedContext; + +// Must set context before calling this function +Texture::Texture(TextureType type, std::string path) { + _type = type; + _path = path; + + OPENGL_FUNCTIONS->glGenTextures(1, &_id); + OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, _id); + + // Set the texture wrapping parameters + OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic wrapping method) + OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + // Set texture filtering parameters + OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + // Load image, create texture and generate mipmaps + int width, height, nrChannels; + unsigned char* data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0); + if (data) { + GLenum format; + if (nrChannels == 1) { + format = GL_RED; + } + else if (nrChannels == 3) { + format = GL_RGB; + } + else if (nrChannels == 4) { + format = GL_RGBA; + } + + OPENGL_FUNCTIONS->glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); + OPENGL_FUNCTIONS->glGenerateMipmap(GL_TEXTURE_2D); + } + else { + Logger::error("Failed to load texture: " + path); + } + stbi_image_free(data); +} \ No newline at end of file diff --git a/FinalProject/texture.h b/FinalProject/texture.h index 1da7b0f..52c222d 100644 --- a/FinalProject/texture.h +++ b/FinalProject/texture.h @@ -1,6 +1,7 @@ #pragma once #include +#include enum TextureType { DIFFUSE, SPECULAR }; diff --git a/FinalProject/utils.h b/FinalProject/utils.h index 74e0609..e51ff06 100644 --- a/FinalProject/utils.h +++ b/FinalProject/utils.h @@ -1,3 +1,10 @@ #pragma once +#include +#include +#include + #define offsetof(s,m) (size_t)&reinterpret_cast((((s *)0)->m)) + +#define OPENGL_FUNCTIONS QOpenGLContext::currentContext()->functions() +#define OPENGL_EXTRA_FUNCTIONS QOpenGLContext::currentContext()->extraFunctions() diff --git a/FinalProject/vao.cpp b/FinalProject/vao.cpp index 6f70f09..27fb6db 100644 --- a/FinalProject/vao.cpp +++ b/FinalProject/vao.cpp @@ -1 +1,107 @@ #pragma once + +#include + +#include "vao.h" +#include "utils.h" +#include "logger.h" + +VertexArrayObject::VertexArrayObject() { + OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id); +} + +VertexArrayObject::VertexArrayObject(const VertexBufferObject& vbo) : + _vbo(vbo) { + OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id()); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); +} + +VertexArrayObject::VertexArrayObject(VertexBufferObject&& vbo) : + _vbo(std::move(vbo)) { + OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id()); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); +} + +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); +} + +inline void VertexArrayObject::setActive() const { + // Check is id is valid + if (_id == -1) { + Logger::error("Binding an invalid VertexArrayObject"); + return; + } + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); +} + +inline void VertexArrayObject::setInactive() const { + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); +} + +inline void VertexArrayObject::bindVertexBufferObject(const VertexBufferObject& vbo) { + _vbo = vbo; + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id()); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); +} + +inline void VertexArrayObject::bindVertexBufferObject(VertexBufferObject&& vbo) { + _vbo = std::move(vbo); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id()); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); +} + +inline void VertexArrayObject::bindElementBufferObject(const ElementBufferObject& ebo) { + _ebo = ebo; + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_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); +} + +inline void VertexArrayObject::setVertexAttributePointer( + unsigned int index, + int size, + unsigned int type, + bool normalized, + unsigned int stride, + const void* pointer +) const { + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id); + OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer( + index, + size, + type, + normalized, + stride, + pointer + ); + OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(index); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); +} \ No newline at end of file diff --git a/FinalProject/vao.h b/FinalProject/vao.h index 3db6aaa..668490b 100644 --- a/FinalProject/vao.h +++ b/FinalProject/vao.h @@ -7,20 +7,28 @@ class VertexArrayObject { private: - unsigned int _id; + unsigned int _id = 0; VertexBufferObject _vbo; ElementBufferObject _ebo; public: - VertexArrayObject(VertexBufferObject vbo); - VertexArrayObject(std::vector vertices); - VertexArrayObject(VertexBufferObject vbo, ElementBufferObject ebo); - VertexArrayObject(std::vector vertices, std::vector indices); + VertexArrayObject(); + VertexArrayObject(const VertexBufferObject& vbo); + VertexArrayObject(VertexBufferObject&& vbo); + VertexArrayObject(const VertexBufferObject& vbo, const ElementBufferObject& ebo); + VertexArrayObject(VertexBufferObject&& vbo, ElementBufferObject&& ebo); inline unsigned int id() const { return _id; } inline void setActive() const; inline void setInactive() const; - void setVertexAttributePointer(unsigned int index, int size, unsigned int type, bool normalized, unsigned int stride, const void* pointer) const; + inline void bindVertexBufferObject(const VertexBufferObject& vbo); + inline void bindVertexBufferObject(VertexBufferObject&& vbo); + 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 enableVertexAttribute(unsigned int index) const; + inline void disableVertexAttribute(unsigned int index) const; }; \ No newline at end of file diff --git a/FinalProject/vbo.cpp b/FinalProject/vbo.cpp index 6f70f09..115ed72 100644 --- a/FinalProject/vbo.cpp +++ b/FinalProject/vbo.cpp @@ -1 +1,38 @@ #pragma once + +#include "vbo.h" +#include "utils.h" + +VertexBufferObject::VertexBufferObject() { + OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); +} + +VertexBufferObject::VertexBufferObject(const std::vector& vertices) : + _vertices(vertices) { + OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _id); + OPENGL_EXTRA_FUNCTIONS->glBufferData( + GL_ARRAY_BUFFER, + _vertices.size() * sizeof(Vertex), + _vertices.data(), + GL_STATIC_DRAW + ); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +VertexBufferObject::VertexBufferObject(std::vector&& vertices) : + _vertices(std::move(vertices)) { + OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _id); + OPENGL_EXTRA_FUNCTIONS->glBufferData( + GL_ARRAY_BUFFER, + _vertices.size() * sizeof(Vertex), + _vertices.data(), + GL_STATIC_DRAW + ); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +inline void VertexBufferObject::dispose() const { + OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id); +} diff --git a/FinalProject/vbo.h b/FinalProject/vbo.h index cc78123..69230ff 100644 --- a/FinalProject/vbo.h +++ b/FinalProject/vbo.h @@ -6,14 +6,16 @@ class VertexBufferObject { private: - unsigned int _id; + unsigned int _id = 0; + std::vector _vertices; public: - VertexBufferObject(std::vector vertices); - ~VertexBufferObject(); + VertexBufferObject(); + VertexBufferObject(const std::vector& vertices); + VertexBufferObject(std::vector&& vertices); inline unsigned int id() const { return _id; } - - inline void bind() const; - inline void unbind() const; + inline std::vector vertices() const { return _vertices; } + + inline void dispose() const; }; \ No newline at end of file