src/rcrt/textures/ImageTexture.cpp

Go to the documentation of this file.
00001 #include "ImageTexture.h"
00002 
00003 namespace rcrt
00004 {
00005 
00006 ImageTexture::ImageTexture(Image* img) : image(img)
00007 {
00008 }
00009 
00010 ImageTexture::~ImageTexture()
00011 {
00012         delete image;
00013 }
00014 
00015 RGBAColor ImageTexture::getColor(const Point2D& p) const
00016 {
00017         float u = p.x();
00018         float v = p.y();
00019         float resX = image->getWidth();
00020         float resY = image->getHeight();
00021         // compute texel coordinates
00022         // we add 0.5, since the texel is sampled in the middle of the pixel (see OpenGL linear interpolation)
00023         float px = resX * u + 0.5;
00024         float py = resY * v + 0.5;
00025         
00026 //      // find sampling positions of neighbour texels
00027     Point2D uv[2][2];
00028     uv[0][0] = Point2D(floor(px - 1), floor(py - 1));
00029     uv[0][1] = Point2D(floor(px - 1), floor(py));
00030     uv[1][0] = Point2D(floor(px), floor(py - 1));
00031     uv[1][1] = Point2D(floor(px), floor(py));
00032 
00033 //      // get the four neighbours
00034 //      // make use of _GetTexel method to ensure also handling of proper wrap modes
00035     //RGBAColor tex[2][2];
00036     const RGBAColor& tex00 = image->getPixel((int)uv[0][0].x(), (int)uv[0][0].y());
00037     const RGBAColor& tex01 = image->getPixel((int)uv[0][1].x(), (int)uv[0][1].y());
00038     const RGBAColor& tex10 = image->getPixel((int)uv[1][0].x(), (int)uv[1][0].y());
00039     const RGBAColor& tex11 = image->getPixel((int)uv[1][1].x(), (int)uv[1][1].y());
00040 
00041     // get fractional parts of the texel coordinates
00042     // based on the fractional part we interpolate the values
00043     double ix, iy;
00044     float fx = modf(px, &ix);
00045     float fy = modf(py, &iy);
00046 
00047     // interpolate texels bilinearly
00048     RGBAColor result = tex00*(1-fx)*(1-fy) + tex10*fx*(1-fy) + tex01*(1-fx)*fy + tex11*fx*fy;
00049 
00050     // return new value
00051     return result;
00052 }
00053 
00054 Vec3D ImageTexture::getBump(const Point2D& p, const float& scale,const Matrix4D& onb) const
00055 {
00056         float dx = 1.0f / (image->getWidth() - 1);
00057     float dy = 1.0f / (image->getHeight() - 1);
00058     float b0 = getColor(p).getLuminance();
00059     float bx = image->getPixel(p.x() + dx, p.y()).getLuminance();
00060     float by = image->getPixel(p.x(), p.y() + dy).getLuminance();
00061     
00062 //    w.normalize(onb.w);
00063 //    if ((Math.abs(onb.w.x) < Math.abs(onb.w.y)) && (Math.abs(onb.w.x) < Math.abs(onb.w.z))) {
00064 //        onb.v.x = 0;
00065 //        onb.v.y = onb.w.z;
00066 //        onb.v.z = -onb.w.y;
00067 //    } else if (Math.abs(onb.w.y) < Math.abs(onb.w.z)) {
00068 //        onb.v.x = onb.w.z;
00069 //        onb.v.y = 0;
00070 //        onb.v.z = -onb.w.x;
00071 //    } else {
00072 //        onb.v.x = onb.w.y;
00073 //        onb.v.y = -onb.w.x;
00074 //        onb.v.z = 0;
00075 //    }
00076 //    Vector3.cross(onb.v.normalize(), onb.w, onb.u);
00077     
00078     //return onb * Vec3D(0,0,1);
00079     
00080     //return Vec3D(0,1,0);
00081     
00082     //return onb * (Vec3D(1,1,1) * b0);
00083     
00084         return (onb * Vec3D(scale * (bx - b0) / dx, scale * (by - b0) / dy, 1)).normalize();
00085 }
00086 
00087 }

Generated on Thu Jan 31 19:26:20 2008 for RenderingCompetitionRayTracer by  doxygen 1.5.3