summaryrefslogtreecommitdiff
path: root/gfx/models/texture.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-01-17 19:36:30 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-01-17 22:16:48 +0000
commit0bf4ad9e4a9e1c97e92aa23a365405dfef89bd7c (patch)
tree68a80976a247836bbb1eecc835af437e3489fbb7 /gfx/models/texture.cpp
parentFirst cut modernizing and sanitizing (diff)
downloadilt-0bf4ad9e4a9e1c97e92aa23a365405dfef89bd7c.tar.bz2
ilt-0bf4ad9e4a9e1c97e92aa23a365405dfef89bd7c.tar.xz
ilt-0bf4ad9e4a9e1c97e92aa23a365405dfef89bd7c.zip
Big reshuffle
Fixes code quality warnings now picked up.
Diffstat (limited to 'gfx/models/texture.cpp')
-rw-r--r--gfx/models/texture.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp
new file mode 100644
index 0000000..a388b32
--- /dev/null
+++ b/gfx/models/texture.cpp
@@ -0,0 +1,35 @@
+#include "texture.h"
+#include "stb_image.h"
+#include <stdexcept>
+
+Texture::Texture(const std::string & fileName) : m_texture {}
+{
+ int width, height, numComponents;
+ unsigned char * data = stbi_load((fileName).c_str(), &width, &height, &numComponents, 4);
+
+ if (!data) {
+ throw std::runtime_error {"Unable to load texture: " + fileName};
+ }
+
+ glGenTextures(1, &m_texture);
+ glBindTexture(GL_TEXTURE_2D, m_texture);
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
+ stbi_image_free(data);
+}
+
+Texture::~Texture()
+{
+ glDeleteTextures(1, &m_texture);
+}
+
+void
+Texture::Bind() const
+{
+ glBindTexture(GL_TEXTURE_2D, m_texture);
+}