www.pudn.com > AndreasHalm-src.zip > texture.h
// ********************************************************************** // * \author Andreas Halm, Institute for Computer Graphics, * // * TU Braunschweig, Muehlenpfordtstrasse 23, * // * 38106 Braunschweig, Germany * // * http://www.cg.cs.tu-bs.de mailto:a.halm@tu-bs.de * // * \date 01.06.2003 * // ********************************************************************** #ifndef _HEADER_TEXTURE_H #define _HEADER_TEXTURE_H //#ifdef WIN32 // all we need from windows.h is HMODULE. // we define it here instead of including something =) //#ifndef HMODULE //typedef void* HMODULE; //#endif //#endif // was: #ifdef WIN32 #ifdef WITH_MFC #include#else #include "windows.h" #endif #endif #include #include #include "gale.h" /** Class holding the bitmap data. If you want to reuse textures (use a texture in more than one viewport), use one TextureData with several texture objects. Currently supported pixel types: GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA, GL_HILO_NV */ class TextureData { public: // construct/destruct TextureData(); TextureData(const TextureData&); virtual ~TextureData(); // load data from file virtual bool loadfile(const char* filename, bool force_rgba=false); // create tiled copy virtual bool tile( const TextureData& data, unsigned int tile_u, unsigned int tile_v ); // create texture with size height/width // not initialized - to initialize call blackness() or whiteness() // type is an open gl texture type like GL_RGB, GL_RGBA virtual bool createbuffer(unsigned int width, unsigned int height, unsigned int type); #if defined WIN32 // for windows, allow construction from resource id (numeric or string). // the data must be PNG-encoded (in "PNG") or JPEG-encoded (in "JPEG"). // the hModule parameter may be NULL in which case the resource is loaded from the calling process virtual bool loadresource(const unsigned int resourceid, HMODULE hModule=0, bool force_rgba=false); #endif // attributes unsigned int width () const { return v_width; } unsigned int height () const { return v_height; } bool ok () const { return v_ok; } unsigned char* data () const { return v_texture_data; } GLenum type () const { return v_type; } GLenum datatype () const { if (type() == GL_HILO_NV) return GL_UNSIGNED_SHORT; return GL_UNSIGNED_BYTE; } unsigned short pixelsize () const { if (type() == GL_HILO_NV) return 4; return channels_per_pixel(); } unsigned short channels_per_pixel () const { switch (type()) { case GL_LUMINANCE: return 1; case GL_LUMINANCE_ALPHA: case GL_HILO_NV: return 2; case GL_RGB: return 3; } return 4; } operator bool () const { return ok(); } protected: void cleanup(); unsigned char* data( unsigned int x, unsigned int y ) { return v_texture_data+(x+y*width())*pixelsize(); } const unsigned char* data( unsigned int x, unsigned int y ) const { return v_texture_data+(x+y*width())*pixelsize(); } bool v_ok; unsigned char* v_texture_data; unsigned int v_width, v_height; GLenum v_type; private: std::string v_filename; }; /** Main texture class */ /*\todo{Document features}*/ class Texture { public: Texture(); Texture( Texture& ); virtual inline ~Texture() { unload(); } // load texture data onto current context bool load( const TextureData&, bool createmipmaps = false ); void unload(); // the one method that does it all for you // if you don't need any special functionality bool loadfile( const char* filename ); // after creating a texture object (or loading it) // you should specify the default texture environment. // however, you can use this function any time. void setParameters( int environment, // GL_DECAL, GL_MODULATE int minfilter, // minification filter int magfilter, // magnification filter int wrap_s = GL_CLAMP, // GL_CLAMP, GL_REPEAT int wrap_t = GL_CLAMP); // GL_CLAMP, GL_REPEAT void activate() const { glBindTexture(GL_TEXTURE_2D,v_texture_name); } // use this to activate this texture on a specific // multitexture level. the 32 constants are usable: // GL_TEXTURE0 ... GL_TEXTURE31 void activate( GLint level ) const { glActiveTextureARB(level); activate(); glEnable(GL_TEXTURE_2D); } static void deactivate( GLint level ) { glActiveTextureARB(level); glDisable(GL_TEXTURE_2D); } // the following functions are just for ease of use. // the texture must have been activated before! static void setEnvMode( GLint env = GL_MODULATE ) { glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,env); } static void setWrapS( GLint clamp = GL_CLAMP ) { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,clamp); } static void setWrapT( GLint clamp = GL_CLAMP ) { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,clamp); } static void setMinificationFilter( GLint filter = GL_LINEAR ) { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,filter); } static void setMagnificationFilter( GLint filter = GL_LINEAR ) { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,filter); } bool isMipmap() const { return v_mipmaptype != 0; } bool ok() const { return v_ok; } unsigned int width () const { return v_texture_data->width(); } unsigned int height () const { return v_texture_data->height(); } unsigned int type () const { return v_texture_data->type(); } operator bool () const { return ok(); } // you must upload a texture if you have changed pixels // in the texture data void upload() const; const TextureData* GetTextureData() const { return v_texture_data; } const TextureData* GetTextureData() { return v_texture_data; } protected: const TextureData* v_texture_data; char v_mipmaptype; // 0-none 1-native 2-glu-generated bool v_ok; bool v_owns_texture_data; unsigned int v_texture_name; }; #endif