Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix illuminant for spectral reflectance data #294

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/core/paramset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void ParamSet::AddBlackbodySpectrum(const std::string &name,

void ParamSet::AddSampledSpectrum(const std::string &name,
std::unique_ptr<Float[]> values,
int nValues) {
int nValues, SpectrumType spectrumType) {
EraseSpectrum(name);
CHECK_EQ(nValues % 2, 0);
nValues /= 2;
Expand All @@ -162,14 +162,15 @@ void ParamSet::AddSampledSpectrum(const std::string &name,
v[i] = values[2 * i + 1];
}
std::unique_ptr<Spectrum[]> s(new Spectrum[1]);
s[0] = Spectrum::FromSampled(wl.get(), v.get(), nValues);
s[0] = Spectrum::FromSampled(wl.get(), v.get(), nValues, spectrumType);
std::shared_ptr<ParamSetItem<Spectrum>> psi(
new ParamSetItem<Spectrum>(name, std::move(s), 1));
spectra.push_back(psi);
}

void ParamSet::AddSampledSpectrumFiles(const std::string &name,
const char **names, int nValues) {
const char **names, int nValues,
SpectrumType spectrumType) {
EraseSpectrum(name);
std::unique_ptr<Spectrum[]> s(new Spectrum[nValues]);
for (int i = 0; i < nValues; ++i) {
Expand Down Expand Up @@ -197,7 +198,7 @@ void ParamSet::AddSampledSpectrumFiles(const std::string &name,
wls.push_back(vals[2 * j]);
v.push_back(vals[2 * j + 1]);
}
s[i] = Spectrum::FromSampled(&wls[0], &v[0], wls.size());
s[i] = Spectrum::FromSampled(&wls[0], &v[0], wls.size(), spectrumType);
}
cachedSpectra[fn] = s[i];
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/paramset.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ class ParamSet {
void AddBlackbodySpectrum(const std::string &, std::unique_ptr<Float[]> v,
int nValues);
void AddSampledSpectrumFiles(const std::string &, const char **,
int nValues);
int nValues, SpectrumType spectrumType);
void AddSampledSpectrum(const std::string &, std::unique_ptr<Float[]> v,
int nValues);
int nValues, SpectrumType spectrumType);
bool EraseInt(const std::string &);
bool EraseBool(const std::string &);
bool EraseFloat(const std::string &);
Expand Down
6 changes: 4 additions & 2 deletions src/core/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@ static void AddParam(ParamSet &ps, const ParamListItem &item,
ps.AddBlackbodySpectrum(name, std::move(floats), nItems);
} else if (type == PARAM_TYPE_SPECTRUM) {
if (item.stringValues) {
ps.AddSampledSpectrumFiles(name, item.stringValues, nItems);
ps.AddSampledSpectrumFiles(name, item.stringValues, nItems,
spectrumType);
} else {
if ((nItems % 2) != 0) {
Warning(
Expand All @@ -687,7 +688,8 @@ static void AddParam(ParamSet &ps, const ParamListItem &item,
std::unique_ptr<Float[]> floats(new Float[nItems]);
for (int j = 0; j < nItems; ++j)
floats[j] = item.doubleValues[j];
ps.AddSampledSpectrum(name, std::move(floats), nItems);
ps.AddSampledSpectrum(name, std::move(floats), nItems,
spectrumType);
}
} else if (type == PARAM_TYPE_STRING) {
std::unique_ptr<std::string[]> strings(new std::string[nItems]);
Expand Down
16 changes: 11 additions & 5 deletions src/core/spectrum.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,14 @@ class SampledSpectrum : public CoefficientSpectrum<nSpectralSamples> {
SampledSpectrum(Float v = 0.f) : CoefficientSpectrum(v) {}
SampledSpectrum(const CoefficientSpectrum<nSpectralSamples> &v)
: CoefficientSpectrum<nSpectralSamples>(v) {}
static SampledSpectrum FromSampled(const Float *lambda, const Float *v,
int n) {
static SampledSpectrum FromSampled(const Float *lambda, const Float *v,
int n, SpectrumType type = SpectrumType::Illuminant) {
// Sort samples if unordered, use sorted for returned spectrum
if (!SpectrumSamplesSorted(lambda, v, n)) {
std::vector<Float> slambda(&lambda[0], &lambda[n]);
std::vector<Float> sv(&v[0], &v[n]);
SortSpectrumSamples(&slambda[0], &sv[0], n);
return FromSampled(&slambda[0], &sv[0], n);
return FromSampled(&slambda[0], &sv[0], n, type);
}
SampledSpectrum r;
for (int i = 0; i < nSpectralSamples; ++i) {
Expand Down Expand Up @@ -463,13 +463,14 @@ class RGBSpectrum : public CoefficientSpectrum<3> {
const Float YWeight[3] = {0.212671f, 0.715160f, 0.072169f};
return YWeight[0] * c[0] + YWeight[1] * c[1] + YWeight[2] * c[2];
}
static RGBSpectrum FromSampled(const Float *lambda, const Float *v, int n) {
static RGBSpectrum FromSampled(const Float *lambda, const Float *v, int n,
SpectrumType type = SpectrumType::Illuminant) {
// Sort samples if unordered, use sorted for returned spectrum
if (!SpectrumSamplesSorted(lambda, v, n)) {
std::vector<Float> slambda(&lambda[0], &lambda[n]);
std::vector<Float> sv(&v[0], &v[n]);
SortSpectrumSamples(&slambda[0], &sv[0], n);
return FromSampled(&slambda[0], &sv[0], n);
return FromSampled(&slambda[0], &sv[0], n, type);
}
Float xyz[3] = {0, 0, 0};
for (int i = 0; i < nCIESamples; ++i) {
Expand All @@ -483,6 +484,11 @@ class RGBSpectrum : public CoefficientSpectrum<3> {
xyz[0] *= scale;
xyz[1] *= scale;
xyz[2] *= scale;
// Apply D65 white point for reflectance
if (type == SpectrumType::Reflectance) {
xyz[0] *= 0.950470f;
xyz[2] *= 1.088827f;
}
return FromXYZ(xyz);
}
};
Expand Down
6 changes: 4 additions & 2 deletions src/materials/metal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ const Float CopperK[CopperSamples] = {

MetalMaterial *CreateMetalMaterial(const TextureParams &mp) {
static Spectrum copperN =
Spectrum::FromSampled(CopperWavelengths, CopperN, CopperSamples);
Spectrum::FromSampled(CopperWavelengths, CopperN, CopperSamples,
SpectrumType::Reflectance);
std::shared_ptr<Texture<Spectrum>> eta =
mp.GetSpectrumTexture("eta", copperN);
static Spectrum copperK =
Spectrum::FromSampled(CopperWavelengths, CopperK, CopperSamples);
Spectrum::FromSampled(CopperWavelengths, CopperK, CopperSamples,
SpectrumType::Reflectance);
std::shared_ptr<Texture<Spectrum>> k = mp.GetSpectrumTexture("k", copperK);
std::shared_ptr<Texture<Float>> roughness =
mp.GetFloatTexture("roughness", .01f);
Expand Down