From 8251be3e7ec0e381391c951fd4c8f1ab8080bef9 Mon Sep 17 00:00:00 2001 From: Julian T Date: Tue, 12 Jan 2021 18:48:40 +0100 Subject: Reorgranize source file structure --- CMakeLists.txt | 11 +++++- app/main.cpp | 8 ++-- src/common.hpp | 6 --- src/core/common.hpp | 6 +++ src/core/ray.cpp | 17 +++++++++ src/core/ray.hpp | 15 ++++++++ src/core/vector.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/vector.hpp | 33 ++++++++++++++++ src/object.cpp | 2 +- src/object.hpp | 4 +- src/ray.cpp | 17 --------- src/ray.hpp | 15 -------- src/render.cpp | 4 +- src/render.hpp | 4 +- src/vector.cpp | 106 ---------------------------------------------------- src/vector.hpp | 33 ---------------- 16 files changed, 197 insertions(+), 190 deletions(-) delete mode 100644 src/common.hpp create mode 100644 src/core/common.hpp create mode 100644 src/core/ray.cpp create mode 100644 src/core/ray.hpp create mode 100644 src/core/vector.cpp create mode 100644 src/core/vector.hpp delete mode 100644 src/ray.cpp delete mode 100644 src/ray.hpp delete mode 100644 src/vector.cpp delete mode 100644 src/vector.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index df9bd70..7ae5197 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.13) project(pathtracing) @@ -15,7 +15,14 @@ set(CMAKE_CXX_FLAGS_DEBUG "-g") set(CMAKE_CXX_FLAGS_RELEASE "-O3") FILE(GLOB app_sources ${CMAKE_SOURCE_DIR}/app/*.cpp) -FILE(GLOB sources ${CMAKE_SOURCE_DIR}/src/*.cpp) +SET(sources + src/core/ray.cpp + src/core/vector.cpp + src/object.cpp + src/render.cpp + src/scene.cpp + ) +list(TRANSFORM sources PREPEND ${CMAKE_SOURCE_DIR}/) FILE(GLOB test_sources ${CMAKE_SOURCE_DIR}/test/*.cpp) add_executable(pathtracing ${sources} ${app_sources}) diff --git a/app/main.cpp b/app/main.cpp index 16de1f6..ebb0536 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -3,7 +3,7 @@ #include #include "mainwindow.hpp" -#include "vector.hpp" +#include #include #include #include @@ -15,10 +15,10 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); Scene scn; Config conf; - conf.m_width = 500; - conf.m_height = 500; + conf.m_width = 1000; + conf.m_height = 1000; conf.m_maxhops = 5; - conf.m_samples = 100; + conf.m_samples = 10000; conf.m_framerate = 3; conf.m_workers = 4; diff --git a/src/common.hpp b/src/common.hpp deleted file mode 100644 index fd219bf..0000000 --- a/src/common.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef COMMON_H -#define COMMON_H - -#define ZERO_APPROX 1e-6 - -#endif diff --git a/src/core/common.hpp b/src/core/common.hpp new file mode 100644 index 0000000..fd219bf --- /dev/null +++ b/src/core/common.hpp @@ -0,0 +1,6 @@ +#ifndef COMMON_H +#define COMMON_H + +#define ZERO_APPROX 1e-6 + +#endif diff --git a/src/core/ray.cpp b/src/core/ray.cpp new file mode 100644 index 0000000..7bc6201 --- /dev/null +++ b/src/core/ray.cpp @@ -0,0 +1,17 @@ +#include "ray.hpp" + +Ray::Ray(Vec3d start, Vec3d direction, bool normalize) { + m_start = start; + m_direction = direction; + + if (normalize) { + m_direction.normalize(); + } +} + +Ray::Ray(Vec3d a, Vec3d b) { + m_start = a; + m_direction = b - a; + + m_direction.normalize(); +} diff --git a/src/core/ray.hpp b/src/core/ray.hpp new file mode 100644 index 0000000..6341d44 --- /dev/null +++ b/src/core/ray.hpp @@ -0,0 +1,15 @@ +#ifndef RAY_H +#define RAY_H + +#include "vector.hpp" + +class Ray { + public: + Ray(Vec3d start, Vec3d direction, bool normalize); + Ray(Vec3d a, Vec3d b); + + Vec3d m_start; + Vec3d m_direction; +}; + +#endif diff --git a/src/core/vector.cpp b/src/core/vector.cpp new file mode 100644 index 0000000..51d8e2e --- /dev/null +++ b/src/core/vector.cpp @@ -0,0 +1,106 @@ +#include "vector.hpp" + +#include +#include + +Vec3d::Vec3d() { + set(0, 0, 0); +} + +Vec3d::Vec3d(double x, double y, double z) { + set(x, y, z); +} + +void Vec3d::set(double x, double y, double z) { + m_x = x; + m_y = y; + m_z = z; +} + +void Vec3d::normalize() { + auto len = length(); + if (len == 0) { + throw std::runtime_error("Normalizing zero vector"); + } + + m_x /= len; + m_y /= len; + m_z /= len; +} + +double Vec3d::length() const { + return sqrt(m_x * m_x + m_y * m_y + m_z * m_z); +} + +double Vec3d::dot(const Vec3d &vec) const { + return m_x * vec.m_x + m_y * vec.m_y + m_z * vec.m_z; +} + +Vec3d Vec3d::cross(const Vec3d &vec) const { + return Vec3d( + m_y * vec.m_z - m_z * vec.m_y, + m_z * vec.m_x - m_x * vec.m_z, + m_x * vec.m_y - m_y * vec.m_x + ); +} + +Vec3d Vec3d::operator+(const Vec3d &vec) const { + return Vec3d( + m_x + vec.m_x, + m_y + vec.m_y, + m_z + vec.m_z + ); +} + +Vec3d& Vec3d::operator+=(const Vec3d &vec) { + m_x += vec.m_x; + m_y += vec.m_y; + m_z += vec.m_z; + return *this; +} + +Vec3d Vec3d::operator-(const Vec3d &vec) const { + return Vec3d( + m_x - vec.m_x, + m_y - vec.m_y, + m_z - vec.m_z + ); +} + +Vec3d Vec3d::operator-() const { + return Vec3d( + -m_x, + -m_y, + -m_z + ); +} + + +Vec3d Vec3d::operator*(double op) const { + return Vec3d( + m_x * op, + m_y * op, + m_z * op + ); +} + +Vec3d Vec3d::operator*(const Vec3d &vec) const { + return Vec3d( + m_x * vec.m_x, + m_y * vec.m_y, + m_z * vec.m_z + ); +} + +Vec3d Vec3d::operator/(double op) const { + return Vec3d( + m_x / op, + m_y / op, + m_z / op + ); +} + +std::ostream& operator<<(std::ostream &out, const Vec3d &v){ + out << "[ " << v.m_x << ", " << v.m_y << ", " << v.m_z << " ]"; + return out; +} diff --git a/src/core/vector.hpp b/src/core/vector.hpp new file mode 100644 index 0000000..20e8210 --- /dev/null +++ b/src/core/vector.hpp @@ -0,0 +1,33 @@ +#ifndef VECTOR_H +#define VECTOR_H + +#include + +class Vec3d { + public: + Vec3d(); + Vec3d(double x, double y, double z); + + void set(double x, double y, double z); + void normalize(); + + double length() const; + double dot(const Vec3d &vec) const; + + Vec3d cross(const Vec3d &vec) const; + + // Operators + Vec3d operator+(const Vec3d &vec) const; + Vec3d& operator+=(const Vec3d &vec); + Vec3d operator-(const Vec3d &vec) const; + Vec3d operator-() const; + Vec3d operator*(double) const; + Vec3d operator*(const Vec3d &vec) const; + Vec3d operator/(double) const; + + friend std::ostream& operator<<(std::ostream& os, const Vec3d &v); + + double m_x, m_y, m_z; +}; + +#endif diff --git a/src/object.cpp b/src/object.cpp index 15fc267..0f7332f 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -2,7 +2,7 @@ #include #include -#include "common.hpp" +#include "core/common.hpp" void Color::clamp() { if (m_x > 1) { m_x = 1; } diff --git a/src/object.hpp b/src/object.hpp index 3b108fb..1cfb254 100644 --- a/src/object.hpp +++ b/src/object.hpp @@ -2,8 +2,8 @@ #define OBJECT_H #include -#include "vector.hpp" -#include "ray.hpp" +#include "core/vector.hpp" +#include "core/ray.hpp" class Color : public Vec3d { public: diff --git a/src/ray.cpp b/src/ray.cpp deleted file mode 100644 index 7bc6201..0000000 --- a/src/ray.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "ray.hpp" - -Ray::Ray(Vec3d start, Vec3d direction, bool normalize) { - m_start = start; - m_direction = direction; - - if (normalize) { - m_direction.normalize(); - } -} - -Ray::Ray(Vec3d a, Vec3d b) { - m_start = a; - m_direction = b - a; - - m_direction.normalize(); -} diff --git a/src/ray.hpp b/src/ray.hpp deleted file mode 100644 index 6341d44..0000000 --- a/src/ray.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef RAY_H -#define RAY_H - -#include "vector.hpp" - -class Ray { - public: - Ray(Vec3d start, Vec3d direction, bool normalize); - Ray(Vec3d a, Vec3d b); - - Vec3d m_start; - Vec3d m_direction; -}; - -#endif diff --git a/src/render.cpp b/src/render.cpp index 3f2280d..a81c6bb 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -1,6 +1,6 @@ #include "render.hpp" -#include "vector.hpp" -#include "common.hpp" +#include "core/vector.hpp" +#include "core/common.hpp" #include #include diff --git a/src/render.hpp b/src/render.hpp index 7557fce..8102686 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -1,8 +1,8 @@ #ifndef RENDER_H #define RENDER_H -#include "vector.hpp" -#include "ray.hpp" +#include "core/vector.hpp" +#include "core/ray.hpp" #include "scene.hpp" class Random { diff --git a/src/vector.cpp b/src/vector.cpp deleted file mode 100644 index 51d8e2e..0000000 --- a/src/vector.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include "vector.hpp" - -#include -#include - -Vec3d::Vec3d() { - set(0, 0, 0); -} - -Vec3d::Vec3d(double x, double y, double z) { - set(x, y, z); -} - -void Vec3d::set(double x, double y, double z) { - m_x = x; - m_y = y; - m_z = z; -} - -void Vec3d::normalize() { - auto len = length(); - if (len == 0) { - throw std::runtime_error("Normalizing zero vector"); - } - - m_x /= len; - m_y /= len; - m_z /= len; -} - -double Vec3d::length() const { - return sqrt(m_x * m_x + m_y * m_y + m_z * m_z); -} - -double Vec3d::dot(const Vec3d &vec) const { - return m_x * vec.m_x + m_y * vec.m_y + m_z * vec.m_z; -} - -Vec3d Vec3d::cross(const Vec3d &vec) const { - return Vec3d( - m_y * vec.m_z - m_z * vec.m_y, - m_z * vec.m_x - m_x * vec.m_z, - m_x * vec.m_y - m_y * vec.m_x - ); -} - -Vec3d Vec3d::operator+(const Vec3d &vec) const { - return Vec3d( - m_x + vec.m_x, - m_y + vec.m_y, - m_z + vec.m_z - ); -} - -Vec3d& Vec3d::operator+=(const Vec3d &vec) { - m_x += vec.m_x; - m_y += vec.m_y; - m_z += vec.m_z; - return *this; -} - -Vec3d Vec3d::operator-(const Vec3d &vec) const { - return Vec3d( - m_x - vec.m_x, - m_y - vec.m_y, - m_z - vec.m_z - ); -} - -Vec3d Vec3d::operator-() const { - return Vec3d( - -m_x, - -m_y, - -m_z - ); -} - - -Vec3d Vec3d::operator*(double op) const { - return Vec3d( - m_x * op, - m_y * op, - m_z * op - ); -} - -Vec3d Vec3d::operator*(const Vec3d &vec) const { - return Vec3d( - m_x * vec.m_x, - m_y * vec.m_y, - m_z * vec.m_z - ); -} - -Vec3d Vec3d::operator/(double op) const { - return Vec3d( - m_x / op, - m_y / op, - m_z / op - ); -} - -std::ostream& operator<<(std::ostream &out, const Vec3d &v){ - out << "[ " << v.m_x << ", " << v.m_y << ", " << v.m_z << " ]"; - return out; -} diff --git a/src/vector.hpp b/src/vector.hpp deleted file mode 100644 index 20e8210..0000000 --- a/src/vector.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef VECTOR_H -#define VECTOR_H - -#include - -class Vec3d { - public: - Vec3d(); - Vec3d(double x, double y, double z); - - void set(double x, double y, double z); - void normalize(); - - double length() const; - double dot(const Vec3d &vec) const; - - Vec3d cross(const Vec3d &vec) const; - - // Operators - Vec3d operator+(const Vec3d &vec) const; - Vec3d& operator+=(const Vec3d &vec); - Vec3d operator-(const Vec3d &vec) const; - Vec3d operator-() const; - Vec3d operator*(double) const; - Vec3d operator*(const Vec3d &vec) const; - Vec3d operator/(double) const; - - friend std::ostream& operator<<(std::ostream& os, const Vec3d &v); - - double m_x, m_y, m_z; -}; - -#endif -- cgit v1.2.3