00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "stdafx.h"
00027 #include "textures/imagemap.h"
00028 #include "imageio.h"
00029
00030
00031 template <typename Tmemory, typename Treturn>
00032 ImageTexture<Tmemory, Treturn>::ImageTexture(TextureMapping2D *m,
00033 const string &filename, bool doTrilinear, float maxAniso,
00034 ImageWrap wrapMode, float scale, float gamma) {
00035 mapping = m;
00036 mipmap = GetTexture(filename, doTrilinear, maxAniso,
00037 wrapMode, scale, gamma);
00038 }
00039
00040
00041 template <typename Tmemory, typename Treturn>
00042 ImageTexture<Tmemory, Treturn>::~ImageTexture() {
00043 delete mapping;
00044 }
00045
00046
00047 template <typename Tmemory, typename Treturn> MIPMap<Tmemory> *
00048 ImageTexture<Tmemory, Treturn>::GetTexture(const string &filename,
00049 bool doTrilinear, float maxAniso, ImageWrap wrap,
00050 float scale, float gamma) {
00051
00052 TexInfo texInfo(filename, doTrilinear, maxAniso, wrap, scale, gamma);
00053 if (textures.find(texInfo) != textures.end())
00054 return textures[texInfo];
00055 int width, height;
00056 RGBSpectrum *texels = ReadImage(filename, &width, &height);
00057 MIPMap<Tmemory> *ret = NULL;
00058 if (texels) {
00059
00060 Tmemory *convertedTexels = new Tmemory[width*height];
00061 for (int i = 0; i < width*height; ++i)
00062 convertIn(texels[i], &convertedTexels[i], scale, gamma);
00063 ret = new MIPMap<Tmemory>(width, height, convertedTexels, doTrilinear,
00064 maxAniso, wrap);
00065 delete[] texels;
00066 delete[] convertedTexels;
00067 }
00068 else {
00069
00070 Tmemory *oneVal = new Tmemory[1];
00071 oneVal[0] = powf(scale, gamma);
00072 ret = new MIPMap<Tmemory>(1, 1, oneVal);
00073 delete[] oneVal;
00074 }
00075 textures[texInfo] = ret;
00076 PBRT_LOADED_IMAGE_MAP(const_cast<char *>(filename.c_str()), width, height, sizeof(Tmemory), ret);
00077 return ret;
00078 }
00079
00080
00081 template <typename Tmemory, typename Treturn>
00082 std::map<TexInfo,
00083 MIPMap<Tmemory> *> ImageTexture<Tmemory, Treturn>::textures;
00084 template <typename Tmemory, typename Treturn> Treturn
00085 ImageTexture<Tmemory,
00086 Treturn>::Evaluate(const DifferentialGeometry &dg) const {
00087 float s, t, dsdx, dtdx, dsdy, dtdy;
00088 mapping->Map(dg, &s, &t, &dsdx, &dtdx, &dsdy, &dtdy);
00089 Tmemory mem = mipmap->Lookup(s, t, dsdx, dtdx, dsdy, dtdy);
00090 Treturn ret;
00091 convertOut(mem, &ret);
00092 return ret;
00093 }
00094
00095
00096 ImageTexture<float, float> *CreateImageFloatTexture(const Transform &tex2world,
00097 const TextureParams &tp) {
00098
00099 TextureMapping2D *map = NULL;
00100 string type = tp.FindString("mapping", "uv");
00101 if (type == "uv") {
00102 float su = tp.FindFloat("uscale", 1.);
00103 float sv = tp.FindFloat("vscale", 1.);
00104 float du = tp.FindFloat("udelta", 0.);
00105 float dv = tp.FindFloat("vdelta", 0.);
00106 map = new UVMapping2D(su, sv, du, dv);
00107 }
00108 else if (type == "spherical") map = new SphericalMapping2D(Inverse(tex2world));
00109 else if (type == "cylindrical") map = new CylindricalMapping2D(Inverse(tex2world));
00110 else if (type == "planar")
00111 map = new PlanarMapping2D(tp.FindVector("v1", Vector(1,0,0)),
00112 tp.FindVector("v2", Vector(0,1,0)),
00113 tp.FindFloat("udelta", 0.f), tp.FindFloat("vdelta", 0.f));
00114 else {
00115 Error("2D texture mapping \"%s\" unknown", type.c_str());
00116 map = new UVMapping2D;
00117 }
00118
00119
00120 float maxAniso = tp.FindFloat("maxanisotropy", 8.f);
00121 bool trilerp = tp.FindBool("trilinear", false);
00122 string wrap = tp.FindString("wrap", "repeat");
00123 ImageWrap wrapMode = TEXTURE_REPEAT;
00124 if (wrap == "black") wrapMode = TEXTURE_BLACK;
00125 else if (wrap == "clamp") wrapMode = TEXTURE_CLAMP;
00126 float scale = tp.FindFloat("scale", 1.f);
00127 float gamma = tp.FindFloat("gamma", 1.f);
00128 return new ImageTexture<float, float>(map, tp.FindString("filename"),
00129 trilerp, maxAniso, wrapMode, scale, gamma);
00130 }
00131
00132
00133
00134 ImageTexture<RGBSpectrum, Spectrum> *CreateImageSpectrumTexture(const Transform &tex2world,
00135 const TextureParams &tp) {
00136
00137 TextureMapping2D *map = NULL;
00138 string type = tp.FindString("mapping", "uv");
00139 if (type == "uv") {
00140 float su = tp.FindFloat("uscale", 1.);
00141 float sv = tp.FindFloat("vscale", 1.);
00142 float du = tp.FindFloat("udelta", 0.);
00143 float dv = tp.FindFloat("vdelta", 0.);
00144 map = new UVMapping2D(su, sv, du, dv);
00145 }
00146 else if (type == "spherical") map = new SphericalMapping2D(Inverse(tex2world));
00147 else if (type == "cylindrical") map = new CylindricalMapping2D(Inverse(tex2world));
00148 else if (type == "planar")
00149 map = new PlanarMapping2D(tp.FindVector("v1", Vector(1,0,0)),
00150 tp.FindVector("v2", Vector(0,1,0)),
00151 tp.FindFloat("udelta", 0.f), tp.FindFloat("vdelta", 0.f));
00152 else {
00153 Error("2D texture mapping \"%s\" unknown", type.c_str());
00154 map = new UVMapping2D;
00155 }
00156
00157
00158 float maxAniso = tp.FindFloat("maxanisotropy", 8.f);
00159 bool trilerp = tp.FindBool("trilinear", false);
00160 string wrap = tp.FindString("wrap", "repeat");
00161 ImageWrap wrapMode = TEXTURE_REPEAT;
00162 if (wrap == "black") wrapMode = TEXTURE_BLACK;
00163 else if (wrap == "clamp") wrapMode = TEXTURE_CLAMP;
00164 float scale = tp.FindFloat("scale", 1.f);
00165 float gamma = tp.FindFloat("gamma", 1.f);
00166 return new ImageTexture<RGBSpectrum, Spectrum>(map, tp.FindString("filename"),
00167 trilerp, maxAniso, wrapMode, scale, gamma);
00168 }
00169
00170