Texture2D.h

Go to the documentation of this file.
00001 
00007 #ifndef TEXTURE2D_H
00008 #define TEXTURE2D_H TEXTURE2D_H
00009 
00010 #include "Texture.h"
00011 
00016 class Texture2D : public Texture
00017 {
00018 private:
00019     
00024     Image* mImage;
00025 
00030     ColorRGBA _GetTexel(int px, int py)
00031     {
00032         if (mImage == NULL) return ColorRGBA();
00033 
00034         // compute correct texture coordiantes based on the wrap mode 
00035         switch (mMode)
00036         {
00037             case CLAMP:
00038                 // clamp coordinates
00039                 px = MAX(0, MIN(px, resX - 1));
00040                 py = MAX(0, MIN(py, resY - 1));
00041                 break;
00042 
00043             case REPEAT:
00044                 // repeat coordinates
00045                 px = (resX + px % resX) % resX;
00046                 py = (resY + py % resY) % resY;
00047                 break;
00048 
00049             default:
00050                 break;
00051         };
00052 
00053         return mImage->getPixel(px, py);
00054     }
00055 
00060     ColorRGBA _GetSmoothTexel(float u, float v)
00061     {
00062         // compute texel coordinates
00063         // we add 0.5, since the texel is sampled in the middle of the pixel (see OpenGL linear interpolation)
00064         float px = resX * u + 0.5;
00065         float py = resY * v + 0.5;
00066 
00067         // find sampling positions of neighbour texels
00068         PAIR uv[2][2];
00069         uv[0][0] = PAIR(floor(px - 1), floor(py - 1));
00070         uv[0][1] = PAIR(floor(px - 1), floor(py));
00071         uv[1][0] = PAIR(floor(px), floor(py - 1));
00072         uv[1][1] = PAIR(floor(px), floor(py));
00073 
00074         // get the four neighbours
00075         // make use of _GetTexel method to ensure also handling of proper wrap modes
00076         ColorRGBA tex[2][2];
00077         tex[0][0] = _GetTexel((int)uv[0][0].first, (int)uv[0][0].second);
00078         tex[0][1] = _GetTexel((int)uv[0][1].first, (int)uv[0][1].second);
00079         tex[1][0] = _GetTexel((int)uv[1][0].first, (int)uv[1][0].second);
00080         tex[1][1] = _GetTexel((int)uv[1][1].first, (int)uv[1][1].second);
00081 
00082         // get fractional parts of the texel coordinates
00083         // based on the fractional part we interpolate the values
00084         double ix, iy;
00085         float fx = fabs(px - (int)px);
00086         float fy = fabs(py - (int)py);
00087 
00088         // interpolate texels bilinearly
00089         ColorRGBA result = tex[0][0]*(1-fx)*(1-fy) + tex[1][0]*fx*(1-fy) + tex[0][1]*(1-fx)*fy + tex[1][1]*fx*fy;
00090 
00091         // return new value
00092         return result;
00093     }
00094 
00096     int resX;
00097 
00099     int resY;
00100 
00101 public:
00102 
00107     Texture2D(int resX, int resY) : Texture(), resX(resX), resY(resY)
00108     {
00109         mImage = new Image(resX, resY);
00110     };
00111     
00117     Texture2D(char* filename) : Texture(), mImage(NULL), resX(1), resY(1)
00118     {
00119         Image* img = new Image(1,1);
00120         if (img->LoadImage(filename))
00121         {
00122             mImage = img;
00123             resX = img->getWidth();
00124             resY = img->getHeight();
00125         }else
00126             delete img;
00127     }
00128     
00130     ~Texture2D()
00131     {
00132         if (mImage) delete mImage;
00133     }
00134 
00142     ColorRGBA GetTexel(float u, float v)
00143     {
00144         // check which interpolation mode is used
00145         switch (getInterpolationMode())
00146         {
00147             // nearest interpolation
00148             case NEAREST:
00149                 return _GetTexel(int(resX * u), int(resY * v));
00150                 break;
00151 
00152             // linear interpolation
00153             case LINEAR:
00154                 return _GetSmoothTexel(u,v);
00155                 break;
00156 
00157             default:
00158                 break;
00159         };
00160         return ColorRGBA();
00161     }
00162 
00163 };
00164 
00165 #endif

Generated on Thu Jan 31 21:48:49 2008 for RayTracer by  doxygen 1.5.4