]> git.lizzy.rs Git - dragonblocks3d.git/blob - src/texture.cpp
Set antialiasing to 8
[dragonblocks3d.git] / src / texture.cpp
1 #include <iostream>
2 #include <stdexcept>
3 #define STB_IMAGE_IMPLEMENTATION
4 #include "stb_image.h"
5 #include "texture.hpp"
6
7 using namespace std;
8 using namespace dragonblocks;
9
10 bool Texture::mipmap;
11 bool Texture::bilinear_filter;
12 GLenum Texture::min_filter, Texture::mag_filter;
13
14 void Texture::initArgs()
15 {
16         min_filter = mag_filter = bilinear_filter ? GL_LINEAR : GL_NEAREST;
17         if (mipmap) {
18                 if (min_filter == GL_NEAREST) {
19                         min_filter = GL_NEAREST_MIPMAP_NEAREST;
20                 } else {
21                         min_filter = GL_LINEAR_MIPMAP_NEAREST;
22                 }
23         }
24         stbi_set_flip_vertically_on_load(true);
25 }
26
27 void Texture::bind() const
28 {
29         glBindTexture(GL_TEXTURE_2D, id); CHECKERR
30 }
31
32 void Texture::load(const string &path)
33 {
34         int width, height, nrChannels;
35         unsigned char *data = stbi_load(path.c_str(), &width, &height, &nrChannels, 0);
36         if (! data)
37                 throw runtime_error("Failed to load texture " + path);
38         glGenTextures(1, &id); CHECKERR
39         bind();
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
45         if (Texture::mipmap)
46                 glGenerateMipmap(GL_TEXTURE_2D); CHECKERR
47         stbi_image_free(data);
48         glBindTexture(GL_TEXTURE_2D, 0); CHECKERR
49         cout << "Loaded texture " << path << endl;
50 }
51