Skip to content
Snippets Groups Projects
Group.cpp 1.23 KiB
Newer Older
Postea's avatar
Postea committed
#include "Group.h"
#include "../material/Material.h"
#include "../tools/Vec3.h"
Postea's avatar
Postea committed

namespace shapes {
Group::Group(const util::Transformation& transform)
    : shapeList(std::vector<std::shared_ptr<Shape>>())
    , transform(transform)
{
}
Group::Group(const util::Mat4& matrix)
    : shapeList(std::vector<std::shared_ptr<Shape>>())
    , transform(util::Transformation(matrix))
{
}
Postea's avatar
Postea committed

std::shared_ptr<cam::Hit> Group::intersect(const cam::Ray& r)
{
    cam::Ray imagR(transform.fromWorld.transformPoint(r.x0), transform.fromWorld.transformDir(r.d), r.tmin, r.tmax, r.normalize);
Postea's avatar
Postea committed

    std::shared_ptr<cam::Hit> result;
Postea's avatar
Postea committed

    for (std::shared_ptr<Shape> s : shapeList) {
        std::shared_ptr<cam::Hit> temp = s->intersect(imagR);
        if (temp != nullptr) {
            if (result == nullptr) {
                result = temp;
            } else if (result->t > temp->t) {
                result = temp;
            }
        }
    }
    if (result != nullptr) {
        result = std::make_shared<cam::Hit>(cam::Hit(transform.toWorld.transformPoint(result->hit), transform.toWorldN.transformDir(result->n), result->t, result->material));
    }
    return result;
}
void Group::add(const std::shared_ptr<Shape>& shape)
{
    shapeList.push_back(shape);
}
Postea's avatar
Postea committed
}