90 if (survival <= 0.0f)
return 0.0f;
91 if (survival >= 1.0f)
return 1.0f;
93 return (
rnd01() < survival) ? survival : 0.0f;
140 return (exponent + 2.0f) / (exponent + 1.0f) * cosTheta;
153 if (pdfThis <= 0.0f)
return 0.0f;
155 SLfloat ratio = pdfOther / pdfThis;
156 return 1.0f / (1.0f + ratio * ratio);
170 if (!rect->isOn())
return 0.0f;
177 SLfloat cosLight = -dir.
dot(rect->spotDirWS());
178 if (cosLight <= 0.0f)
return 0.0f;
180 return (ray->
length * ray->
length) / (cosLight * rect->area());
226 _lumSum.assign(numPixels, 0.0);
242 SL_LOG(
"\nCurrent Sample: ");
243 for (
int currentSample = 1; currentSample <=
_aaSamples; currentSample++)
245 vector<thread> threads;
255 for (
auto& thread : threads)
304 SLint maxX = std::min(minX + 4, imgW);
306 for (
SLint x = minX; x < maxX; ++x)
308 for (
SLint y = 0; y < imgH; ++y)
341 const size_t pixel = (size_t)y * (
size_t)imgW + (size_t)x;
344 radianceSum += color;
362 color = radianceSum / (
SLfloat)currentSample;
409 if (root) root->
hitRec(ray);
414 if (ray->
length >= FLT_MAX)
452 return mat->
emissive() * absorption;
456 if (lightPdf <= 0.0f)
470 return mat->
emissive() * absorption;
480 objectColor = objectColor * absorption;
492 finalColor +=
shade(ray, &objectColor) * scaleBy;
520 finalColor += (
trace(&scatter, scatterPdf) & objectColor) *
521 (scaleBy / survival);
529 if (survival <= 0.0f)
return finalColor;
548 finalColor += (lobeWeight * (
trace(&reflected,
PDF_NO_MIS) & objectColor)) *
549 (scaleBy / survival);
557 if (survival <= 0.0f)
return finalColor;
586 nbig = n > nt ? n : nt;
587 nsmall = n < nt ? n : nt;
588 SLfloat R0 = ((nbig - nsmall) / (nbig + nsmall));
590 SLbool into = (rayDir * hitNormal) < 0;
591 SLfloat c = 1.0f - (into ? (-rayDir * hitNormal) : (refrDir * hitNormal));
592 SLfloat schlick = R0 + (1 - R0) * c * c * c * c * c;
594 SLfloat P = 0.25f + 0.5f * schlick;
595 SLfloat reflectionProbability = schlick / P;
596 SLfloat refractionProbability = (1.0f - schlick) / (1.0f - P);
600 SLbool refractIsValid =
true;
603 refractIsValid = ray->
refractMC(&refracted,
613 if (
rnd01() > (0.25f + 0.5f * schlick))
618 finalColor += (lobeWeight *
620 refractionProbability) *
621 (scaleBy / survival);
660 finalColor += (reflLobeWeight *
662 reflectionProbability) *
663 (scaleBy / survival);
705 if (!light || !light->isOn())
continue;
718 if (distSqr < FLT_EPSILON)
continue;
725 SLfloat cosLight = -toLight.
dot(rect->spotDirWS());
728 if (cosSurface <= 0.0f || cosLight <= 0.0f)
continue;
732 SLRay shadowRay(dist, toLight, ray);
733 root3D->
hitRec(&shadowRay);
734 if (shadowRay.
length < dist)
continue;
737 SLfloat geometry = cosSurface * cosLight / distSqr;
749 SLfloat lightPdf = distSqr / (cosLight * rect->area());
755 color += (*mat & light->diffuse()) *
770 if (LdN <= 0.0f)
continue;
772 SLfloat lighted = light->shadowTestMC(ray, L, lightDist, root3D);
773 if (lighted <= 0.0f)
continue;
777 if (light->spotCutOffDEG() < 180.0f)
779 SLfloat LdS = std::max(-L.
dot(light->spotDirWS()), 0.0f);
782 if (LdS <= light->spotCosCut())
continue;
784 spotEffect = pow(LdS, (
SLfloat)light->spotExponent());
788 SLCol4f diffuseColor = (*mat & (light->diffuse() * LdN)) *
791 color += light->attenuation(lightDist) * spotEffect * diffuseColor;
824 const size_t numPixels =
_lumSum.size();
840 vector<SLfloat> rse(numPixels);
843 for (
size_t i = 0; i < numPixels; ++i)
849 SLdouble variance = (s2 - s1 * s1 / n) / (n - 1.0);
857 rse[i] = (
SLfloat)(sqrt(variance / n) / (mu + eps));
866 size_t k = (size_t)(0.999 * (
SLdouble)numPixels);
869 std::nth_element(rse.begin(),
870 rse.begin() + (std::ptrdiff_t)k,
882 "Pathtraced_%d_%d.png",
#define PROFILE_FUNCTION()
#define PROFILE_THREAD(name)
float SLfloat
analog to GLfloat
#define SL_LOG(...)
Some debugging and error handling macros.
unsigned int SLuint
analog to GLuint
char SLchar
analog to GLchar (char is signed [-128 ... 127]!)
double SLdouble
analog to GLdouble
bool SLbool
analog to GLbool
static SLfloat misWeight(SLfloat pdfThis, SLfloat pdfOther)
Power heuristic (beta = 2) weight of the strategy whose density is pdfThis.
static SLfloat russianRoulette(SLint depth, SLCol4f albedo)
Russian roulette survival test for the continuation of a path.
static const SLfloat PDF_NO_MIS
Sentinel pdf for SLPathtracer::trace.
static const SLint RR_START_DEPTH
Path depth up to which Russian roulette always lets the path continue.
static SLfloat lightPdfMC(SLRay *ray)
Solid angle density with which shade() would have sampled this light hit.
static SLfloat phongLobeWeight(SLfloat exponent, const SLVec3f &sampleDir, const SLVec3f &normal)
Weight of one sample drawn from the Phong lobe of the given exponent.
CVVImage _images
Vector of CVImage pointers.
std::mutex _mutex
Mutex to protect parallel access (used in ray tracing)
Abstract Light class for OpenGL light sources.
Light node class for a rectangular light source.
Defines a standard CG material with textures and a shader program.
void translucency(SLfloat transl)
static SLfloat PERFECT
PM: shininess/translucency limit.
void specular(const SLCol4f &spec)
void diffuse(const SLCol4f &diff)
void shininess(SLfloat shin)
void transmissive(const SLCol4f &transm)
void emissive(const SLCol4f &emis)
virtual void preShade(SLRay *ray)
SLNode represents a node in a hierarchical scene graph.
virtual bool hitRec(SLRay *ray)
const SLstring & name() const
SLbool _calcIndirect
flag to calculate indirect illumination
SLCol4f shade(SLRay *ray, SLCol4f *mat)
SLbool render(SLSceneView *sv)
vector< SLdouble > _lumSumSq
SLbool _calcDirect
flag to calculate direct illumination
vector< SLCol4f > _radianceSum
Linear, unclamped sum of all radiance samples taken so far per pixel.
SLfloat _noiseRSE
mean relative standard error, see noiseRSE()
void renderSlices(bool isMainThread, SLint currentSample, SLuint threadNum)
void saveImage()
Saves the current PT image as PNG image.
SLfloat _sampleClamp
Upper limit on the radiance of a single sample, 0 to switch it off.
function< void(bool, int, SLuint)> renderSlicesPTAsync
SLfloat _noiseRSE999
the same for the worst 0.1%, see noiseRSE999()
SLCol4f trace(SLRay *ray, SLfloat bsdfPdf)
Traces one ray. bsdfPdf is the solid angle density with which the.
vector< SLdouble > _lumSum
Sum and sum of squares of the luminance of every sample, per pixel.
Ray class with ray and intersection properties.
SLbool hitMatIsTransparent() const
Returns true if the hit material transmission color is not black.
bool reflectMC(SLRay *reflected, const SLMat3f &rotMat) const
SLCol4f backgroundColor
Background color at pixel x,y.
SLMesh * hitMesh
Points to the intersected mesh.
SLbool isOutside
Flag if ray is inside of a material.
SLint depth
Recursion depth for ray tracing.
SLVec3f dir
Direction vector of ray in WS.
void refract(SLRay *refracted)
static SLMat3f lobeToWorld(const SLVec3f &lobeAxis)
Rotation matrix that maps a sample drawn around +z onto lobeAxis.
SLbool hitMatIsReflective() const
Returns true if the hit material specular color is not black.
SLfloat length
length from origin to an intersection
SLNode * hitNode
Points to the intersected node.
SLVec3f hitPoint
Point of intersection.
void diffuseMC(SLRay *scattered) const
static SLuint totalNumRays()
Total NO. of rays shot during RT.
bool refractMC(SLRay *refracted, const SLMat3f &rotMat) const
void reflect(SLRay *reflected) const
SLCol4f hitTexColor
Color at intersection for texture or color attributes.
SLVec3f hitNormal
Surface normal at intersection point.
SLbool hitMatIsDiffuse() const
Returns true if the hit material diffuse color is not black.
SLfloat _oneOverGamma
one over gamma correction value
virtual void prepareImage()
SLSceneView * _sv
Parent sceneview.
SLint _nextLine
next line index to render RT in a thread
SLRTState _state
RT state;.
SLfloat _renderSec
Rendering time in seconds.
SLint _aaSamples
SQRT of uneven num. of AA samples.
void renderUIBeforeUpdate()
Must be called before an inbetween frame updateRec.
void setPrimaryRay(SLfloat x, SLfloat y, SLRay *primaryRay)
Set the parameters of a primary ray for a pixel position at x, y.
virtual void initStats(SLint depth)
SLint _progressPC
progress in %
AvgFloat _raysPerMS
Rays per ms of the last completed render.
void root3D(SLNode *root3D)
SceneView class represents a dynamic real time 3D view onto the scene.
cbOnWndUpdate onWndUpdate
C-Callback for app for intermediate window repaint.
T dot(const SLVec3 &v) const
void gammaCorrect(T oneOverGamma)
Gamma correction.
void clampMinMax(const T min, const T max)
void set(T value)
Sets the current value in the value array and builds the average.
static const float ONEOVERPI
unsigned int maxThreads()
Returns in release config the max. NO. of threads otherwise 1.