SuzanneIsland: An island of Real-time rendering effects!
texture.hpp
1 #pragma once
2 
3 #include <iostream>
4 #include <string>
5 
6 #include <GL/glew.h>
7 #include <GLFW/glfw3.h>
8 #include <FreeImagePlus.h>
9 
10 
13 class Texture
14 {
15  GLuint handle;
16  const std::string filePath;
17 
18 public:
19  Texture(const std::string &filePath);
20  ~Texture();
21 
22  enum FilterType {
23  NEAREST_MIPMAP_OFF = 0, // use nearest neighbor texel color for interpolated pixel
24  NEAREST_MIPMAP_NEAREST = 1, // nearest with mipmapping (nearest mipmap level)
25  NEAREST_MIPMAP_LINEAR = 2, // nearest with mipmapping (interpolation between mipmap levels)
26  LINEAR_MIPMAP_OFF = 3, // use bilinear interpolation of neighboring texel colors
27  LINEAR_MIPMAP_NEAREST = 4, // bilinear with mipmapping (nearest mipmap level)
28  LINEAR_MIPMAP_LINEAR = 5 // bilinear with mipmapping (interpolation between mipmap levels), aka trilinear
29  };
30 
32  void bind(
33  int unit
34  );
35 
42  void setFilterMode(FilterType filterType);
43 
46  std::string getFilePath() const;
47 };
48 
49 
50 inline Texture::Texture(const std::string &filePath_)
51  : filePath(filePath_)
52 {
53  glGenTextures(1, &handle); // generate texture object and get its id (object state not yet initialized)
54  glActiveTexture(GL_TEXTURE0); // select the active texture unit of the context
55  glBindTexture(GL_TEXTURE_2D, handle); // first bind to context initializes object state
56 
57  // load image from file using FreeImagePlus (the FreeImage C++ wrapper)
58  fipImage img;
59  if (!img.load(filePath.c_str(), 0)) {
60  std::cerr << "ERROR: FreeImage could not load image file '" << filePath << "'." << std::endl;
61  }
62 
63  // specify a texture of the active texture unit at given target
64  // a unit can contain multiple texture targets, but recommended to use only one per unit
65  // parameters: target, mipmap level, internal format, width, heigth, border width, internal format, data format, image data
66  // note: for some reason it seems that 8 bit RGB images are really stored in BGR format.
67  // color texture are usually stored in sRGB gamma corrected color space
68  if (img.isTransparent()) {
69  glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, img.getWidth(), img.getHeight(), 0,
70  GL_BGRA, GL_UNSIGNED_BYTE, img.accessPixels());
71  std::cout << "found texture with alpha channel: " << filePath << std::endl;
72  } else {
73  glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB, img.getWidth(), img.getHeight(), 0,
74  GL_BGR, GL_UNSIGNED_BYTE, img.accessPixels());
75  }
76 
77  // automatically generate mipmaps (mip = multum in parvo, i.e. 'much in little')
78  // mipmaps are filtered and downsampled copies of the texture stored compactly in a single file,
79  // used to avoid aliasing effects when the sampling rate is too low for the texture frequency
80  // e.g. for far away surfaces. by taking a filtered average it doesnt matter where the sample hits.
81  glGenerateMipmap(GL_TEXTURE_2D);
82 
83  setFilterMode(LINEAR_MIPMAP_LINEAR);
84 }
85 
86 inline Texture::~Texture()
87 {
88  glDeleteTextures(1, &handle);
89 }
90 
91 inline void Texture::bind(int unit)
92 {
93  glActiveTexture(GL_TEXTURE0 + unit);
94  glBindTexture(GL_TEXTURE_2D, handle);
95 }
96 
97 inline void Texture::setFilterMode(FilterType filterType)
98 {
99  switch (filterType) {
100  case NEAREST_MIPMAP_OFF:
101  glTexParameterf(
102  GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
103  glTexParameterf(
104  GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
105  break;
106  case NEAREST_MIPMAP_NEAREST:
107  glTexParameterf(
108  GL_TEXTURE_2D,
109  GL_TEXTURE_MIN_FILTER,
110  GL_NEAREST_MIPMAP_NEAREST);
111  glTexParameterf(
112  GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
113  break;
114  case NEAREST_MIPMAP_LINEAR:
115  glTexParameterf(
116  GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
117  glTexParameterf(
118  GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
119  break;
120  case LINEAR_MIPMAP_OFF:
121  glTexParameterf(
122  GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
123  glTexParameterf(
124  GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
125  break;
126  case LINEAR_MIPMAP_NEAREST:
127  glTexParameterf(
128  GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
129  glTexParameterf(
130  GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
131  break;
132  case LINEAR_MIPMAP_LINEAR:
133  glTexParameterf(
134  GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
135  glTexParameterf(
136  GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
137  break;
138  }
139 }
140 
141 inline std::string Texture::getFilePath() const
142 {
143  return filePath;
144 }
145 
Texture class.
Definition: texture.hpp:13
void bind(int unit)
binds this texture to the given opengl texture unit
Definition: texture.hpp:91
void setFilterMode(FilterType filterType)
set texture minification and magnification filters minification: how to filter texture in case of und...
Definition: texture.hpp:97
std::string getFilePath() const
get the texture file path
Definition: texture.hpp:141