00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "lux.h"
00025 #include "texture.h"
00026 #include "paramset.h"
00027 #include "error.h"
00028
00029 namespace lux
00030 {
00031
00032
00033 template <class T> class DotsTexture : public Texture<T> {
00034 public:
00035
00036 ~DotsTexture() {
00037 delete mapping;
00038 }
00039 DotsTexture(TextureMapping2D *m, boost::shared_ptr<Texture<T> > c1,
00040 boost::shared_ptr<Texture<T> > c2) {
00041 mapping = m;
00042 outsideDot = c1;
00043 insideDot = c2;
00044 }
00045 T Evaluate(const DifferentialGeometry &dg) const {
00046
00047 float s, t, dsdx, dtdx, dsdy, dtdy;
00048 mapping->Map(dg, &s, &t, &dsdx, &dtdx, &dsdy, &dtdy);
00049 int sCell = Floor2Int(s + .5f), tCell = Floor2Int(t + .5f);
00050
00051 if (Noise(sCell+.5f, tCell+.5f) > 0) {
00052 float radius = .35f;
00053 float maxShift = 0.5f - radius;
00054 float sCenter = sCell + maxShift *
00055 Noise(sCell + 1.5f, tCell + 2.8f);
00056 float tCenter = tCell + maxShift *
00057 Noise(sCell + 4.5f, tCell + 9.8f);
00058 float ds = s - sCenter, dt = t - tCenter;
00059 if (ds*ds + dt*dt < radius*radius)
00060 return insideDot->Evaluate(dg);
00061 }
00062 return outsideDot->Evaluate(dg);
00063 }
00064 static Texture<float> * CreateFloatTexture(const Transform &tex2world, const TextureParams &tp);
00065 static Texture<Spectrum> * CreateSpectrumTexture(const Transform &tex2world, const TextureParams &tp);
00066
00067 private:
00068
00069 boost::shared_ptr<Texture<T> > outsideDot, insideDot;
00070 TextureMapping2D *mapping;
00071 };
00072
00073
00074
00075 template <class T> inline Texture<float> * DotsTexture<T>::CreateFloatTexture(const Transform &tex2world,
00076 const TextureParams &tp) {
00077
00078 TextureMapping2D *map = NULL;
00079 string type = tp.FindString("mapping");
00080 if (type == "" || type == "uv") {
00081 float su = tp.FindFloat("uscale", 1.);
00082 float sv = tp.FindFloat("vscale", 1.);
00083 float du = tp.FindFloat("udelta", 0.);
00084 float dv = tp.FindFloat("vdelta", 0.);
00085 map = new UVMapping2D(su, sv, du, dv);
00086 }
00087 else if (type == "spherical") map = new SphericalMapping2D(tex2world.GetInverse());
00088 else if (type == "cylindrical") map = new CylindricalMapping2D(tex2world.GetInverse());
00089 else if (type == "planar")
00090 map = new PlanarMapping2D(tp.FindVector("v1", Vector(1,0,0)),
00091 tp.FindVector("v2", Vector(0,1,0)),
00092 tp.FindFloat("udelta", 0.f), tp.FindFloat("vdelta", 0.f));
00093 else {
00094
00095 std::stringstream ss;
00096 ss<<"2D texture mapping '"<<type<<"' unknown";
00097 luxError(LUX_BADTOKEN,LUX_ERROR,ss.str().c_str());
00098 map = new UVMapping2D;
00099 }
00100 return new DotsTexture<float>(map,
00101 tp.GetFloatTexture("inside", 1.f),
00102 tp.GetFloatTexture("outside", 0.f));
00103 }
00104
00105 template <class T> inline Texture<Spectrum> * DotsTexture<T>::CreateSpectrumTexture(const Transform &tex2world,
00106 const TextureParams &tp) {
00107
00108 TextureMapping2D *map = NULL;
00109 string type = tp.FindString("mapping");
00110 if (type == "" || type == "uv") {
00111 float su = tp.FindFloat("uscale", 1.);
00112 float sv = tp.FindFloat("vscale", 1.);
00113 float du = tp.FindFloat("udelta", 0.);
00114 float dv = tp.FindFloat("vdelta", 0.);
00115 map = new UVMapping2D(su, sv, du, dv);
00116 }
00117 else if (type == "spherical") map = new SphericalMapping2D(tex2world.GetInverse());
00118 else if (type == "cylindrical") map = new CylindricalMapping2D(tex2world.GetInverse());
00119 else if (type == "planar")
00120 map = new PlanarMapping2D(tp.FindVector("v1", Vector(1,0,0)),
00121 tp.FindVector("v2", Vector(0,1,0)),
00122 tp.FindFloat("udelta", 0.f), tp.FindFloat("vdelta", 0.f));
00123 else {
00124
00125 std::stringstream ss;
00126 ss<<"2D texture mapping '"<<type<<"' unknown";
00127 luxError(LUX_BADTOKEN,LUX_ERROR,ss.str().c_str());
00128 map = new UVMapping2D;
00129 }
00130 return new DotsTexture<Spectrum>(map,
00131 tp.GetSpectrumTexture("inside", 1.f),
00132 tp.GetSpectrumTexture("outside", 0.f));
00133 }
00134
00135 }
00136