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 #ifndef WIN32_LEAN_AND_MEAN
00023 #define WIN32_LEAN_AND_MEAN
00024 #endif  // WIN32_LEAN_AND_MEAN
00025 #include <windows.h>
00026 #endif // _WIN32
00027 
00028 #include <GL/gl.h>
00029 #include <memory>
00030 #include <stdexcept>
00031 #include <string>
00032 #include <vector>
00033 #include <map>
00034 
00035 #include "Image.h"
00036 
00037 using std::string;
00038 using std::vector;
00039 using std::map;
00040 using std::auto_ptr;
00041 
00042 /////////////////////////////////////////////////////////////////////////////
00043 // Texture class diagram:
00044 //
00045 //     +--------- (abs)
00046 //     |  Texture  |
00047 //     +-----------+
00048 //          ^
00049 //          |
00050 //    +-------------+
00051 //    |  Texture2D  |
00052 //    +-------------+
00053 //
00054 /////////////////////////////////////////////////////////////////////////////
00055 
00056 
00057 /////////////////////////////////////////////////////////////////////////////
00058 //
00059 // class Texture -- OpenGL texture base class.  This is an abstract
00060 // class for more specialized OpenGL texture classes.
00061 //
00062 /////////////////////////////////////////////////////////////////////////////
00063 
00064 class Texture
00065 {
00066 public:
00067   // Constructor/destructor
00068   Texture ();
00069   virtual ~Texture ();
00070 
00071 public:
00072   // Constants
00073   enum  {
00074     //Default behaviour
00075     kDefault  = 0,
00076 
00077     // Use texture compression
00078     kCompress = (1 << 0),
00079   };
00080 
00081   typedef int TextureFlags;
00082 
00083 public:
00084   // Public interface
00085   void bind () const;
00086   bool fail () const { return _fail; }
00087   bool stdCoordSystem () const { return _standardCoordSystem; }
00088 
00089   // Accessors
00090   const string &name () const { return _name; }
00091   GLuint handle () const { return _handle; }
00092 
00093   virtual GLenum target () const = 0;
00094 
00095 private:
00096   // Copy operations are not allowed for textures, because
00097   // when the source texture is destroyed, it releases
00098   // its texture handle and so dest texture's handle is
00099   // not valid anymore.
00100   Texture (const Texture &);
00101   Texture &operator= (const Texture &);
00102 
00103 protected:
00104   // Internal functions
00105   GLubyte *loadImageFile (const string &filename);
00106   GLint getCompressionFormat (GLint internalFormat);
00107   GLint getInternalFormat (GLint components);
00108 
00109 protected:
00110   // Member variables
00111   string _name;
00112   GLuint _handle;
00113 
00114   TextureFlags _flags;
00115   bool _standardCoordSystem;
00116   bool _fail;
00117 };
00118 
00119 
00120 /////////////////////////////////////////////////////////////////////////////
00121 //
00122 // class Texture2D -- OpenGL texture 2D object.
00123 //
00124 /////////////////////////////////////////////////////////////////////////////
00125 
00126 class Texture2D : public Texture
00127 {
00128 public:
00129   // Constructors
00130   Texture2D (const string &filename, TextureFlags flags = kDefault);
00131   Texture2D (const Image *img, TextureFlags flags = kDefault);
00132 
00133 protected:
00134   // Default constructor is not public
00135   Texture2D ();
00136 
00137   // Internal functions
00138   virtual void create (const Image *img, TextureFlags flags);
00139 
00140 public:
00141   // Accessors
00142   virtual GLenum target () const { return GL_TEXTURE_2D; }
00143 };
00144 
00145 #endif // __TEXTURE_H__