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

Texture.H

Go to the documentation of this file.
00001 
00010 #ifndef TEXTURE_H
00011 #define TEXTURE_H
00012 
00013 #include "Color.H"
00014 #include "glwrap.H"
00015 
00016 
00017 namespace InSpace {
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 
00136   int load(str_ptr filename); 
00138   int save(str_ptr filename);
00139 
00140  protected:
00141   
00142   int loadRGB(const char *fname);
00143   int loadPNM(const char *fname);
00144   int loadTarga(const char *fname);
00145   int loadJPG(const char *fname);
00146   int loadBMP(const char *fname);
00147   int loadG3DFormat(const char *fname, const char *formatString);
00148 
00149   int saveRGB(const char *fname);
00150   int savePNM(const char *fname);
00151   int saveTarga(const char *fname);
00152   int saveJPG(const char *fname);
00153   int saveBMP(const char *fname);
00154   int saveG3DFormat(const char *fname, const char *formatString);
00155   
00156   str_ptr _name;
00157   str_ptr _filename;
00158 
00159   // stored in floats b/c GL converts everything to floats anyway??
00160   // check on that!
00161   GLfloat *_texels;
00162   isGlContextData<GLuint> _glname;
00163   isGlContextData<GLuint> _glAlphaName;
00164   
00165 
00166   int _texType;
00167   int _width;
00168   int _height;
00169   int _hasRealAlpha;
00170   int _isStencil;
00171   double _aspect;
00172 };
00173 
00174 }
00175 
00176 #endif

Generated on Thu Jul 8 15:19:29 2004 for inspace by doxygen 1.3.4