2022-10-16 19:24:37 -04:00
|
|
|
/*
|
|
|
|
* Created by Brett Terpstra 6920201 on 16/10/22.
|
|
|
|
* Copyright (c) 2022 Brett Terpstra. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef STEP_2_WORLD_H
|
|
|
|
#define STEP_2_WORLD_H
|
|
|
|
|
|
|
|
#include <util/std.h>
|
2022-10-17 00:29:34 -04:00
|
|
|
#include <math/vectors.h>
|
2022-10-17 19:16:10 -04:00
|
|
|
#include <util/models.h>
|
2022-10-18 00:44:49 -04:00
|
|
|
#include <math/bvh.h>
|
|
|
|
#include <types.h>
|
2022-10-17 19:16:10 -04:00
|
|
|
|
|
|
|
#include <utility>
|
2022-10-16 19:24:37 -04:00
|
|
|
|
|
|
|
namespace Raytracing {
|
2022-10-17 00:29:34 -04:00
|
|
|
|
|
|
|
class SphereObject : public Object {
|
|
|
|
private:
|
|
|
|
PRECISION_TYPE radius;
|
|
|
|
public:
|
2022-10-18 23:11:51 -04:00
|
|
|
SphereObject(const Vec4& position, PRECISION_TYPE radius, Material* material): radius(radius), Object(material, position) {}
|
2022-10-17 00:29:34 -04:00
|
|
|
|
|
|
|
[[nodiscard]] virtual HitData checkIfHit(const Ray& ray, PRECISION_TYPE min, PRECISION_TYPE max) const;
|
2022-10-18 23:11:51 -04:00
|
|
|
virtual Object* clone(){
|
|
|
|
return new SphereObject(position, radius, material);
|
|
|
|
}
|
2022-10-17 00:29:34 -04:00
|
|
|
};
|
|
|
|
|
2022-10-17 19:16:10 -04:00
|
|
|
class TriangleObject : public Object {
|
|
|
|
private:
|
|
|
|
Triangle theTriangle;
|
|
|
|
public:
|
2022-10-18 23:11:51 -04:00
|
|
|
TriangleObject(const Vec4& position, Triangle theTriangle, Material* material): Object(material, position),
|
2022-10-17 19:16:10 -04:00
|
|
|
theTriangle(std::move(theTriangle)) {}
|
|
|
|
[[nodiscard]] virtual HitData checkIfHit(const Ray& ray, PRECISION_TYPE min, PRECISION_TYPE max) const;
|
2022-10-18 23:11:51 -04:00
|
|
|
virtual Object* clone() {
|
|
|
|
return new TriangleObject(position, theTriangle, material);
|
|
|
|
}
|
2022-10-17 19:16:10 -04:00
|
|
|
};
|
|
|
|
|
2022-10-18 00:44:49 -04:00
|
|
|
class ModelObject : public Object {
|
|
|
|
private:
|
|
|
|
std::vector<Triangle> triangles;
|
2022-10-18 23:11:51 -04:00
|
|
|
ModelData& data;
|
|
|
|
// basically we have to store this crap here because c++ loves to copy stuff
|
2022-10-19 00:43:16 -04:00
|
|
|
//std::vector<Object*> createdTreeObjects{};
|
|
|
|
//BVHTree* tree = nullptr;
|
2022-10-18 00:44:49 -04:00
|
|
|
public:
|
2022-10-18 23:11:51 -04:00
|
|
|
ModelObject(const Vec4& position, ModelData& data, Material* material): Object(material, position), data(data) {
|
|
|
|
// since all of this occurs before the main ray tracing algorithm it's fine to do sequentially
|
2022-10-18 00:44:49 -04:00
|
|
|
triangles = data.toTriangles();
|
|
|
|
this->aabb = data.aabb;
|
2022-10-19 00:43:16 -04:00
|
|
|
//createdTreeObjects = Raytracing::ModelData::createBVHTree(triangles, position);
|
|
|
|
//tree = new BVHTree(createdTreeObjects);
|
2022-10-18 00:44:49 -04:00
|
|
|
}
|
|
|
|
[[nodiscard]] virtual HitData checkIfHit(const Ray& ray, PRECISION_TYPE min, PRECISION_TYPE max) const;
|
2022-10-18 23:11:51 -04:00
|
|
|
virtual Object* clone() {
|
|
|
|
return new ModelObject(position, data, material);
|
|
|
|
}
|
|
|
|
virtual ~ModelObject() {
|
2022-10-19 00:43:16 -04:00
|
|
|
// Disabled for now, causing bugs when on release mode.
|
|
|
|
//for (auto* p : createdTreeObjects)
|
|
|
|
// delete(p);
|
|
|
|
//delete(tree);
|
2022-10-18 23:11:51 -04:00
|
|
|
}
|
2022-10-18 00:44:49 -04:00
|
|
|
};
|
|
|
|
|
2022-10-17 19:16:10 -04:00
|
|
|
class DiffuseMaterial : public Material {
|
|
|
|
private:
|
|
|
|
public:
|
2022-10-18 23:11:51 -04:00
|
|
|
explicit DiffuseMaterial(const Vec4& scatterColor): Material(scatterColor) {}
|
2022-10-17 19:16:10 -04:00
|
|
|
|
|
|
|
[[nodiscard]] virtual ScatterResults scatter(const Ray& ray, const HitData& hitData) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MetalMaterial : public Material {
|
|
|
|
protected:
|
2022-10-18 23:11:51 -04:00
|
|
|
static inline Vec4 reflect(const Vec4& incomingVector, const Vec4& normal) {
|
|
|
|
return incomingVector - 2 * Vec4::dot(incomingVector, normal) * normal;
|
2022-10-17 19:16:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2022-10-18 23:11:51 -04:00
|
|
|
explicit MetalMaterial(const Vec4& metalColor): Material(metalColor) {}
|
2022-10-17 19:16:10 -04:00
|
|
|
|
|
|
|
[[nodiscard]] virtual ScatterResults scatter(const Ray& ray, const HitData& hitData) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BrushedMetalMaterial : public MetalMaterial {
|
|
|
|
private:
|
|
|
|
PRECISION_TYPE fuzzyness;
|
|
|
|
public:
|
2022-10-18 23:11:51 -04:00
|
|
|
explicit BrushedMetalMaterial(const Vec4& metalColor, PRECISION_TYPE fuzzyness): MetalMaterial(metalColor), fuzzyness(fuzzyness) {}
|
2022-10-17 19:16:10 -04:00
|
|
|
|
|
|
|
[[nodiscard]] virtual ScatterResults scatter(const Ray& ray, const HitData& hitData) const;
|
|
|
|
};
|
|
|
|
|
2022-10-19 11:19:08 -04:00
|
|
|
class TexturedMaterial : public Material {
|
|
|
|
public:
|
|
|
|
TexturedMaterial(const std::string& file): Material({}) {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-17 00:29:34 -04:00
|
|
|
class World {
|
|
|
|
private:
|
|
|
|
// store all the objects in the world,
|
|
|
|
std::vector<Object*> objects;
|
|
|
|
/*TODO: create a kd-tree or bvh version to store the objects
|
|
|
|
* this way we can easily tell if a ray is near and object or not
|
|
|
|
* saving on computation
|
|
|
|
*/
|
2022-10-18 23:11:51 -04:00
|
|
|
// TODO: the above todo has been done, now we need to test the performance advantage of the BVH
|
2022-10-19 00:43:16 -04:00
|
|
|
//BVHTree* bvhTree = nullptr;
|
2022-10-17 19:16:10 -04:00
|
|
|
std::unordered_map<std::string, Material*> materials;
|
2022-10-17 00:29:34 -04:00
|
|
|
public:
|
|
|
|
World() = default;
|
|
|
|
World(const World& world) = delete;
|
|
|
|
World(const World&& world) = delete;
|
|
|
|
|
2022-10-18 23:11:51 -04:00
|
|
|
// call this after you've added all the objects to the world. (Called by the raycaster class)
|
|
|
|
void generateBVH();
|
|
|
|
|
2022-10-17 00:29:34 -04:00
|
|
|
inline void add(Object* object) { objects.push_back(object); }
|
|
|
|
|
2022-10-17 19:16:10 -04:00
|
|
|
inline void addMaterial(const std::string& materialName, Material* mat) { materials.insert({materialName, mat}); }
|
|
|
|
|
|
|
|
inline Material* getMaterial(const std::string& materialName) { return materials.at(materialName); }
|
|
|
|
|
|
|
|
[[nodiscard]] virtual std::pair<HitData, Object*> checkIfHit(const Ray& ray, PRECISION_TYPE min, PRECISION_TYPE max) const;
|
2022-10-17 00:29:34 -04:00
|
|
|
~World();
|
|
|
|
|
|
|
|
};
|
2022-10-16 19:24:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //STEP_2_WORLD_H
|