Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions src/Camera/Camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,50 @@ OR OTHER DEALINGS IN THE SOFTWARE. */
#include "../Engine/Constant.h"

namespace rt {
Camera::Camera(): _pos(Vector3<float>(0, 0, 0)), _c1(Vector3<float>(1, 0, 0)),
_c2(Vector3<float>(0, 1, 0)), _c3(Vector3<float>(0, 0, 1)),
_gen(std::random_device()()), _dis(0.f, 1.f) {
Camera::Camera() : _pos(Vector3<float>(0, 0, 0)), _c1(Vector3<float>(1, 0, 0)),
_c2(Vector3<float>(0, 1, 0)), _c3(Vector3<float>(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<unsigned int> 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<float> 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<float> const& Camera::GetPos(void) const {
Vector3<float> const& Camera::GetPos() const {
return _pos;
}

Vector2<unsigned int> const& Camera::GetRes(void) const {
Vector2<unsigned int> const& Camera::GetRes() const {
return _screenRes;
}

void Camera::SetMatrix(Vector3<float> const& c1, Vector3<float> const& c2,
Vector3<float> const& c3, Vector3<float> const& pos) {
_c1 = c1;
_c2 = c2;
_c3 = c3;
_pos = pos;
generateScreen();
void Camera::SetMatrix(Vector3<float> const& c1, Vector3<float> const& c2,
Vector3<float> const& c3, Vector3<float> const& pos) {
_c1 = c1;
_c2 = c2;
_c3 = c3;
_pos = pos;
generateScreen();
}

void Camera::generateScreen() {
_screenDist = 1.f;
void Camera::generateScreen() {
_screenRes = Vector2<unsigned int>(Constant::DefaultScreenWidth, Constant::DefaultScreenHeight);
float screenWidth = 2.f * std::tan((Constant::DefaultScreenFOV / 2.f) * static_cast<float>(M_PI) / 180.f) * _screenDist;
_screenSize = Vector2<float>(screenWidth, screenWidth * _screenRes.Y / _screenRes.X);
_screenCorner = _pos + _c3 * (-1.f) - _c1 * (_screenSize.X / 2.f) + _c2 * (_screenSize.Y / 2.f);
}
}
} // namespace rt
52 changes: 26 additions & 26 deletions src/Camera/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned int> const &pos);
Vector3<float> const& GetPos(void) const;
Vector2<unsigned int> const& GetRes(void) const;
void SetMatrix(Vector3<float> const& pos, Vector3<float> const& c1, Vector3<float> const& c2, Vector3<float> const& c3);
private:
Vector3<float> _pos;
Vector3<float> _c1;
Vector3<float> _c2;
Vector3<float> _c3;
Vector2<unsigned int> _screenRes;
Vector2<float> _screenSize;
Vector3<float> _screenCorner;
float _screenDist;
std::mt19937 _gen;
std::uniform_real_distribution<float> _dis;


void generateScreen();
};
class Camera {
public:
Camera();
virtual ~Camera();

Ray const GenerateRay(Vector2<unsigned int> const &pos);

Vector3<float> const& GetPos() const;
Vector2<unsigned int> const& GetRes() const;
void SetMatrix(Vector3<float> const& pos, Vector3<float> const& c1, Vector3<float> const& c2, Vector3<float> const& c3);

private:
Vector3<float> _pos;
Vector3<float> _c1;
Vector3<float> _c2;
Vector3<float> _c3;
Vector2<unsigned int> _screenRes;
Vector2<float> _screenSize;
Vector3<float> _screenCorner;
float _screenDist;
std::mt19937 _gen;
std::uniform_real_distribution<float> _dis;


void generateScreen();
};
} // namespace rt
15 changes: 11 additions & 4 deletions src/Dispatcher/Dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <chrono>
#include <thread>
#include "Dispatcher.h"

Expand All @@ -25,9 +26,6 @@ namespace rt {
_image.resize(size, Color(0x000000ff));
}

Dispatcher::~Dispatcher() {
}

void Dispatcher::Start() {
_running = true;
std::size_t cores = std::thread::hardware_concurrency();
Expand Down Expand Up @@ -56,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++;
Expand All @@ -64,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<std::chrono::milliseconds>(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<unsigned int>(current % _res.X, current / _res.X));
Expand Down
14 changes: 8 additions & 6 deletions src/Dispatcher/Dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ namespace rt {
class Dispatcher {
public:
explicit Dispatcher(Engine const& engine, Vector2<unsigned int> const& res);
virtual ~Dispatcher();
virtual ~Dispatcher() = default;

void Start(void);
void Stop(void);
std::vector<Color> Flush(void);
std::size_t GetNumberOfProcessed(void) const;
void Start();
void Stop();
std::vector<Color> Flush();
std::size_t GetNumberOfProcessed() const;

private:
void execute(void);
void execute();

bool _running;
Engine _engine;
Expand All @@ -45,5 +45,7 @@ namespace rt {
std::mutex _image_mutex;
std::vector<Color> _image;
std::atomic<std::size_t> _sample;
std::vector<std::size_t> _frames_durations;
std::mutex _frames_mutex;
};
} // namespace rt
2 changes: 1 addition & 1 deletion src/Dispatcher/Dispatcher_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float> const& vec);
virtual ~Color(void);
virtual ~Color();

Color_Component GetColor() const;
void SetColor(Color_Component const& component);
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Color_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace rt {
TEST(Color, Add) {
Color color(0x0f0f0fff);
color += Color(0x123456ff);

ASSERT_EQ(color.GetColor().hexcode, 0x214365ff);
}
} // namespace rt
17 changes: 6 additions & 11 deletions src/Engine/Engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,20 @@ 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<unsigned int> &pixel) {
Color color = Color();
Ray ray = _camera.GenerateRay(pixel);
Intersection inter = _intersect(ray);
if (inter.Intersect) {
for (size_t i = 0; i < _lights.size(); ++i) {
Vector3<float> lightDir = _lights[i]->GetPos() - inter.Point;
for (auto & _light : _lights) {
Vector3<float> 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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Engine/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace rt {
class Engine {
public:
explicit Engine(AssimpLoader const& loader);
virtual ~Engine();
virtual ~Engine() = default;

Color Raytrace(Vector2<unsigned int> const& pixel);
Vector2<unsigned int> GetRes() const;
Expand All @@ -44,7 +44,7 @@ namespace rt {
std::vector<std::shared_ptr<Mesh>> _meshes;
std::vector<std::shared_ptr<Light>> _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
12 changes: 4 additions & 8 deletions src/KDTree/KDBox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ OR OTHER DEALINGS IN THE SOFTWARE. */
#include "KDBox.h"

namespace rt {

KDBox::KDBox(std::vector<Triangle> 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;
Expand All @@ -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;
}
Expand All @@ -54,12 +50,12 @@ namespace rt {
Vector2<float> 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;
}

Expand Down
30 changes: 15 additions & 15 deletions src/KDTree/KDBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ OR OTHER DEALINGS IN THE SOFTWARE. */
#include "../Engine/Color.h"

namespace rt {
class KDBox {
public:
KDBox(std::vector<Triangle> const& triangles);
~KDBox();
class KDBox {
public:
explicit KDBox(std::vector<Triangle> const& triangles);
~KDBox() = default;

bool Intersect(Ray const& ray);
bool Intersect(Ray const& ray);

Vector2<float> const& GetX() const;
Vector2<float> const& GetY() const;
Vector2<float> const& GetZ() const;
std::size_t GetLongestAxis() const;
Vector2<float> const& GetX() const;
Vector2<float> const& GetY() const;
Vector2<float> const& GetZ() const;
std::size_t GetLongestAxis() const;

private:
Vector2<float> _x; // Containing Xmin and Xmax
Vector2<float> _y; // Containing Ymin and Ymax
Vector2<float> _z; // Containing Zmin and Zmax
private:
Vector2<float> _x; // Containing Xmin and Xmax
Vector2<float> _y; // Containing Ymin and Ymax
Vector2<float> _z; // Containing Zmin and Zmax

void setMinMax(std::vector<Triangle> const& triangles);
};
void setMinMax(std::vector<Triangle> const& triangles);
};
} // namespace rt
Loading