Skip to content

Commit

Permalink
assignment 4 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyskeys committed May 3, 2022
1 parent 07c8837 commit 2293daa
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/build
/build_debug
.DS_Store
/nori
/warptest
Expand Down
5 changes: 4 additions & 1 deletion include/nori/accel.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ NORI_NAMESPACE_BEGIN
*/
class Accel {
public:
Accel(std::vector<Mesh*> *ms) : m_meshes(ms) {}

/**
* \brief Register a triangle mesh for inclusion in the acceleration
* data structure
Expand Down Expand Up @@ -82,7 +84,8 @@ class Accel {
bool rayIntersect(const Ray3f &ray, Intersection &its, bool shadowRay) const;

private:
Mesh *m_mesh = nullptr; ///< Mesh (only a single one for now)
//Mesh *m_mesh = nullptr; ///< Mesh (only a single one for now)
std::vector<Mesh*> *m_meshes = nullptr;
BoundingBox3f m_bbox; ///< Bounding box of the entire scene

#ifdef USE_OCTREE_ACCEL
Expand Down
3 changes: 2 additions & 1 deletion include/nori/emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ struct EmitterQueryRecord {
: ref(r)
{}

EmitterQueryRecord(const Point3f &r, const Point3f &p)
EmitterQueryRecord(const Point3f &r, const Point3f &p, const Normal3f &n)
: ref(r)
, pos(p)
, n(n)
{
wi = (pos - ref).normalized();
}
Expand Down
3 changes: 3 additions & 0 deletions include/nori/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class Scene : public NoriObject {
/// Return a reference to an array containing all meshes
const std::vector<Mesh *> &getMeshes() const { return m_meshes; }

/// Return a reference to an array of emitters
std::vector<Emitter *> &getEmitters() { return m_emitters; }

/// Return a random light
const Emitter *getRandomEmitter(const float &xi, float &pdf) const;

Expand Down
20 changes: 19 additions & 1 deletion src/accel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
NORI_NAMESPACE_BEGIN

void Accel::addMesh(Mesh *mesh) {
/*
if (m_mesh)
throw NoriException("Accel: only a single mesh is supported!");
m_mesh = mesh;
m_bbox = m_mesh->getBoundingBox();
*/
}

#ifdef USE_OCTREE_ACCEL
Expand Down Expand Up @@ -78,6 +80,7 @@ void Accel::build() {
rtcSetSceneFlags(m_scene, RTC_SCENE_FLAG_ROBUST);
rtcSetSceneBuildQuality(m_scene, RTC_BUILD_QUALITY_HIGH);

/*
RTCGeometry geom = rtcNewGeometry(m_device, RTC_GEOMETRY_TYPE_TRIANGLE);
rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, 0,
RTC_FORMAT_FLOAT3, m_mesh->getVertexPositionsPtr(), 0, sizeof(float) * 3,
Expand All @@ -88,6 +91,20 @@ void Accel::build() {
rtcCommitGeometry(geom);
rtcAttachGeometryByID(m_scene, geom, 0); // ID 0 since we only have one mesh now
rtcReleaseGeometry(geom);
*/

for (size_t i = 0; const auto mesh_ptr : *m_meshes) {
RTCGeometry geom = rtcNewGeometry(m_device, RTC_GEOMETRY_TYPE_TRIANGLE);
rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, 0,
RTC_FORMAT_FLOAT3, mesh_ptr->getVertexPositionsPtr(), 0, sizeof(float) * 3,
mesh_ptr->getVertexCount());
rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_INDEX, 0,
RTC_FORMAT_UINT3, mesh_ptr->getIndicesPtr(), 0, sizeof(uint32_t) * 3,
mesh_ptr->getTriangleCount());
rtcCommitGeometry(geom);
rtcAttachGeometryByID(m_scene, geom, i++);
rtcReleaseGeometry(geom);
}

rtcCommitScene(m_scene);

Expand Down Expand Up @@ -154,7 +171,8 @@ bool Accel::rayIntersect(const Ray3f &ray_, Intersection &its, bool shadowRay) c

its.t = rayhit.ray.tfar;
its.uv = Point2f(rayhit.hit.u, rayhit.hit.v);
its.mesh = m_mesh;
//its.mesh = m_mesh;
its.mesh = m_meshes->at(rayhit.hit.geomID);
f = rayhit.hit.primID;
foundIntersection = true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ void AreaLight::setParent(NoriObject *parent) {
Color3f AreaLight::sample(Sampler *sampler, EmitterQueryRecord &erec, float &pdf) const {
m_mesh->sample(sampler, erec.pos, erec.n, pdf);
erec.wi = (erec.pos - erec.ref).normalized();
float cosTheta = erec.n.dot(-erec.wi);
if (cosTheta > 0.f) {
auto distance2 = (erec.pos - erec.ref).squaredNorm();
pdf = pdf * distance2 / cosTheta;
}

if (pdf > 0.f)
return eval(erec) / pdf;
Expand Down
1 change: 1 addition & 0 deletions src/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <nori/bbox.h>
#include <nori/bsdf.h>
#include <nori/emitter.h>
#include <nori/scene.h>
#include <nori/warp.h>
#include <Eigen/Geometry>
#include <tbb/parallel_for.h>
Expand Down
15 changes: 11 additions & 4 deletions src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
NORI_NAMESPACE_BEGIN

Scene::Scene(const PropertyList &) {
m_accel = new Accel();
//m_accel = new Accel();
m_accel = new Accel(&m_meshes);
}

Scene::~Scene() {
Expand Down Expand Up @@ -60,6 +61,11 @@ void Scene::activate() {
NoriObjectFactory::createInstance("independent", PropertyList()));
}

for (const auto mesh : m_meshes) {
if (mesh->isEmitter())
m_emitters.push_back(mesh->getEmitter());
}

cout << endl;
cout << "Configuration: " << toString() << endl;
cout << endl;
Expand All @@ -69,14 +75,15 @@ void Scene::addChild(NoriObject *obj) {
switch (obj->getClassType()) {
case EMesh: {
Mesh *mesh = static_cast<Mesh *>(obj);
m_accel->addMesh(mesh);
//m_accel->addMesh(mesh);
m_meshes.push_back(mesh);
}
break;

case EEmitter: {
Emitter *emitter = static_cast<Emitter *>(obj);
m_emitters.push_back(emitter);
//Emitter *emitter = static_cast<Emitter *>(obj);
/* TBD */
throw NoriException("Scene::addChild(): You need to implement this for emitters");
}
break;

Expand Down
17 changes: 9 additions & 8 deletions src/whitted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class WhittedIntegrator : public Integrator {
// Calculate Le if primary ray hit emitter
Color3f Le(0.f);
if (its.mesh->isEmitter()) {
EmitterQueryRecord erec(ray.o, its.p);
EmitterQueryRecord erec(ray.o, its.p, its.shFrame.n);
Le = its.mesh->getEmitter()->eval(erec);
}

Expand All @@ -29,18 +29,19 @@ class WhittedIntegrator : public Integrator {
float lpdf;
auto Ls = emitter->sample(sampler, erec, lpdf);

Ray3f shadow_ray;
shadow_ray.o = its.p;
auto light_dir = (erec.pos - its.p).normalized();
shadow_ray.d = light_dir;
auto light_vec = (erec.pos - its.p);
auto light_dir = light_vec.normalized();
Ray3f shadow_ray(its.p, light_dir, Epsilon, light_vec.norm() - Epsilon);
if (scene->rayIntersect(shadow_ray))
return Le;

auto bsdf = its.mesh->getBSDF();
BSDFQueryRecord brec(light_dir);
auto f = bsdf->eval(brec);
BSDFQueryRecord brec(its.toLocal(-ray.d), its.toLocal(erec.wi), ESolidAngle);
Color3f f = bsdf->eval(brec);
auto cosTheta = Frame::cosTheta(its.shFrame.toLocal(light_dir));
auto Li = f * Ls * cosTheta / pdf;
if (cosTheta < 0.f)
cosTheta = 0.f;
Color3f Li = f * Ls * cosTheta / pdf;

return Le + Li;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integrator/normal_integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using namespace nori;

int main() {
filesystem::path path("../scenes/pa2/ajax-normals.xml");
filesystem::path path("../scenes/pa4/cbox/cbox-distributed.xml");

try {
getFileResolver()->prepend(path.parent_path());
Expand All @@ -24,7 +24,7 @@ int main() {
if (root->getClassType() == NoriObject::EScene) {
auto scene = static_cast<Scene*>(root.get());
std::unique_ptr<Sampler> sampler(scene->getSampler()->clone());
Point2f pixelSample = Point2f(250.f, 698.f) + sampler->next2D();
Point2f pixelSample = Point2f(400.f, 60.f) + sampler->next2D();
Point2f apertureSample = sampler->next2D();

Ray3f ray;
Expand Down

0 comments on commit 2293daa

Please sign in to comment.