From cf1aee508dca530141bda66277ac5075fcec1f70 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 28 Dec 2017 02:24:34 +0100 Subject: Add texture loading (not usage) using SOIL Add soil add dependency --- include/RenderBackend/OpenGL/Image.hpp | 26 ++++++++++++++++ include/RenderBackend/OpenGL/Texture2D.hpp | 19 ++++++++++++ project.conf | 3 +- src/RenderBackend/OpenGL/Image.cpp | 48 ++++++++++++++++++++++++++++++ src/RenderBackend/OpenGL/Texture2D.cpp | 26 ++++++++++++++++ 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 include/RenderBackend/OpenGL/Image.hpp create mode 100644 include/RenderBackend/OpenGL/Texture2D.hpp create mode 100644 src/RenderBackend/OpenGL/Image.cpp create mode 100644 src/RenderBackend/OpenGL/Texture2D.cpp diff --git a/include/RenderBackend/OpenGL/Image.hpp b/include/RenderBackend/OpenGL/Image.hpp new file mode 100644 index 0000000..06f0630 --- /dev/null +++ b/include/RenderBackend/OpenGL/Image.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "../../utils.hpp" +#include "../../types.hpp" +#include "../../Result.hpp" + +namespace amalgine +{ + class Image + { + DISABLE_COPY(Image) + public: + static Result loadFromFile(const char *filepath); + ~Image(); + + const unsigned char* getData() const; + i32 getWidth() const; + i32 getHeight() const; + private: + Image(unsigned char *_imageData, i32 width, i32 height); + private: + unsigned char *imageData; + i32 width; + i32 height; + }; +} diff --git a/include/RenderBackend/OpenGL/Texture2D.hpp b/include/RenderBackend/OpenGL/Texture2D.hpp new file mode 100644 index 0000000..9f7c855 --- /dev/null +++ b/include/RenderBackend/OpenGL/Texture2D.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "../../types.hpp" +#include "../../utils.hpp" + +namespace amalgine +{ + class Image; + + class Texture2D + { + DISABLE_COPY(Texture2D) + public: + Texture2D(Image *image); + ~Texture2D(); + private: + u32 textureId; + }; +} diff --git a/project.conf b/project.conf index 977aee7..4a69183 100644 --- a/project.conf +++ b/project.conf @@ -6,4 +6,5 @@ version = "0.1.0" [dependencies] glfw3 = "3.2.1" glew = "2.1.0" -glm = "0.9.8" \ No newline at end of file +glm = "0.9.8" +soil = "1.16.0" diff --git a/src/RenderBackend/OpenGL/Image.cpp b/src/RenderBackend/OpenGL/Image.cpp new file mode 100644 index 0000000..ba93a51 --- /dev/null +++ b/src/RenderBackend/OpenGL/Image.cpp @@ -0,0 +1,48 @@ +#include "../../../include/RenderBackend/OpenGL/Image.hpp" +#include "soil/1.16.0/inc/SOIL/SOIL.h" + +using namespace std; + +namespace amalgine +{ + Image::Image(unsigned char *_imageData, i32 _width, i32 _height) : imageData(_imageData), width(_width), height(_height) + { + + } + + Result Image::loadFromFile(const char *filepath) + { + int width; + int height; + unsigned char *imageData = SOIL_load_image(filepath, &width, &height, 0, SOIL_LOAD_RGB); + if(!imageData) + { + string errMsg = "Failed to load image from file: "; + errMsg += filepath; + errMsg += "; SOIL error message: "; + errMsg += SOIL_last_result(); + return Result::Err(errMsg); + } + return Result::Ok(new Image(imageData, width, height)); + } + + const unsigned char* Image::getData() const + { + return imageData; + } + + i32 Image::getWidth() const + { + return width; + } + + i32 Image::getHeight() const + { + return height; + } + + Image::~Image() + { + SOIL_free_image_data(imageData); + } +} diff --git a/src/RenderBackend/OpenGL/Texture2D.cpp b/src/RenderBackend/OpenGL/Texture2D.cpp new file mode 100644 index 0000000..07d807a --- /dev/null +++ b/src/RenderBackend/OpenGL/Texture2D.cpp @@ -0,0 +1,26 @@ +#include "../../../include/RenderBackend/OpenGL/Texture2D.hpp" +#include "../../../include/RenderBackend/OpenGL/Image.hpp" +#include "../../../include/RenderBackend/OpenGL/opengl.hpp" +#include + +namespace amalgine +{ + Texture2D::Texture2D(Image *image) + { + assert(image); + glGenTextures(1, &textureId); + glBindTexture(GL_TEXTURE_2D, textureId); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->getWidth(), image->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image->getData()); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glGenerateMipmap(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + } + + Texture2D::~Texture2D() + { + glDeleteTextures(1, &textureId); + } +} -- cgit v1.2.3