Skip to content

Commit

Permalink
Implement AABB merge & scene radius (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjessesco authored Dec 30, 2024
1 parent e1e3101 commit bf5815b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions include/aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ namespace Caramel{

std::tuple<bool, Float, Float> ray_intersect(const Ray &ray) const;

static AABB merge(const AABB &a, const AABB &b) {
return {{std::min(a.m_min[0], b.m_min[0]),
std::min(a.m_min[1], b.m_min[1]),
std::min(a.m_min[2], b.m_min[2])},
{std::max(a.m_max[0], b.m_max[0]),
std::max(a.m_max[1], b.m_max[1]),
std::max(a.m_max[2], b.m_max[2])}};
}

Vector3f m_min;
Vector3f m_max;
};
Expand Down
4 changes: 4 additions & 0 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ namespace Caramel{
return {std::sqrt(a[0]), std::sqrt(a[1]), std::sqrt(a[2])};
}

inline Float L2(const Vector3f &a, const Vector3f &b) {
return std::sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]) + (a[2] - b[2]) * (a[2] - b[2]));
}

template <typename T>
inline T interpolate(const T &a, const T &b, const T &c, Float u, Float v){
return (a*(Float1-u-v)) + (b*u) + (c*v);
Expand Down
4 changes: 4 additions & 0 deletions include/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <vector>

#include <common.h>
#include <aabb.h>

namespace Caramel{
class Camera;
Expand All @@ -51,6 +52,9 @@ namespace Caramel{

std::vector<const Light*> m_lights;
std::vector<const Shape*> m_meshes;
Vector3f m_sceneCenterPos;
Float m_sceneRadius;
AABB m_aabb;
const Camera *m_cam;
};
}
9 changes: 8 additions & 1 deletion src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
#include <light.h>
#include <rayintersectinfo.h>
#include <sampler.h>
#include <aabb.h>

namespace Caramel{

Scene::Scene() : m_cam{nullptr} {}
Scene::Scene()
: m_cam{nullptr}, m_aabb{Vector3f::zeros(), Vector3f::zeros()}, m_sceneRadius{0.0f}, m_sceneCenterPos{Vector3f::zeros()} {}

void Scene::set_camera(Camera *camera) {
m_cam = camera;
Expand Down Expand Up @@ -68,6 +70,11 @@ namespace Caramel{
if(shape->is_light()){
m_lights.push_back(shape->get_arealight());
}

m_aabb = m_meshes.empty() ? shape->get_aabb() :
AABB::merge(m_aabb, shape->get_aabb());
m_sceneCenterPos = (m_aabb.m_max + m_aabb.m_min) * 0.5f;
m_sceneRadius = L2(m_sceneCenterPos, m_aabb.m_max);
}

void Scene::add_light(const Light *light){
Expand Down

0 comments on commit bf5815b

Please sign in to comment.