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 "material.h"
00025 #include "shape.h"
00026 #include "texture.h"
00027 #include "paramset.h"
00028
00029 using namespace lux;
00030
00031
00032 Material::Material() : bumpmapSampleDistance(.001f) {
00033 compParams = NULL;
00034 }
00035
00036 Material::~Material() {
00037 if(compParams) delete compParams;
00038 }
00039
00040 void Material::InitGeneralParams(const TextureParams &mp) {
00041 bumpmapSampleDistance = mp.FindFloat("bumpmapsampledistance", .001f);
00042 }
00043
00044 void Material::FindCompositingParams(const TextureParams &mp, CompositingParams *cp)
00045 {
00046 cp->tVm = mp.FindBool("compo_visible_material", true);
00047 cp->tVl = mp.FindBool("compo_visible_emission", true);
00048 cp->tiVm = mp.FindBool("compo_visible_indirect_material", true);
00049 cp->tiVl = mp.FindBool("compo_visible_indirect_emission", true);
00050 cp->oA = mp.FindBool("compo_override_alpha", false);
00051 cp->A = mp.FindFloat("compo_override_alpha_value", 0.f);
00052 cp->K = mp.FindBool("compo_use_key", false);
00053 float cc[3] = { 0.f, 0.f, 1.f };
00054 cp->Kc = mp.FindRGBColor("compo_key_color", RGBColor(cc));
00055 }
00056
00057 void Material::Bump(boost::shared_ptr<Texture<float> > d,
00058 const DifferentialGeometry &dgGeom,
00059 const DifferentialGeometry &dgs,
00060 DifferentialGeometry *dgBump) const {
00061
00062 DifferentialGeometry dgEval = dgs;
00063
00064
00065 float du = .5f * (fabsf(dgs.dudx) + fabsf(dgs.dudy));
00066 if (du == 0.f) du = bumpmapSampleDistance;
00067 dgEval.p += du * dgs.dpdu;
00068 dgEval.u += du;
00069 dgEval.nn = Normalize(dgs.nn + du * dgs.dndu);
00070 float uDisplace = d->Evaluate(NULL, dgEval);
00071
00072
00073 float dv = .5f * (fabsf(dgs.dvdx) + fabsf(dgs.dvdy));
00074 if (dv == 0.f) dv = bumpmapSampleDistance;
00075 dgEval.p = dgs.p + dv * dgs.dpdv;
00076 dgEval.u = dgs.u;
00077 dgEval.v += dv;
00078 dgEval.nn = Normalize(dgs.nn + dv * dgs.dndv);
00079 float vDisplace = d->Evaluate(NULL, dgEval);
00080
00081
00082 float displace = d->Evaluate(NULL, dgs);
00083
00084
00085 *dgBump = dgs;
00086 dgBump->dpdu += (uDisplace - displace) / du * Vector(dgs.nn);
00087 dgBump->dpdv += (vDisplace - displace) / dv * Vector(dgs.nn);
00088 dgBump->nn = Normal(Normalize(Cross(dgBump->dpdu, dgBump->dpdv)));
00089
00090
00091
00092
00093 if (Dot(dgGeom.nn, dgBump->nn) < 0.f)
00094 dgBump->nn *= -1.f;
00095 }