/*! -------------------------------------------------------------------- \file texture.cpp \brief Encapsulate texture data \author James Kuffner \date 2002-09-18 // -------------------------------------------------------------------*/ #ifndef IGNORE_DEPEND #include #include #include #endif #include "texturemap.h" //--------------------------------------------------------------------- // TextureMap // Method: Constructor // //--------------------------------------------------------------------- TextureMap::TextureMap() { _textureBuf = NULL; _width = 0; _height = 0; _name = 0; } //--------------------------------------------------------------------- // TextureMap // Method: Destructor // //--------------------------------------------------------------------- TextureMap::~TextureMap() { if (_textureBuf) delete [] _textureBuf; } //--------------------------------------------------------------------- // TextureMap // Method: InitTextureParams // //! create mipmap and setup texture params // //--------------------------------------------------------------------- bool TextureMap::InitTextureParams(bool bMipmap) { // generate unique "name" and create a new texture object glGenTextures(1, &_name); glBindTexture(GL_TEXTURE_2D, _name); // set up texture mapping parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Build mipmaps for the texture image if needed if (bMipmap) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, 4, _width, _height, GL_RGBA, GL_UNSIGNED_BYTE, _textureBuf); } else { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 4, _width, _height, 0, GL_RGBA, GL_UNSIGNED_BYTE, _textureBuf); } return true; } //--------------------------------------------------------------------- // TextureMap // Method: LoadTIFF // //! Load a TIFF image // //--------------------------------------------------------------------- bool TextureMap::LoadTIFF(char *fname) { cerr << "loading TIFF texture: " << fname << endl; // load the file TIFF *tif = TIFFOpen(fname, "r"); if (tif == NULL) { cerr << "Error opening: " << fname << endl; return false; } // load file and create raster char errormsg[1024]; TIFFRGBAImage img; bool bError = false; uint32* textureBuf = NULL; if (TIFFRGBAImageBegin(&(img), tif, 0, errormsg)) { textureBuf = new uint32[img.width * img.height]; if (textureBuf != NULL) { if (TIFFRGBAImageGet(&(img), textureBuf, img.width, img.height) == 0) { TIFFError(fname, errormsg); bError = true; } } TIFFRGBAImageEnd(&(img)); } else { TIFFError(fname, errormsg); bError = true; } // clean up on error if (bError) { cerr << "ERROR - unable to load texture." << endl; // deallocate textureBuf if (textureBuf) delete [] textureBuf; return false; } // setup texture raster if (_textureBuf) delete [] _textureBuf; _textureBuf = textureBuf; _width = img.width; _height = img.height; cerr << "Texture map (" << _width << " x " << _height << ") successfully loaded." << endl; return true; }