diff --git a/RT b/RT new file mode 100644 index 0000000..1e4ac0b Binary files /dev/null and b/RT differ diff --git a/includes/RT/Scene.hpp b/includes/RT/Scene.hpp index f48eb80..c12d9e8 100644 --- a/includes/RT/Scene.hpp +++ b/includes/RT/Scene.hpp @@ -57,22 +57,16 @@ class Scene void addObject(Object *object); void addMaterial(Material *material); - void updateGPUData(); - const std::vector &getObjectData() const; std::vector &getMaterialData(); Camera *getCamera(void) const; - Material *getMaterial(int material_index); + GPUMaterial getMaterial(int material_index); private: - std::vector _objects; - std::vector _materials; - std::vector _gpu_objects; std::vector _gpu_materials; - Camera *_camera; }; diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index 8f476a9..9710ebb 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -50,103 +50,87 @@ bool Scene::parseScene(char *name) } -void Scene::addObject(Object *object) +void Scene::addObject(Object *obj) { - _objects.push_back(object); - this->updateGPUData(); + GPUObject gpu_obj; + + gpu_obj.mat_index = obj->getMaterialIndex(); + gpu_obj.position = obj->getPosition(); + gpu_obj.type = static_cast(obj->getType()); + + if (obj->getType() == Object::Type::SPHERE) + { + auto sphere = static_cast(obj); + gpu_obj.radius = sphere->getRadius(); + } + else if (obj->getType() == Object::Type::PLANE) + { + auto plane = static_cast(obj); + gpu_obj.normal = plane->getNormal(); + } + else if (obj->getType() == Object::Type::QUAD) + { + auto quad = static_cast(obj); + gpu_obj.vertex1 = quad->getUp(); + gpu_obj.vertex2 = quad->getRight(); + } + else if (obj->getType() == Object::Type::TRIANGLE) + { + auto triangle = static_cast(obj); + gpu_obj.vertex1 = triangle->getVertex2(); + gpu_obj.vertex2 = triangle->getVertex3(); + gpu_obj.normal = triangle->getNormal(); + } + else if (obj->getType() == Object::Type::CUBE) + { + auto cube = static_cast(obj); + gpu_obj.position = cube->getPosition(); + gpu_obj.vertex1 = cube->getSize(); + } + else if (obj->getType() == Object::Type::CYLINDER) + { + auto cylinder = static_cast(obj); + gpu_obj.normal = glm::vec3(cylinder->getRadius(), cylinder->getHeight(), 0.0f); + gpu_obj.transform = glm::mat4(cylinder->getRotation()); + } + else if (obj->getType() == Object::Type::PORTAL) + { + auto portal = static_cast(obj); + gpu_obj.vertex1 = portal->getUp(); + gpu_obj.vertex2 = portal->getRight(); + gpu_obj.normal = portal->getNormal(); + gpu_obj.transform = glm::mat4(portal->getRotation()); + + int i = _gpu_objects.size(); + + GPUObject &linked = _gpu_objects[i - 2]; + + if (portal->getLinkedPortalIndex() == -1) + { + if (linked.type == (int)Object::Type::PORTAL && linked.radius == static_cast(i)) + portal->setLinkedPortalIndex(i - 2); + else + portal->setLinkedPortalIndex(i + 2); + } + + gpu_obj.radius = portal->getLinkedPortalIndex(); + } + + _gpu_objects.push_back(gpu_obj); } void Scene::addMaterial(Material *material) { - _materials.push_back(material); - this->updateGPUData(); -} - -void Scene::updateGPUData() -{ - GPUObject gpu_obj; GPUMaterial gpu_mat; - _gpu_objects.clear(); - _gpu_materials.clear(); - - for (unsigned int i = 0; i < _objects.size(); i++) - { - Object *obj = _objects[i]; + gpu_mat.color = material->color; + gpu_mat.emission = material->emission; + gpu_mat.roughness = material->roughness; + gpu_mat.metallic = material->metallic; + gpu_mat.refraction = material->refraction; + gpu_mat.type = material->type; - gpu_obj.mat_index = obj->getMaterialIndex(); - gpu_obj.position = obj->getPosition(); - gpu_obj.type = static_cast(obj->getType()); - - if (obj->getType() == Object::Type::SPHERE) - { - auto sphere = static_cast(obj); - gpu_obj.radius = sphere->getRadius(); - } - else if (obj->getType() == Object::Type::PLANE) - { - auto plane = static_cast(obj); - gpu_obj.normal = plane->getNormal(); - } - else if (obj->getType() == Object::Type::QUAD) - { - auto quad = static_cast(obj); - gpu_obj.vertex1 = quad->getUp(); - gpu_obj.vertex2 = quad->getRight(); - } - else if (obj->getType() == Object::Type::TRIANGLE) - { - auto triangle = static_cast(obj); - gpu_obj.vertex1 = triangle->getVertex2(); - gpu_obj.vertex2 = triangle->getVertex3(); - gpu_obj.normal = triangle->getNormal(); - } - else if (obj->getType() == Object::Type::CUBE) - { - auto cube = static_cast(obj); - gpu_obj.position = cube->getPosition(); - gpu_obj.vertex1 = cube->getSize(); - } - else if (obj->getType() == Object::Type::CYLINDER) - { - auto cylinder = static_cast(obj); - gpu_obj.normal = glm::vec3(cylinder->getRadius(), cylinder->getHeight(), 0.0f); - gpu_obj.transform = glm::mat4(cylinder->getRotation()); - } - else if (obj->getType() == Object::Type::PORTAL) - { - auto portal = static_cast(obj); - gpu_obj.vertex1 = portal->getUp(); - gpu_obj.vertex2 = portal->getRight(); - gpu_obj.normal = portal->getNormal(); - gpu_obj.transform = glm::mat4(portal->getRotation()); - - Portal *linked = static_cast(_objects[i - 2]); - - if (portal->getLinkedPortalIndex() == -1) - { - if (linked->getType() == Object::Type::PORTAL && linked->getLinkedPortalIndex() == static_cast(i)) - portal->setLinkedPortalIndex(i - 2); - else - portal->setLinkedPortalIndex(i + 2); - } - - gpu_obj.radius = portal->getLinkedPortalIndex(); - } - - _gpu_objects.push_back(gpu_obj); - } - for (const auto &material : _materials) - { - gpu_mat.color = material->color; - gpu_mat.emission = material->emission; - gpu_mat.roughness = material->roughness; - gpu_mat.metallic = material->metallic; - gpu_mat.refraction = material->refraction; - gpu_mat.type = material->type; - - _gpu_materials.push_back(gpu_mat); - } + _gpu_materials.push_back(gpu_mat); } const std::vector& Scene::getObjectData() const @@ -164,9 +148,9 @@ Camera *Scene::getCamera(void) const return (_camera); } -Material *Scene::getMaterial(int material_index) +GPUMaterial Scene::getMaterial(int material_index) { - if (material_index < 0 || material_index >= (int)_materials.size()) + if (material_index < 0 || material_index >= (int)_gpu_materials.size()) throw std::runtime_error("Incorrect material index"); - return (_materials[material_index]); + return (_gpu_materials[material_index]); } \ No newline at end of file