diff --git a/source/engine/graphics/back.hpp b/source/engine/graphics/back.hpp index d56b9d070e92a6b2d833a34d51d7997b79128606..9c564d61a44b67e11d50b8cae21e6a91682d5985 100644 --- a/source/engine/graphics/back.hpp +++ b/source/engine/graphics/back.hpp @@ -10,10 +10,11 @@ #include "back/cameras/View.hpp" #include "back/cameras/Walkaround.hpp" -#include "back/geometry/Transform.hpp" +#include "back/geometry/Transformable.hpp" #include "back/shaders/Program.hpp" #include "back/shaders/Source.hpp" #include "back/shaders/UniformObject.hpp" -#include "back/textures/Shared_Material.hpp" \ No newline at end of file +#include "back/textures/TextureBuffer.hpp" +#include "back/textures/Texture.hpp" \ No newline at end of file diff --git a/source/engine/graphics/back/buffers/FrameBuffer.cpp b/source/engine/graphics/back/buffers/FrameBuffer.cpp index 72a54b983eab238ac786f7020aa7145c82f0404e..e2f823469750d665641988afe326c90020acb570 100644 --- a/source/engine/graphics/back/buffers/FrameBuffer.cpp +++ b/source/engine/graphics/back/buffers/FrameBuffer.cpp @@ -23,7 +23,6 @@ namespace megu { if(!this->usable()) { throw std::runtime_error("Incomplete FrameBuffer."); } - } FrameBuffer::~FrameBuffer() { diff --git a/source/engine/graphics/back/geometry/Counter.hpp b/source/engine/graphics/back/geometry/Counter.hpp deleted file mode 100644 index 84de6dc30ece6b926562d7469d2b9e647fb3c465..0000000000000000000000000000000000000000 --- a/source/engine/graphics/back/geometry/Counter.hpp +++ /dev/null @@ -1,93 +0,0 @@ -#pragma once - -#include <list> -#include <typeindex> - -namespace megu { - template <typename T> - class Counter { - public: - template <T U> - static std::list<T> Count() { - std::list<T> list; - list.push_back(U); - return list; - } - - template <T U> - static std::list<T> Count(std::list<T> & list) { - list.push_back(U); - return list; - } - - template <T U, T V, T ... L> - static std::list<T> Count() { - std::list<T> list; - list.push_back(U); - return Counter<T>::Count<V, L...>(list); - } - - template <T U, T V, T ... L> - static std::list<T> Count(std::list<T> & list) { - list.push_back(U); - return Counter<T>::Count<V, L...>(list); - } - }; - - class Type_Counter { - public: - template <class T> - static std::list<std::type_index> Count() { - std::list<std::type_index> list; - list.push_back(typeid(T)); - return list; - } - - template <class T> - static std::list<std::type_index> Count(std::list<std::type_index> & list) { - list.push_back(typeid(T)); - return list; - } - - template <class T, class U, class ... V> - static std::list<std::type_index> Count() { - std::list<std::type_index> list; - list.push_back(typeid(T)); - return Type_Counter::Count<U, V...>(list); - } - - template <class T, class U, class ... V> - static std::list<std::type_index> Count(std::list<std::type_index> & list) { - list.push_back(typeid(T)); - return Type_Counter::Count<U, V...>(list); - } - }; - - template <typename T> - class Sum { - public: - template <T U> - static constexpr size_t sum() { - size_t sum = U; - return sum; - } - - template <T U> - static constexpr size_t sum(size_t & sum) { - sum += U; - return sum; - } - - template <T U, T V, T ... S> - static constexpr size_t sum() { - size_t sum = U; - return Sum::sum<V, S...>(sum); - } - - template <T U, T V, T ... S> - static constexpr size_t sum(size_t & sum) { - sum += U; - return Sum::sum<V, S...>(sum); - } - }; -} \ No newline at end of file diff --git a/source/engine/graphics/back/geometry/Geometry.cpp b/source/engine/graphics/back/geometry/Geometry.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26a8f590e14f3b6c3d52d9a248ad3ae7c0da6d62 --- /dev/null +++ b/source/engine/graphics/back/geometry/Geometry.cpp @@ -0,0 +1,19 @@ +#include "Geometry.hpp" + +namespace megu { + template <Primitive P> + Geometry<P>::Geometry(const Layout_Initializer & layout) + : _layout(layout) {} + + template <Primitive P> + Geometry_Indiced<P>::Geometry_Indiced(const Layout_Initializer & layout) + : Geometry<P>(layout) {} + + template <class T, Primitive P> + Static_Geometry<T,P>::Static_Geometry(const Layout_Initializer & layout) + : Geometry<P>(layout) {} + + template <class T, Primitive P> + Static_Geometry_Indiced<T, P>:: Static_Geometry_Indiced(const Layout_Initializer & layout) + : Geometry<P>(layout) {} +} \ No newline at end of file diff --git a/source/engine/graphics/back/geometry/Geometry.hpp b/source/engine/graphics/back/geometry/Geometry.hpp index c80bef27800212ddca6a97dba9b6f6c8c03c3fb5..0d51a917e8a7348c26f1b1be46f86ecaf15440f6 100644 --- a/source/engine/graphics/back/geometry/Geometry.hpp +++ b/source/engine/graphics/back/geometry/Geometry.hpp @@ -5,60 +5,63 @@ #include <array> #include <thread> -#include "Counter.hpp" +#include <GL/glew.h> + #include "../buffers/Layout.hpp" namespace megu { - using Primitive = uint32_t; + using Vertices = std::vector<float>; + using Elements = std::vector<unsigned int>; - using tVertices = std::vector<float>; - using tIndices = std::vector<unsigned int>; + enum Primitive { + TRIANGLES = GL_TRIANGLES, + QUADS = GL_QUADS, + }; - class LayoutStorage { - public: - LayoutStorage() = delete; - LayoutStorage(const Layout & layout) : _layout(layout) {} - ~LayoutStorage() = default; + template <Primitive P> + class Geometry { + public: + Geometry() = delete; + Geometry(const Layout_Initializer &); + ~Geometry() = default; - inline const Layout & layout() const {return this->_layout;} + virtual const Vertices & vertices() const = 0; private: - const Layout _layout; - }; - + Layout _layout; + }; + template <Primitive P> - class Geometry : public LayoutStorage { + class Geometry_Indiced : virtual public Geometry<P> { public: - Geometry() = delete; - Geometry(const Layout & layout) : LayoutStorage(layout) {} - ~Geometry() = default; + Geometry_Indiced() = delete; + Geometry_Indiced(const Layout_Initializer &); + ~Geometry_Indiced() = default; - inline static Primitive Primitive() {return P;} - - virtual void retrieve(tVertices &, tIndices &) const = 0; + virtual const Elements & elements() const = 0; }; template <class T, Primitive P> - class Static_Geometry : public Geometry<P> { + class Static_Geometry : virtual public Geometry<P> { public: Static_Geometry() = delete; - Static_Geometry(const Layout & layout) : Geometry<P>(layout) {} + Static_Geometry(const Layout_Initializer &); ~Static_Geometry() = default; - virtual void retrieve(tVertices & vertices, tIndices & indices) const final { - std::thread tVertices([&vertices]() { - vertices = Static_Geometry<T, P>::Vertices(); - }); - - std::thread tIndices([&indices]() { - indices = Static_Geometry<T, P>::Indices(); - }); + const Vertices & vertices() const final override {return Static_Geometry<T, P>::Vertices();} + static const Vertices & Vertices() {return T::Vertices();} + }; - tVertices.join(); - tIndices.join(); - } + template <class T, Primitive P> + class Static_Geometry_Indiced : public Geometry_Indiced<P>, public Static_Geometry<T, P> { + public: + Static_Geometry_Indiced() = delete; + Static_Geometry_Indiced(const Layout_Initializer &); + ~Static_Geometry_Indiced() = default; - inline static tVertices Vertices() {return T::Vertices();} - inline static tIndices Indices() {return T::Indices();} + const Vertices & elements() const final override {return Static_Geometry_Indiced<T, P>::Elements();} + static const Vertices & Elements() {return T::Elements();} }; + + } \ No newline at end of file diff --git a/source/engine/graphics/back/geometry/Transform.hpp b/source/engine/graphics/back/geometry/Transform.hpp deleted file mode 100644 index 899871c599fd1e83a26341e53a4e6b88eb2667da..0000000000000000000000000000000000000000 --- a/source/engine/graphics/back/geometry/Transform.hpp +++ /dev/null @@ -1,72 +0,0 @@ -#pragma once - -#include <glm/vec3.hpp> -#include <glm/mat4x4.hpp> - -#include "Transformable.hpp" - -namespace megu { - class Transform : public Transformable { - public: - enum class Axis : uint8_t { - X = 1, - Y = 2, - Z = 4 - }; - - inline friend uint8_t operator&(Axis a, Axis b) {return static_cast<uint8_t>(a) & static_cast<uint8_t>(b);} - inline friend uint8_t operator|(Axis a, Axis b) {return static_cast<uint8_t>(a) | static_cast<uint8_t>(b);} - inline friend uint8_t operator^(Axis a, Axis b) {return static_cast<uint8_t>(a) ^ static_cast<uint8_t>(b);} - - Transform(const glm::mat4 & = glm::mat4(1.f)); - Transform(const glm::vec3 &, const glm::vec3 & = glm::vec3(0.f)); - Transform(const Transform &); - Transform(const Transform &&); - ~Transform() = default; - - inline const glm::vec3 & position() const {return this->_position;} - inline const glm::vec3 & rotation() const {return this->_rotation;} - inline const glm::vec3 & scaling() const {return this->_scaling;} - inline const glm::vec3 & origine() const {return this->_origine;} - - inline glm::vec3 & position() {return this->_position;} - inline glm::vec3 & rotation() {return this->_rotation;} - inline glm::vec3 & scaling() {return this->_scaling;} - inline glm::vec3 & origine() {return this->_origine;} - - inline float x() const {return this->_position.x;} - inline float y() const {return this->_position.y;} - inline float z() const {return this->_position.z;} - - inline float pitch() const {return this->_rotation.x;} - inline float yaw() const {return this->_rotation.y;} - inline float roll() const {return this->_rotation.z;} - - void setPosition(const glm::vec3 &); - void setRotation(float, Axis); - void setScaling(const glm::vec3 &); - - inline void setMatrix(const glm::mat4 & matrix) {this->_model = matrix;} - inline void setPosition(float x, float y, float z = 0.f) {this->setPosition({x, y, z});} - inline void setScaling(float x, float y, float z = 0.f) {this->setScaling({x, y, z});} - inline void setOrigine(const glm::vec3 & position) {this->_origine = position;} - - void move(const glm::vec3 &); - void rotate(float, Axis); - void scale(const glm::vec3 &); - - inline void translate(const glm::vec3 & translation) {this->move(translation);} - inline void move(float x, float y, float z = 0.f) {this->move({x, y, z});} - inline void scale(float x, float y, float z = 0.f) {this->scale({x, y, z});} - - virtual const glm::mat4x4 & model() const; - - private: - mutable glm::mat4 _model; - glm::vec3 _position; - glm::vec3 _rotation; - glm::vec3 _scaling; - glm::vec3 _origine; - mutable bool _updated; - }; -} \ No newline at end of file diff --git a/source/engine/graphics/back/geometry/Transform.cpp b/source/engine/graphics/back/geometry/Transformable.cpp similarity index 76% rename from source/engine/graphics/back/geometry/Transform.cpp rename to source/engine/graphics/back/geometry/Transformable.cpp index b54248611b1b348844f0b16e45d8b107caed3596..482a57ef6108f35281fcafc70eed8366e504ab7c 100644 --- a/source/engine/graphics/back/geometry/Transform.cpp +++ b/source/engine/graphics/back/geometry/Transformable.cpp @@ -1,11 +1,11 @@ -#include "Transform.hpp" +#include "Transformable.hpp" #include <glm/gtc/matrix_transform.hpp> #include <iostream> namespace megu { - Transform::Transform(const glm::mat4x4 & matrix) + Transformable::Transformable(const glm::mat4x4 & matrix) : _model(matrix), _position(0.f), _rotation(0.f), @@ -13,7 +13,7 @@ namespace megu { _origine(0.f), _updated(true) {} - Transform::Transform(const glm::vec3 & position, const glm::vec3 & origine) + Transformable::Transformable(const glm::vec3 & position, const glm::vec3 & origine) : _model(1.0f), _position(position), _rotation(0.f), @@ -21,7 +21,7 @@ namespace megu { _origine(origine), _updated(false) {} - Transform::Transform(const Transform & src) + Transformable::Transformable(const Transformable & src) : _model(src._model), _position(src._position), _rotation(src._rotation), @@ -29,7 +29,7 @@ namespace megu { _origine(src._origine), _updated(false) {} - Transform::Transform(const Transform && src) + Transformable::Transformable(const Transformable && src) : _model(std::move(src._model)), _position(std::move(src._position)), _rotation(std::move(src._rotation)), @@ -37,12 +37,12 @@ namespace megu { _origine(std::move(src._origine)), _updated(false) {} - void Transform::setPosition(const glm::vec3 & position) { + void Transformable::setPosition(const glm::vec3 & position) { this->_position = position; this->_updated = true; } - void Transform::setRotation(float angle, Axis axis) { + void Transformable::setRotation(float angle, Axis axis) { if((Axis::X & axis) != 0) { this->_rotation.x = angle; } @@ -58,17 +58,17 @@ namespace megu { this->_updated = true; } - void Transform::setScaling(const glm::vec3 & scaling) { + void Transformable::setScaling(const glm::vec3 & scaling) { this->_scaling = scaling; this->_updated = true; } - void Transform::move(const glm::vec3 & position) { + void Transformable::move(const glm::vec3 & position) { this->_position += position; this->_updated = true; } - void Transform::rotate(float angle, Axis axis) { + void Transformable::rotate(float angle, Axis axis) { if((Axis::X & axis) != 0) { this->_rotation.x += angle; } @@ -84,12 +84,12 @@ namespace megu { this->_updated = true; } - void Transform::scale(const glm::vec3 & scaling) { + void Transformable::scale(const glm::vec3 & scaling) { this->_scaling += scaling; this->_updated = true; } - const glm::mat4x4 & Transform::model() const { + const glm::mat4x4 & Transformable::model() const { if(this->_updated) { glm::mat4x4 model = glm::mat4x4(1.f); diff --git a/source/engine/graphics/back/geometry/Transformable.hpp b/source/engine/graphics/back/geometry/Transformable.hpp index 8855ccf992b9880eb091afb70ae1d637dc2cc603..ce39c3be765e3cd50960b1d48e5e0743703fefb0 100644 --- a/source/engine/graphics/back/geometry/Transformable.hpp +++ b/source/engine/graphics/back/geometry/Transformable.hpp @@ -1,10 +1,70 @@ #pragma once +#include <glm/vec3.hpp> #include <glm/mat4x4.hpp> namespace megu { class Transformable { - public: - virtual const glm::mat4x4 & model() const = 0; + public: + enum class Axis : uint8_t { + X = 1, + Y = 2, + Z = 4 + }; + + inline friend uint8_t operator&(Axis a, Axis b) {return static_cast<uint8_t>(a) & static_cast<uint8_t>(b);} + inline friend uint8_t operator|(Axis a, Axis b) {return static_cast<uint8_t>(a) | static_cast<uint8_t>(b);} + inline friend uint8_t operator^(Axis a, Axis b) {return static_cast<uint8_t>(a) ^ static_cast<uint8_t>(b);} + + Transformable(const glm::mat4 & = glm::mat4(1.f)); + Transformable(const glm::vec3 &, const glm::vec3 & = glm::vec3(0.f)); + Transformable(const Transformable &); + Transformable(const Transformable &&); + ~Transformable() = default; + + inline const glm::vec3 & position() const {return this->_position;} + inline const glm::vec3 & rotation() const {return this->_rotation;} + inline const glm::vec3 & scaling() const {return this->_scaling;} + inline const glm::vec3 & origine() const {return this->_origine;} + + inline glm::vec3 & position() {return this->_position;} + inline glm::vec3 & rotation() {return this->_rotation;} + inline glm::vec3 & scaling() {return this->_scaling;} + inline glm::vec3 & origine() {return this->_origine;} + + inline float x() const {return this->_position.x;} + inline float y() const {return this->_position.y;} + inline float z() const {return this->_position.z;} + + inline float pitch() const {return this->_rotation.x;} + inline float yaw() const {return this->_rotation.y;} + inline float roll() const {return this->_rotation.z;} + + void setPosition(const glm::vec3 &); + void setRotation(float, Axis); + void setScaling(const glm::vec3 &); + + inline void setMatrix(const glm::mat4 & matrix) {this->_model = matrix;} + inline void setPosition(float x, float y, float z = 0.f) {this->setPosition({x, y, z});} + inline void setScaling(float x, float y, float z = 0.f) {this->setScaling({x, y, z});} + inline void setOrigine(const glm::vec3 & position) {this->_origine = position;} + + void move(const glm::vec3 &); + void rotate(float, Axis); + void scale(const glm::vec3 &); + + inline void translate(const glm::vec3 & translation) {this->move(translation);} + inline void move(float x, float y, float z = 0.f) {this->move({x, y, z});} + inline void scale(float x, float y, float z = 0.f) {this->scale({x, y, z});} + + virtual const glm::mat4x4 & model() const; + + private: + mutable glm::mat4 _model; + glm::vec3 _position; + glm::vec3 _rotation; + glm::vec3 _scaling; + glm::vec3 _origine; + mutable bool _updated; }; } \ No newline at end of file diff --git a/source/engine/graphics/back/textures/Material.cpp b/source/engine/graphics/back/textures/Material.cpp deleted file mode 100644 index 962db762f60a6b44823ec543bef7fe109357056b..0000000000000000000000000000000000000000 --- a/source/engine/graphics/back/textures/Material.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "Material.hpp" - -#include <exception> - -#define STB_IMAGE_IMPLEMENTATION -#include <stb/image/stb_image.h> - -namespace megu { - Material::Material() - : Texture(0, GL_TEXTURE_2D), _ressource("./") { - - } - - Material::Material(const std::string & path, bool invert) - : Texture(), _ressource() { - this->load(path, invert); - } - - void Material::load(const std::string & path, bool invert) { - this->_ressource = std::filesystem::canonical(path); - if(std::filesystem::is_directory(this->_ressource) || !std::filesystem::exists(this->_ressource)) { - throw std::runtime_error("Cannot load ressource !"); - } - - int width = 0, height = 0, nrChannels = 0; - stbi_set_flip_vertically_on_load(invert); - unsigned char * data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0); - - Texture::Format format = Texture::RGB; - switch (nrChannels) { - case 4: - format = Texture::RGBA; - break; - - case 3: - format = Texture::RGB; - break; - - default: - format = Texture::RGB; - break; - } - - this->store(width, height, data, format); - stbi_image_free(data); - stbi_set_flip_vertically_on_load(false); - } -} \ No newline at end of file diff --git a/source/engine/graphics/back/textures/Material.hpp b/source/engine/graphics/back/textures/Material.hpp deleted file mode 100644 index 37fd87267d9acc1f9cce0251820d8b2718a663f8..0000000000000000000000000000000000000000 --- a/source/engine/graphics/back/textures/Material.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include <filesystem> -#include <string> - -#include "Texture.hpp" - -namespace megu { - class Material : public Texture { - public: - Material(); - Material(const Material &) = delete; - Material(const std::string &, bool = false); - Material operator=(const Material &) = delete; - - inline const std::filesystem::path & ressource() const {return this->_ressource;} - - void load(const std::string &, bool = false); - - private: - std::filesystem::path _ressource; - }; -} \ No newline at end of file diff --git a/source/engine/graphics/back/textures/Shared_Material.cpp b/source/engine/graphics/back/textures/Shared_Material.cpp deleted file mode 100644 index 6b89740c30c3ef7b0f4c0e04d1437b9d21fca3af..0000000000000000000000000000000000000000 --- a/source/engine/graphics/back/textures/Shared_Material.cpp +++ /dev/null @@ -1,148 +0,0 @@ -#include "Shared_Material.hpp" - -namespace megu { - - std::map<std::filesystem::path, std::set<std::reference_wrapper<Shared_Material>, std::less<Shared_Material>>> Shared_Material::_shared = std::map<std::filesystem::path, std::set<std::reference_wrapper<Shared_Material>, std::less<Shared_Material>>>(); - - Shared_Material::Shared_Material() - : _material(nullptr) { - - } - - Shared_Material::Shared_Material(const std::string & path, bool invert) - : _material(nullptr) { - if(!path.empty() && Shared_Material::_shared.contains(path)) { - this->_material = Shared_Material::_shared.at(path).begin()->get()._material; - Shared_Material::_shared.at(path).insert(*this); - } - else { - this->_material = std::make_shared<Material>(path, invert); - Shared_Material::_shared.insert(std::pair<std::filesystem::path, std::set<std::reference_wrapper<Shared_Material>, std::less<Shared_Material>>>(path, {*this})); - } - } - - Shared_Material::Shared_Material(Material & material) - : _material(nullptr) { - if(!material.ressource().empty() && Shared_Material::_shared.contains(material.ressource())) { - this->_material = Shared_Material::_shared.at(material.ressource()).begin()->get()._material; - Shared_Material::_shared.at(material.ressource()).insert(*this); - } - else { - this->_material = std::shared_ptr<Material>(&material); - Shared_Material::_shared.insert(std::pair<std::filesystem::path, std::set<std::reference_wrapper<Shared_Material>, std::less<Shared_Material>>>(material.ressource(), {*this})); - } - } - - Shared_Material::~Shared_Material() { - if(Shared_Material::_shared.contains(this->_material->ressource())) { - Shared_Material::_shared.at(this->_material->ressource()).erase(*this); - if(Shared_Material::_shared.at(this->_material->ressource()).empty()) { - Shared_Material::_shared.erase(this->_material->ressource()); - } - } - } - - const Material & Shared_Material::get() const { - return *this->_material.get(); - } - - GLuint Shared_Material::identifier() const { - return this->_material->identifier(); - } - - GLuint Shared_Material::format() const { - return this->_material->type(); - } - - uint16_t Shared_Material::width() const { - return this->_material->width(); - } - - uint16_t Shared_Material::height() const { - return this->_material->height(); - } - - const std::filesystem::path & Shared_Material::path() const { - return this->_material->ressource(); - } - - void Shared_Material::bind() const { - this->_material->bind(); - } - - void Shared_Material::bind(uint32_t slot) const { - this->_material->bind(slot); - } - - void Shared_Material::load(const std::string & path, bool flip_y) { - if(!this->_material->ressource().empty()) { - Shared_Material::_shared.at(this->_material->ressource()).erase(*this); - if(Shared_Material::_shared.at(this->_material->ressource()).empty()) { - Shared_Material::_shared.erase(this->_material->ressource()); - } - } - - if(Shared_Material::_shared.contains(path)) { - this->_material = Shared_Material::_shared.at(path).begin()->get()._material; - Shared_Material::_shared.at(path).insert(*this); - } - else { - this->_material->load(path, flip_y); - Shared_Material::_shared.insert(std::pair<std::filesystem::path, std::set<std::reference_wrapper<Shared_Material>, std::less<Shared_Material>>>(path, {*this})); - } - } - - void Shared_Material::load(Material & material) { - if(!material.ressource().empty() && Shared_Material::_shared.contains(material.ressource())) { - this->_material = Shared_Material::_shared.at(material.ressource()).begin()->get()._material; - Shared_Material::_shared.at(material.ressource()).insert(*this); - } - else { - this->_material = std::shared_ptr<Material>(&material); - } - } - - bool Shared_Material::valid() const { - return this->empty() ? false : this->_material->valid(); - } - - bool Shared_Material::operator==(const Shared_Material & material) const { - return this->get() == material.get(); - } - - bool Shared_Material::operator!=(const Shared_Material & material) const { - return this->get() != material.get(); - } - - bool Shared_Material::operator>=(const Shared_Material & material) const { - return this->get() >= material.get(); - } - - bool Shared_Material::operator<=(const Shared_Material & material) const { - return this->get() <= material.get(); - } - - bool Shared_Material::operator>(const Shared_Material & material) const { - return this->get() > material.get(); - } - - bool Shared_Material::operator<(const Shared_Material & material) const { - return this->get() < material.get(); - } - - Material * Shared_Material::operator->() const { - return this->_material.get(); - } - - Material & Shared_Material::operator*() const { - return *this->_material.get(); - } - - std::strong_ordering Shared_Material::operator<=>(const Shared_Material & material) const { - return this->get() <=> material.get(); - } - - size_t Shared_Material::Total() { - return Shared_Material::_shared.size(); - } -} \ No newline at end of file diff --git a/source/engine/graphics/back/textures/Shared_Material.hpp b/source/engine/graphics/back/textures/Shared_Material.hpp deleted file mode 100644 index 7c85f670174b6ed3599577044a7f45279c29905a..0000000000000000000000000000000000000000 --- a/source/engine/graphics/back/textures/Shared_Material.hpp +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - -#include <memory> -#include <map> -#include <set> -#include <unordered_set> -#include <filesystem> - -#include "Material.hpp" - -namespace megu { - class Shared_Material { - public: - Shared_Material(); - Shared_Material(const std::string &, bool = false); - Shared_Material(Material &); - virtual ~Shared_Material(); - - const Material & get() const; - - GLuint identifier() const; - GLuint format() const; - - uint16_t width() const; - uint16_t height() const; - - const std::filesystem::path & path() const; - - void bind() const; - void bind(uint32_t slot) const; - - void load(const std::string &, bool=false); - void load(Material &); - - bool valid() const; - inline bool empty() const {return this->_material.get() != nullptr;} - - bool operator==(const Shared_Material &) const; - bool operator!=(const Shared_Material &) const; - bool operator>=(const Shared_Material &) const; - bool operator<=(const Shared_Material &) const; - bool operator>(const Shared_Material &) const; - bool operator<(const Shared_Material &) const; - - Material * operator->() const; - Material & operator*() const; - - std::strong_ordering operator<=>(const Shared_Material &) const; - - static size_t Total(); - - private: - std::shared_ptr<Material> _material; - static std::map<std::filesystem::path, std::set<std::reference_wrapper<Shared_Material>, std::less<Shared_Material>>> _shared; - - }; -} \ No newline at end of file diff --git a/source/engine/graphics/back/textures/Texture.cpp b/source/engine/graphics/back/textures/Texture.cpp index d1b0a30287fb0aac7259aa1adf22c7e035217298..e47163a402c3bdfd815e8ce830234b613f67baed 100644 --- a/source/engine/graphics/back/textures/Texture.cpp +++ b/source/engine/graphics/back/textures/Texture.cpp @@ -43,7 +43,7 @@ namespace megu { return height; } - void Texture::changeTextureSlot(GLuint slot) { + void Texture::changeSlot(GLuint slot) { this->_slot = slot; } @@ -70,7 +70,7 @@ namespace megu { glTexParameteri(this->_type, GL_TEXTURE_MAG_FILTER, state ? GL_LINEAR : GL_NEAREST); } - void Texture::store(GLsizei width, GLsizei height, const void * data, GLuint format) { + void Texture::store(GLsizei width, GLsizei height, const GLubyte * data, GLuint format) { if(this->valid()) { glDeleteTextures(1, &this->_id); glGenTextures(1, &this->_id); @@ -79,10 +79,19 @@ namespace megu { glBindTexture(this->_type, this->_id); glTexImage2D(this->_type, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); glGenerateMipmap(this->_type); + this->setWraping(CLAMP_TO_BORDER, S | T ); this->setSmoothing(false); } + void Texture::store(const TextureBuffer & texture) { + if(texture.empty()) { + throw std::runtime_error("Cannot store texture."); + } + + this->store(texture.width(), texture.height(), texture.data_ptr(), texture.format()); + } + bool Texture::valid() const { return this->_id != 0; } diff --git a/source/engine/graphics/back/textures/Texture.hpp b/source/engine/graphics/back/textures/Texture.hpp index 80e8b1b0c56762a54cd038065ef2d351ef43894e..55f70524c6e2c60784ee174e6e0a8f02fc6bda27 100644 --- a/source/engine/graphics/back/textures/Texture.hpp +++ b/source/engine/graphics/back/textures/Texture.hpp @@ -3,6 +3,8 @@ #include <GL/glew.h> #include <compare> +#include "TextureBuffer.hpp" + namespace megu { class Texture { public: @@ -43,11 +45,12 @@ namespace megu { inline GLuint type() const {return this->_type;} inline GLuint identifier() const {return this->_id;} - void changeTextureSlot(GLuint); + void changeSlot(GLuint); void setWraping(Wraping, uint32_t = S | T); void setSmoothing(bool); - void store(GLsizei, GLsizei, const void *, GLuint = RGBA); + void store(GLsizei, GLsizei, const GLubyte *, GLuint = RGBA); + void store(const TextureBuffer &); bool valid() const; bool operator==(const Texture &) const; diff --git a/source/engine/graphics/back/textures/TextureBuffer.cpp b/source/engine/graphics/back/textures/TextureBuffer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c7e3f521030a8a2fbd45011825d3215a48e82de --- /dev/null +++ b/source/engine/graphics/back/textures/TextureBuffer.cpp @@ -0,0 +1,81 @@ +#include "TextureBuffer.hpp" + +#define STB_IMAGE_IMPLEMENTATION +#include <stb/image/stb_image.h> + +#include <iostream> + +namespace megu { + TextureBuffer::TextureBuffer() + : _width(0), _height(0), _format(GL_RED) {} + + TextureBuffer::TextureBuffer(GLuint width, GLuint height, GLubyte * data, GLenum format) + : _data(data, data + (width * height)), _width(width), _height(height), _format(format) {} + + TextureBuffer::TextureBuffer(const TextureBuffer & src) + : _data(src._data), _width(src._width), _height(src._height) {} + + TextureBuffer & TextureBuffer::operator=(const TextureBuffer & src) { + this->_data = src._data; + this->_width = src._width; + this->_height = src._height; + + return *this; + } + + GLuint TextureBuffer::getPixel(GLuint x, GLuint y) const { + return this->_data.at((this->_width * y) + x); + } + + void TextureBuffer::load(const GLubyte * data, size_t size) { + this->_data = std::vector<GLubyte>(data, data + size); + } + + void TextureBuffer::load(const std::filesystem::path & path, bool flip) { + if(std::filesystem::is_directory(path) || !std::filesystem::exists(path)) { + throw std::runtime_error("Cannot load file : \"" + path.string() + "\""); + } + + int width = 0, height = 0, nrChannels = 0; + stbi_set_flip_vertically_on_load(flip); + GLubyte * data = stbi_load(path.string().c_str(), &width, &height, &nrChannels, 0); + + if(data == NULL) { + throw std::runtime_error("File is NULL : \"" + path.string() + "\""); + } + + GLenum format = GL_RGB; + switch (nrChannels) { + case 4: + format = GL_RGBA; + break; + + case 3: + format = GL_RGB; + break; + + default: + format = GL_RGBA; + break; + } + + this->_format = format; + this->_width = width; + this->_height = height; + this->load(data, width * height * nrChannels); + + stbi_image_free(data); + stbi_set_flip_vertically_on_load(false); + } + + void TextureBuffer::free() { + this->_width = 0; + this->_height = 0; + this->_format = GL_RED; + this->_data.clear(); + } + + void TextureBuffer::setPixel(GLuint x, GLuint y, GLubyte v) { + this->_data[(y * this->_width) + x] = v; + } +} \ No newline at end of file diff --git a/source/engine/graphics/back/textures/TextureBuffer.hpp b/source/engine/graphics/back/textures/TextureBuffer.hpp new file mode 100644 index 0000000000000000000000000000000000000000..1c96c4235123215a8e25026b4683bf7190a11053 --- /dev/null +++ b/source/engine/graphics/back/textures/TextureBuffer.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include <vector> +#include <filesystem> +#include <GL/glew.h> + +namespace megu { + class TextureBuffer { + public: + TextureBuffer(); + TextureBuffer(GLuint, GLuint, GLubyte *, GLenum); + TextureBuffer(const TextureBuffer &); + TextureBuffer & operator=(const TextureBuffer & src); + ~TextureBuffer() = default; + + inline GLuint width() const {return this->_width;} + inline GLuint height() const {return this->_height;} + inline GLenum format() const {return this->_format;} + inline const std::vector<GLubyte> & data() const {return this->_data;} + + inline const GLubyte * data_ptr() const {return this->_data.data();} + inline bool empty() const {return this->_data.empty();} + + GLuint getPixel(GLuint, GLuint) const; + + void load(const GLubyte *, size_t); + void load(const std::filesystem::path &, bool = true); + void free(); + + void setPixel(GLuint, GLuint, GLubyte); + + private: + GLuint _width, _height; + GLenum _format; + std::vector<GLubyte> _data; + }; +} \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index c879050c0cf9fb2742be9efd1082d247def572f3..4e5c853aabd654e55d5b1b5bb99490c28c199a05 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -110,6 +110,15 @@ int main(int argc, const char * argv) { std::cout << "VBO - FBO" << std::endl; + //? Material + + megu::TextureBuffer buffer; + buffer.load("assets/textures/All_Might_Armored.png"); + + megu::Texture texture; + texture.store(buffer); + buffer.free(); + //? FBO megu::FrameBuffer fbo(WINDOW_WIDTH, WINDOW_HEIGHT); @@ -154,7 +163,7 @@ int main(int argc, const char * argv) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwPollEvents(); - fbo.bind(); + /*fbo.bind(); vao.bind(); program.use(); glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vbo.size())); @@ -162,13 +171,15 @@ int main(int argc, const char * argv) { fbo_2.bind(); vao_2.bind(); program.use(); - glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vbo_2.size())); + glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vbo_2.size()));*/ megu::FrameBuffer::BindDefaultFrameBuffer(); vao_F.bind(); - fbo.texture().bind(0); - fbo_2.texture().bind(1); + //fbo.texture().bind(0); + //fbo_2.texture().bind(1); + + texture.bind(0); program_F.use(); program_F.setUniform("uSamp", std::vector<GLint>({0, 1}));