aboutsummaryrefslogtreecommitdiff
path: root/src/RenderBackend
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2017-12-28 02:24:34 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-18 15:21:48 +0100
commitcf1aee508dca530141bda66277ac5075fcec1f70 (patch)
tree0b058ce11f8e85374d6e23be3d67bd355fe17cff /src/RenderBackend
parentff4daae11db0ab811cac66e262d289a4107bba4a (diff)
Add texture loading (not usage) using SOIL
Add soil add dependency
Diffstat (limited to 'src/RenderBackend')
-rw-r--r--src/RenderBackend/OpenGL/Image.cpp48
-rw-r--r--src/RenderBackend/OpenGL/Texture2D.cpp26
2 files changed, 74 insertions, 0 deletions
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);
+ }
+}