7 #include <GLFW\glfw3.h>
9 #define STB_IMAGE_IMPLEMENTATION
10 #include "stb_image.h"
16 unsigned int texture_id;
17 Texture(std::string filepath, bool clamp = false);
20 void bindOnSlot(int slot);
27 bool USE_DEBUG2 = false;
30 Texture::Texture(std::string filepath, bool clamp)
32 stbi_set_flip_vertically_on_load(true);
34 glGenTextures(1, &this->texture_id);
36 //Load texture using stb_image
37 int width, height, nrChannels;
38 unsigned char* data = stbi_load(filepath.c_str(), &width, &height, &nrChannels, 0);
44 else if (nrChannels == 3)
46 else if (nrChannels == 4)
49 glBindTexture(GL_TEXTURE_2D, this->texture_id);
50 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, format, GL_UNSIGNED_BYTE, data);
51 glGenerateMipmap(GL_TEXTURE_2D);
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
64 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
65 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
67 stbi_image_free(data);
71 std::cout << "Texture loaded, info:" << std::endl;
72 std::cout << filepath << std::endl;
73 std::cout << "width: " << width << std::endl;
74 std::cout << "height: " << height << std::endl;
78 std::cout << "Loading texture: " << filepath << std::endl;
83 std::cout << "ERROR::IMAGE::LOAD_FAILED" << std::endl;
93 glBindTexture(GL_TEXTURE_2D, this->texture_id);
96 void Texture::bindOnSlot(int slot = 0) {
97 glActiveTexture(GL_TEXTURE0 + slot);
98 glBindTexture(GL_TEXTURE_2D, this->texture_id);