V-ART
texture.cpp
Go to the documentation of this file.
1 
5 #include "vart/texture.h"
6 #include <cassert>
7 #include <iostream>
8 
9 using std::cerr;
10 using std::clog;
11 using std::string;
12 
13 bool VART::Texture::notInitalized = true;
14 bool VART::Texture::hasWhiteTexture = false;
15 unsigned int VART::Texture::whiteTextureId = 0;
16 
18  : hasTexture(false), textureId(0)
19 // Creates a texture with no data that does not affect the redering of a material.
20 {
21  if (notInitalized)
22  Initialize();
23  WhiteTexture();
24 }
25 
26 VART::Texture::Texture(const string& fileName)
27 {
28  if (notInitalized)
29  Initialize();
30  LoadFromFile(fileName);
31 }
32 
34 {
35  this->operator=(texture);
36 }
37 
39  cerr << "\aWarning: Texture::HasTextureLoad() is deprecated.\n";
40  return hasTexture;
41 }
42 
44 {
45  textureId = texture.textureId;
46  hasTexture = texture.hasTexture;
47  return *this;
48 }
49 
50 bool VART::Texture::LoadFromFile(const std::string& fileName)
51 {
52  // The following symbols of devIL match OpenGL's:
53  // IL_COLOUR_INDEX, IL_COLOR_INDEX, IL_ALPHA, IL_RGB, IL_RGBA, IL_BGR,
54  // IL_BGRA, IL_LUMINANCE, IL_LUMINANCE_ALPHA
55 
56  // Reads the image data, and assign it to width, height and imageData variables.
57  // This is dependent of the image reader library.
58 #ifdef IL_LIB
59  ILuint ilTextName;
60 
61  ilGenImages(1, &ilTextName);
62  ilBindImage(ilTextName);
63  ilEnable(IL_ORIGIN_SET);
64  ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
65  ilLoadImage( fileName.c_str() );
66  ILenum error = ilGetError();
67  if (error != IL_NO_ERROR)
68  {
69  cerr << "Error while loading image file in Texture::LoadFromFile.\n"
70  << "ilLoadImage returned " << error << "\n";
71  throw 1;
72  }
73  unsigned int width = ilGetInteger(IL_IMAGE_WIDTH);
74  unsigned int height = ilGetInteger(IL_IMAGE_HEIGHT);
75  ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
76  ILubyte* imageData = ilGetData();
77  error = ilGetError();
78  if (error == IL_NO_ERROR)
79  {
80  clog << "Image data from " << fileName << " loaded successfully.\n";
81  }
82  else
83  {
84  cerr << "Error while getting image data in Texture::LoadFromFile.\n"
85  << "Caught error: " << error << "\n";
86  throw 2;
87  }
88 #else
89 #error No image handling library defined. Try compiling with -DIL_LIB.
90 #endif //IL_LIB
91 
92 #ifdef VART_OGL
93  //If we have the image data, generate a OpenGL texture.
94  if(imageData != NULL)
95  {
96  hasTexture = true;
97  glGenTextures(1, &textureId);
98  glBindTexture(GL_TEXTURE_2D, textureId);
99  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
100  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
101  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
102  assert(glGetError()==GL_NO_ERROR);
103  }
104  else
105  {
106  cerr << "Error binding texture in Texture::LoadFromFile.\n"
107  << "Texture file: '" << fileName << "'\n"
108  << "imageData: " << imageData << "\n";
109  throw 3;
110  }
111 #else
112 #error No rendering library defined. Try compiling with -DVART_OGL.
113 #endif //VART_OGL
114 
115 #ifdef IL_LIB
116  //Free the image data.
117  ilDeleteImages(1, &ilTextName);
118 #endif //IL_LIB
119 
120  return true;
121 }
122 
124 {
125  // FixMe: Manage reference count so that texture objects can be copied while texture data
126  // remains valid in the renderer.
127 
128 //#ifdef VART_OGL
129  //if(textureId)
130  //glDeleteTextures(1, &textureId);
131 //#endif //VART_OGL
132 }
133 
135 {
136 #ifdef VART_OGL
137  static bool textureIsEnabled = false;
138  if (textureIsEnabled)
139  {
140  if (hasTexture)
141  {
142  glBindTexture(GL_TEXTURE_2D, textureId);
143  //std::cout << "DrawOGL textureId: " << textureId << ".\n";
144  }
145  else
146  {
147  glDisable(GL_TEXTURE_2D);
148  //glDisableClientState(GL_TEXTURE_COORD_ARRAY);
149  textureIsEnabled = false;
150  }
151  }
152  else
153  { // texture is not enabled
154  if (hasTexture)
155  {
156  glEnable(GL_TEXTURE_2D);
157  textureIsEnabled = true;
158  glBindTexture(GL_TEXTURE_2D, textureId);
159  }
160  }
161 #endif //VART_OGL
162  return true;
163 }
164 
165 void VART::Texture::WhiteTexture()
166 {
167  if( VART::Texture::hasWhiteTexture )
168  return;
169 #ifdef VART_OGL
170  unsigned char data[3]={255,255,255};
171  glGenTextures( 1, &whiteTextureId );
172  glBindTexture( GL_TEXTURE_2D, whiteTextureId );
173  glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR );
174  glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR );
175  glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
176  VART::Texture::hasWhiteTexture=true;
177 #endif //VART_OGL
178 }
179 
180 void VART::Texture::Initialize()
181 {
182  notInitalized = false;
183 #ifdef IL_LIB
184  ilInit();
185  clog << "libDevIL Initialized.\n";
186 #endif //IL_LIB
187 #ifdef VART_OGL
188  glEnable(GL_BLEND);
189  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
190  glBlendEquation(GL_FUNC_ADD);
191  // FixMe: The user will not always want GL_REPLACE
192  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
193  clog << "OpenGL texture Initialized.\n";
194 #endif //VART_OGL
195 }
bool DrawOGL() const
Sets the texture to draw with the next object.
Definition: texture.cpp:134
Header file for V-ART class "Texture".
2D image to use as texture.
Definition: texture.h:48
Texture & operator=(const Texture &texture)
Copies texture data.
Definition: texture.cpp:43
Texture()
Creates an unitialized texture.
Definition: texture.cpp:17
virtual ~Texture()
Destructor class.
Definition: texture.cpp:123
bool HasTextureLoad() const
Indicates if a texture has been loaded previously.
Definition: texture.cpp:38
bool LoadFromFile(const std::string &fileName)
Loads a texture from a file.
Definition: texture.cpp:50