Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Texture.H

Go to the documentation of this file.
00001 
00010 #ifndef TEXTURE_H
00011 #define TEXTURE_H
00012 
00013 #include "TexDB.H"
00014 #include "Color.H"
00015 
00016 #include "glwrap.H"
00017 
00018 
00019 // IMPORTANT NOTE:  Must call contextInit() from within a GL context
00020 // before you can use the texture!!
00021 
00022 
00023 
00024 // As with OpenGL:
00025 // 1 channel texture is luminance (grayscale).
00026 // 2 channel texture is luminance and alpha.
00027 // 3 channel texture is RGB.
00028 // 4 channel texture is RGBA.
00029 
00030 // values for texType...
00031 enum {
00032   TEX_LUMINANCE = 1,
00033   TEX_LUMINANCE_ALPHA = 2,
00034   TEX_RGB = 3,
00035   TEX_RGBA = 4
00036 };
00037 
00038 class Texture
00039 {
00040  public:
00041 
00042   Texture(str_ptr name, int texType, int width, int height);
00043   // this constructor loads the texture from a file
00044   Texture(str_ptr filename, str_ptr name,bool hasRealAlpha=false,int isStencil=false);
00045   
00046   virtual ~Texture();
00047 
00048 
00049   float* pixelPtr(int w, int h) {
00050     return &_texels[_texType*(w + h*_width)];
00051   }
00052 
00053   void setPixel(int w, int h, Color c) {
00054     float *a = c.array();
00055     for (int i=0;i<_texType;i++) {
00056       pixelPtr(w,h)[i] = a[i];
00057     }
00058   }
00059 
00060   Color getPixel(int w, int h) {
00061     float c[4];
00062     int i;
00063     c[0] = 0.0;
00064     c[1] = 0.0;
00065     c[2] = 0.0;
00066     c[3] = 1.0;
00067     for (i=0;i<_texType;i++)
00068       c[i] = pixelPtr(w,h)[i];
00069     Color col(c[0],c[1],c[2],c[3]);
00070     return col;
00071   }
00072 
00073   // expand to a 4 channel texture
00074   void expandToRGBA();
00075   // replace all occurances of col1 with col2
00076   void replaceCol(Color col1, Color col2);
00077 
00078   // initialize texture data with GL
00079   void contextInit();
00080   // after texture data has changed, pass changes on to GL
00081   void bindChangesToGL();
00082 
00083 
00084   str_ptr name()         const  { return _name; }
00085   str_ptr filename()     const  { return _filename; }
00086   int     type()         const  { return _texType; }
00087   int     width()        const  { return _width; }
00088   int     height()       const  { return _height; }
00089   GLuint  glName()              { return *_glname; }
00090   GLuint  glAlphaName()         { return *_glAlphaName; } 
00091   int     hasRealAlpha() const  { return _hasRealAlpha; }
00092   int     isStencil()    const  { return _isStencil;}
00093   double  aspect()       const  { 
00094     if (_height != 0)
00095       return (double)_width/(double)_height;
00096     else
00097       return 1.0;
00098   }
00099 
00100   void setHasRealAlpha(const int a) { _hasRealAlpha = a; }
00101   void setStencil(const int sten) {_isStencil=sten;}
00102 
00103   // fill the entire texture with this color
00104   void fill(Color c) {
00105     float *a = c.array();
00106     for (int h=0;h<_height;h++) {
00107       for (int w=0;w<_width;w++) {
00108     for (int i=0;i<_texType;i++) {
00109       pixelPtr(w,h)[i] = a[i];
00110     }
00111       }
00112     }
00113   }
00114 
00115   void print() {
00116     cout << "Texture '" << _name << "':" << endl;
00117     cout << "  Height = " << _height << "  Width = " << _width 
00118      << "  Num channels = "  << _texType << endl;
00119     for (int h=0;h<_height;h++) {
00120       for (int w=0;w<_width;w++) {
00121     cout << "("<<w<<","<<h<<") ";
00122     cout << getPixel(w,h) << endl;
00123       }
00124       cout << endl;
00125     }
00126   }
00127 
00128   void printRow(int h) {
00129     for (int w=0;w<_width;w++) {
00130       cout << "("<<w<<","<<h<<") ";
00131       cout << getPixel(w,h) << endl;
00132     }
00133   }
00134 
00135   int load(str_ptr filename); 
00136   int save(str_ptr filename);
00137 
00138   int loadRGB(const char *fname);
00139   int loadPNM(const char *fname);
00140 
00141   int loadTarga(const char *fname);
00142   int saveTarga(const char *fname);
00143 
00144 
00145  protected:
00146 
00147   str_ptr _name;
00148   str_ptr _filename;
00149 
00150   // stored in floats b/c GL converts everything to floats anyway??
00151   // check on that!
00152   GLfloat *_texels;
00153   isGlContextData<GLuint> _glname;
00154   isGlContextData<GLuint> _glAlphaName;
00155   
00156 
00157   int _texType;
00158   int _width;
00159   int _height;
00160   int _hasRealAlpha;
00161   int _isStencil;
00162   double _aspect;
00163 };
00164 
00165 #endif

Generated on Mon Sep 15 16:27:56 2003 for inspace by doxygen1.2.18