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
00022
00023 float px = resX * u + 0.5;
00024 float py = resY * v + 0.5;
00025
00026
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
00034
00035
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
00042
00043 double ix, iy;
00044 float fx = modf(px, &ix);
00045 float fy = modf(py, &iy);
00046
00047
00048 RGBAColor result = tex00*(1-fx)*(1-fy) + tex10*fx*(1-fy) + tex01*(1-fx)*fy + tex11*fx*fy;
00049
00050
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
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 return (onb * Vec3D(scale * (bx - b0) / dx, scale * (by - b0) / dy, 1)).normalize();
00085 }
00086
00087 }