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 virtual ~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 virtual T Evaluate(const TsPack *tspack, 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(tspack, dg);
00061 }
00062 return outsideDot->Evaluate(tspack, dg);
00063 }
00064 virtual void SetPower(float power, float area) {
00065
00066 outsideDot->SetPower(power, area);
00067 insideDot->SetPower(power, area);
00068 }
00069 virtual void SetIlluminant() {
00070
00071 outsideDot->SetIlluminant();
00072 insideDot->SetIlluminant();
00073 }
00074 static Texture<float> * CreateFloatTexture(const Transform &tex2world, const TextureParams &tp);
00075 static Texture<SWCSpectrum> * CreateSWCSpectrumTexture(const Transform &tex2world, const TextureParams &tp);
00076
00077 private:
00078
00079 boost::shared_ptr<Texture<T> > outsideDot, insideDot;
00080 TextureMapping2D *mapping;
00081 };
00082
00083
00084
00085 template <class T> inline Texture<float> * DotsTexture<T>::CreateFloatTexture(const Transform &tex2world,
00086 const TextureParams &tp) {
00087
00088 TextureMapping2D *map = NULL;
00089 string type = tp.FindString("mapping");
00090 if (type == "" || type == "uv") {
00091 float su = tp.FindFloat("uscale", 1.);
00092 float sv = tp.FindFloat("vscale", 1.);
00093 float du = tp.FindFloat("udelta", 0.);
00094 float dv = tp.FindFloat("vdelta", 0.);
00095 map = new UVMapping2D(su, sv, du, dv);
00096 }
00097 else if (type == "spherical") map = new SphericalMapping2D(tex2world.GetInverse());
00098 else if (type == "cylindrical") map = new CylindricalMapping2D(tex2world.GetInverse());
00099 else if (type == "planar")
00100 map = new PlanarMapping2D(tp.FindVector("v1", Vector(1,0,0)),
00101 tp.FindVector("v2", Vector(0,1,0)),
00102 tp.FindFloat("udelta", 0.f), tp.FindFloat("vdelta", 0.f));
00103 else {
00104
00105 std::stringstream ss;
00106 ss<<"2D texture mapping '"<<type<<"' unknown";
00107 luxError(LUX_BADTOKEN,LUX_ERROR,ss.str().c_str());
00108 map = new UVMapping2D;
00109 }
00110 return new DotsTexture<float>(map,
00111 tp.GetFloatTexture("inside", 1.f),
00112 tp.GetFloatTexture("outside", 0.f));
00113 }
00114
00115 template <class T> inline Texture<SWCSpectrum> * DotsTexture<T>::CreateSWCSpectrumTexture(const Transform &tex2world,
00116 const TextureParams &tp) {
00117
00118 TextureMapping2D *map = NULL;
00119 string type = tp.FindString("mapping");
00120 if (type == "" || type == "uv") {
00121 float su = tp.FindFloat("uscale", 1.);
00122 float sv = tp.FindFloat("vscale", 1.);
00123 float du = tp.FindFloat("udelta", 0.);
00124 float dv = tp.FindFloat("vdelta", 0.);
00125 map = new UVMapping2D(su, sv, du, dv);
00126 }
00127 else if (type == "spherical") map = new SphericalMapping2D(tex2world.GetInverse());
00128 else if (type == "cylindrical") map = new CylindricalMapping2D(tex2world.GetInverse());
00129 else if (type == "planar")
00130 map = new PlanarMapping2D(tp.FindVector("v1", Vector(1,0,0)),
00131 tp.FindVector("v2", Vector(0,1,0)),
00132 tp.FindFloat("udelta", 0.f), tp.FindFloat("vdelta", 0.f));
00133 else {
00134
00135 std::stringstream ss;
00136 ss<<"2D texture mapping '"<<type<<"' unknown";
00137 luxError(LUX_BADTOKEN,LUX_ERROR,ss.str().c_str());
00138 map = new UVMapping2D;
00139 }
00140 return new DotsTexture<SWCSpectrum>(map,
00141 tp.GetSWCSpectrumTexture("inside", 1.f),
00142 tp.GetSWCSpectrumTexture("outside", 0.f));
00143 }
00144
00145 }
00146