00001 /* 00002 File: Texture.cc 00003 00004 Function: 00005 00006 Author: Andrew Willmott 00007 00008 Notes: 00009 */ 00010 00011 #include "gcl/Texture.h" 00012 #include "gcl/Geometry.h" 00013 00014 Texture::Texture(const Texture &tex) 00015 { 00016 flags = tex.flags; 00017 textureFile = tex.textureFile; 00018 image = tex.image; 00019 } 00020 00021 Int Texture::Load(StrConst str) 00022 { 00023 Bool bad = true; 00024 00025 textureFile.SetPath(SubstituteEnvVars(str)); 00026 image = new RGBAImage; 00027 00028 if (image->Load(textureFile) != 0) 00029 _Warning( 00030 "Bad texture: not found"); 00031 else if (image->Width() <= 0 || !IsPowerOfTwo(image->Width())) 00032 _Warning( 00033 "Bad texture: width is not a positive power of 2"); 00034 else if (image->Height() <= 0 || !IsPowerOfTwo(image->Height())) 00035 _Warning( 00036 "Bad texture: height is not a positive power of 2"); 00037 else 00038 bad = false; 00039 00040 if (bad) 00041 // dummy up a texture 00042 { 00043 Int i; 00044 image->SetSize(16, 16); 00045 image->Clear(cWhite); 00046 for (i = 0; i < 8; i++) 00047 { 00048 image->SetPixel(i, i, cRed); 00049 image->SetPixel(i, 15 - i, cRed); 00050 image->SetPixel(15 - i, i, cRed); 00051 image->SetPixel(15 - i, 15 - i, cRed); 00052 } 00053 } 00054 00055 flags = 0; 00056 00057 return(0); 00058 } 00059 00060 #ifdef NEW 00061 Colour Texture::Sample(Coord c) 00062 { 00063 return(image->GetPixel(c[0] * (image->Width() - 1) + 0.5, 00064 c[1] * (image->Height() - 1) + 0.5)); 00065 } 00066 #endif 00067 00068 00069 Colour Texture::FilterBox(const Coord &bl, const Coord &ur) 00070 { 00071 Int x, y, rx[2], ry[2]; 00072 Colour clrSum; 00073 00074 cout << "filter: " << bl << ' ' << ur << endl; 00075 rx[0] = Int(image->Width() * bl[0] + 0.5); 00076 rx[1] = Int(image->Width() * ur[0] + 0.5); 00077 ry[0] = Int(image->Height() * bl[1] + 0.5); 00078 ry[1] = Int(image->Height() * ur[1] + 0.5); 00079 00080 if (rx[0] > rx[1]) 00081 Swap(rx[0], rx[1]); 00082 if (ry[0] > ry[1]) 00083 Swap(ry[0], ry[1]); 00084 00085 cout << "rx[0] " << rx[0] << endl; 00086 cout << "rx[1] " << rx[1] << endl; 00087 cout << "ry[0] " << ry[0] << endl; 00088 cout << "ry[1] " << ry[1] << endl; 00089 00090 clrSum = vl_0; 00091 00092 for (y = ry[0]; y < ry[1]; y++) 00093 for (x = rx[0]; x < rx[1]; x++) 00094 clrSum += image->GetPixel(x, y); 00095 00096 clrSum /= (ry[1] - ry[0]) * (rx[1] - rx[0]); 00097 00098 return(clrSum); 00099 }