util::Texture2D Class Reference

A utility class for two-dimensional textures. More...

#include <util/texture.h>

List of all members.

Public Member Functions

void bind ()
 Bind the texture.
const Texture2Doperator= (const Texture2D &texture)
 Assignment operator.
 Texture2D (std::string filename)
 Constructor initialising from an image file.
 Texture2D (const Texture2D &texture)
 Copy constructor.
 Texture2D ()
 Default constructor.
 ~Texture2D ()
 Destructor.

Static Public Member Functions

static void blend ()
 Sets the blending mode to blend.
static void clamp (bool enable=true)
 Enables texture coordinate clamping.
static void decal ()
 Sets the blending mode to decal.
static void filter (bool enable=true)
 Enables filtering.
static void modulate ()
 Sets the blending mode to modulate.
static void replace ()
 Sets the blending mode to replace.

Detailed Description

A utility class for two-dimensional textures.

Definition at line 51 of file texture.h.


Constructor & Destructor Documentation

util::Texture2D::Texture2D (  ) 

Default constructor.

Definition at line 165 of file texture.cpp.

00166                          :
00167   data(0),
00168   size(0),
00169   width(0),
00170   height(0),
00171   tex_name(0)
{}

util::Texture2D::Texture2D ( const Texture2D texture  ) 

Copy constructor.

Definition at line 174 of file texture.cpp.

References clamp(), filter(), and modulate().

00175                                                  :
00176   data(0),
00177   size(0),
00178   tex_name(0)
00179 {
00180   if (!texture.data) return;
00181 
00182   size = texture.size;
00183   width = texture.width;
00184   height = texture.height;
00185   data = new GLubyte[size * 4];
00186 
00187   for (unsigned int i = 0; i < size * 4; i++)
00188     data[i] = texture.data[i];
00189 
00190   glEnable(GL_TEXTURE_2D);
00191 
00192   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00193 
00194   glGenTextures(1, &tex_name);
00195   glBindTexture(GL_TEXTURE_2D, tex_name);
00196   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
00197 
00198   clamp();
00199   filter();
00200   modulate();
}

util::Texture2D::Texture2D ( std::string  filename  ) 

Constructor initialising from an image file.

Parameters:
filename The file containing the image. The image must be in a format supported by the Qt image filters.

Definition at line 206 of file texture.cpp.

References clamp(), filter(), and modulate().

00207                                            :
00208   data(0),
00209   size(0),
00210   tex_name(0)
00211 {
00212   QImage image(filename.c_str());
00213   if (!image.width()) {
00214     std::cerr << "Error: Texture initialised by "
00215               << filename << " is empty." << std::endl;
00216     return;
00217   }
00218 
00219   width = image.width();
00220   height = image.height();
00221   size = width * height;
00222 
00223   data = new GLubyte[size * 4];
00224   for (unsigned int x = 0; x < width; x++) {
00225     for (unsigned int y = 0; y < height; y++) {
00226       data[(x + y * width) * 4 + 0] = (GLubyte)qRed(image.pixel(x, y));
00227       data[(x + y * width) * 4 + 1] = (GLubyte)qGreen(image.pixel(x, y));
00228       data[(x + y * width) * 4 + 2] = (GLubyte)qBlue(image.pixel(x, y));
00229       data[(x + y * width) * 4 + 3] = (GLubyte)qAlpha(image.pixel(x, y));
00230     }
00231   }
00232 
00233   glEnable(GL_TEXTURE_2D);
00234 
00235   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00236 
00237   glGenTextures(1, &tex_name);
00238   glBindTexture(GL_TEXTURE_2D, tex_name);
00239   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
00240 
00241   clamp();
00242   filter();
00243   modulate();
}

util::Texture2D::~Texture2D (  ) 

Destructor.

Definition at line 278 of file texture.cpp.

00278                           {
00279   glDeleteTextures(1, &tex_name);
00280   delete[] data;
00281 }


Member Function Documentation

void util::Texture2D::bind (  ) 

Bind the texture.

Definition at line 284 of file texture.cpp.

00284                          {
00285   glBindTexture(GL_TEXTURE_2D, tex_name);
00286 }

void util::Texture2D::blend (  )  [static]

Sets the blending mode to blend.

Definition at line 327 of file texture.cpp.

00327                           {
00328   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
00329 }

void util::Texture2D::clamp ( bool  enable = true  )  [static]

Enables texture coordinate clamping.

Parameters:
enable If true enable clamping, if false repeat.

Definition at line 291 of file texture.cpp.

Referenced by operator=(), and Texture2D().

00291                                      {
00292   if (enable) {
00293     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00294     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00295   }
00296   else {
00297     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00298     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00299   }
00300 }

void util::Texture2D::decal (  )  [static]

Sets the blending mode to decal.

Definition at line 322 of file texture.cpp.

00322                           {
00323   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
00324 }

void util::Texture2D::filter ( bool  enable = true  )  [static]

Enables filtering.

Parameters:
enable If true, filtering is enabled, if false it is disabled/

Definition at line 305 of file texture.cpp.

Referenced by operator=(), and Texture2D().

00305                                       {
00306   if (enable) {
00307     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00308     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00309   }
00310   else {
00311     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00312     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00313   }     
00314 }

void util::Texture2D::modulate (  )  [static]

Sets the blending mode to modulate.

Definition at line 317 of file texture.cpp.

Referenced by operator=(), and Texture2D().

00317                              {
00318   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00319 }

const util::Texture2D & util::Texture2D::operator= ( const Texture2D texture  ) 

Assignment operator.

Definition at line 246 of file texture.cpp.

References clamp(), filter(), and modulate().

00246                                                                           {
00247   glDeleteTextures(1, &tex_name);
00248   delete[] data;
00249   data = 0;
00250   size = 0;
00251   width = 0;
00252   height = 0;
00253 
00254   if (!texture.data) return *this;
00255 
00256   size = texture.size;
00257   data = new GLubyte[size * 4];
00258 
00259   for (unsigned int i = 0; i < size * 4; i++)
00260     data[i] = texture.data[i];
00261 
00262   glEnable(GL_TEXTURE_2D);
00263 
00264   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00265 
00266   glGenTextures(1, &tex_name);
00267   glBindTexture(GL_TEXTURE_2D, tex_name);
00268   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
00269 
00270   clamp();
00271   filter();
00272   modulate();
00273 
00274   return *this;
00275 }

void util::Texture2D::replace (  )  [static]

Sets the blending mode to replace.

Definition at line 332 of file texture.cpp.

00332                             {
00333   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00334 }


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Fri May 31 15:38:35 2013 for VVE by  doxygen 1.6.3