From 224dae316134ea95fed9a76f116bbe005033a141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Leclerc?= Date: Mon, 13 May 2019 23:13:27 +0100 Subject: [PATCH 1/6] improv(performance): Improv performance --- src/Material/Material.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Material/Material.cc b/src/Material/Material.cc index 5587414..2055c8f 100644 --- a/src/Material/Material.cc +++ b/src/Material/Material.cc @@ -20,7 +20,6 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { Material::Material() { - name = ""; Ns = 96; Ka = Vector3(0.75, 0.75, 0.75); Kd = Vector3(0, 1, 0); From d4afd51db9c474fd973fae13f3074a3dc5ef4bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Leclerc?= Date: Tue, 14 May 2019 00:31:05 +0100 Subject: [PATCH 2/6] improv(performance/style): Improved performance and style --- src/Camera/Camera.cc | 37 +++--- src/Camera/Camera.h | 52 ++++---- src/Dispatcher/Dispatcher.cc | 3 - src/Dispatcher/Dispatcher.h | 12 +- src/Dispatcher/Dispatcher_test.cc | 2 +- src/Engine/Color.h | 2 +- src/Engine/Color_test.cc | 2 +- src/Engine/Engine.cc | 17 +-- src/Engine/Engine.h | 4 +- src/KDTree/KDBox.cc | 12 +- src/KDTree/KDBox.h | 30 ++--- src/KDTree/KDBox_test.cc | 64 +++++----- src/KDTree/KDNode.cc | 21 ++-- src/KDTree/KDNode.h | 22 ++-- src/KDTree/KDNode_test.cc | 82 ++++++------- src/Light/Light.cc | 6 +- src/Light/Light.h | 6 +- src/Light/PointLight.cc | 5 +- src/Light/PointLight.h | 9 +- src/Loader/AssimpLoader.cc | 58 +++++---- src/Loader/AssimpLoader.h | 36 +++--- src/Material/Material.cc | 13 +- src/Material/Material.h | 4 +- src/Mesh/Mesh.cc | 2 +- src/Mesh/Mesh.h | 20 ++-- src/Mesh/Object.cc | 4 +- src/Mesh/Object.h | 18 +-- src/Mesh/Triangle.cc | 117 +++++++++--------- src/Mesh/Triangle.h | 50 ++++---- src/Mesh/Triangle_test.cc | 86 +++++++------- src/Mesh/Vertex.cc | 7 +- src/Mesh/Vertex.h | 6 +- src/Sky/SphereSky.cc | 3 - src/Sky/SphereSky.h | 2 +- src/Vector/Vector2.h | 4 +- src/Vector/Vector3.h | 190 +++++++++++++++--------------- 36 files changed, 483 insertions(+), 525 deletions(-) diff --git a/src/Camera/Camera.cc b/src/Camera/Camera.cc index 2c83bd8..521e528 100644 --- a/src/Camera/Camera.cc +++ b/src/Camera/Camera.cc @@ -21,51 +21,50 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "../Engine/Constant.h" namespace rt { - Camera::Camera(): _pos(Vector3(0, 0, 0)), _c1(Vector3(1, 0, 0)), - _c2(Vector3(0, 1, 0)), _c3(Vector3(0, 0, 1)), - _gen(std::random_device()()), _dis(0.f, 1.f) { + Camera::Camera() : _pos(Vector3(0, 0, 0)), _c1(Vector3(1, 0, 0)), + _c2(Vector3(0, 1, 0)), _c3(Vector3(0, 0, 1)), + _screenDist(1.f), _gen(std::random_device()()), _dis(0.f, 1.f) { generateScreen(); } - Camera::~Camera(void) { + Camera::~Camera() { } Ray const Camera::GenerateRay(Vector2 const &pos) { - #ifndef RT_TESTING_ENV +#ifndef RT_TESTING_ENV float Rx = _dis(_gen); float Ry = _dis(_gen); - #else +#else float Rx = 0.0f; float Ry = 0.0f; - #endif +#endif Vector3 direction = (_screenCorner + _c1 * ((pos.X + Rx) / _screenRes.X) * _screenSize.X - _c2 * ((pos.Y + Ry) / _screenRes.Y) * _screenSize.Y - _pos) * _screenDist; direction.Normalize(); Ray ray(_pos, direction); return ray; } - Vector3 const& Camera::GetPos(void) const { + Vector3 const& Camera::GetPos() const { return _pos; } - Vector2 const& Camera::GetRes(void) const { + Vector2 const& Camera::GetRes() const { return _screenRes; } - void Camera::SetMatrix(Vector3 const& c1, Vector3 const& c2, - Vector3 const& c3, Vector3 const& pos) { - _c1 = c1; - _c2 = c2; - _c3 = c3; - _pos = pos; - generateScreen(); + void Camera::SetMatrix(Vector3 const& c1, Vector3 const& c2, + Vector3 const& c3, Vector3 const& pos) { + _c1 = c1; + _c2 = c2; + _c3 = c3; + _pos = pos; + generateScreen(); } - void Camera::generateScreen() { - _screenDist = 1.f; + void Camera::generateScreen() { _screenRes = Vector2(Constant::DefaultScreenWidth, Constant::DefaultScreenHeight); float screenWidth = 2.f * std::tan((Constant::DefaultScreenFOV / 2.f) * static_cast(M_PI) / 180.f) * _screenDist; _screenSize = Vector2(screenWidth, screenWidth * _screenRes.Y / _screenRes.X); _screenCorner = _pos + _c3 * (-1.f) - _c1 * (_screenSize.X / 2.f) + _c2 * (_screenSize.Y / 2.f); - } + } } // namespace rt diff --git a/src/Camera/Camera.h b/src/Camera/Camera.h index eea0ec5..4b89339 100644 --- a/src/Camera/Camera.h +++ b/src/Camera/Camera.h @@ -24,30 +24,30 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "../Engine/Tools.h" namespace rt { -class Camera { - public: - Camera(); - virtual ~Camera(void); - - Ray const GenerateRay(Vector2 const &pos); - - Vector3 const& GetPos(void) const; - Vector2 const& GetRes(void) const; - void SetMatrix(Vector3 const& pos, Vector3 const& c1, Vector3 const& c2, Vector3 const& c3); - - private: - Vector3 _pos; - Vector3 _c1; - Vector3 _c2; - Vector3 _c3; - Vector2 _screenRes; - Vector2 _screenSize; - Vector3 _screenCorner; - float _screenDist; - std::mt19937 _gen; - std::uniform_real_distribution _dis; - - - void generateScreen(); -}; + class Camera { + public: + Camera(); + virtual ~Camera(); + + Ray const GenerateRay(Vector2 const &pos); + + Vector3 const& GetPos() const; + Vector2 const& GetRes() const; + void SetMatrix(Vector3 const& pos, Vector3 const& c1, Vector3 const& c2, Vector3 const& c3); + + private: + Vector3 _pos; + Vector3 _c1; + Vector3 _c2; + Vector3 _c3; + Vector2 _screenRes; + Vector2 _screenSize; + Vector3 _screenCorner; + float _screenDist; + std::mt19937 _gen; + std::uniform_real_distribution _dis; + + + void generateScreen(); + }; } // namespace rt diff --git a/src/Dispatcher/Dispatcher.cc b/src/Dispatcher/Dispatcher.cc index c6141a3..eccc3a4 100644 --- a/src/Dispatcher/Dispatcher.cc +++ b/src/Dispatcher/Dispatcher.cc @@ -25,9 +25,6 @@ namespace rt { _image.resize(size, Color(0x000000ff)); } - Dispatcher::~Dispatcher() { - } - void Dispatcher::Start() { _running = true; std::size_t cores = std::thread::hardware_concurrency(); diff --git a/src/Dispatcher/Dispatcher.h b/src/Dispatcher/Dispatcher.h index eb96b76..f5e2ff3 100644 --- a/src/Dispatcher/Dispatcher.h +++ b/src/Dispatcher/Dispatcher.h @@ -27,15 +27,15 @@ namespace rt { class Dispatcher { public: explicit Dispatcher(Engine const& engine, Vector2 const& res); - virtual ~Dispatcher(); + virtual ~Dispatcher() = default; - void Start(void); - void Stop(void); - std::vector Flush(void); - std::size_t GetNumberOfProcessed(void) const; + void Start(); + void Stop(); + std::vector Flush(); + std::size_t GetNumberOfProcessed() const; private: - void execute(void); + void execute(); bool _running; Engine _engine; diff --git a/src/Dispatcher/Dispatcher_test.cc b/src/Dispatcher/Dispatcher_test.cc index dfc2494..ca255a5 100644 --- a/src/Dispatcher/Dispatcher_test.cc +++ b/src/Dispatcher/Dispatcher_test.cc @@ -23,7 +23,7 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { TEST(Dispatcher, start_and_stop) { - AssimpLoader loader; + AssimpLoader loader; if (!loader.LoadFile("../scenes/Cube.dae")) { ASSERT_TRUE(false); } diff --git a/src/Engine/Color.h b/src/Engine/Color.h index dfec1f9..3c04316 100644 --- a/src/Engine/Color.h +++ b/src/Engine/Color.h @@ -38,7 +38,7 @@ namespace rt { explicit Color(Color_Component const& component); explicit Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue); explicit Color(Vector3 const& vec); - virtual ~Color(void); + virtual ~Color(); Color_Component GetColor() const; void SetColor(Color_Component const& component); diff --git a/src/Engine/Color_test.cc b/src/Engine/Color_test.cc index 5325c8f..5309a08 100644 --- a/src/Engine/Color_test.cc +++ b/src/Engine/Color_test.cc @@ -99,7 +99,7 @@ namespace rt { TEST(Color, Add) { Color color(0x0f0f0fff); color += Color(0x123456ff); - + ASSERT_EQ(color.GetColor().hexcode, 0x214365ff); } } // namespace rt diff --git a/src/Engine/Engine.cc b/src/Engine/Engine.cc index 454fd43..dd8602f 100644 --- a/src/Engine/Engine.cc +++ b/src/Engine/Engine.cc @@ -22,12 +22,7 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "../Light/PointLight.h" namespace rt { - Engine::Engine(AssimpLoader const &loader) : _loader(loader), _sky(nullptr), _camera(loader.GetCameraFromScene()) { - _meshes = loader.GetMeshesFromScene(); - _lights = loader.GetLightsFromScene(); - } - - Engine::~Engine() { + Engine::Engine(AssimpLoader const &loader) : _loader(loader), _sky(nullptr), _camera(loader.GetCameraFromScene()), _meshes(loader.GetMeshesFromScene()), _lights(loader.GetLightsFromScene()) { } Color Engine::Raytrace(const rt::Vector2 &pixel) { @@ -35,12 +30,12 @@ namespace rt { Ray ray = _camera.GenerateRay(pixel); Intersection inter = _intersect(ray); if (inter.Intersect) { - for (size_t i = 0; i < _lights.size(); ++i) { - Vector3 lightDir = _lights[i]->GetPos() - inter.Point; + for (auto & _light : _lights) { + Vector3 lightDir = _light->GetPos() - inter.Point; lightDir.Normalize(); Intersection interLight = _intersect(Ray(inter.Point, lightDir)); if (!interLight.Intersect || - interLight.Dist > (_lights[i]->GetPos() - inter.Point).Norm()) { + interLight.Dist > (_light->GetPos() - inter.Point).Norm()) { float angle = lightDir.Angle(inter.Normal); if (angle > 90.f) { angle = 180.f - angle; @@ -62,8 +57,8 @@ namespace rt { Intersection rtn; float min = -1; - for (size_t i = 0; i < _meshes.size(); ++i) { - Intersection inter = _meshes[i]->Intersect(ray); + for (auto & _mesh : _meshes) { + Intersection inter = _mesh->Intersect(ray); if (inter.Intersect) { if (min == -1 || inter.Dist < min) { min = inter.Dist; diff --git a/src/Engine/Engine.h b/src/Engine/Engine.h index 5302d10..dbcfb80 100644 --- a/src/Engine/Engine.h +++ b/src/Engine/Engine.h @@ -31,7 +31,7 @@ namespace rt { class Engine { public: explicit Engine(AssimpLoader const& loader); - virtual ~Engine(); + virtual ~Engine() = default; Color Raytrace(Vector2 const& pixel); Vector2 GetRes() const; @@ -44,7 +44,7 @@ namespace rt { std::vector> _meshes; std::vector> _lights; - void _pathtrace(Ray const& ray, unsigned int const& depth, Color & color); + //void _pathtrace(Ray const& ray, unsigned int const& depth, Color & color); Intersection const _intersect(Ray const& ray); }; } // namespace rt diff --git a/src/KDTree/KDBox.cc b/src/KDTree/KDBox.cc index 751a7ca..ede780d 100644 --- a/src/KDTree/KDBox.cc +++ b/src/KDTree/KDBox.cc @@ -19,14 +19,11 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "KDBox.h" namespace rt { - + KDBox::KDBox(std::vector const& triangles) { this->setMinMax(triangles); } - KDBox::~KDBox() { - } - bool KDBox::Intersect(Ray const& ray) { float tx1 = (_x.X - ray.Origin.X) / ray.Direction.X; float tx2 = (_x.Y - ray.Origin.X) / ray.Direction.X; @@ -37,8 +34,7 @@ namespace rt { float tMin = std::max(std::max(std::min(tx1, tx2), std::min(ty1, ty2)), std::min(tz1, tz2)); float tMax = std::min(std::min(std::max(tx1, tx2), std::max(ty1, ty2)), std::max(tz1, tz2)); if (tMin <= tMax) { - if (tMin <= Constant::Epsilon && tMax <= Constant::Epsilon) return false; - return true; + return !(tMin <= Constant::Epsilon && tMax <= Constant::Epsilon); } return false; } @@ -54,12 +50,12 @@ namespace rt { Vector2 const& KDBox::GetZ() const { return _z; } - + std::size_t KDBox::GetLongestAxis() const { float xDiff = _x.Y - _x.X; float yDiff = _y.Y - _y.X; float zDiff = _z.Y - _z.X; - float max = std::max(xDiff, std::max(yDiff, zDiff)); + float max = std::max(xDiff, std::max(yDiff, zDiff)); return max == xDiff ? 0 : max == yDiff ? 1 : 2; } diff --git a/src/KDTree/KDBox.h b/src/KDTree/KDBox.h index 5589575..59f0dd3 100644 --- a/src/KDTree/KDBox.h +++ b/src/KDTree/KDBox.h @@ -23,23 +23,23 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "../Engine/Color.h" namespace rt { -class KDBox { - public: - KDBox(std::vector const& triangles); - ~KDBox(); + class KDBox { + public: + explicit KDBox(std::vector const& triangles); + ~KDBox() = default; - bool Intersect(Ray const& ray); + bool Intersect(Ray const& ray); - Vector2 const& GetX() const; - Vector2 const& GetY() const; - Vector2 const& GetZ() const; - std::size_t GetLongestAxis() const; + Vector2 const& GetX() const; + Vector2 const& GetY() const; + Vector2 const& GetZ() const; + std::size_t GetLongestAxis() const; - private: - Vector2 _x; // Containing Xmin and Xmax - Vector2 _y; // Containing Ymin and Ymax - Vector2 _z; // Containing Zmin and Zmax + private: + Vector2 _x; // Containing Xmin and Xmax + Vector2 _y; // Containing Ymin and Ymax + Vector2 _z; // Containing Zmin and Zmax - void setMinMax(std::vector const& triangles); -}; + void setMinMax(std::vector const& triangles); + }; } // namespace rt diff --git a/src/KDTree/KDBox_test.cc b/src/KDTree/KDBox_test.cc index 1e4ff92..43f315a 100644 --- a/src/KDTree/KDBox_test.cc +++ b/src/KDTree/KDBox_test.cc @@ -21,14 +21,14 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { void feedVector(std::vector &triangles) { triangles.emplace_back( - Vector3(0, 0.5, -0.5), - Vector3(1.5, 0.5, 0), - Vector3(1.5, 0, -0.5) + Vector3(0, 0.5, -0.5), + Vector3(1.5, 0.5, 0), + Vector3(1.5, 0, -0.5) ); triangles.emplace_back( - Vector3(3, 0.5, -0.5), - Vector3(1.5, 0.5, -1), - Vector3(1.5, 1, -0.5) + Vector3(3, 0.5, -0.5), + Vector3(1.5, 0.5, -1), + Vector3(1.5, 1, -0.5) ); } @@ -51,14 +51,14 @@ namespace rt { TEST(KDBox, LongestAxisY) { std::vector triangles; triangles.emplace_back( - Vector3(0, 7.5, -0.5), - Vector3(1.5, 0.5, 0), - Vector3(1.5, 0, -0.5) + Vector3(0, 7.5, -0.5), + Vector3(1.5, 0.5, 0), + Vector3(1.5, 0, -0.5) ); triangles.emplace_back( - Vector3(3, 0.5, -0.5), - Vector3(1.5, -0.5, -1), - Vector3(1.5, 1, -0.5) + Vector3(3, 0.5, -0.5), + Vector3(1.5, -0.5, -1), + Vector3(1.5, 1, -0.5) ); KDBox box = KDBox(triangles); EXPECT_EQ(box.GetLongestAxis(), 1); @@ -67,14 +67,14 @@ namespace rt { TEST(KDBox, LongestAxisZ) { std::vector triangles; triangles.emplace_back( - Vector3(0, 3.5, -10.5), - Vector3(1.5, 0.5, 0), - Vector3(1.5, 0, -0.5) + Vector3(0, 3.5, -10.5), + Vector3(1.5, 0.5, 0), + Vector3(1.5, 0, -0.5) ); triangles.emplace_back( - Vector3(3, 0.5, -0.5), - Vector3(1.5, -0.5, -1), - Vector3(1.5, 1, 20.5) + Vector3(3, 0.5, -0.5), + Vector3(1.5, -0.5, -1), + Vector3(1.5, 1, 20.5) ); KDBox box = KDBox(triangles); EXPECT_EQ(box.GetLongestAxis(), 2); @@ -86,32 +86,32 @@ namespace rt { KDBox box = KDBox(triangles); EXPECT_TRUE(box.Intersect(Ray( - Vector3(1.5, 0.5, 3), - Vector3(0, 0, -1) + Vector3(1.5, 0.5, 3), + Vector3(0, 0, -1) ))); EXPECT_TRUE(box.Intersect(Ray( - Vector3(1.5, 0.5, -3), - Vector3(0, 0, 1) + Vector3(1.5, 0.5, -3), + Vector3(0, 0, 1) ))); EXPECT_TRUE(box.Intersect(Ray( - Vector3(5, 0.5, -0.5), - Vector3(-1, 0, 0) + Vector3(5, 0.5, -0.5), + Vector3(-1, 0, 0) ))); EXPECT_TRUE(box.Intersect(Ray( - Vector3(-3, 0.5, -0.5), - Vector3(1, 0, 0) + Vector3(-3, 0.5, -0.5), + Vector3(1, 0, 0) ))); EXPECT_TRUE(box.Intersect(Ray( - Vector3(1.5, 3, -0.5), - Vector3(0, -1, 0) + Vector3(1.5, 3, -0.5), + Vector3(0, -1, 0) ))); EXPECT_TRUE(box.Intersect(Ray( - Vector3(1.5, -3, -0.5), - Vector3(0, 1, 0) + Vector3(1.5, -3, -0.5), + Vector3(0, 1, 0) ))); EXPECT_FALSE(box.Intersect(Ray( - Vector3(1.5, 3, -0.5), - Vector3(0, 1, 0) + Vector3(1.5, 3, -0.5), + Vector3(0, 1, 0) ))); } } // namespace rt diff --git a/src/KDTree/KDNode.cc b/src/KDTree/KDNode.cc index ee02033..fe54740 100644 --- a/src/KDTree/KDNode.cc +++ b/src/KDTree/KDNode.cc @@ -1,3 +1,7 @@ +#include + +#include + /* Copyright (c) 2018 mickael.leclerc@epitech.eu charles.fraisse@epitech.eu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -32,26 +36,23 @@ namespace rt { for (std::size_t i = 0; i < size; ++i) { switch (axis) { case 0: - triangles[i].GetMidPoint().X > midpoint.X ? tright.push_back(std::move(triangles[i])) : tleft.push_back(std::move(triangles[i])); + triangles[i].GetMidPoint().X > midpoint.X ? tright.push_back(triangles[i]) : tleft.push_back(triangles[i]); break; case 1: - triangles[i].GetMidPoint().Y > midpoint.Y ? tright.push_back(std::move(triangles[i])) : tleft.push_back(std::move(triangles[i])); + triangles[i].GetMidPoint().Y > midpoint.Y ? tright.push_back(triangles[i]) : tleft.push_back(triangles[i]); break; case 2: - triangles[i].GetMidPoint().Z > midpoint.Z ? tright.push_back(std::move(triangles[i])) : tleft.push_back(std::move(triangles[i])); + triangles[i].GetMidPoint().Z > midpoint.Z ? tright.push_back(triangles[i]) : tleft.push_back(triangles[i]); break; } } - _left = std::shared_ptr(new KDNode(tleft, depth - 1)); - _right = std::shared_ptr(new KDNode(tright, depth - 1)); + _left = std::make_shared(tleft, depth - 1); + _right = std::make_shared(tright, depth - 1); } else { _triangles = triangles; } } - KDNode::~KDNode() { - } - Intersection KDNode::Intersect(Ray const& ray) { Intersection intersection = Intersection(); if (!(_box.Intersect(ray))) { @@ -59,8 +60,8 @@ namespace rt { } else if (!_left && !_right) { Intersection inter; float min = -1; - for (std::size_t i = 0; i < _triangles.size(); ++i) { - inter = _triangles[i].Intersect(ray); + for (auto & _triangle : _triangles) { + inter = _triangle.Intersect(ray); if (inter.Intersect) { if (min == -1 || inter.Dist < min) { min = inter.Dist; diff --git a/src/KDTree/KDNode.h b/src/KDTree/KDNode.h index 886fad8..a952fa5 100644 --- a/src/KDTree/KDNode.h +++ b/src/KDTree/KDNode.h @@ -24,17 +24,17 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "KDBox.h" namespace rt { -class KDNode { - public: - KDNode(std::vector const& triangles, std::size_t depth); - ~KDNode(); + class KDNode { + public: + KDNode(std::vector const& triangles, std::size_t depth); + ~KDNode() = default; - Intersection Intersect(Ray const& ray); + Intersection Intersect(Ray const& ray); - private: - KDBox _box; - std::shared_ptr _left; - std::shared_ptr _right; - std::vector _triangles; -}; + private: + KDBox _box; + std::shared_ptr _left; + std::shared_ptr _right; + std::vector _triangles; + }; } // namespace rt diff --git a/src/KDTree/KDNode_test.cc b/src/KDTree/KDNode_test.cc index 3a9df84..94902bd 100644 --- a/src/KDTree/KDNode_test.cc +++ b/src/KDTree/KDNode_test.cc @@ -1,41 +1,41 @@ -/* Copyright (c) 2018 mickael.leclerc@epitech.eu charles.fraisse@epitech.eu - Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE -OR OTHER DEALINGS IN THE SOFTWARE. */ - -#include "gtest/gtest.h" -#include "KDNode.h" - -namespace rt { - TEST(KDNode, raytrace) { - std::vector triangles; - triangles.emplace_back( - Vector3(), - Vector3(1, 0, 0), - Vector3(0, 1, 0) - ); - triangles.emplace_back( - Vector3(), - Vector3(0.5, 0, -3), - Vector3(1.5, 2, -0.5) - ); - KDNode tree = KDNode(triangles, triangles.size()); - Intersection inter = tree.Intersect(Ray( - Vector3(0.01, 0, 1), - Vector3(0, .3, -1) - )); - EXPECT_TRUE(inter.Intersect); - } -} // namespace rt +/* Copyright (c) 2018 mickael.leclerc@epitech.eu charles.fraisse@epitech.eu + Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +OR OTHER DEALINGS IN THE SOFTWARE. */ + +#include "gtest/gtest.h" +#include "KDNode.h" + +namespace rt { + TEST(KDNode, raytrace) { + std::vector triangles; + triangles.emplace_back( + Vector3(), + Vector3(1, 0, 0), + Vector3(0, 1, 0) + ); + triangles.emplace_back( + Vector3(), + Vector3(0.5, 0, -3), + Vector3(1.5, 2, -0.5) + ); + KDNode tree = KDNode(triangles, triangles.size()); + Intersection inter = tree.Intersect(Ray( + Vector3(0.01, 0, 1), + Vector3(0, .3, -1) + )); + EXPECT_TRUE(inter.Intersect); + } +} // namespace rt diff --git a/src/Light/Light.cc b/src/Light/Light.cc index fbb16c9..e0a9bab 100644 --- a/src/Light/Light.cc +++ b/src/Light/Light.cc @@ -21,7 +21,7 @@ namespace rt { Light::Light(): _color(0xffffffff), _intensity(1.f) { } - + Color const& Light::GetColor() const { return _color; } @@ -29,8 +29,4 @@ namespace rt { float const& Light::GetBrightness() const { return _intensity; } - - - Light::~Light() { - } } //namespace rt diff --git a/src/Light/Light.h b/src/Light/Light.h index 7fc6d01..920ebe1 100644 --- a/src/Light/Light.h +++ b/src/Light/Light.h @@ -22,15 +22,15 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { class Light { - public: + public: Light(); - virtual ~Light(); + virtual ~Light() = default; virtual Vector3 GetPos() const = 0; Color const& GetColor() const; float const& GetBrightness() const; - protected: + protected: Color _color; float _intensity; }; diff --git a/src/Light/PointLight.cc b/src/Light/PointLight.cc index 3058ba3..1697546 100644 --- a/src/Light/PointLight.cc +++ b/src/Light/PointLight.cc @@ -21,7 +21,7 @@ namespace rt { PointLight::PointLight(Vector3 const& pos): _pos(pos) { } - + PointLight::PointLight(Vector3 const& pos, Color const& color): _pos(pos) { _color = color; } @@ -29,7 +29,4 @@ namespace rt { Vector3 PointLight::GetPos() const { return _pos; } - - PointLight::~PointLight() { - } } //namespace rt diff --git a/src/Light/PointLight.h b/src/Light/PointLight.h index cbd10d2..01546c9 100644 --- a/src/Light/PointLight.h +++ b/src/Light/PointLight.h @@ -21,15 +21,14 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { class PointLight : public Light { - public: + public: PointLight(Vector3 const& pos); PointLight(Vector3 const& pos, Color const& color); - virtual ~PointLight(); + virtual ~PointLight() = default; - virtual Vector3 GetPos() const; + Vector3 GetPos() const override; - - private: + private: Vector3 _pos; }; } \ No newline at end of file diff --git a/src/Loader/AssimpLoader.cc b/src/Loader/AssimpLoader.cc index d3775ea..6fec441 100644 --- a/src/Loader/AssimpLoader.cc +++ b/src/Loader/AssimpLoader.cc @@ -21,22 +21,18 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "../Light/PointLight.h" namespace rt { - AssimpLoader::AssimpLoader(): _camera() { - _scene = nullptr; - } - - AssimpLoader::~AssimpLoader() { + AssimpLoader::AssimpLoader(): _scene(nullptr), _camera() { } bool AssimpLoader::LoadFile(std::string const& filePath) { _scene = _importer.ReadFile(filePath, aiProcess_Triangulate - | aiProcess_GenSmoothNormals - | aiProcess_FixInfacingNormals); + | aiProcess_GenSmoothNormals + | aiProcess_FixInfacingNormals); if (!_scene) { std::cerr << "Error while importing scene: " << _importer.GetErrorString() << std::endl; return false; } - _loadNode(_scene->mRootNode, aiMatrix4x4()); + _loadNode(_scene->mRootNode, aiMatrix4x4()); return true; } @@ -54,9 +50,9 @@ namespace rt { Vector3 AssimpLoader::_transform(aiMatrix4x4 const& mat, Vector3 const& point) const { return Vector3( - mat.a1 * point.X + mat.a2 * point.Y + mat.a3 * point.Z + mat.a4, - -mat.c1 * point.X - mat.c2 * point.Y - mat.c3 * point.Z - mat.c4, - mat.b1 * point.X + mat.b2 * point.Y + mat.b3 * point.Z + mat.b4 + mat.a1 * point.X + mat.a2 * point.Y + mat.a3 * point.Z + mat.a4, + -mat.c1 * point.X - mat.c2 * point.Y - mat.c3 * point.Z - mat.c4, + mat.b1 * point.X + mat.b2 * point.Y + mat.b3 * point.Z + mat.b4 ); } @@ -66,7 +62,7 @@ namespace rt { aiColor3D color; float coef; if (aiMat->Get(AI_MATKEY_SHININESS, coef) == AI_SUCCESS) { - mat.Ns = coef; + mat.Ns = coef; } aiString name; aiMat->Get(AI_MATKEY_NAME, name); @@ -100,20 +96,20 @@ namespace rt { if (_scene->mNumCameras > 0 && node->mName == _scene->mCameras[0]->mName) { _camera.SetMatrix( - Vector3(matrix.a1, -matrix.c1, matrix.b1), - Vector3(matrix.a2, -matrix.c2, matrix.b2), - Vector3(matrix.a3, -matrix.c3, matrix.b3), - Vector3(matrix.a4, -matrix.c4, matrix.b4) + Vector3(matrix.a1, -matrix.c1, matrix.b1), + Vector3(matrix.a2, -matrix.c2, matrix.b2), + Vector3(matrix.a3, -matrix.c3, matrix.b3), + Vector3(matrix.a4, -matrix.c4, matrix.b4) ); } - + for (std::uint32_t lightIdx = 0; lightIdx < _scene->mNumLights; ++lightIdx) { aiLight* light = _scene->mLights[lightIdx]; if (light->mName == node->mName) { if (light->mType == aiLightSource_POINT) { _lights.emplace_back(new PointLight( - _transform(matrix, Vector3(light->mPosition.x, light->mPosition.y, light->mPosition.z)), - Color(light->mColorDiffuse.r, light->mColorDiffuse.g, light->mColorDiffuse.b) + _transform(matrix, Vector3(light->mPosition.x, light->mPosition.y, light->mPosition.z)), + Color(light->mColorDiffuse.r, light->mColorDiffuse.g, light->mColorDiffuse.b) )); } } @@ -132,32 +128,32 @@ namespace rt { mesh->mVertices[v1Idx].x, mesh->mVertices[v1Idx].y, mesh->mVertices[v1Idx].z - ))); + ))); Vertex v2 = Vertex(_transform(matrix, Vector3( mesh->mVertices[v2Idx].x, mesh->mVertices[v2Idx].y, mesh->mVertices[v2Idx].z - ))); + ))); Vertex v3 = Vertex(_transform(matrix, Vector3( mesh->mVertices[v3Idx].x, mesh->mVertices[v3Idx].y, mesh->mVertices[v3Idx].z - ))); + ))); if (mesh->mNormals) { v1.SetNormal(_transform(matrix, Vector3( - mesh->mNormals[v1Idx].x, - mesh->mNormals[v1Idx].y, - mesh->mNormals[v1Idx].z + mesh->mNormals[v1Idx].x, + mesh->mNormals[v1Idx].y, + mesh->mNormals[v1Idx].z ))); v2.SetNormal(_transform(matrix, Vector3( - mesh->mNormals[v2Idx].x, - mesh->mNormals[v2Idx].y, - mesh->mNormals[v2Idx].z + mesh->mNormals[v2Idx].x, + mesh->mNormals[v2Idx].y, + mesh->mNormals[v2Idx].z ))); v2.SetNormal(_transform(matrix, Vector3( - mesh->mNormals[v3Idx].x, - mesh->mNormals[v3Idx].y, - mesh->mNormals[v3Idx].z + mesh->mNormals[v3Idx].x, + mesh->mNormals[v3Idx].y, + mesh->mNormals[v3Idx].z ))); } triangles.emplace_back(v1, v2, v3); diff --git a/src/Loader/AssimpLoader.h b/src/Loader/AssimpLoader.h index d510a7c..8b0a6a3 100644 --- a/src/Loader/AssimpLoader.h +++ b/src/Loader/AssimpLoader.h @@ -27,26 +27,26 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "../Light/Light.h" namespace rt { -class AssimpLoader { - public: - AssimpLoader(); - ~AssimpLoader(); + class AssimpLoader { + public: + AssimpLoader(); + ~AssimpLoader() = default; - bool LoadFile(std::string const& filePath); - Camera GetCameraFromScene() const; - std::vector> const& GetMeshesFromScene() const; - std::vector> const& GetLightsFromScene() const; + bool LoadFile(std::string const& filePath); + Camera GetCameraFromScene() const; + std::vector> const& GetMeshesFromScene() const; + std::vector> const& GetLightsFromScene() const; - private: - const aiScene* _scene; - Assimp::Importer _importer; - Camera _camera; - std::vector> _meshes; - std::vector> _lights; + private: + const aiScene* _scene; + Assimp::Importer _importer; + Camera _camera; + std::vector> _meshes; + std::vector> _lights; - Vector3 _transform(aiMatrix4x4 const& mat, Vector3 const& point) const; - Material const _loadMaterialFromMesh(unsigned int matIdx) const; - void _loadNode(aiNode *node, aiMatrix4x4 const& parent); -}; + Vector3 _transform(aiMatrix4x4 const& mat, Vector3 const& point) const; + Material const _loadMaterialFromMesh(unsigned int matIdx) const; + void _loadNode(aiNode *node, aiMatrix4x4 const& parent); + }; } //namespace rt diff --git a/src/Material/Material.cc b/src/Material/Material.cc index 2055c8f..2a57b6c 100644 --- a/src/Material/Material.cc +++ b/src/Material/Material.cc @@ -19,18 +19,7 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "Material.h" namespace rt { - Material::Material() { - Ns = 96; - Ka = Vector3(0.75, 0.75, 0.75); - Kd = Vector3(0, 1, 0); - Ks = Vector3(0.5, 0.5, 0.5); - Ke = Vector3(0, 0, 0); - Ni = 1.f; - d = 1.f; - illum = 0; - } - - Material::~Material() { + Material::Material() : Ns(96), Ka(Vector3(0.75, 0.75, 0.75)), Kd(Vector3(0, 1, 0)), Ks(Vector3(0.5, 0.5, 0.5)), Ke(Vector3(0, 0, 0)), Ni(1.f), d(1.f), illum(0) { } Ray const Material::CreateNewRay(Intersection const& inter) { diff --git a/src/Material/Material.h b/src/Material/Material.h index f1d6bb9..562a61c 100644 --- a/src/Material/Material.h +++ b/src/Material/Material.h @@ -24,9 +24,9 @@ namespace rt { struct Ray; struct Intersection; class Material { - public: + public: Material(); - ~Material(); + ~Material() = default; std::string name; float Ns; //Specular exponent diff --git a/src/Mesh/Mesh.cc b/src/Mesh/Mesh.cc index e0a2023..f0ea619 100644 --- a/src/Mesh/Mesh.cc +++ b/src/Mesh/Mesh.cc @@ -20,5 +20,5 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { Material const& Mesh::GetMaterial() const { return _material; - } + } } // namespace rt diff --git a/src/Mesh/Mesh.h b/src/Mesh/Mesh.h index cd0309c..ecfabfa 100644 --- a/src/Mesh/Mesh.h +++ b/src/Mesh/Mesh.h @@ -21,14 +21,14 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "../Material/Material.h" namespace rt { -class Mesh { - public: - virtual Intersection const Intersect(Ray const& ray) = 0; - - Material const& GetMaterial() const; - - protected: - Material _material; - std::string _name; -}; + class Mesh { + public: + virtual Intersection const Intersect(Ray const& ray) = 0; + + Material const& GetMaterial() const; + + protected: + Material _material; + std::string _name; + }; } // namespace rt diff --git a/src/Mesh/Object.cc b/src/Mesh/Object.cc index fd5e3d6..ea68a77 100644 --- a/src/Mesh/Object.cc +++ b/src/Mesh/Object.cc @@ -1,3 +1,5 @@ +#include + /* Copyright (c) 2018 mickael.leclerc@epitech.eu charles.fraisse@epitech.eu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -19,7 +21,7 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "Object.h" namespace rt { - Object::Object(std::vector const& triangles, Material const& mat, std::string const& name) : _triangles(triangles), _KDTree(_triangles, Constant::KDTreeDepth) { + Object::Object(std::vector triangles, Material const& mat, std::string const& name) : _triangles(std::move(triangles)), _KDTree(_triangles, Constant::KDTreeDepth) { _material = mat; _name = name; } diff --git a/src/Mesh/Object.h b/src/Mesh/Object.h index 93934dc..128fb29 100644 --- a/src/Mesh/Object.h +++ b/src/Mesh/Object.h @@ -23,15 +23,15 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "Triangle.h" namespace rt { -class Object : public Mesh { - public: - Object(std::vector const& triangles, Material const& mat, std::string const& name); - ~Object(); + class Object : public Mesh { + public: + Object(std::vector triangles, Material const& mat, std::string const& name); + ~Object(); - virtual Intersection const Intersect(Ray const& ray); + Intersection const Intersect(Ray const& ray) override; - private: - std::vector _triangles; - KDNode _KDTree; -}; + private: + std::vector _triangles; + KDNode _KDTree; + }; } // namespace rt diff --git a/src/Mesh/Triangle.cc b/src/Mesh/Triangle.cc index f46df7f..f111277 100644 --- a/src/Mesh/Triangle.cc +++ b/src/Mesh/Triangle.cc @@ -22,19 +22,16 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { Triangle::Triangle(Vertex const& v1, Vertex const& v2, - Vertex const& v3): _v1(v1), _v2(v2), _v3(v3) { + Vertex const& v3): _v1(v1), _v2(v2), _v3(v3) { this->generateCharacteristics(); } Triangle::Triangle(Vertex const& v1, Vertex const& v2, - Vertex const& v3, Material const& material): _v1(v1), _v2(v2), _v3(v3) { + Vertex const& v3, Material const& material): _v1(v1), _v2(v2), _v3(v3) { _material = material; this->generateCharacteristics(); } - Triangle::~Triangle() { - } - Intersection const Triangle::Intersect(Ray const& ray) { Intersection ret; Vector3 pvec = ray.Direction.Cross(_edge2); @@ -68,62 +65,62 @@ namespace rt { return ret; } - Vector2 const Triangle::GetMinMaxX() const { - return Vector2( - std::min(_v1.GetPos().X, std::min(_v2.GetPos().X, _v3.GetPos().X)), - std::max(_v1.GetPos().X, std::max(_v2.GetPos().X, _v3.GetPos().X)) - ); - } - - Vector2 const Triangle::GetMinMaxY() const { - return Vector2( - std::min(_v1.GetPos().Y, std::min(_v2.GetPos().Y, _v3.GetPos().Y)), - std::max(_v1.GetPos().Y, std::max(_v2.GetPos().Y, _v3.GetPos().Y)) - ); - } - - Vector2 const Triangle::GetMinMaxZ() const { - return Vector2( - std::min(_v1.GetPos().Z, std::min(_v2.GetPos().Z, _v3.GetPos().Z)), - std::max(_v1.GetPos().Z, std::max(_v2.GetPos().Z, _v3.GetPos().Z)) - ); - } - - Vector3 const Triangle::GetMidPoint() const { - return Vector3( - (this->GetMinMaxX().X + this->GetMinMaxX().Y) / 2.f, - (this->GetMinMaxY().X + this->GetMinMaxY().Y) / 2.f, - (this->GetMinMaxZ().X + this->GetMinMaxZ().Y) / 2.f + Vector2 const Triangle::GetMinMaxX() const { + return Vector2( + std::min(_v1.GetPos().X, std::min(_v2.GetPos().X, _v3.GetPos().X)), + std::max(_v1.GetPos().X, std::max(_v2.GetPos().X, _v3.GetPos().X)) ); - } - - Vertex const& Triangle::GetV1() const { - return _v1; - } - - Vertex const& Triangle::GetV2() const { - return _v2; - } - - Vertex const& Triangle::GetV3() const { - return _v3; - } - - Vector3 const& Triangle::GetEdge1() const { - return _edge1; - } - - Vector3 const& Triangle::GetEdge2() const { - return _edge2; - } - - Vector3 const& Triangle::GetNormal() const { - return _normal; - } - - bool Triangle::operator==(Triangle const& other) const { - return (_v1 == other.GetV1() && _v2 == other.GetV2() && _v3 == other.GetV3()); - } + } + + Vector2 const Triangle::GetMinMaxY() const { + return Vector2( + std::min(_v1.GetPos().Y, std::min(_v2.GetPos().Y, _v3.GetPos().Y)), + std::max(_v1.GetPos().Y, std::max(_v2.GetPos().Y, _v3.GetPos().Y)) + ); + } + + Vector2 const Triangle::GetMinMaxZ() const { + return Vector2( + std::min(_v1.GetPos().Z, std::min(_v2.GetPos().Z, _v3.GetPos().Z)), + std::max(_v1.GetPos().Z, std::max(_v2.GetPos().Z, _v3.GetPos().Z)) + ); + } + + Vector3 const Triangle::GetMidPoint() const { + return Vector3( + (this->GetMinMaxX().X + this->GetMinMaxX().Y) / 2.f, + (this->GetMinMaxY().X + this->GetMinMaxY().Y) / 2.f, + (this->GetMinMaxZ().X + this->GetMinMaxZ().Y) / 2.f + ); + } + + Vertex const& Triangle::GetV1() const { + return _v1; + } + + Vertex const& Triangle::GetV2() const { + return _v2; + } + + Vertex const& Triangle::GetV3() const { + return _v3; + } + + Vector3 const& Triangle::GetEdge1() const { + return _edge1; + } + + Vector3 const& Triangle::GetEdge2() const { + return _edge2; + } + + Vector3 const& Triangle::GetNormal() const { + return _normal; + } + + bool Triangle::operator==(Triangle const& other) const { + return (_v1 == other.GetV1() && _v2 == other.GetV2() && _v3 == other.GetV3()); + } void Triangle::generateCharacteristics() { _edge1 = _v2.GetPos() - _v1.GetPos(); diff --git a/src/Mesh/Triangle.h b/src/Mesh/Triangle.h index f5d0895..44c1f56 100644 --- a/src/Mesh/Triangle.h +++ b/src/Mesh/Triangle.h @@ -23,35 +23,35 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "../Vector/Vector3.h" namespace rt { - class Triangle : public Mesh { + class Triangle : public Mesh { public: - Triangle(Vertex const& v1, Vertex const& v2, Vertex const& v3); - Triangle(Vertex const& v1, Vertex const& v2, Vertex const& v3, Material const& material); - ~Triangle(); + Triangle(Vertex const& v1, Vertex const& v2, Vertex const& v3); + Triangle(Vertex const& v1, Vertex const& v2, Vertex const& v3, Material const& material); + ~Triangle() = default; - virtual Intersection const Intersect(Ray const& ray); + Intersection const Intersect(Ray const& ray) override; - Vector2 const GetMinMaxX() const; - Vector2 const GetMinMaxY() const; - Vector2 const GetMinMaxZ() const; - Vector3 const GetMidPoint() const; - Vertex const& GetV1() const; - Vertex const& GetV2() const; - Vertex const& GetV3() const; - Vector3 const& GetEdge1() const; - Vector3 const& GetEdge2() const; - Vector3 const& GetNormal() const; + Vector2 const GetMinMaxX() const; + Vector2 const GetMinMaxY() const; + Vector2 const GetMinMaxZ() const; + Vector3 const GetMidPoint() const; + Vertex const& GetV1() const; + Vertex const& GetV2() const; + Vertex const& GetV3() const; + Vector3 const& GetEdge1() const; + Vector3 const& GetEdge2() const; + Vector3 const& GetNormal() const; - bool operator==(Triangle const& other) const; + bool operator==(Triangle const& other) const; private: - Vertex _v1; - Vertex _v2; - Vertex _v3; - Vector3 _normal; - Vector3 _edge1; - Vector3 _edge2; - - void generateCharacteristics(); - }; + Vertex _v1; + Vertex _v2; + Vertex _v3; + Vector3 _normal; + Vector3 _edge1; + Vector3 _edge2; + + void generateCharacteristics(); + }; } // namespace rt diff --git a/src/Mesh/Triangle_test.cc b/src/Mesh/Triangle_test.cc index 5f26e66..ed86339 100644 --- a/src/Mesh/Triangle_test.cc +++ b/src/Mesh/Triangle_test.cc @@ -22,9 +22,9 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { TEST(Triangle, initBasic) { Triangle t = Triangle( - Vector3(), - Vector3(1, 1, 1), - Vector3(2, 2, 2) + Vector3(), + Vector3(1, 1, 1), + Vector3(2, 2, 2) ); EXPECT_EQ(t.GetEdge1(), Vector3(1, 1, 1)); @@ -36,10 +36,10 @@ namespace rt { TEST(Triangle, initBasic2) { Triangle t = Triangle( - Vector3(), - Vector3(1, 1, 1), - Vector3(2, 2, 2), - Material() + Vector3(), + Vector3(1, 1, 1), + Vector3(2, 2, 2), + Material() ); EXPECT_EQ(t.GetEdge1(), Vector3(1, 1, 1)); @@ -48,44 +48,44 @@ namespace rt { TEST(Triangle, Intersection) { Triangle t2 = Triangle( - Vector3(), - Vector3(1, 0, 0), - Vector3(0, 1, 0) + Vector3(), + Vector3(1, 0, 0), + Vector3(0, 1, 0) ); EXPECT_TRUE(t2.Intersect(Ray( - Vector3(.3, .3, 1), - Vector3(0, 0, -1) + Vector3(.3, .3, 1), + Vector3(0, 0, -1) )).Intersect); EXPECT_FALSE(t2.Intersect(Ray( - Vector3(.3, .3, 1), - Vector3(0, 0, 1) + Vector3(.3, .3, 1), + Vector3(0, 0, 1) )).Intersect); t2 = Triangle( - Vector3(), - Vector3(0, 1, 0), - Vector3(1, 0, 0) + Vector3(), + Vector3(0, 1, 0), + Vector3(1, 0, 0) ); EXPECT_TRUE(t2.Intersect(Ray( - Vector3(.3, .3, 1), - Vector3(0, 0, -1) + Vector3(.3, .3, 1), + Vector3(0, 0, -1) )).Intersect); Triangle t3 = Triangle( - Vector3(0, 0, -1), - Vector3(3, 0, -1), - Vector3(0, 0, 0) + Vector3(0, 0, -1), + Vector3(3, 0, -1), + Vector3(0, 0, 0) ); EXPECT_FALSE(t3.Intersect(Ray( - Vector3(1.5, 3, -0.5), - Vector3(0, 1, 0) + Vector3(1.5, 3, -0.5), + Vector3(0, 1, 0) )).Intersect); } - + TEST(Triangle, MaterialRetrieving) { Triangle t = Triangle( - Vector3(), - Vector3(1, 1, 1), - Vector3(2, 2, 2) + Vector3(), + Vector3(1, 1, 1), + Vector3(2, 2, 2) ); Material mat = t.GetMaterial(); EXPECT_EQ(mat.Kd, Vector3(0, 1, 0)); @@ -93,18 +93,18 @@ namespace rt { TEST(Triangle, GetNormal) { Triangle t = Triangle( - Vector3(), - Vector3(0, 0, -1), - Vector3(1, 0, 0) + Vector3(), + Vector3(0, 0, -1), + Vector3(1, 0, 0) ); EXPECT_EQ(t.GetNormal(), Vector3(0, -1, 0)); } TEST(Triangle, GetMinMax) { Triangle t = Triangle( - Vector3(1, 4, -5), - Vector3(5, -3, 2), - Vector3(2, 3, -8) + Vector3(1, 4, -5), + Vector3(5, -3, 2), + Vector3(2, 3, -8) ); EXPECT_EQ(t.GetMinMaxX(), Vector2(1, 5)); EXPECT_EQ(t.GetMinMaxY(), Vector2(-3, 4)); @@ -113,23 +113,23 @@ namespace rt { TEST(Triangle, GetMidPoint) { Triangle t = Triangle( - Vector3(1, 4, -5), - Vector3(5, -3, 2), - Vector3(2, 3, -8) + Vector3(1, 4, -5), + Vector3(5, -3, 2), + Vector3(2, 3, -8) ); EXPECT_EQ(t.GetMidPoint(), Vector3(3, .5, -3)); } TEST(Triangle, EqualOperator) { Triangle t = Triangle( - Vector3(1, 4, -5), - Vector3(5, -3, 2), - Vector3(2, 3, -8) + Vector3(1, 4, -5), + Vector3(5, -3, 2), + Vector3(2, 3, -8) ); Triangle t2 = Triangle( - Vector3(1, 4, -5), - Vector3(5, -3, 2), - Vector3(2, 3, -8) + Vector3(1, 4, -5), + Vector3(5, -3, 2), + Vector3(2, 3, -8) ); EXPECT_TRUE(t == t2); } diff --git a/src/Mesh/Vertex.cc b/src/Mesh/Vertex.cc index 292b402..ed6bf34 100644 --- a/src/Mesh/Vertex.cc +++ b/src/Mesh/Vertex.cc @@ -20,9 +20,6 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { Vertex::Vertex(Vector3 const& pos) : _pos(pos) { } - - Vertex::~Vertex() { - } Vector3 const& Vertex::GetPos() const { return _pos; @@ -37,7 +34,7 @@ namespace rt { } bool Vertex::operator==(Vertex const& other) const { - return (_pos == other._pos && _normal == other._normal); - } + return (_pos == other._pos && _normal == other._normal); + } } // namespace rt diff --git a/src/Mesh/Vertex.h b/src/Mesh/Vertex.h index 1a6562a..5baeecb 100644 --- a/src/Mesh/Vertex.h +++ b/src/Mesh/Vertex.h @@ -21,9 +21,9 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { class Vertex { - public: + public: Vertex(Vector3 const& pos); - ~Vertex(); + ~Vertex() = default; Vector3 const& GetPos() const; Vector3 const& GetNormal() const; @@ -31,7 +31,7 @@ namespace rt { bool operator==(Vertex const& other) const; - private: + private: Vector3 _pos; Vector3 _normal; }; diff --git a/src/Sky/SphereSky.cc b/src/Sky/SphereSky.cc index 86795f9..c128f7c 100644 --- a/src/Sky/SphereSky.cc +++ b/src/Sky/SphereSky.cc @@ -20,9 +20,6 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ rt::SphereSky::SphereSky(const rt::Vector2 &size, const std::uint8_t *pixels) : _size(size), _pixels(pixels, pixels + (size.X * size.Y * 4)) { } -rt::SphereSky::~SphereSky() { -} - rt::Color rt::SphereSky::GetPixel(const rt::Vector3 &direction) const { unsigned int u = (0.5f + (std::atan2(direction.Y, direction.X) / (2.f * static_cast(M_PI)))) * (_size.X - 1); unsigned int v = (0.5f - std::asin(direction.Z) / static_cast(M_PI)) * (_size.Y - 1); diff --git a/src/Sky/SphereSky.h b/src/Sky/SphereSky.h index 7dfc1a1..323a3d1 100644 --- a/src/Sky/SphereSky.h +++ b/src/Sky/SphereSky.h @@ -28,7 +28,7 @@ namespace rt { class SphereSky : public Sky { public: SphereSky(Vector2 const& size, const std::uint8_t *pixels); - ~SphereSky() override; + ~SphereSky() override = default; Color GetPixel(Vector3 const& direction) const override; diff --git a/src/Vector/Vector2.h b/src/Vector/Vector2.h index 3d3974e..db9996a 100644 --- a/src/Vector/Vector2.h +++ b/src/Vector/Vector2.h @@ -23,9 +23,9 @@ namespace rt { template struct Vector2 { public: - Vector2(void) : X(0), Y(0) {}; + Vector2() : X(0), Y(0) {}; Vector2(T _X, T _Y) : X(_X), Y(_Y) {}; - virtual ~Vector2(void) {}; + virtual ~Vector2() = default; bool operator==(Vector2 const& other) const { return (X == other.X && Y == other.Y); diff --git a/src/Vector/Vector3.h b/src/Vector/Vector3.h index d25ac34..ca1ee38 100644 --- a/src/Vector/Vector3.h +++ b/src/Vector/Vector3.h @@ -1,95 +1,95 @@ -/* Copyright (c) 2018 mickael.leclerc@epitech.eu charles.fraisse@epitech.eu - Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, -DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE -OR OTHER DEALINGS IN THE SOFTWARE. */ - -#pragma once - -#include -#include -#include "Vector2.h" - -namespace rt { - template - struct Vector3 : public Vector2 { - public: - Vector3(void) : Z(0) { - this->X = 0; - this->Y = 0; - }; - Vector3(T _X, T _Y, T _Z) : Z(_Z) { - this->X = _X; - this->Y = _Y; - }; - virtual ~Vector3(void) {}; - - Vector3 Cross(Vector3 const& other) const { - return Vector3(this->Y * other.Z - Z * other.Y, - Z * other.X - this->X * other.Z, - this->X * other.Y - this->Y * other.X); - } - - T Dot(Vector3 const& other) const { - return (this->X * other.X) + (this->Y * other.Y) + (Z * other.Z); - } - - T Norm(void) const { - return (std::sqrt(std::pow(this->X, 2) + std::pow(this->Y, 2) + std::pow(Z, 2))); - } - - T Angle(Vector3 const & other) const { - return std::acos(this->Dot(other) / (this->Norm() * other.Norm())) * 180.f / static_cast(M_PI); - } - - void Normalize(void) { - float norm = this->Norm(); - this->X = this->X / norm; - this->Y = this->Y / norm; - Z = Z / norm; - } - - bool operator==(Vector3 const& other) const { - return (this->X == other.X && this->Y == other.Y && Z == other.Z); - } - - bool operator!=(Vector3 const& other) const { - return (this->X != other.X || this->Y != other.Y || Z != other.Z); - } - - Vector3 operator+(Vector3 const& right) const { - return Vector3(this->X + right.X, this->Y + right.Y, Z + right.Z); - } - - Vector3 operator-(Vector3 const& right) const { - return Vector3(this->X - right.X, this->Y - right.Y, Z - right.Z); - } - - Vector3 operator*(T const& other) const { - return Vector3(this->X * other, this->Y * other, Z * other); - } - - Vector3 operator/(T const& other) const { - return Vector3(this->X / other, this->Y / other, Z / other); - } - - T Z; - }; - - template - std::ostream& operator<<(std::ostream& out, const Vector3& v) { - out << "(" << v.X << ", " << v.Y << ", " << v.Z << ")"; - return out; - } -} // namespace rt +/* Copyright (c) 2018 mickael.leclerc@epitech.eu charles.fraisse@epitech.eu + Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +OR OTHER DEALINGS IN THE SOFTWARE. */ + +#pragma once + +#include +#include +#include "Vector2.h" + +namespace rt { + template + struct Vector3 : public Vector2 { + public: + Vector3(void) : Z(0) { + this->X = 0; + this->Y = 0; + }; + Vector3(T _X, T _Y, T _Z) : Z(_Z) { + this->X = _X; + this->Y = _Y; + }; + virtual ~Vector3(void) {}; + + Vector3 Cross(Vector3 const& other) const { + return Vector3(this->Y * other.Z - Z * other.Y, + Z * other.X - this->X * other.Z, + this->X * other.Y - this->Y * other.X); + } + + T Dot(Vector3 const& other) const { + return (this->X * other.X) + (this->Y * other.Y) + (Z * other.Z); + } + + T Norm(void) const { + return (std::sqrt(std::pow(this->X, 2) + std::pow(this->Y, 2) + std::pow(Z, 2))); + } + + T Angle(Vector3 const & other) const { + return std::acos(this->Dot(other) / (this->Norm() * other.Norm())) * 180.f / static_cast(M_PI); + } + + void Normalize(void) { + T norm = this->Norm(); + this->X = this->X / norm; + this->Y = this->Y / norm; + Z = Z / norm; + } + + bool operator==(Vector3 const& other) const { + return (this->X == other.X && this->Y == other.Y && Z == other.Z); + } + + bool operator!=(Vector3 const& other) const { + return (this->X != other.X || this->Y != other.Y || Z != other.Z); + } + + Vector3 operator+(Vector3 const& right) const { + return Vector3(this->X + right.X, this->Y + right.Y, Z + right.Z); + } + + Vector3 operator-(Vector3 const& right) const { + return Vector3(this->X - right.X, this->Y - right.Y, Z - right.Z); + } + + Vector3 operator*(T const& other) const { + return Vector3(this->X * other, this->Y * other, Z * other); + } + + Vector3 operator/(T const& other) const { + return Vector3(this->X / other, this->Y / other, Z / other); + } + + T Z; + }; + + template + std::ostream& operator<<(std::ostream& out, const Vector3& v) { + out << "(" << v.X << ", " << v.Y << ", " << v.Z << ")"; + return out; + } +} // namespace rt From f5af6d40e975c0b04b38f787271b5a9542572cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Leclerc?= Date: Tue, 14 May 2019 00:38:06 +0100 Subject: [PATCH 3/6] improv(performance/style): Improved performance and style --- src/Mesh/Object.cc | 2 +- src/Mesh/Object.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mesh/Object.cc b/src/Mesh/Object.cc index ea68a77..3e674f5 100644 --- a/src/Mesh/Object.cc +++ b/src/Mesh/Object.cc @@ -21,7 +21,7 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ #include "Object.h" namespace rt { - Object::Object(std::vector triangles, Material const& mat, std::string const& name) : _triangles(std::move(triangles)), _KDTree(_triangles, Constant::KDTreeDepth) { + Object::Object(std::vector const& triangles, Material const& mat, std::string const& name) : _triangles(triangles), _KDTree(_triangles, Constant::KDTreeDepth) { _material = mat; _name = name; } diff --git a/src/Mesh/Object.h b/src/Mesh/Object.h index 128fb29..2fc4419 100644 --- a/src/Mesh/Object.h +++ b/src/Mesh/Object.h @@ -25,7 +25,7 @@ OR OTHER DEALINGS IN THE SOFTWARE. */ namespace rt { class Object : public Mesh { public: - Object(std::vector triangles, Material const& mat, std::string const& name); + Object(std::vector const& triangles, Material const& mat, std::string const& name); ~Object(); Intersection const Intersect(Ray const& ray) override; From dffa744410588f73ae3447a8e764d536e41ad5ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Leclerc?= Date: Tue, 14 May 2019 00:39:57 +0100 Subject: [PATCH 4/6] improv(performance/style): Improved performance and style --- src/Mesh/Object.cc | 3 --- src/Mesh/Object.h | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Mesh/Object.cc b/src/Mesh/Object.cc index 3e674f5..85fbab3 100644 --- a/src/Mesh/Object.cc +++ b/src/Mesh/Object.cc @@ -26,9 +26,6 @@ namespace rt { _name = name; } - Object::~Object() { - } - Intersection const Object::Intersect(Ray const& ray) { Intersection inter = _KDTree.Intersect(ray); inter.Mat = _material; diff --git a/src/Mesh/Object.h b/src/Mesh/Object.h index 2fc4419..1091870 100644 --- a/src/Mesh/Object.h +++ b/src/Mesh/Object.h @@ -26,7 +26,7 @@ namespace rt { class Object : public Mesh { public: Object(std::vector const& triangles, Material const& mat, std::string const& name); - ~Object(); + ~Object() = default; Intersection const Intersect(Ray const& ray) override; From 65568c85a0cd486240667019a660ca04ab6377b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FRA=C3=8FSSE=20Charles?= Date: Tue, 28 Apr 2020 11:38:14 +0200 Subject: [PATCH 5/6] style(KDNode): removing duplicate --- src/KDTree/KDNode.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/KDTree/KDNode.cc b/src/KDTree/KDNode.cc index fe54740..be9f4c5 100644 --- a/src/KDTree/KDNode.cc +++ b/src/KDTree/KDNode.cc @@ -1,7 +1,5 @@ #include -#include - /* Copyright (c) 2018 mickael.leclerc@epitech.eu charles.fraisse@epitech.eu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 4016330fa8e52e81e9fc036bfb16b3ed459cf72e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?FRA=C3=8FSSE=20Charles?= Date: Tue, 28 Apr 2020 11:47:43 +0200 Subject: [PATCH 6/6] feat(dispatcher): calculating frame's render time --- src/Dispatcher/Dispatcher.cc | 12 +++++++++++- src/Dispatcher/Dispatcher.h | 2 ++ src/Loader/AssimpLoader.cc | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Dispatcher/Dispatcher.cc b/src/Dispatcher/Dispatcher.cc index eccc3a4..4a7dc6e 100644 --- a/src/Dispatcher/Dispatcher.cc +++ b/src/Dispatcher/Dispatcher.cc @@ -15,6 +15,7 @@ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include #include #include "Dispatcher.h" @@ -53,6 +54,8 @@ namespace rt { void Dispatcher::execute() { std::size_t size = _res.Y * _res.X; + std::chrono::_V2::steady_clock::time_point start = std::chrono::steady_clock::now(); + std::chrono::_V2::steady_clock::time_point end; while (_running) { std::size_t current = _image_index++; @@ -61,7 +64,14 @@ namespace rt { current %= size; if (current == 0) { _sample++; - std::cout << "FRAME" << std::endl; + end = std::chrono::steady_clock::now(); + std::size_t ms = std::chrono::duration_cast(end - start).count(); + _frames_mutex.lock(); + _frames_durations.push_back(ms); + unsigned long avg = std::accumulate(_frames_durations.begin(), _frames_durations.end(), 0) / _frames_durations.size(); + _frames_mutex.unlock(); + std::cout << "Frame rendered in " << ms << "ms.\tAverage render time " << avg << " ms"<< std::endl; + start = std::chrono::steady_clock::now(); } } Color color = _engine.Raytrace(Vector2(current % _res.X, current / _res.X)); diff --git a/src/Dispatcher/Dispatcher.h b/src/Dispatcher/Dispatcher.h index f5e2ff3..f0450e3 100644 --- a/src/Dispatcher/Dispatcher.h +++ b/src/Dispatcher/Dispatcher.h @@ -45,5 +45,7 @@ namespace rt { std::mutex _image_mutex; std::vector _image; std::atomic _sample; + std::vector _frames_durations; + std::mutex _frames_mutex; }; } // namespace rt diff --git a/src/Loader/AssimpLoader.cc b/src/Loader/AssimpLoader.cc index 6fec441..68b6765 100644 --- a/src/Loader/AssimpLoader.cc +++ b/src/Loader/AssimpLoader.cc @@ -159,7 +159,7 @@ namespace rt { triangles.emplace_back(v1, v2, v3); } } - std::cout << "Creating KDTree for " << mesh->mName.C_Str() << " composed of " << triangles.size() << std::endl; + std::cout << "Creating KDTree for " << mesh->mName.C_Str() << " composed of " << triangles.size() << " triangles." << std::endl; _meshes.emplace_back(new Object(triangles, _loadMaterialFromMesh(mesh->mMaterialIndex), mesh->mName.C_Str())); std::cout << "Done" << std::endl; }