Texture.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /////////////////////////////////////////////////////////////////////////////
00003 //
00004 // Texture.h -- Copyright (c) 2006 David Henry
00005 // last modification: feb. 10, 2006
00006 //
00007 // This code is licenced under the MIT license.
00008 //
00009 // This software is provided "as is" without express or implied
00010 // warranties. You may freely copy and compile this source into
00011 // applications you distribute provided that the copyright text
00012 // below is included in the resulting source code.
00013 //
00014 // Definition of an OpenGL texture classes.
00015 //
00016 /////////////////////////////////////////////////////////////////////////////
00017 
00018 #ifndef __TEXTURE_H__
00019 #define __TEXTURE_H__
00020 
00021 #ifdef _WIN32
00022 #define WIN32_LEAN_AND_MEAN
00023 #include <windows.h>
00024 #endif // _WIN32
00025 
00026 #include <GL/gl.h>
00027 #include <memory>
00028 #include <stdexcept>
00029 #include <string>
00030 #include <vector>
00031 #include <map>
00032 
00033 #include "Image.h"
00034 
00035 using std::string;
00036 using std::vector;
00037 using std::map;
00038 using std::auto_ptr;
00039 
00040 /////////////////////////////////////////////////////////////////////////////
00041 // Texture class diagram:
00042 //
00043 //     +--------- (abs)
00044 //     |  Texture  |
00045 //     +-----------+
00046 //          ^
00047 //          |
00048 //    +-------------+
00049 //    |  Texture2D  |
00050 //    +-------------+
00051 //
00052 /////////////////////////////////////////////////////////////////////////////
00053 
00054 
00055 /////////////////////////////////////////////////////////////////////////////
00056 //
00057 // class Texture -- OpenGL texture base class.  This is an abstract
00058 // class for more specialized OpenGL texture classes.
00059 //
00060 /////////////////////////////////////////////////////////////////////////////
00061 
00062 class Texture
00063 {
00064 public:
00065   // Constructor/destructor
00066   Texture ();
00067   virtual ~Texture ();
00068 
00069 public:
00070   // Constants
00071   enum  {
00072     //Default behaviour
00073     kDefault  = 0,
00074 
00075     // Use texture compression
00076     kCompress = (1 << 0),
00077   };
00078 
00079   typedef int TextureFlags;
00080 
00081 public:
00082   // Public interface
00083   void bind () const;
00084   bool fail () const { return _fail; }
00085   bool stdCoordSystem () const { return _standardCoordSystem; }
00086 
00087   // Accessors
00088   const string &name () const { return _name; }
00089   GLuint handle () const { return _handle; }
00090 
00091   virtual GLenum target () const = 0;
00092 
00093 private:
00094   // Copy operations are not allowed for textures, because
00095   // when the source texture is destroyed, it releases
00096   // its texture handle and so dest texture's handle is
00097   // not valid anymore.
00098   Texture (const Texture &);
00099   Texture &operator= (const Texture &);
00100 
00101 protected:
00102   // Internal functions
00103   GLubyte *loadImageFile (const string &filename);
00104   GLint getCompressionFormat (GLint internalFormat);
00105   GLint getInternalFormat (GLint components);
00106 
00107 protected:
00108   // Member variables
00109   string _name;
00110   GLuint _handle;
00111 
00112   TextureFlags _flags;
00113   bool _standardCoordSystem;
00114   bool _fail;
00115 };
00116 
00117 
00118 /////////////////////////////////////////////////////////////////////////////
00119 //
00120 // class Texture2D -- OpenGL texture 2D object.
00121 //
00122 /////////////////////////////////////////////////////////////////////////////
00123 
00124 class Texture2D : public Texture
00125 {
00126 public:
00127   // Constructors
00128   Texture2D (const string &filename, TextureFlags flags = kDefault);
00129   Texture2D (const Image *img, TextureFlags flags = kDefault);
00130 
00131 protected:
00132   // Default constructor is not public
00133   Texture2D ();
00134 
00135   // Internal functions
00136   virtual void create (const Image *img, TextureFlags flags);
00137 
00138 public:
00139   // Accessors
00140   virtual GLenum target () const { return GL_TEXTURE_2D; }
00141 };
00142 
00143 #endif // __TEXTURE_H__