aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-04-02 15:12:37 +0000
committerJoursoir <chat@joursoir.net>2021-04-02 15:15:03 +0000
commit8217e19e42dbfbfe9b56ac0174699ea9c36ba667 (patch)
treea35eeb845b568a14a6be6280af4932eae8b73be0
parent56812c9af55b776db0a6b5f263e747492bc7d47b (diff)
downloadspace-simulator-8217e19e42dbfbfe9b56ac0174699ea9c36ba667.tar.gz
space-simulator-8217e19e42dbfbfe9b56ac0174699ea9c36ba667.tar.bz2
space-simulator-8217e19e42dbfbfe9b56ac0174699ea9c36ba667.zip
add texture routines
-rw-r--r--src/graphics/Texture.cpp77
-rw-r--r--src/graphics/Texture.hpp16
2 files changed, 93 insertions, 0 deletions
diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp
new file mode 100644
index 0000000..561133e
--- /dev/null
+++ b/src/graphics/Texture.cpp
@@ -0,0 +1,77 @@
+#include <GL/glew.h>
+
+#define STB_IMAGE_IMPLEMENTATION
+#include "../stb_image.h"
+
+#include "Texture.hpp"
+
+Texture::Texture(int type) : target(type)
+{
+ glGenTextures(1, &texture_id);
+}
+
+Texture::~Texture()
+{
+ glDeleteTextures(1, &texture_id);
+}
+
+int Texture::LoadImage(const char *path)
+{
+ int width, height, nrChannels;
+ unsigned char *image;
+
+ glBindTexture(target, texture_id);
+
+ image = stbi_load(path, &width, &height, &nrChannels, 0);
+ if(!image)
+ return 1;
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
+ GL_UNSIGNED_BYTE, image);
+ stbi_image_free(image);
+
+ glGenerateMipmap(target);
+ glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
+ glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
+ glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+
+ glBindTexture(target, 0);
+ return 0;
+}
+
+int Texture::LoadImage(const char **paths)
+{
+ if(target != GL_TEXTURE_CUBE_MAP)
+ return 1;
+
+ int width, height, nrChannels;
+ unsigned int i;
+ unsigned char *image;
+
+ glBindTexture(target, texture_id);
+
+ for(i = 0; i < 6; i++) {
+ image = stbi_load(paths[i], &width, &height, &nrChannels, 0);
+ if(!image)
+ return 1;
+
+ glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
+ 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
+ stbi_image_free(image);
+ }
+
+ glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
+
+ glBindTexture(target, 0);
+ return 0;
+}
+
+void Texture::Bind()
+{
+ glBindTexture(target, texture_id);
+}
diff --git a/src/graphics/Texture.hpp b/src/graphics/Texture.hpp
new file mode 100644
index 0000000..02298f2
--- /dev/null
+++ b/src/graphics/Texture.hpp
@@ -0,0 +1,16 @@
+#ifndef ENGINE_TEXTURE_H
+#define ENGINE_TEXTURE_H
+
+class Texture {
+ int target;
+ unsigned int texture_id;
+public:
+ Texture(int type);
+ ~Texture();
+
+ int LoadImage(const char *path);
+ int LoadImage(const char **paths);
+ void Bind();
+};
+
+#endif /* ENGINE_TEXTURE_H */