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 #include "blender_texlib.h"
00029
00030 namespace lux {
00031
00032
00033 template <class T>
00034 class BlenderMarbleTexture3D : public Texture<T> {
00035 public:
00036
00037
00038 ~BlenderMarbleTexture3D() {
00039 delete mapping;
00040 }
00041
00042 BlenderMarbleTexture3D(
00043 boost::shared_ptr<Texture<T> > c1,
00044 boost::shared_ptr<Texture<T> > c2,
00045 float noiseSize,
00046 short noiseType,
00047 short noiseDepth,
00048 float turbul,
00049 short sType,
00050 short noiseBasis2,
00051 short noiseBasis,
00052 float bright,
00053 float contrast,
00054 TextureMapping3D *map) : mapping(map) {
00055 tex.type = TEX_MARBLE;
00056
00057 tex.noisesize = noiseSize;
00058 tex.noisetype = noiseType;
00059 tex.noisedepth = noiseDepth;
00060 tex.turbul = turbul;
00061 tex.stype = sType;
00062 tex.noisebasis = noiseBasis;
00063 tex.noisebasis2 = noiseBasis2;
00064 tex.bright = bright;
00065 tex.contrast = contrast;
00066 tex1 = c1;
00067 tex2 = c2;
00068 }
00069
00070 T Evaluate(const DifferentialGeometry &dg) const {
00071 Vector dpdx, dpdy;
00072 Point P = mapping->Map(dg, &dpdx, &dpdy);
00073
00074 blender::TexResult texres;
00075 int resultType = multitex(&tex, &P.x, &texres);
00076
00077 if(resultType & TEX_RGB)
00078 texres.tin = (0.35 * texres.tr + 0.45 * texres.tg
00079 + 0.2 * texres.tb);
00080 else
00081 texres.tr = texres.tg = texres.tb = texres.tin;
00082
00083 T t1 = tex1->Evaluate(dg), t2 = tex2->Evaluate(dg);
00084 return (1.f - texres.tin) * t1 + texres.tin * t2;
00085 }
00086
00087 static Texture<float> *CreateFloatTexture(const Transform &tex2world, const TextureParams &tp);
00088 static Texture<Spectrum> *CreateSpectrumTexture(const Transform &tex2world, const TextureParams &tp);
00089 private:
00090
00091
00092 TextureMapping3D *mapping;
00093 boost::shared_ptr<Texture<T> > tex1, tex2;
00094 blender::Tex tex;
00095 };
00096
00097 template <class T> Texture<float> *BlenderMarbleTexture3D<T>::CreateFloatTexture(
00098 const Transform &tex2world,
00099 const TextureParams &tp) {
00100
00101 TextureMapping3D *map = new IdentityMapping3D(tex2world);
00102
00103 IdentityMapping3D *imap = (IdentityMapping3D*) map;
00104 imap->Apply3DTextureMappingOptions(tp);
00105
00106 boost::shared_ptr<Texture<float> > tex1 = tp.GetFloatTexture("tex1", 1.f);
00107 boost::shared_ptr<Texture<float> > tex2 = tp.GetFloatTexture("tex2", 0.f);
00108
00109
00110 short type = TEX_SOFT;
00111 string stype = tp.FindString("type");
00112 if ((stype == "soft") || (stype == ""))
00113 type = TEX_SOFT;
00114 else if (stype == "sharp")
00115 type = TEX_SHARP;
00116 else if (stype == "sharper")
00117 type = TEX_SHARPER;
00118 else {
00119 std::stringstream ss;
00120 ss << "Unknown noise type '" << stype << "'";
00121 luxError(LUX_BADTOKEN, LUX_ERROR, ss.str().c_str());
00122 }
00123
00124
00125 short ntype = TEX_NOISEPERL;
00126 string noiseType = tp.FindString("noisetype");
00127 if ((noiseType == "soft_noise") || (noiseType == ""))
00128 ntype = TEX_NOISESOFT;
00129 else if (noiseType == "hard_noise")
00130 ntype = TEX_NOISEPERL;
00131 else {
00132 std::stringstream ss;
00133 ss << "Unknown noise type '" << noiseType << "'";
00134 luxError(LUX_BADTOKEN, LUX_ERROR, ss.str().c_str());
00135 }
00136
00137
00138 short basis = TEX_BLENDER;
00139 string noiseBasis = tp.FindString("noisebasis");
00140 if ((noiseBasis == "blender_original") || (noiseBasis == ""))
00141 basis = TEX_BLENDER;
00142 else if (noiseBasis == "original_perlin")
00143 basis = TEX_STDPERLIN;
00144 else if (noiseBasis == "improved_perlin")
00145 basis = TEX_NEWPERLIN;
00146 else if (noiseBasis == "voronoi_f1")
00147 basis = TEX_VORONOI_F1;
00148 else if (noiseBasis == "voronoi_f2")
00149 basis = TEX_VORONOI_F2;
00150 else if (noiseBasis == "voronoi_f3")
00151 basis = TEX_VORONOI_F3;
00152 else if (noiseBasis == "voronoi_f4")
00153 basis = TEX_VORONOI_F4;
00154 else if (noiseBasis == "voronoi_f2f1")
00155 basis = TEX_VORONOI_F2F1;
00156 else if (noiseBasis == "voronoi_crackle")
00157 basis = TEX_VORONOI_CRACKLE;
00158 else if (noiseBasis == "cell_noise")
00159 basis = TEX_CELLNOISE;
00160 else {
00161 std::stringstream ss;
00162 ss << "Unknown noise basis '" << noiseBasis << "'";
00163 luxError(LUX_BADTOKEN, LUX_ERROR, ss.str().c_str());
00164 }
00165
00166
00167 short basis2 = TEX_SIN;
00168 string noiseBasis2 = tp.FindString("noisebasis2");
00169 if ((noiseBasis2 == "sin") || (noiseBasis2 == ""))
00170 basis2 = TEX_SIN;
00171 else if (noiseBasis2 == "saw")
00172 basis2 = TEX_SAW;
00173 else if (noiseBasis2 == "tri")
00174 basis2 = TEX_TRI;
00175 else {
00176 std::stringstream ss;
00177 ss << "Unknown noise basis2 '" << noiseBasis2 << "'";
00178 luxError(LUX_BADTOKEN, LUX_ERROR, ss.str().c_str());
00179 }
00180
00181 return new BlenderMarbleTexture3D<float>(
00182 tex1,
00183 tex2,
00184 tp.FindFloat("noisesize", 0.250f),
00185 ntype,
00186 (short)tp.FindInt("noisedepth", 2),
00187 tp.FindFloat("turbulance", 5.0f),
00188 type,
00189 basis2,
00190 basis,
00191 tp.FindFloat("bright", 1.0f),
00192 tp.FindFloat("contrast", 1.0f),
00193 map);
00194 }
00195
00196 template <class T> Texture<Spectrum> *BlenderMarbleTexture3D<T>::CreateSpectrumTexture(
00197 const Transform &tex2world,
00198 const TextureParams &tp) {
00199
00200 TextureMapping3D *map = new IdentityMapping3D(tex2world);
00201
00202 IdentityMapping3D *imap = (IdentityMapping3D*) map;
00203 imap->Apply3DTextureMappingOptions(tp);
00204
00205 boost::shared_ptr<Texture<Spectrum> > tex1 = tp.GetSpectrumTexture("tex1", 1.f);
00206 boost::shared_ptr<Texture<Spectrum> > tex2 = tp.GetSpectrumTexture("tex2", 0.f);
00207
00208
00209 short type = TEX_SOFT;
00210 string stype = tp.FindString("type");
00211 if ((stype == "soft") || (stype == ""))
00212 type = TEX_SOFT;
00213 else if (stype == "sharp")
00214 type = TEX_SHARP;
00215 else if (stype == "sharper")
00216 type = TEX_SHARPER;
00217 else {
00218 std::stringstream ss;
00219 ss << "Unknown noise type '" << stype << "'";
00220 luxError(LUX_BADTOKEN, LUX_ERROR, ss.str().c_str());
00221 }
00222
00223
00224 short ntype = TEX_NOISEPERL;
00225 string noiseType = tp.FindString("noisetype");
00226 if ((noiseType == "soft_noise") || (noiseType == ""))
00227 ntype = TEX_NOISESOFT;
00228 else if (noiseType == "hard_noise")
00229 ntype = TEX_NOISEPERL;
00230 else {
00231 std::stringstream ss;
00232 ss << "Unknown noise type '" << noiseType << "'";
00233 luxError(LUX_BADTOKEN, LUX_ERROR, ss.str().c_str());
00234 }
00235
00236
00237 short basis = TEX_BLENDER;
00238 string noiseBasis = tp.FindString("noisebasis");
00239 if ((noiseBasis == "blender_original") || (noiseBasis == ""))
00240 basis = TEX_BLENDER;
00241 else if (noiseBasis == "original_perlin")
00242 basis = TEX_STDPERLIN;
00243 else if (noiseBasis == "improved_perlin")
00244 basis = TEX_NEWPERLIN;
00245 else if (noiseBasis == "voronoi_f1")
00246 basis = TEX_VORONOI_F1;
00247 else if (noiseBasis == "voronoi_f2")
00248 basis = TEX_VORONOI_F2;
00249 else if (noiseBasis == "voronoi_f3")
00250 basis = TEX_VORONOI_F3;
00251 else if (noiseBasis == "voronoi_f4")
00252 basis = TEX_VORONOI_F4;
00253 else if (noiseBasis == "voronoi_f2f1")
00254 basis = TEX_VORONOI_F2F1;
00255 else if (noiseBasis == "voronoi_crackle")
00256 basis = TEX_VORONOI_CRACKLE;
00257 else if (noiseBasis == "cell_noise")
00258 basis = TEX_CELLNOISE;
00259 else {
00260 std::stringstream ss;
00261 ss << "Unknown noise basis '" << noiseBasis << "'";
00262 luxError(LUX_BADTOKEN, LUX_ERROR, ss.str().c_str());
00263 }
00264
00265
00266 short basis2 = TEX_SIN;
00267 string noiseBasis2 = tp.FindString("noisebasis2");
00268 if ((noiseBasis2 == "sin") || (noiseBasis2 == ""))
00269 basis2 = TEX_SIN;
00270 else if (noiseBasis2 == "saw")
00271 basis2 = TEX_SAW;
00272 else if (noiseBasis2 == "tri")
00273 basis2 = TEX_TRI;
00274 else {
00275 std::stringstream ss;
00276 ss << "Unknown noise basis2 '" << noiseBasis2 << "'";
00277 luxError(LUX_BADTOKEN, LUX_ERROR, ss.str().c_str());
00278 }
00279
00280 return new BlenderMarbleTexture3D<Spectrum>(
00281 tex1,
00282 tex2,
00283 tp.FindFloat("noisesize", 0.250f),
00284 ntype,
00285 (short)tp.FindInt("noisedepth", 2),
00286 tp.FindFloat("turbulance", 5.0f),
00287 type,
00288 basis2,
00289 basis,
00290 tp.FindFloat("bright", 1.0f),
00291 tp.FindFloat("contrast", 1.0f),
00292 map);
00293 }
00294
00295 }