From 88fe48647393dd065779d202663c23d2fe806c18 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sat, 27 Nov 2021 14:11:07 -0500 Subject: [PATCH] Move Texture to simpleengine::gfx, separate texture to cpp file --- examples/dev_testing/src/main.cpp | 4 +- include/simpleengine/gfx/texture.h | 83 ++------------------ include/simpleengine/shapes/2d/square.h | 4 +- include/simpleengine/shapes/2d/triangle.h | 4 +- src/gfx/texture.cpp | 93 +++++++++++++++++++++++ src/shapes/2d/square.cpp | 2 +- src/shapes/2d/triangle.cpp | 2 +- 7 files changed, 106 insertions(+), 86 deletions(-) create mode 100644 src/gfx/texture.cpp diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index 86a4422..8a556ea 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -44,8 +44,8 @@ int main(int argc, char *argv[]) { shader_prog.link(); std::shared_ptr base_shader_program = shader_prog.program; - simpleengine::Texture wall_texture("resources/wall.jpg"); - simpleengine::Texture crate_texture("resources/container.jpg", true, true); + simpleengine::gfx::Texture wall_texture("resources/wall.jpg"); + simpleengine::gfx::Texture crate_texture("resources/container.jpg", true, true); std::vector vertices = { {glm::vec3(-0.5f, -0.5f, 0.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f)}, // bottom left diff --git a/include/simpleengine/gfx/texture.h b/include/simpleengine/gfx/texture.h index 8fa1746..04cf104 100644 --- a/include/simpleengine/gfx/texture.h +++ b/include/simpleengine/gfx/texture.h @@ -19,7 +19,7 @@ #include #include -namespace simpleengine { +namespace simpleengine::gfx { class Texture { private: unsigned char* img_data; @@ -38,38 +38,7 @@ namespace simpleengine { * @param img_2d Whether or not the texture is 2D. * @param mipmap Whether or not to generate mipmaps for this texture. */ - Texture(const char* path, bool img_2d = true, bool mipmap = true) { - image_type = img_2d ? GL_TEXTURE_2D : GL_TEXTURE_3D; - - glGenTextures(1, &texture_id); - bind(); - - glTexParameteri(image_type, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(image_type, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - img_data = SOIL_load_image - ( - path, - &width, &height, &channels, - SOIL_LOAD_RGBA - ); - - if (!img_data) { - std::cerr << "Failed to load texture from memory! (" << SOIL_last_result() << ")" << std::endl; - throw std::runtime_error("Failed to load texture from memory!"); - } - - glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data); - - if (mipmap) { - glGenerateMipmap(image_type); - } - - SOIL_free_image_data(img_data); - glBindTexture(image_type, 0); - } + Texture(const char* path, bool img_2d = true, bool mipmap = true); /** * @brief Construct a new Texture object from the loaded file buffer. @@ -79,38 +48,7 @@ namespace simpleengine { * @param img_2d Whether or not the texture is 2D. * @param mipmap Whether or not to generate mipmaps for this texture. */ - Texture(const unsigned char *const buffer, int buffer_length, bool img_2d = true, bool mipmap = true) { - image_type = img_2d ? GL_TEXTURE_2D : GL_TEXTURE_3D; - - glGenTextures(1, &texture_id); - bind(); - - glTexParameteri(image_type, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(image_type, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - img_data = SOIL_load_image_from_memory - ( - buffer, buffer_length, - &width, &height, &channels, - SOIL_LOAD_RGBA - ); - - if (!img_data) { - std::cerr << "Failed to load texture from memory! (" << SOIL_last_result() << ")" << std::endl; - throw std::runtime_error("Failed to load texture from memory!"); - } - - glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data); - - if (mipmap) { - glGenerateMipmap(image_type); - } - - SOIL_free_image_data(img_data); - glBindTexture(image_type, 0); - } + Texture(const unsigned char *const buffer, int buffer_length, bool img_2d = true, bool mipmap = true); /** * @brief Construct a new Texture object from the loaded file buffer. @@ -119,20 +57,9 @@ namespace simpleengine { * @param img_2d Whether or not the texture is 2D. * @param mipmap Whether or not to generate mipmaps for this texture. */ - Texture(std::vector buffer, bool img_2d = true, bool mipmap = true) : - Texture(buffer.data(), buffer.size(), img_2d, mipmap) { - - } + Texture(std::vector buffer, bool img_2d = true, bool mipmap = true); - /* ~Texture() { - if (img_data != nullptr) { - SOIL_free_image_data(img_data); - } - } */ - - void bind() const { - glBindTexture(image_type, texture_id); - } + void bind() const; }; } diff --git a/include/simpleengine/shapes/2d/square.h b/include/simpleengine/shapes/2d/square.h index 054207a..b2c9fec 100644 --- a/include/simpleengine/shapes/2d/square.h +++ b/include/simpleengine/shapes/2d/square.h @@ -32,7 +32,7 @@ namespace simpleengine::shapes_2d { using super = simpleengine::Renderable; private: gfx::Shader shader; - nonstd::optional texture; + nonstd::optional texture; public: std::vector vertices; std::vector indicies; @@ -46,7 +46,7 @@ namespace simpleengine::shapes_2d { virtual ~Square() = default; - void set_texture(Texture texture); + void set_texture(gfx::Texture texture); virtual void update(const float& delta_time) override; diff --git a/include/simpleengine/shapes/2d/triangle.h b/include/simpleengine/shapes/2d/triangle.h index 4990bd4..ae65aae 100644 --- a/include/simpleengine/shapes/2d/triangle.h +++ b/include/simpleengine/shapes/2d/triangle.h @@ -29,7 +29,7 @@ namespace simpleengine::shapes_2d { using super = simpleengine::Renderable; private: gfx::Shader shader; // This only stores the shader program - nonstd::optional texture; + nonstd::optional texture; public: std::vector vertices; gfx::VBO vbo; @@ -41,7 +41,7 @@ namespace simpleengine::shapes_2d { virtual ~Triangle() = default; - void set_texture(Texture texture); + void set_texture(gfx::Texture texture); virtual void update(const float& delta_time) override; diff --git a/src/gfx/texture.cpp b/src/gfx/texture.cpp new file mode 100644 index 0000000..1add5c8 --- /dev/null +++ b/src/gfx/texture.cpp @@ -0,0 +1,93 @@ +#include "gfx/texture.h" + +namespace simpleengine::gfx { + Texture::Texture(const char* path, bool img_2d, bool mipmap) { + image_type = img_2d ? GL_TEXTURE_2D : GL_TEXTURE_3D; + + glGenTextures(1, &texture_id); + bind(); + + glTexParameteri(image_type, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(image_type, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + img_data = SOIL_load_image + ( + path, + &width, &height, &channels, + SOIL_LOAD_RGBA + ); + + if (!img_data) { + std::cerr << "Failed to load texture from memory! (" << SOIL_last_result() << ")" << std::endl; + throw std::runtime_error("Failed to load texture from memory!"); + } + + glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data); + + if (mipmap) { + glGenerateMipmap(image_type); + } + + SOIL_free_image_data(img_data); + glBindTexture(image_type, 0); + } + + /** + * @brief Construct a new Texture object from the loaded file buffer. + * + * @param buffer The bytes of the loaded file. + * @param buffer_length The length of the buffer. + * @param img_2d Whether or not the texture is 2D. + * @param mipmap Whether or not to generate mipmaps for this texture. + */ + Texture::Texture(const unsigned char *const buffer, int buffer_length, bool img_2d, bool mipmap) { + image_type = img_2d ? GL_TEXTURE_2D : GL_TEXTURE_3D; + + glGenTextures(1, &texture_id); + bind(); + + glTexParameteri(image_type, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(image_type, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + img_data = SOIL_load_image_from_memory + ( + buffer, buffer_length, + &width, &height, &channels, + SOIL_LOAD_RGBA + ); + + if (!img_data) { + std::cerr << "Failed to load texture from memory! (" << SOIL_last_result() << ")" << std::endl; + throw std::runtime_error("Failed to load texture from memory!"); + } + + glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data); + + if (mipmap) { + glGenerateMipmap(image_type); + } + + SOIL_free_image_data(img_data); + glBindTexture(image_type, 0); + } + + /** + * @brief Construct a new Texture object from the loaded file buffer. + * + * @param buffer The bytes of the loaded file. + * @param img_2d Whether or not the texture is 2D. + * @param mipmap Whether or not to generate mipmaps for this texture. + */ + Texture::Texture(std::vector buffer, bool img_2d, bool mipmap) : + Texture(buffer.data(), buffer.size(), img_2d, mipmap) { + + } + + void Texture::bind() const { + glBindTexture(image_type, texture_id); + } +} \ No newline at end of file diff --git a/src/shapes/2d/square.cpp b/src/shapes/2d/square.cpp index ac8eb2a..a904e64 100644 --- a/src/shapes/2d/square.cpp +++ b/src/shapes/2d/square.cpp @@ -28,7 +28,7 @@ namespace simpleengine::shapes_2d { } - void Square::set_texture(Texture texture) { + void Square::set_texture(gfx::Texture texture) { this->texture = texture; } diff --git a/src/shapes/2d/triangle.cpp b/src/shapes/2d/triangle.cpp index b85fc42..6c0e94e 100644 --- a/src/shapes/2d/triangle.cpp +++ b/src/shapes/2d/triangle.cpp @@ -19,7 +19,7 @@ namespace simpleengine::shapes_2d { } - void Triangle::set_texture(Texture texture) { + void Triangle::set_texture(gfx::Texture texture) { this->texture = texture; }