18 #ifndef OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED 19 #define OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED 34 #include <type_traits> 47 template<
typename Gr
idT>
51 size_t pixelSamples = 1,
52 unsigned int seed = 0,
53 bool threaded =
true);
56 template<
typename Gr
idT,
typename IntersectorT>
61 size_t pixelSamples = 1,
62 unsigned int seed = 0,
63 bool threaded =
true);
70 template<
typename Gr
idT,
typename IntersectorT = tools::LevelSetRayIntersector<Gr
idT> >
75 using Vec3Type =
typename IntersectorT::Vec3Type;
76 using RayType =
typename IntersectorT::RayType;
82 size_t pixelSamples = 1,
83 unsigned int seed = 0);
90 size_t pixelSamples = 1,
91 unsigned int seed = 0);
100 void setGrid(
const GridT& grid);
104 void setIntersector(
const IntersectorT& inter);
122 void setPixelSamples(
size_t pixelSamples,
unsigned int seed = 0);
125 void render(
bool threaded =
true)
const;
129 void operator()(
const tbb::blocked_range<size_t>& range)
const;
132 const bool mIsMaster;
135 std::unique_ptr<const BaseShader> mShader;
147 template <
typename IntersectorT,
typename SamplerT = tools::BoxSampler>
153 using RayType =
typename IntersectorT::RayType;
158 "VolumeRender requires a floating-point-valued grid");
167 void render(
bool threaded=
true)
const;
174 void setIntersector(
const IntersectorT& inter);
206 void print(std::ostream& os = std::cout,
int verboseLevel = 1);
210 void operator()(
const tbb::blocked_range<size_t>& range)
const;
216 std::unique_ptr<IntersectorT> mPrimary, mShadow;
217 Real mPrimaryStep, mShadowStep, mCutOff, mLightGain;
218 Vec3R mLightDir, mLightColor, mAbsorption, mScattering;
234 RGBA() : r(0), g(0), b(0), a(1) {}
235 explicit RGBA(
ValueT intensity) : r(intensity), g(intensity), b(intensity), a(1) {}
237 r(_r), g(_g), b(_b), a(_a)
239 RGBA(
double _r,
double _g,
double _b,
double _a = 1.0)
240 : r(static_cast<
ValueT>(_r))
241 , g(static_cast<
ValueT>(_g))
242 , b(static_cast<
ValueT>(_b))
243 , a(static_cast<
ValueT>(_a))
253 const float s = rhs.
a*(1.0f-a);
264 Film(
size_t width,
size_t height)
265 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new
RGBA[mSize])
269 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new
RGBA[mSize])
278 return mPixels[w + h*mWidth];
285 return mPixels[w + h*mWidth];
288 void fill(
const RGBA& rgb=
RGBA(0)) {
for (
size_t i=0; i<mSize; ++i) mPixels[i] = rgb; }
291 RGBA *p = mPixels.get();
292 for (
size_t j = 0; j < mHeight; ++j) {
293 for (
size_t i = 0; i < mWidth; ++i, ++p) {
294 *p = ((i & size) ^ (j & size)) ? c1 : c2;
299 template <
typename Type =
unsigned char>
302 const size_t totalSize = mSize * (alpha ? 4 : 3);
303 std::unique_ptr<Type[]> buffer(
new Type[totalSize]);
304 Type *q = buffer.get();
305 const RGBA* p = this->pixels();
308 *q++ =
static_cast<Type
>(255.0f*(*p).r);
309 *q++ =
static_cast<Type
>(255.0f*(*p).g);
310 *q++ =
static_cast<Type
>(255.0f*(*p).b);
312 *q++ =
static_cast<Type
>(255.0f*(*p).a);
320 std::string name(fileName);
321 if (name.find_last_of(
".") == std::string::npos) name.append(
".ppm");
323 std::ofstream os(name.c_str(), std::ios_base::binary);
325 std::cerr <<
"Error opening PPM file \"" << name <<
"\"" << std::endl;
329 auto buf = this->convertToBitBuffer<unsigned char>(
false);
330 unsigned char* tmp = buf.get();
332 os <<
"P6\n" << mWidth <<
" " << mHeight <<
"\n255\n";
333 os.write(reinterpret_cast<const char*>(&(*tmp)), 3 * mSize *
sizeof(
unsigned char));
336 size_t width()
const {
return mWidth; }
337 size_t height()
const {
return mHeight; }
342 size_t mWidth, mHeight, mSize;
343 std::unique_ptr<RGBA[]> mPixels;
354 double frameWidth,
double nearPlane,
double farPlane)
356 , mScaleWidth(frameWidth)
357 , mScaleHeight(frameWidth * double(film.height()) / double(film.width()))
359 assert(nearPlane > 0 && farPlane > nearPlane);
360 mScreenToWorld.accumPostRotation(
math::X_AXIS, rotation[0] * M_PI / 180.0);
361 mScreenToWorld.accumPostRotation(
math::Y_AXIS, rotation[1] * M_PI / 180.0);
362 mScreenToWorld.accumPostRotation(
math::Z_AXIS, rotation[2] * M_PI / 180.0);
363 mScreenToWorld.accumPostTranslation(translation);
364 this->initRay(nearPlane, farPlane);
371 size_t width()
const {
return mFilm->width(); }
372 size_t height()
const {
return mFilm->height(); }
380 const Vec3R orig = mScreenToWorld.applyMap(
Vec3R(0.0));
381 const Vec3R dir = orig - xyz;
383 Mat4d xform = math::aim<Mat4d>(dir, up);
386 this->initRay(mRay.t0(), mRay.t1());
392 return Vec3R( (2 * i /
double(mFilm->width()) - 1) * mScaleWidth,
393 (1 - 2 * j / double(mFilm->height())) * mScaleHeight, z );
400 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const = 0;
405 mRay.setTimes(t0, t1);
406 mRay.setEye(mScreenToWorld.applyMap(
Vec3R(0.0)));
407 mRay.setDir(mScreenToWorld.applyJacobian(
Vec3R(0.0, 0.0, -1.0)));
438 double focalLength = 50.0,
439 double aperture = 41.2136,
440 double nearPlane = 1e-3,
442 :
BaseCamera(film,
rotation, translation, 0.5*aperture/focalLength, nearPlane, farPlane)
452 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const override 455 Vec3R dir = BaseCamera::rasterToScreen(
Real(i) + iOffset,
Real(j) + jOffset, -1.0);
456 dir = BaseCamera::mScreenToWorld.applyJacobian(dir);
467 return 360.0 / M_PI * atan(aperture/(2.0*length));
473 return aperture/(2.0*(tan(fov * M_PI / 360.0)));
496 double frameWidth = 1.0,
497 double nearPlane = 1e-3,
505 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const override 508 Vec3R eye = BaseCamera::rasterToScreen(
Real(i) + iOffset,
Real(j) + jOffset, 0.0);
509 ray.
setEye(BaseCamera::mScreenToWorld.applyMap(eye));
546 MatteShader(
const GridT& grid) : mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
551 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
552 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
558 typename GridT::ConstAccessor mAcc;
563 template<
typename SamplerType>
593 NormalShader(
const GridT& grid) : mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
598 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
599 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
600 return Film::RGBA(v[0]*(normal[0]+1.0), v[1]*(normal[1]+1.0), v[2]*(normal[2]+1.0));
605 typename GridT::ConstAccessor mAcc;
610 template<
typename SamplerType>
619 return mRGBA *
Film::RGBA(normal[0] + 1.0, normal[1] + 1.0, normal[2] + 1.0);
642 , mInvDim(1.0/bbox.extents())
643 , mAcc(grid.getAccessor())
644 , mXform(&grid.transform())
651 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
652 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
653 const Vec3R rgb = (xyz - mMin) * mInvDim;
659 const Vec3R mMin, mInvDim;
660 typename GridT::ConstAccessor mAcc;
665 template<
typename SamplerType>
670 : mMin(bbox.
min()), mInvDim(1.0/bbox.extents()), mRGBA(c) {}
675 const Vec3R rgb = (xyz - mMin)*mInvDim;
676 return mRGBA*
Film::RGBA(rgb[0], rgb[1], rgb[2]);
681 const Vec3R mMin, mInvDim;
700 DiffuseShader(
const GridT& grid): mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
705 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
706 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
715 typename GridT::ConstAccessor mAcc;
720 template <
typename SamplerType>
749 template<
typename Gr
idT>
758 tracer(grid, shader, camera, pixelSamples, seed);
763 template<
typename Gr
idT,
typename IntersectorT>
765 const IntersectorT& inter,
780 template<
typename Gr
idT,
typename IntersectorT>
790 mShader(shader.copy()),
796 template<
typename Gr
idT,
typename IntersectorT>
806 mShader(shader.copy()),
812 template<
typename Gr
idT,
typename IntersectorT>
817 mInter(other.mInter),
818 mShader(other.mShader->copy()),
819 mCamera(other.mCamera),
820 mSubPixels(other.mSubPixels)
824 template<
typename Gr
idT,
typename IntersectorT>
828 if (mIsMaster)
delete [] mRand;
831 template<
typename Gr
idT,
typename IntersectorT>
836 mInter = IntersectorT(grid);
839 template<
typename Gr
idT,
typename IntersectorT>
847 template<
typename Gr
idT,
typename IntersectorT>
852 mShader.reset(shader.
copy());
855 template<
typename Gr
idT,
typename IntersectorT>
863 template<
typename Gr
idT,
typename IntersectorT>
868 if (pixelSamples == 0) {
871 mSubPixels = pixelSamples - 1;
873 if (mSubPixels > 0) {
874 mRand =
new double[16];
876 for (
size_t i=0; i<16; ++i) mRand[i] = rand();
882 template<
typename Gr
idT,
typename IntersectorT>
886 tbb::blocked_range<size_t> range(0, mCamera->
height());
887 threaded ? tbb::parallel_for(range, *
this) : (*this)(range);
890 template<
typename Gr
idT,
typename IntersectorT>
896 const float frac = 1.0f / (1.0f + float(mSubPixels));
897 for (
size_t j=range.begin(), n=0, je = range.end(); j<je; ++j) {
898 for (
size_t i=0, ie = mCamera->
width(); i<ie; ++i) {
901 Film::RGBA c = mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
902 for (
size_t k=0; k<mSubPixels; ++k, n +=2 ) {
903 ray = mCamera->
getRay(i, j, mRand[n & 15], mRand[(n+1) & 15]);
904 c += mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
913 template<
typename IntersectorT,
typename SampleT>
916 : mAccessor(inter.grid().getConstAccessor())
918 , mPrimary(new IntersectorT(inter))
919 , mShadow(new IntersectorT(inter))
925 , mLightColor(0.7, 0.7, 0.7)
931 template<
typename IntersectorT,
typename SampleT>
934 : mAccessor(other.mAccessor)
935 , mCamera(other.mCamera)
936 , mPrimary(new IntersectorT(*(other.mPrimary)))
937 , mShadow(new IntersectorT(*(other.mShadow)))
938 , mPrimaryStep(other.mPrimaryStep)
939 , mShadowStep(other.mShadowStep)
940 , mCutOff(other.mCutOff)
941 , mLightGain(other.mLightGain)
942 , mLightDir(other.mLightDir)
943 , mLightColor(other.mLightColor)
944 , mAbsorption(other.mAbsorption)
945 , mScattering(other.mScattering)
949 template<
typename IntersectorT,
typename SampleT>
951 print(std::ostream& os,
int verboseLevel)
953 if (verboseLevel>0) {
954 os <<
"\nPrimary step: " << mPrimaryStep
955 <<
"\nShadow step: " << mShadowStep
956 <<
"\nCutoff: " << mCutOff
957 <<
"\nLightGain: " << mLightGain
958 <<
"\nLightDir: " << mLightDir
959 <<
"\nLightColor: " << mLightColor
960 <<
"\nAbsorption: " << mAbsorption
961 <<
"\nScattering: " << mScattering << std::endl;
963 mPrimary->print(os, verboseLevel);
966 template<
typename IntersectorT,
typename SampleT>
970 mPrimary.reset(
new IntersectorT(inter));
971 mShadow.reset(
new IntersectorT(inter));
974 template<
typename IntersectorT,
typename SampleT>
978 tbb::blocked_range<size_t> range(0, mCamera->
height());
979 threaded ? tbb::parallel_for(range, *
this) : (*this)(range);
982 template<
typename IntersectorT,
typename SampleT>
986 SamplerType sampler(mAccessor, mShadow->grid().transform());
989 const Vec3R extinction = -mScattering-mAbsorption, One(1.0);
990 const Vec3R albedo = mLightColor*mScattering/(mScattering+mAbsorption);
991 const Real sGain = mLightGain;
992 const Real pStep = mPrimaryStep;
993 const Real sStep = mShadowStep;
994 const Real cutoff = mCutOff;
1003 std::vector<typename RayType::TimeSpan> pTS, sTS;
1008 for (
size_t j=range.begin(), je = range.end(); j<je; ++j) {
1009 for (
size_t i=0, ie = mCamera->
width(); i<ie; ++i) {
1011 bg.
a = bg.
r = bg.
g = bg.
b = 0;
1013 if( !mPrimary->setWorldRay(pRay))
continue;
1014 Vec3R pTrans(1.0), pLumi(0.0);
1017 while (mPrimary->march(pT0, pT1)) {
1018 for (
Real pT = pStep*ceil(pT0/pStep); pT <= pT1; pT += pStep) {
1020 mPrimary->hits(pTS);
1021 for (
size_t k=0; k<pTS.size(); ++k) {
1022 Real pT = pStep*ceil(pTS[k].t0/pStep), pT1=pTS[k].t1;
1023 for (; pT <= pT1; pT += pStep) {
1025 Vec3R pPos = mPrimary->getWorldPos(pT);
1026 const Real density = sampler.wsSample(pPos);
1027 if (density < cutoff)
continue;
1031 if( !mShadow->setWorldRay(sRay))
continue;
1034 while (mShadow->march(sT0, sT1)) {
1035 for (
Real sT = sStep*ceil(sT0/sStep); sT <= sT1; sT+= sStep) {
1038 for (
size_t l=0; l<sTS.size(); ++l) {
1039 Real sT = sStep*ceil(sTS[l].t0/sStep), sT1=sTS[l].t1;
1040 for (; sT <= sT1; sT+= sStep) {
1042 const Real d = sampler.wsSample(mShadow->getWorldPos(sT));
1043 if (d < cutoff)
continue;
1044 sTrans *=
math::Exp(extinction * d * sStep/(1.0+sT*sGain));
1045 if (sTrans.
lengthSqr()<cutoff)
goto Luminance;
1049 pLumi += albedo * sTrans * pTrans * (One-dT);
1051 if (pTrans.lengthSqr()<cutoff)
goto Pixel;
1069 #ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION 1071 #ifdef OPENVDB_INSTANTIATE_RAYTRACER 1075 #define _FUNCTION(TreeT) \ 1076 void rayTrace(const Grid<TreeT>&, const BaseShader&, BaseCamera&, size_t, unsigned int, bool) 1080 #define _FUNCTION(TreeT) \ 1081 void rayTrace(const Grid<TreeT>&, const tools::LevelSetRayIntersector<Grid<TreeT>>&, const BaseShader&, BaseCamera&, size_t, unsigned int, bool) 1088 #endif // OPENVDB_USE_EXPLICIT_INSTANTIATION 1095 #endif // OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
Mat4< double > Mat4d
Definition: Mat4.h:1368
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:165
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Multiply m0 by m1 and return the resulting matrix.
Definition: Mat3.h:611
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Axis-aligned bounding box.
Definition: BBox.h:23
Type Exp(const Type &x)
Return ex.
Definition: Math.h:713
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:378
Definition: Exceptions.h:65
MatType rotation(const Quat< typename MatType::value_type > &q, typename MatType::value_type eps=static_cast< typename MatType::value_type >(1.0e-8))
Return the rotation matrix specified by the given quaternion.
Definition: Mat.h:194
Vec3< typename promote< T, typename Coord::ValueType >::type > operator+(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be added to or subtracted from a Vec3.
Definition: Coord.h:524
void postTranslate(const Vec3< T0 > &tr)
Right multiplies by the specified translation matrix, i.e. (*this) * Trans.
Definition: Mat4.h:728
double Real
Definition: Types.h:60
MatType unit(const MatType &mat, typename MatType::value_type eps=1.0e-8)
Return a copy of the given matrix with its upper 3×3 rows normalized.
Definition: Mat.h:670
Accelerated intersection of a ray with a narrow-band level set or a generic (e.g. density) volume...
math::Vec3< Real > Vec3R
Definition: Types.h:72
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:195
void scaleTimes(RealT scale)
Definition: Ray.h:84
Definition: Exceptions.h:13
ValueT value
Definition: GridBuilder.h:1287
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:637
#define OPENVDB_INSTANTIATE_CLASS
Definition: version.h.in:143
GridType
List of types that are currently supported by NanoVDB.
Definition: NanoVDB.h:216
A general linear transform using homogeneous coordinates to perform rotation, scaling, shear and translation.
Definition: Maps.h:309
void setDir(const Vec3Type &dir)
Definition: Ray.h:66
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:366
T lengthSqr() const
Definition: Vec3.h:215
const Vec3T & dir() const
Definition: Ray.h:99
void setEye(const Vec3Type &eye)
Definition: Ray.h:64
#define OPENVDB_REAL_TREE_INSTANTIATE(Function)
Definition: version.h.in:147
Coord Abs(const Coord &xyz)
Definition: Coord.h:514
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
void print(const ast::Node &node, const bool numberStatements=true, std::ostream &os=std::cout, const char *indent=" ")
Writes a descriptive printout of a Node hierarchy into a target stream.
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202