#include "Material.h" #include "im.h" #include "im_image.h" Material::Material() { color_ = Color(255, 0, 0); ka_ = (real)0.8; kd_ = (real)0.8; ks_ = (real)0.8; } Material::Material(Color color, real ka, real kd, real ks) { color_ = color; ka_ = ka; kd_ = kd; ks_ = ks; } Material::Material(unsigned char r, unsigned char g, unsigned char b, real ka, real kd, real ks) { color_ = Color(r, g, b); ka_ = ka; kd_ = kd; ks_ = ks; } void Material::getColorInfo(real xTexture, real yTexture, Color& color, real& ka, real& kd, real& ks) { color = color_; ka = ka_; kd = kd_; ks = ks_; } ImageTexture::ImageTexture(const char *file_name, real ka, real kd, real ks) { ka_ = ka; kd_ = kd; ks_ = ks; image_ = 0; hasImage_ = loadImage(file_name); } void ImageTexture::getColorInfo(real xTexture, real yTexture, Color& color, real& ka, real& kd, real& ks) { if(hasImage_) { unsigned char r, g, b; imgGetPixel(image_, (int)(xTexture*(width_-1)), (int)(yTexture*(height_-1)), &b, &g, &r); color.setRGB(r, g, b); } ka = ka_; kd = kd_; ks = ks_; } bool ImageTexture::loadImage(const char *file_name) { imImage* image; int error; unsigned char* r; unsigned char* g; unsigned char* b; int i, j; imFile* ifile = imFileOpen(file_name, &error); if (!ifile) return false; image = imFileLoadBitmap(ifile, 0, &error); /* load the first image in the file. */ imFileClose(ifile); if(!image) return false; width_ = image->width; height_ = image->height; r = (unsigned char*)image->data[0]; g = (unsigned char*)image->data[1]; b = (unsigned char*)image->data[2]; if(image_ != 0) imgDestroy(image_); image_ = imgCreate(width_, height_); for(i=0; i