Add NEE for heterogenous media.

This commit is contained in:
hal8174 2024-06-06 15:04:46 +02:00
parent fa42ce7dca
commit d26a2309c7
2 changed files with 168 additions and 2 deletions

View file

@ -141,11 +141,175 @@ Vec3fa Application2::renderPixel(float x, float y, const ISPCCamera& camera, Ray
return renderPixelHomogeneous(x, y, camera, stats, sampler); return renderPixelHomogeneous(x, y, camera, stats, sampler);
} else if (selected == 2) { } else if (selected == 2) {
return renderPixelHeterogeneous(x, y, camera, stats, sampler); return renderPixelHeterogeneous(x, y, camera, stats, sampler);
} else if (selected == 3) {
return renderPixelHeterogeneousNEE(x, y, camera, stats, sampler);
} else { } else {
return Vec3fa(0.0f); return Vec3fa(0.0f);
} }
} }
Vec3fa Application2::renderPixelHeterogeneousNEE(float x, float y, const ISPCCamera& camera, RayStats& stats, RandomSampler& sampler) {
Vec3fa L = Vec3fa(0.0f);
Vec3fa Lw = Vec3fa(1.0f);
bool in_volume = false;
float max_density = 1.32 * density; // tested
/* initialize ray */
Ray ray(Vec3fa(camera.xfm.p), Vec3fa(normalize(x * camera.xfm.l.vx + y * camera.xfm.l.vy + camera.xfm.l.vz)), 0.0f,
inf);
for (int i = 0; i < ray_depth; i++) {
/* intersect ray with scene */
RTCIntersectArguments iargs;
rtcInitIntersectArguments(&iargs);
iargs.feature_mask = RTC_FEATURE_FLAG_TRIANGLE;
rtcIntersect1(data.g_scene, RTCRayHit_(ray), &iargs);
RayStats_addRay(stats);
const Vec3fa wo = neg(ray.dir);
float t;
if (!in_volume | (density == 0)) {
t = inf;
} else {
// printf("%f\n", density);
t = - std::log(1.0 - RandomSampler_get1D(sampler)) / max_density;
}
if (t > 0.0 && t < ray.tfar) {
if (ray.geomID == RTC_INVALID_GEOMETRY_ID) {
// printf("here(%f,%f,%f)\n", ray.org.x, ray.org.y, ray.org.z);
break;
}
float r = RandomSampler_get1D(sampler);
Vec3fa p = ray.org + t * ray.dir;
// printf("%f\t%f\t(%f,%f,%f)\n", t, ray.tfar, p.x, p.y, p.z);
float s = data.densityGrid->sampleW(p);
// if (s != -1.0) {
// printf("(%f,%f,%f)\tt: %f\ttfar: %f\tr: %f\t%f\ts: %f\t%f\tp:%f\n",p.x, p.y, p.z, t, ray.tfar, r, r * max_density, s, s * density, (s * density) / max_density);
// }
// if (s > 0.5) {
// printf("(%f,%f,%f)\tr: %f\t%f\ts: %f\t%f\tp:%f\n",p.x, p.y, p.z , r, r * max_density, s, s * density, (s * density) / max_density);
// }
if (RandomSampler_get1D(sampler) * max_density < s * density) {
// NEE
int id = (int)(RandomSampler_get1D(sampler) * data.scene->lights.size());
if (id == data.scene->lights.size())
id = data.scene->lights.size() - 1;
const Light* l = data.scene->lights.at(id);
Sample sample;
sample.P = ray.org + t * ray.dir;
sample.Ng = ray.dir;
sample.Ns = ray.dir;
Light_SampleRes ls = Lights_sample(l, sample, RandomSampler_get2D(sampler));
/* initialize shadow ray */
Ray shadow(sample.P, ls.dir, EPS, ls.dist - EPS, 0.0f);
/* trace shadow ray */
RTCOccludedArguments sargs;
rtcInitOccludedArguments(&sargs);
sargs.feature_mask = RTC_FEATURE_FLAG_TRIANGLE;
rtcOccluded1(data.g_scene, RTCRay_(shadow), &sargs);
RayStats_addShadowRay(stats);
if (shadow.tfar >= 0.0) {
float nee_T = 1.0;
float nee_t = 0.0;
while (1) {
float r = RandomSampler_get1D(sampler);
nee_t -= std::log(1 - r) / max_density;
if (shadow.tfar >= nee_t) {
break;
}
nee_T *= 1 - data.densityGrid->sampleW(shadow.org + nee_t * shadow.dir) / max_density;
}
float scatter = phase(scattering_parameter, -dot(-ray.dir,shadow.dir));
/* add light contribution if not occluded (NEE) */
L += Lw * nee_T * scatter * (1.0 - absorbtion) * ls.weight / data.scene->lights.size();
}
float pdf;
Vec3fa o = sample_phase_function(-ray.dir, scattering_parameter, RandomSampler_get2D(sampler), pdf);
float temp = data.tempGrid->sampleW(p); // Sample density from the grid
// float temp = 20;
float redWavelength = 700;
float greenWavelength = 530;
float blueWavelength = 470;
Vec3fa emissive = Vec3fa(0.0, 0.0, 0.0);
if (temp != -1.0f && temp > 0.001) {
temp *= tempearture_multiplier;
emissive = Vec3f(blackbody_radiance_normalized(redWavelength, temp),
blackbody_radiance_normalized(greenWavelength, temp),
blackbody_radiance_normalized(blueWavelength, temp));
}
L += Lw * absorbtion * emissive;
Lw *= 1.0 - absorbtion;
ray = Ray(ray.org + t * ray.dir,o,EPS,inf);
} else {
// printf("volume(%f,%f,%f), t: %f, tfar: %f\n", ray.org.x, ray.org.y, ray.org.z, t, ray.tfar);
ray = Ray(ray.org + t * ray.dir, ray.dir, EPS, inf);
i--;
}
} else if (ray.geomID != RTC_INVALID_GEOMETRY_ID) {
Sample sample = createSample(ray);
int matId = data.scene->geometries.at(ray.geomID)->materialID;
unsigned lightID = data.scene->geometries.at(ray.geomID)->lightID;
if (lightID != unsigned(-1)) {
if (i == 0) {
const Light* l = data.scene->lights[lightID];
Light_EvalRes evalRes = Lights_eval(l, sample, -wo);
L += Lw * evalRes.value;
}
break;
}
/* calculate BRDF */
BRDF brdf;
std::vector<Material *> material_array = data.scene->materials;
Material__preprocess(material_array, matId, brdf, wo, sample);
if (brdf.name == "default") {
// printf("surface\n");
in_volume = !in_volume;
// in_volume = dot(normalize(ray.Ng), ray.dir) < 0;
ray = Ray(ray.org + ray.tfar * ray.dir, ray.dir, EPS, inf);
i--;
}
}
}
return L;
}
Vec3fa Application2::renderPixelHeterogeneous(float x, float y, const ISPCCamera& camera, RayStats& stats, RandomSampler& sampler) { Vec3fa Application2::renderPixelHeterogeneous(float x, float y, const ISPCCamera& camera, RayStats& stats, RandomSampler& sampler) {
Vec3fa L = Vec3fa(0.0f); Vec3fa L = Vec3fa(0.0f);
@ -181,6 +345,7 @@ Vec3fa Application2::renderPixelHeterogeneous(float x, float y, const ISPCCamera
if (t > 0.0 && t < ray.tfar) { if (t > 0.0 && t < ray.tfar) {
if (ray.geomID == RTC_INVALID_GEOMETRY_ID) { if (ray.geomID == RTC_INVALID_GEOMETRY_ID) {
// printf("here(%f,%f,%f)\n", ray.org.x, ray.org.y, ray.org.z);
break; break;
} }

View file

@ -14,6 +14,7 @@ private:
Vec3fa renderPixelOrig(float x, float y, const ISPCCamera& camera, RayStats& stats, RandomSampler& sampler); Vec3fa renderPixelOrig(float x, float y, const ISPCCamera& camera, RayStats& stats, RandomSampler& sampler);
Vec3fa renderPixelHomogeneous(float x, float y, const ISPCCamera& camera, RayStats& stats, RandomSampler& sampler); Vec3fa renderPixelHomogeneous(float x, float y, const ISPCCamera& camera, RayStats& stats, RandomSampler& sampler);
Vec3fa renderPixelHeterogeneous(float x, float y, const ISPCCamera& camera, RayStats& stats, RandomSampler& sampler); Vec3fa renderPixelHeterogeneous(float x, float y, const ISPCCamera& camera, RayStats& stats, RandomSampler& sampler);
Vec3fa renderPixelHeterogeneousNEE(float x, float y, const ISPCCamera& camera, RayStats& stats, RandomSampler& sampler);
void drawGUI() override { void drawGUI() override {
ImGui::Checkbox("Bounding Box", &boundingBox); ImGui::Checkbox("Bounding Box", &boundingBox);
@ -24,8 +25,8 @@ private:
ray_depth = 1; ray_depth = 1;
} }
const char* items[] = {"Original", "Homogeneous", "Heterogeneous"}; const char* items[] = {"Original", "Homogeneous", "Heterogeneous", "HeterogeneousNEE"};
if (ImGui::Combo("Version", &selected, items, 3)) if (ImGui::Combo("Version", &selected, items, 4))
clear(); clear();
if (ImGui::SliderFloat("density", &density, 0.0, 1.0)) if (ImGui::SliderFloat("density", &density, 0.0, 1.0))