Number5
Visualisierung 2 Project - Florian Schober (0828151, f.schober@live.com), Andreas Walch (0926780, walch.andreas89@gmail.com)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Texture.cpp
Go to the documentation of this file.
1 #include "Texture.hpp"
2 
3 
4 Texture::Texture(string const & name)
5  : m_name(name)
6 {
7  glGenTextures(1, &m_id);
8 
9  bind();
10 
11  ILuint texid;
12  ILboolean success;
13 
14  ilGenImages(1, &texid);
15  ilBindImage(texid);
16  success = ilLoadImage((const ILstring)("content\\textures\\" + name).c_str());
17  if (!success)
18  {
19  cerr << "ERROR: Loading texture '" << name << "' failed (Could not load file)" << endl;
20  exit(EXIT_FAILURE);
21  }
22 
23  success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
24  if (!success)
25  {
26  cerr << "ERROR: Loading texture '" << name << "' failed (Could not convert image data)" << endl;
27  exit(EXIT_FAILURE);
28  }
29 
30  m_size = uvec2(ilGetInteger(IL_IMAGE_WIDTH),ilGetInteger(IL_IMAGE_HEIGHT));
31 
32  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
33  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
34  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
35  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
36  glTexImage2D(GL_TEXTURE_2D, 0,
37  GL_RGBA,
38  m_size.x,
39  m_size.y,
40  0,
41  ilGetInteger(IL_IMAGE_FORMAT),
42  GL_UNSIGNED_BYTE,
43  ilGetData());
44 
45  glGenerateMipmap(GL_TEXTURE_2D);
46 
47  ilDeleteImages(1, &texid);
48 }
50 {
51  glDeleteTextures(1, &m_id);
52 }
53 
54 void Texture::bind(uint target)
55 {
56  glActiveTexture(GL_TEXTURE0 + target);
57  glBindTexture(GL_TEXTURE_2D, m_id);
58 }
59 
60 
61 
62 
64 {
65  ilInit();
66 }
68 {
69  ilShutDown();
70 }
71 Texture* Textures::get(string const & name)
72 {
73  auto found = m_textures.find(name);
74 
75  if (found != m_textures.end())
76  return found->second.get();
77 
78  auto& entry = m_textures[name];
79  entry.reset(new Texture(name));
80  return entry.get();
81 }
uvec2 m_size
Definition: Texture.hpp:11
GLuint m_id
Definition: Texture.hpp:12
Textures()
Definition: Texture.cpp:63
virtual ~Textures()
Definition: Texture.cpp:67
Texture(string const &name)
Definition: Texture.cpp:4
Texture * get(string const &name)
Definition: Texture.cpp:71
map< string, unique_ptr< Texture > > m_textures
Definition: Texture.hpp:56
void bind(uint target=0)
Definition: Texture.cpp:54
~Texture()
Definition: Texture.cpp:49