src/Texture2D.h

Go to the documentation of this file.
00001 #ifndef TEXTURE2D_HXX
00002 #define TEXTURE2D_HXX
00003 
00004 
00005 #include <string>
00006 
00007 #include "Texture.h"
00008 #include "PNGImage.h"
00009 
00010 
00016 class Texture2D : public Texture
00017 {
00018 public:
00021     Texture2D(int resX, int resY)
00022         : Texture(),
00023             mResX(resX),
00024             mResY(resY)
00025     {
00026         mImage = new PNGImage(resX, resY);
00027     }
00028 
00031     Texture2D(const std::string & filename)
00032         : Texture(),
00033             mResX(1),
00034             mResY(1)
00035     {
00036         mImage = new PNGImage(1, 1);
00037         if (!mImage->read(filename))
00038         {
00039             delete mImage;
00040             assert(false);
00041         }
00042         mResX = mImage->width();
00043         mResY = mImage->height();
00044     }
00045 
00048     ~Texture2D()
00049     {
00050         delete mImage;
00051     }
00052 
00058     RGBAColor texel(float u, float v) const
00059     {
00060         const float x = u * mResX - 0.5f;
00061         const float y = v * mResY - 0.5f;
00062         switch (interpolationMode())
00063         {
00064             case NEAREST:
00065                 return wrappedTexel(static_cast<int>(roundf(x)), static_cast<int>(roundf(y)));
00066             case LINEAR:
00067             {
00068                 const int ix = static_cast<int>(floorf(x));
00069                 const int iy = static_cast<int>(floorf(y));
00070 
00071                 // neighbourhood
00072                 const RGBAColor t00 = wrappedTexel(ix,   iy);
00073                 const RGBAColor t10 = wrappedTexel(ix+1, iy);
00074                 const RGBAColor t01 = wrappedTexel(ix,   iy+1);
00075                 const RGBAColor t11 = wrappedTexel(ix+1, iy+1);
00076 
00077                 // interpolate
00078                 const RGBAColor tx0 = t00 + (x-ix)*(t10-t00);
00079                 const RGBAColor tx1 = t01 + (x-ix)*(t11-t01);
00080                 return tx0 + (y-iy)*(tx1-tx0);
00081             }
00082         }
00083         return RGBAColor();
00084     }
00085 
00086     int resX() const
00087     {
00088         return mResX;
00089     }
00090 
00091     int resY() const
00092     {
00093         return mResY;
00094     }
00095 
00096 private:
00098     Image * mImage;
00099 
00100     int     mResX;
00101     int     mResY;
00102 
00103 
00104     RGBAColor wrappedTexel(int x, int y) const
00105     {
00106         // wrap texture coordinates
00107         switch (wrapMode())
00108         {
00109             case WRAP:
00110                 if (x < 0)
00111                     x = 0;
00112                 if (y < 0)
00113                     y = 0;
00114                 if (x >= mResX)
00115                     x = mResX-1;
00116                 if (y >= mResY)
00117                     y = mResY-1;
00118             case REPEAT:
00119                 x %= mResX;
00120                 y %= mResY;
00121                 if (x < 0)
00122                     x += mResX;
00123                 if (y < 0)
00124                     y += mResY;
00125         }
00126         return RGBAColor(mImage->pixel(x, y));
00127     }
00128 };
00129 
00130 #endif
00131 

Generated on Fri Feb 1 00:01:42 2008 for Grayfall by  doxygen 1.5.1