#ifndef TEXTURE_H #define TEXTURE_H #include #include #include #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" class Texture { public: unsigned int ID; Texture(const char* texturePath) { glGenTextures(1, &ID); glBindTexture(GL_TEXTURE_2D, ID); // set the texture wrapping/filtering options (on the currently bound texture object) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // load and generate the texture int width, height, nrChannels; stbi_set_flip_vertically_on_load(true); unsigned char *data = stbi_load(texturePath, &width, &height, &nrChannels, 0); if (data) { GLenum format = GL_RGB; if (nrChannels == 1) format = GL_RED; else if (nrChannels == 3) format = GL_RGB; else if (nrChannels == 4) format = GL_RGBA; glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout << "Failed to load texture" << std::endl; } stbi_image_free(data); } void use() { glBindTexture(GL_TEXTURE_2D, ID); } }; #endif