18 #ifndef OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
19 #define OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
35 #include <type_traits>
48 template<
typename Gr
idT>
52 size_t pixelSamples = 1,
53 unsigned int seed = 0,
57 template<
typename Gr
idT,
typename IntersectorT>
62 size_t pixelSamples = 1,
63 unsigned int seed = 0,
71 template<
typename Gr
idT,
typename IntersectorT = tools::LevelSetRayIntersector<Gr
idT> >
76 using Vec3Type =
typename IntersectorT::Vec3Type;
77 using RayType =
typename IntersectorT::RayType;
83 size_t pixelSamples = 1,
84 unsigned int seed = 0);
91 size_t pixelSamples = 1,
92 unsigned int seed = 0);
101 void setGrid(
const GridT& grid);
133 const bool mIsMaster;
136 std::unique_ptr<const BaseShader> mShader;
148 template <
typename IntersectorT,
typename SamplerT = tools::BoxSampler>
154 using RayType =
typename IntersectorT::RayType;
159 "VolumeRender requires a floating-point-valued grid");
207 void print(std::ostream& os = std::cout,
int verboseLevel = 1);
217 std::unique_ptr<IntersectorT> mPrimary, mShadow;
218 Real mPrimaryStep, mShadowStep, mCutOff, mLightGain;
219 Vec3R mLightDir, mLightColor, mAbsorption, mScattering;
236 explicit RGBA(
ValueT intensity) :
r(intensity),
g(intensity),
b(intensity),
a(1) {}
238 r(_r),
g(_g),
b(_b),
a(_a)
240 RGBA(
double _r,
double _g,
double _b,
double _a = 1.0)
254 const float s = rhs.
a*(1.0f-
a);
266 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new
RGBA[mSize])
270 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new
RGBA[mSize])
279 return mPixels[w + h*mWidth];
286 return mPixels[w + h*mWidth];
289 void fill(
const RGBA& rgb=
RGBA(0)) {
for (
size_t i=0; i<mSize; ++i) mPixels[i] = rgb; }
292 RGBA *p = mPixels.get();
293 for (
size_t j = 0;
j < mHeight; ++
j) {
294 for (
size_t i = 0; i < mWidth; ++i, ++p) {
295 *p = ((i &
size) ^ (
j &
size)) ? c1 : c2;
300 template <
typename Type =
unsigned char>
303 const size_t totalSize = mSize * (
alpha ? 4 : 3);
304 std::unique_ptr<Type[]>
buffer(
new Type[totalSize]);
305 Type *
q = buffer.get();
309 *q++ =
static_cast<Type>(255.0f*(*p).r);
310 *q++ =
static_cast<Type>(255.0f*(*p).g);
311 *q++ =
static_cast<Type>(255.0f*(*p).b);
313 *q++ =
static_cast<Type>(255.0f*(*p).a);
321 std::string
name(fileName);
322 if (name.find_last_of(
".") == std::string::npos) name.append(
".ppm");
326 std::cerr <<
"Error opening PPM file \"" << name <<
"\"" << std::endl;
330 auto buf = this->convertToBitBuffer<unsigned char>(
false);
331 unsigned char* tmp =
buf.get();
333 os <<
"P6\n" << mWidth <<
" " << mHeight <<
"\n255\n";
334 os.write(reinterpret_cast<const char*>(&(*tmp)), 3 * mSize *
sizeof(
unsigned char));
337 size_t width()
const {
return mWidth; }
338 size_t height()
const {
return mHeight; }
343 size_t mWidth, mHeight, mSize;
344 std::unique_ptr<RGBA[]> mPixels;
355 double frameWidth,
double nearPlane,
double farPlane)
365 this->
initRay(nearPlane, farPlane);
382 const Vec3R dir = orig - xyz;
384 Mat4d xform = math::aim<Mat4d>(dir, up);
401 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const = 0;
439 double focalLength = 50.0,
440 double aperture = 41.2136,
441 double nearPlane = 1e-3,
443 :
BaseCamera(film,
rotation, translation, 0.5*aperture/focalLength, nearPlane, farPlane)
453 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const override
468 return 360.0 / math::pi<double>() *
atan(aperture/(2.0*length));
474 return aperture/(2.0*(
tan(fov * math::pi<double>() / 360.0)));
497 double frameWidth = 1.0,
498 double nearPlane = 1e-3,
506 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const override
542 template<
typename GridT = Film::RGBA,
559 typename GridT::ConstAccessor mAcc;
564 template<
typename SamplerType>
589 template<
typename GridT = Film::RGBA,
601 return Film::RGBA(v[0]*(normal[0]+1.0), v[1]*(normal[1]+1.0), v[2]*(normal[2]+1.0));
606 typename GridT::ConstAccessor mAcc;
611 template<
typename SamplerType>
620 return mRGBA *
Film::RGBA(normal[0] + 1.0, normal[1] + 1.0, normal[2] + 1.0);
636 template<
typename GridT = Film::RGBA,
643 , mInvDim(1.0/bbox.extents())
644 , mAcc(grid.getAccessor())
654 const Vec3R rgb = (xyz - mMin) * mInvDim;
660 const Vec3R mMin, mInvDim;
661 typename GridT::ConstAccessor mAcc;
666 template<
typename SamplerType>
671 : mMin(bbox.
min()), mInvDim(1.0/bbox.extents()), mRGBA(
c) {}
676 const Vec3R rgb = (xyz - mMin)*mInvDim;
677 return mRGBA*
Film::RGBA(rgb[0], rgb[1], rgb[2]);
682 const Vec3R mMin, mInvDim;
696 template<
typename GridT = Film::RGBA,
716 typename GridT::ConstAccessor mAcc;
721 template <
typename SamplerType>
750 template<
typename Gr
idT>
759 tracer(grid, shader, camera, pixelSamples, seed);
764 template<
typename Gr
idT,
typename IntersectorT>
766 const IntersectorT& inter,
781 template<
typename Gr
idT,
typename IntersectorT>
791 mShader(shader.
copy()),
797 template<
typename Gr
idT,
typename IntersectorT>
807 mShader(shader.
copy()),
813 template<
typename Gr
idT,
typename IntersectorT>
818 mInter(other.mInter),
819 mShader(other.mShader->
copy()),
820 mCamera(other.mCamera),
821 mSubPixels(other.mSubPixels)
825 template<
typename Gr
idT,
typename IntersectorT>
829 if (mIsMaster)
delete [] mRand;
832 template<
typename Gr
idT,
typename IntersectorT>
837 mInter = IntersectorT(grid);
840 template<
typename Gr
idT,
typename IntersectorT>
848 template<
typename Gr
idT,
typename IntersectorT>
853 mShader.reset(shader.
copy());
856 template<
typename Gr
idT,
typename IntersectorT>
864 template<
typename Gr
idT,
typename IntersectorT>
869 if (pixelSamples == 0) {
870 OPENVDB_THROW(ValueError,
"pixelSamples must be larger than zero!");
872 mSubPixels = pixelSamples - 1;
874 if (mSubPixels > 0) {
875 mRand =
new double[16];
877 for (
size_t i=0; i<16; ++i) mRand[i] = rand();
883 template<
typename Gr
idT,
typename IntersectorT>
887 tbb::blocked_range<size_t>
range(0, mCamera->height());
891 template<
typename Gr
idT,
typename IntersectorT>
897 const float frac = 1.0f / (1.0f +
float(mSubPixels));
898 for (
size_t j=range.begin(),
n=0, je = range.end();
j<je; ++
j) {
899 for (
size_t i=0, ie = mCamera->width(); i<ie; ++i) {
901 RayType ray = mCamera->getRay(i,
j);
902 Film::RGBA c = mInter.intersectsWS(ray, xyz, nml) ?
shader(xyz, nml, ray.dir()) : bg;
903 for (
size_t k=0; k<mSubPixels; ++k,
n +=2 ) {
904 ray = mCamera->getRay(i,
j, mRand[n & 15], mRand[(n+1) & 15]);
905 c += mInter.intersectsWS(ray, xyz, nml) ?
shader(xyz, nml, ray.dir()) : bg;
914 template<
typename IntersectorT,
typename SampleT>
917 : mAccessor(inter.grid().getConstAccessor())
919 , mPrimary(new IntersectorT(inter))
920 , mShadow(new IntersectorT(inter))
926 , mLightColor(0.7, 0.7, 0.7)
932 template<
typename IntersectorT,
typename SampleT>
935 : mAccessor(other.mAccessor)
936 , mCamera(other.mCamera)
937 , mPrimary(new IntersectorT(*(other.mPrimary)))
938 , mShadow(new IntersectorT(*(other.mShadow)))
939 , mPrimaryStep(other.mPrimaryStep)
940 , mShadowStep(other.mShadowStep)
941 , mCutOff(other.mCutOff)
942 , mLightGain(other.mLightGain)
943 , mLightDir(other.mLightDir)
944 , mLightColor(other.mLightColor)
945 , mAbsorption(other.mAbsorption)
946 , mScattering(other.mScattering)
950 template<
typename IntersectorT,
typename SampleT>
952 print(std::ostream& os,
int verboseLevel)
954 if (verboseLevel>0) {
955 os <<
"\nPrimary step: " << mPrimaryStep
956 <<
"\nShadow step: " << mShadowStep
957 <<
"\nCutoff: " << mCutOff
958 <<
"\nLightGain: " << mLightGain
959 <<
"\nLightDir: " << mLightDir
960 <<
"\nLightColor: " << mLightColor
961 <<
"\nAbsorption: " << mAbsorption
962 <<
"\nScattering: " << mScattering << std::endl;
964 mPrimary->print(os, verboseLevel);
967 template<
typename IntersectorT,
typename SampleT>
971 mPrimary.reset(
new IntersectorT(inter));
972 mShadow.reset(
new IntersectorT(inter));
975 template<
typename IntersectorT,
typename SampleT>
979 tbb::blocked_range<size_t>
range(0, mCamera->height());
983 template<
typename IntersectorT,
typename SampleT>
990 const Vec3R extinction = -mScattering-mAbsorption, One(1.0);
991 const Vec3R albedo = mLightColor*mScattering/(mScattering+mAbsorption);
992 const Real sGain = mLightGain;
993 const Real pStep = mPrimaryStep;
994 const Real sStep = mShadowStep;
995 const Real cutoff = mCutOff;
1004 std::vector<typename RayType::TimeSpan> pTS, sTS;
1009 for (
size_t j=range.begin(), je = range.end();
j<je; ++
j) {
1010 for (
size_t i=0, ie = mCamera->width(); i<ie; ++i) {
1012 bg.
a = bg.
r = bg.
g = bg.
b = 0;
1013 RayType pRay = mCamera->getRay(i,
j);
1014 if( !mPrimary->setWorldRay(pRay))
continue;
1015 Vec3R pTrans(1.0), pLumi(0.0);
1018 while (mPrimary->march(pT0, pT1)) {
1019 for (
Real pT = pStep*
ceil(pT0/pStep); pT <= pT1; pT += pStep) {
1021 mPrimary->hits(pTS);
1022 for (
size_t k=0; k<pTS.size(); ++k) {
1023 Real pT = pStep*
ceil(pTS[k].t0/pStep), pT1=pTS[k].t1;
1024 for (; pT <= pT1; pT += pStep) {
1026 Vec3R pPos = mPrimary->getWorldPos(pT);
1028 if (density < cutoff)
continue;
1032 if( !mShadow->setWorldRay(sRay))
continue;
1035 while (mShadow->march(sT0, sT1)) {
1036 for (
Real sT = sStep*
ceil(sT0/sStep); sT <= sT1; sT+= sStep) {
1039 for (
size_t l=0; l<sTS.size(); ++l) {
1040 Real sT = sStep*
ceil(sTS[l].t0/sStep), sT1=sTS[l].t1;
1041 for (; sT <= sT1; sT+= sStep) {
1043 const Real d =
sampler.wsSample(mShadow->getWorldPos(sT));
1044 if (d < cutoff)
continue;
1045 sTrans *=
math::Exp(extinction * d * sStep/(1.0+sT*sGain));
1046 if (sTrans.
lengthSqr()<cutoff)
goto Luminance;
1050 pLumi += albedo * sTrans * pTrans * (One-dT);
1052 if (pTrans.lengthSqr()<cutoff)
goto Pixel;
1070 #ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION
1072 #ifdef OPENVDB_INSTANTIATE_RAYTRACER
1076 #define _FUNCTION(TreeT) \
1077 void rayTrace(const Grid<TreeT>&, const BaseShader&, BaseCamera&, size_t, unsigned int, bool)
1081 #define _FUNCTION(TreeT) \
1082 void rayTrace(const Grid<TreeT>&, const tools::LevelSetRayIntersector<Grid<TreeT>>&, const BaseShader&, BaseCamera&, size_t, unsigned int, bool)
1089 #endif // OPENVDB_USE_EXPLICIT_INSTANTIATION
1096 #endif // OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
GLenum GLuint GLenum GLsizei const GLchar * buf
void setTimes(RealT t0=math::Delta< RealT >::value(), RealT t1=std::numeric_limits< RealT >::max())
Simple generator of random numbers over the range [0, 1)
bool normalize(T eps=T(1.0e-7))
this = normalized this
GridType
List of types that are currently supported by NanoVDB.
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
void setEye(const Vec3Type &eye)
GLsizei const GLfloat * value
const GLuint GLenum const void * binary
GLdouble GLdouble GLdouble z
#define OPENVDB_REAL_TREE_INSTANTIATE(Function)
void setDir(const Vec3Type &dir)
GLboolean GLboolean GLboolean GLboolean a
GLuint GLsizei GLsizei * length
#define OPENVDB_USE_VERSION_NAMESPACE
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLdouble GLdouble GLdouble q
T dot(const Vec3< T > &v) const
Dot product.
Vec3d applyJacobian(const Vec3d &in, const Vec3d &) const override
Return the Jacobian of the map applied to in.
Accelerated intersection of a ray with a narrow-band level set or a generic (e.g. density) volume...
#define OPENVDB_ASSERT(X)
GA_API const UT_StringHolder scale
GLint GLsizei GLsizei height
void accumPostRotation(Axis axis, double radians)
Modify the existing affine map by post-applying the given operation.
#define OPENVDB_INSTANTIATE_CLASS
Coord Abs(const Coord &xyz)
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
A general linear transform using homogeneous coordinates to perform rotation, scaling, shear and translation.
void postTranslate(const Vec3< T0 > &tr)
Right multiplies by the specified translation matrix, i.e. (*this) * Trans.
OIIO_UTIL_API void parallel_for(int32_t begin, int32_t end, function_view< void(int32_t)> task, paropt opt=0)
png_const_structrp png_const_inforp int * unit
GLfloat GLfloat GLfloat alpha
SIM_API const UT_StringHolder rotation
GLuint const GLchar * name
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
GLboolean GLboolean GLboolean b
GA_API const UT_StringHolder transform
Vec3d applyMap(const Vec3d &in) const override
Return the image of in under the map.
SYS_API double tan(double x)
GLfloat GLfloat GLfloat GLfloat h
IMATH_HOSTDEVICE constexpr int ceil(T x) IMATH_NOEXCEPT
Axis-aligned bounding box.
const Vec3T & dir() const
IMATH_NAMESPACE::V2f IMATH_NAMESPACE::Box2i std::string this attribute is obsolete as of OpenEXR v3 float
SYS_API double atan(double x)
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void accumPostTranslation(const Vec3d &v)
Modify the existing affine map by post-applying the given operation.
GLubyte GLubyte GLubyte GLubyte w
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
void scaleTimes(RealT scale)
#define OPENVDB_THROW(exception, message)
Type Exp(const Type &x)
Return ex.