diff options
-rw-r--r-- | include/RenderBackend/OpenGL/Image.hpp | 26 | ||||
-rw-r--r-- | include/RenderBackend/OpenGL/Texture2D.hpp | 19 | ||||
-rw-r--r-- | project.conf | 3 | ||||
-rw-r--r-- | src/RenderBackend/OpenGL/Image.cpp | 48 | ||||
-rw-r--r-- | src/RenderBackend/OpenGL/Texture2D.cpp | 26 |
5 files changed, 121 insertions, 1 deletions
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<Image*> 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*> 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<Image*>::Err(errMsg); + } + return Result<Image*>::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 <cassert> + +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); + } +} |