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 --- src/RenderBackend/OpenGL/Image.cpp | 48 ++++++++++++++++++++++++++++++++++ src/RenderBackend/OpenGL/Texture2D.cpp | 26 ++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/RenderBackend/OpenGL/Image.cpp create mode 100644 src/RenderBackend/OpenGL/Texture2D.cpp (limited to 'src') 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