2 #define STB_IMAGE_IMPLEMENTATION
9 using namespace dragonblocks;
12 bool Texture::bilinear_filter;
13 GLenum Texture::min_filter, Texture::mag_filter;
15 void Texture::initArgs()
17 min_filter = mag_filter = bilinear_filter ? GL_LINEAR : GL_NEAREST;
19 if (min_filter == GL_NEAREST) {
20 min_filter = GL_NEAREST_MIPMAP_NEAREST;
22 min_filter = GL_LINEAR_MIPMAP_NEAREST;
25 stbi_set_flip_vertically_on_load(true);
28 void Texture::bind() const
30 glBindTexture(GL_TEXTURE_2D, id); CHECKERR
33 void Texture::load(const string &path)
35 int width, height, nrChannels;
36 unsigned char *data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
38 throw runtime_error("Failed to load texture " + path);
39 glGenTextures(1, &id); CHECKERR
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, Texture::min_filter); CHECKERR
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, Texture::mag_filter); CHECKERR
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); CHECKERR
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); CHECKERR
45 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); CHECKERR
47 glGenerateMipmap(GL_TEXTURE_2D); CHECKERR
48 stbi_image_free(data);
49 glBindTexture(GL_TEXTURE_2D, 0); CHECKERR
50 log("Loaded texture " + path);