3 #define STB_IMAGE_IMPLEMENTATION
8 using namespace dragonblocks;
11 bool Texture::bilinear_filter;
12 GLenum Texture::min_filter, Texture::mag_filter;
14 void Texture::initArgs()
16 min_filter = mag_filter = bilinear_filter ? GL_LINEAR : GL_NEAREST;
18 if (min_filter == GL_NEAREST) {
19 min_filter = GL_NEAREST_MIPMAP_NEAREST;
21 min_filter = GL_LINEAR_MIPMAP_NEAREST;
24 stbi_set_flip_vertically_on_load(true);
27 void Texture::bind() const
29 glBindTexture(GL_TEXTURE_2D, id); CHECKERR
32 void Texture::load(const string &path)
34 int width, height, nrChannels;
35 unsigned char *data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
37 throw runtime_error("Failed to load texture " + path);
38 glGenTextures(1, &id); CHECKERR
40 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Texture::min_filter); CHECKERR
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Texture::mag_filter); CHECKERR
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); CHECKERR
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); CHECKERR
44 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); CHECKERR
46 glGenerateMipmap(GL_TEXTURE_2D); CHECKERR
47 stbi_image_free(data);
48 glBindTexture(GL_TEXTURE_2D, 0); CHECKERR
49 cout << "Loaded texture " << path << endl;