From 8217e19e42dbfbfe9b56ac0174699ea9c36ba667 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Fri, 2 Apr 2021 15:12:37 +0000 Subject: add texture routines --- src/graphics/Texture.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ src/graphics/Texture.hpp | 16 ++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/graphics/Texture.cpp create mode 100644 src/graphics/Texture.hpp 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 + +#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 */ -- cgit v1.2.3-18-g5258