Skip to content

Commit

Permalink
add temperature sample logic for vdbvolume
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteday committed Nov 6, 2023
1 parent 130a5b1 commit 0bbc230
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/core/scenegraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,14 @@ class VDBVolume : public Volume {
public:
using SharedPtr = std::shared_ptr<VDBVolume>;

VDBVolume(RGB sigma_a, RGB sigma_s, float g, fs::path density) :
VDBVolume(RGB sigma_a, RGB sigma_s, float g, fs::path filepath) :
sigma_a(sigma_a), sigma_s(sigma_s), g(g) {
densityGrid = loadNanoVDB(density);
densityGrid = loadNanoVDB(filepath);
}

VDBVolume(RGB sigma_a, RGB sigma_s, float g, NanoVDBGrid::SharedPtr density) :
sigma_a(sigma_a), sigma_s(sigma_s), g(g), densityGrid(density) {}
VDBVolume(RGB sigma_a, RGB sigma_s, float g,
NanoVDBGrid::SharedPtr density, NanoVDBGrid::SharedPtr temperature = nullptr) :
sigma_a(sigma_a), sigma_s(sigma_s), g(g), densityGrid(density), temperatureGrid(temperature) {}

virtual std::shared_ptr<SceneGraphLeaf> clone() override;
virtual AABB getLocalBoundingBox() const override { return densityGrid->getBounds(); }
Expand All @@ -178,6 +179,7 @@ class VDBVolume : public Volume {
RGB sigma_s;
float g;
NanoVDBGrid::SharedPtr densityGrid;
NanoVDBGrid::SharedPtr temperatureGrid;

protected:
friend class SceneGraph;
Expand Down
9 changes: 9 additions & 0 deletions src/render/media.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ NanoVDBMedium::NanoVDBMedium(const Affine3f &transform, RGB sigma_a, RGB sigma_s
majorantGrid = MajorantGrid(densityGrid.getBounds(), majorantGridRes);
}

NanoVDBMedium::NanoVDBMedium(const Affine3f &transform, RGB sigma_a, RGB sigma_s, float g,
NanoVDBGrid density, NanoVDBGrid temperature, const RGBColorSpace *colorSpace) :
transform(transform), phase(g), sigma_a(sigma_a), sigma_s(sigma_s), densityGrid(std::move(density)),
temperatureGrid(std::move(temperature)), colorSpace(colorSpace) {
inverseTransform = transform.inverse();
const Vector3f majorantGridRes{64, 64, 64};
majorantGrid = MajorantGrid(densityGrid.getBounds(), majorantGridRes);
}

void NanoVDBMedium::initializeFromHost() {
densityGrid.toDevice();
initializeMajorantGrid(majorantGrid, densityGrid.getFloatGrid());
Expand Down
10 changes: 9 additions & 1 deletion src/render/media.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,19 @@ class NanoVDBMedium {
NanoVDBMedium(const Affine3f &transform, RGB sigma_a, RGB sigma_s, float g,
NanoVDBGrid density, const RGBColorSpace *colorSpace = KRR_DEFAULT_COLORSPACE);

NanoVDBMedium(const Affine3f &transform, RGB sigma_a, RGB sigma_s, float g, NanoVDBGrid density,
NanoVDBGrid temperature, const RGBColorSpace *colorSpace = KRR_DEFAULT_COLORSPACE);

KRR_HOST void initializeFromHost();

KRR_CALLABLE bool isEmissive() const { return false; }

KRR_CALLABLE Spectrum Le(Vector3f p, const SampledWavelengths &lambda) const {
return Spectrum::Zero();
if (!temperatureGrid) return Spectrum::Zero();
p = inverseTransform * p;
float temp = temperatureGrid.getDensity(p);
if (temp <= 100.f) return SampledSpectrum(0);
return BlackbodySpectrum(temp).sample(lambda);
}

KRR_CALLABLE MediumProperties samplePoint(Vector3f p, const SampledWavelengths &lambda) const {
Expand All @@ -169,6 +176,7 @@ class NanoVDBMedium {
}

NanoVDBGrid densityGrid;
NanoVDBGrid temperatureGrid;
MajorantGrid majorantGrid;
Affine3f transform, inverseTransform;
HGPhaseFunction phase;
Expand Down
43 changes: 41 additions & 2 deletions src/render/spectrum.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
KRR_NAMESPACE_BEGIN

class RGBColorSpace;
class BlackbodySpectrum;
class RGBBoundedSpectrum;
class RGBUnboundedSpectrum;
class DenselySampledSpectrum;
Expand Down Expand Up @@ -78,15 +79,53 @@ class SampledSpectrum : public Array<float, nSpectrumSamples> {
};

class Spectra :
public TaggedPointer<RGBBoundedSpectrum, RGBUnboundedSpectrum, DenselySampledSpectrum,
PiecewiseLinearSpectrum> {
public TaggedPointer<BlackbodySpectrum, RGBBoundedSpectrum, RGBUnboundedSpectrum,
DenselySampledSpectrum, PiecewiseLinearSpectrum> {
public:
using TaggedPointer::TaggedPointer;
KRR_HOST_DEVICE float operator()(float lambda) const;
KRR_HOST_DEVICE float maxValue() const;
KRR_HOST_DEVICE SampledSpectrum sample(const SampledWavelengths &lambda) const;
};

class BlackbodySpectrum {
public:
KRR_CALLABLE BlackbodySpectrum(float T) : T(T) {
// Compute blackbody normalization constant for given temperature
float lambdaMax = 2.8977721e-3f / T;
normalizationFactor = 1 / blackbody(lambdaMax * 1e9f, T);
}

KRR_CALLABLE float operator()(float lambda) const {
return blackbody(lambda, T) * normalizationFactor;
}

KRR_CALLABLE SampledSpectrum sample(const SampledWavelengths &lambda) const {
SampledSpectrum s;
for (int i = 0; i < SampledSpectrum::dim; ++i)
s[i] = blackbody(lambda[i], T) * normalizationFactor;
return s;
}

KRR_CALLABLE float maxValue() const { return 1.f; }

static KRR_CALLABLE float blackbody(float lambda, float T) {
if (T <= 0) return 0;
const float c = 299792458.f;
const float h = 6.62606957e-34f;
const float kb = 1.3806488e-23f;
// Return emitted radiance for blackbody at wavelength _lambda_
float l = lambda * 1e-9f;
float Le = (2 * h * c * c) / (pow5(l) * (expf((h * c) / (l * kb * T)) - 1));
DCHECK(!isnan(Le));
return Le;
}

private:
float T;
float normalizationFactor;
};

class RGBBoundedSpectrum {
public:
KRR_CALLABLE float operator()(float lambda) const { return rsp(lambda); }
Expand Down
6 changes: 5 additions & 1 deletion src/util/volume.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class NanoVDBGrid {
using SharedPtr = std::shared_ptr<NanoVDBGrid>;
using VDBSampler = nanovdb::SampleFromVoxels<nanovdb::FloatGrid::TreeType, 1, false>;

NanoVDBGrid() = default;

NanoVDBGrid(nanovdb::GridHandle<nanovdb::CudaDeviceBuffer>&& density, float maxDensity) :
densityHandle(std::move(density)), maxDensity(maxDensity) {
densityGrid = densityHandle.grid<float>();
Expand All @@ -35,6 +37,8 @@ class NanoVDBGrid {

void toDevice() { densityHandle.deviceUpload(); }

KRR_CALLABLE operator bool() const { return densityGrid != nullptr; }

KRR_CALLABLE AABB3f getBounds() const { return bounds; }

KRR_CALLABLE float getDensity(const Vector3f &p) const {
Expand All @@ -49,7 +53,7 @@ class NanoVDBGrid {

private:
AABB3f bounds;
nanovdb::GridHandle<nanovdb::CudaDeviceBuffer> densityHandle;
nanovdb::GridHandle<nanovdb::CudaDeviceBuffer> densityHandle{};
nanovdb::FloatGrid *densityGrid{nullptr};
float maxDensity{0};
};
Expand Down

0 comments on commit 0bbc230

Please sign in to comment.