[WIP] World in progress

This commit is contained in:
2024-02-20 23:10:49 +01:00
parent 6901cefbbc
commit 288f26358e
18 changed files with 452 additions and 38 deletions

View File

@@ -21,6 +21,7 @@ add_library(raytracing
src/ray.cpp src/ray.cpp
src/sphere.cpp src/sphere.cpp
src/tuple.cpp src/tuple.cpp
src/world.cpp
) )
target_include_directories(${PROJECT_NAME} target_include_directories(${PROJECT_NAME}

View File

@@ -36,3 +36,4 @@
#include "ray.h" #include "ray.h"
#include "sphere.h" #include "sphere.h"
#include "tuple.h" #include "tuple.h"
#include "world.h"

View File

@@ -28,6 +28,8 @@
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
#include <algorithm>
#include "intersections.h" #include "intersections.h"
using namespace Raytracer; using namespace Raytracer;
@@ -73,6 +75,26 @@ const Intersections &Intersections::operator=(const Intersections &an_other)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
const Intersections &Intersections::operator+=(const Intersections &an_other)
{
if (this == &an_other)
{
return *this;
}
m_data.reserve(m_data.size() + an_other.m_data.size());
for (auto the_intersection : an_other.m_data)
{
m_data.push_back(the_intersection);
}
std::sort(m_data.begin(), m_data.end());
return *this;
}
/* ------------------------------------------------------------------------- */
bool Intersections::add(const Intersection &an_intersection) bool Intersections::add(const Intersection &an_intersection)
{ {
m_data.push_back(an_intersection); m_data.push_back(an_intersection);

View File

@@ -48,6 +48,7 @@ namespace Raytracer
Intersection &operator[](uint8_t an_index); Intersection &operator[](uint8_t an_index);
const Intersections &operator=(const Intersections &an_other); const Intersections &operator=(const Intersections &an_other);
const Intersections &operator+=(const Intersections &an_other);
bool add(const Intersection &an_intersection); bool add(const Intersection &an_intersection);

View File

@@ -162,7 +162,7 @@ Color Material::lighting(const PointLight &a_light, const Tuple &a_point, const
else else
{ {
// Compute the specular contribution // Compute the specular contribution
double the_factor = pow(the_reflect_dot_eye, m_shininess); double the_factor = std::pow(the_reflect_dot_eye, m_shininess);
the_specular = a_light.intensity() * m_specular * the_factor; the_specular = a_light.intensity() * m_specular * the_factor;
} }
} }

View File

@@ -44,6 +44,34 @@ PointLight::PointLight(const Tuple &a_position, const Color &an_intensity) :
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
PointLight::PointLight(const PointLight &a_light) : m_position(a_light.m_position), m_intensity(a_light.m_intensity)
{
}
/* ------------------------------------------------------------------------- */
const PointLight &PointLight::operator=(const PointLight &a_light)
{
if (this == &a_light)
{
return *this;
}
m_position = a_light.m_position;
m_intensity = a_light.m_intensity;
return *this;
}
/* ------------------------------------------------------------------------- */
bool PointLight::operator==(const PointLight &a_light) const
{
return (m_position == a_light.m_position) && (m_intensity == a_light.m_intensity);
}
/* ------------------------------------------------------------------------- */
const Tuple &PointLight::position(void) const const Tuple &PointLight::position(void) const
{ {
return m_position; return m_position;

View File

@@ -38,7 +38,12 @@ namespace Raytracer
class PointLight class PointLight
{ {
public: public:
PointLight(void) = default;
PointLight(const Tuple &a_position, const Color &an_intensity); PointLight(const Tuple &a_position, const Color &an_intensity);
PointLight(const PointLight &a_light);
const PointLight &operator=(const PointLight &a_light);
bool operator==(const PointLight &a_light) const;
const Tuple &position(void) const; const Tuple &position(void) const;
const Color &intensity(void) const; const Color &intensity(void) const;

View File

@@ -69,7 +69,7 @@ Tuple Ray::position(double a_distance)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
Ray Ray::transform(const Matrix &a_matrix) Ray Ray::transform(const Matrix &a_matrix) const
{ {
Ray the_output_ray; Ray the_output_ray;

View File

@@ -47,7 +47,7 @@ namespace Raytracer
Tuple position(double a_distance); Tuple position(double a_distance);
Ray transform(const Matrix &a_matrix); Ray transform(const Matrix &a_matrix) const;
private: private:
Tuple m_origin; Tuple m_origin;

View File

@@ -48,13 +48,13 @@ Shape::Shape(void) : m_id(kNothing), m_transform(Matrix::identity())
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
Shape::Shape(Shape &a_copy) : m_id(a_copy.m_id), m_transform(Matrix::identity()), m_material(a_copy.m_material) Shape::Shape(Shape &a_copy) : m_id(a_copy.m_id), m_transform(a_copy.m_transform), m_material(a_copy.m_material)
{ {
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
Shape::Shape(const Shape &a_copy) : m_id(a_copy.m_id), m_transform(Matrix::identity()), m_material(a_copy.m_material) Shape::Shape(const Shape &a_copy) : m_id(a_copy.m_id), m_transform(a_copy.m_transform), m_material(a_copy.m_material)
{ {
} }
@@ -78,7 +78,8 @@ const Shape &Shape::operator=(const Shape &a_shape)
bool Shape::operator==(const Shape &a_shape) const bool Shape::operator==(const Shape &a_shape) const
{ {
return (m_id == a_shape.m_id) && (m_transform == a_shape.m_transform) && (m_material == a_shape.m_material); // (m_id == a_shape.m_id) && (
return (m_transform == a_shape.m_transform) && (m_material == a_shape.m_material);
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
@@ -90,6 +91,13 @@ Matrix &Shape::transform(void)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
const Matrix &Shape::transform(void) const
{
return m_transform;
}
/* ------------------------------------------------------------------------- */
void Shape::set_transform(const Matrix &a_transform_matrix) void Shape::set_transform(const Matrix &a_transform_matrix)
{ {
m_transform = a_transform_matrix; m_transform = a_transform_matrix;
@@ -129,7 +137,7 @@ Tuple Shape::normal_at(const Tuple &a_world_point) const
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
Intersections Shape::intersect(Ray &a_ray) Intersections Shape::intersect(const Ray &a_ray) const
{ {
Intersections the_ret; Intersections the_ret;

View File

@@ -53,6 +53,7 @@ namespace Raytracer
bool operator==(const Shape &a_shape) const; bool operator==(const Shape &a_shape) const;
Matrix &transform(void); Matrix &transform(void);
const Matrix &transform(void) const;
void set_transform(const Matrix &a_transform_matrix); void set_transform(const Matrix &a_transform_matrix);
Material &material(void); Material &material(void);
@@ -61,7 +62,7 @@ namespace Raytracer
Tuple normal_at(const Tuple &a_point) const; Tuple normal_at(const Tuple &a_point) const;
virtual Intersections intersect(Ray &a_ray); virtual Intersections intersect(const Ray &a_ray) const;
protected: protected:
void inc_id(void); void inc_id(void);

View File

@@ -46,7 +46,7 @@ Sphere::Sphere(void)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
Intersections Sphere::intersect(Ray &a_ray) Intersections Sphere::intersect(const Ray &a_ray) const
{ {
Intersections the_intersections; Intersections the_intersections;
Ray the_ray = a_ray.transform(transform().inverse()); Ray the_ray = a_ray.transform(transform().inverse());

View File

@@ -38,9 +38,7 @@ namespace Raytracer
{ {
public: public:
Sphere(void); Sphere(void);
Intersections intersect(Ray &a_ray) override; Intersections intersect(const Ray &a_ray) const override;
private:
}; };
}; // namespace Raytracer }; // namespace Raytracer

170
raytracing/src/world.cpp Normal file
View File

@@ -0,0 +1,170 @@
/*!
* world.cpp
*
* Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 29/01/2024
*
*/
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
/* ------------------------------------------------------------------------- */
#include "matrix.h"
#include "sphere.h"
#include "world.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
World::World(void) : m_has_light(false)
{
}
/* ------------------------------------------------------------------------- */
World::World(World &an_other)
{
m_objects.reserve(an_other.m_objects.size());
for (auto &the_object : an_other.m_objects)
{
m_objects.push_back(the_object);
}
m_has_light = an_other.m_has_light;
if (m_has_light == true)
{
m_light = an_other.m_light;
}
}
/* ------------------------------------------------------------------------- */
const World &World::operator=(const World &an_other)
{
if (this == &an_other)
{
return *this;
}
m_objects.reserve(an_other.m_objects.size());
m_objects.assign(an_other.m_objects.begin(), an_other.m_objects.end());
m_has_light = an_other.m_has_light;
if (m_has_light == true)
{
m_light = an_other.m_light;
}
return *this;
}
/* ------------------------------------------------------------------------- */
uint32_t World::objects_count(void)
{
return m_objects.size();
}
/* ------------------------------------------------------------------------- */
bool World::has_light(void)
{
return m_has_light;
}
/* ------------------------------------------------------------------------- */
const PointLight &World::light(void) const
{
return m_light;
}
/* ------------------------------------------------------------------------- */
void World::set_light(const PointLight &a_light)
{
m_light = a_light;
m_has_light = true;
}
/* ------------------------------------------------------------------------- */
void World::add_object(Shape *a_shape)
{
m_objects.push_back(a_shape);
}
/* ------------------------------------------------------------------------- */
bool World::contains(const Shape &a_shape)
{
for (const auto &the_shape : m_objects)
{
if (*the_shape == a_shape)
{
return true;
}
}
return false;
}
/* ------------------------------------------------------------------------- */
Intersections World::intersect_world(const Ray &a_ray)
{
Intersections the_all_intersec;
for (auto the_object : m_objects)
{
Intersections the_intersection = the_object->intersect(a_ray);
the_all_intersec += the_intersection;
}
return the_all_intersec;
}
/* ------------------------------------------------------------------------- */
World World::default_world(void)
{
World the_world;
Sphere *s1, *s2;
the_world.set_light(PointLight(Tuple::Point(-10, 10, 10), Color::White()));
s1 = new Sphere;
Material &the_s1_mat = s1->material();
the_s1_mat.set_color(Color(0.8, 1.0, 0.6));
the_s1_mat.set_diffuse(0.7);
the_s1_mat.set_specular(0.2);
the_world.add_object(s1);
s2 = new Sphere;
s2->set_transform(Matrix::scaling(0.5, 0.5, 0.5));
the_world.add_object(s2);
return the_world;
}

72
raytracing/src/world.h Normal file
View File

@@ -0,0 +1,72 @@
/*!
* world.h
*
* Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 20/02/2024
*
*/
#ifndef _RAYTRACER_WORLD_H
#define _RAYTRACER_WORLD_H
/* ------------------------------------------------------------------------- */
#include <cstdint>
#include <vector>
#include "intersections.h"
#include "point-light.h"
#include "ray.h"
#include "shape.h"
/* ------------------------------------------------------------------------- */
namespace Raytracer
{
class World
{
public:
World(void);
World(World &an_other);
const World &operator=(const World &an_other);
uint32_t objects_count(void);
bool has_light(void);
const PointLight &light(void) const;
void set_light(const PointLight &a_light);
void add_object(Shape *a_shape);
bool contains(const Shape &a_shape);
Intersections intersect_world(const Ray &a_ray);
static World default_world(void);
private:
bool m_has_light;
PointLight m_light;
std::vector<Shape *> m_objects;
};
}; // namespace Raytracer
#endif /* _RAYTRACER_WORLD_H */

View File

@@ -227,32 +227,6 @@ SCENARIO("A sphere is behind a ray", "[features/spheres.feature]")
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
SCENARIO("Test Sphere Object", "[features/spheres.feature]")
{
GIVEN("s1 <- sphere()")
{
Sphere s1;
AND_GIVEN("s2 <- s1")
{
Sphere s2 = s1;
AND_GIVEN("s3 <- sphere()")
{
Sphere s3;
THEN("s1 = s2")
{
REQUIRE(s1 == s2);
}
AND_THEN("s1 != s3")
{
REQUIRE(s1 != s3);
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("An intersection encapsulates t and object", "[features/intersections.feature]") SCENARIO("An intersection encapsulates t and object", "[features/intersections.feature]")
{ {
GIVEN("s <- sphere()") GIVEN("s <- sphere()")

132
tests/07_making_scene.cpp Normal file
View File

@@ -0,0 +1,132 @@
/*!
* 07_making_scene.cpp
*
* Copyright (c) 2015-2024, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 14/02/2024
*
*/
/*---------------------------------------------------------------------------*/
#include <catch.hpp>
#include "raytracing.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
SCENARIO("Creating a world", "[features/world.feature]")
{
GIVEN("w <- world()")
{
World w;
THEN("w contains no objects")
{
REQUIRE(w.objects_count() == 0);
}
AND_THEN("w has no light source")
{
REQUIRE(w.has_light() == false);
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("The default world", "[features/world.feature]")
{
GIVEN("light <- point_light(point(-10, 10, 10), color(1, 1, 1))")
{
PointLight light = PointLight(Tuple::Point(-10, 10, 10), Color(1, 1, 1));
AND_GIVEN("s1 <- sphere")
// with:
// | material.color | (0.8, 1.0, 0.6) |
// | material.diffuse | 0.7 |
// | material.specular | 0.2 |
{
Sphere s1;
s1.material().set_color(Color(0.8, 1.0, 0.6));
s1.material().set_diffuse(0.7);
s1.material().set_specular(0.2);
AND_GIVEN("s2 <- sphere")
// with:
// | transform | scaling(0.5, 0.5, 0.5) |
{
Sphere s2;
s2.set_transform(Matrix::scaling(0.5, 0.5, 0.5));
WHEN("w <- default_world")
{
World w = World::default_world();
THEN("w.light = light")
{
REQUIRE(w.light() == light);
}
AND_THEN("w contains s1")
{
REQUIRE(w.contains(s1) == true);
}
AND_THEN("w contains s2")
{
REQUIRE(w.contains(s2) == true);
}
}
}
}
}
}
/* ------------------------------------------------------------------------- */
SCENARIO("Intersect a world with a ray", "[features/world.feature]")
{
GIVEN("w <- default_world()")
{
World w = World::default_world();
AND_GIVEN("r <- ray(point(0, 0, -5), vector(0, 0, 1))")
{
Ray r(Tuple::Point(0, 0, -5), Tuple::Vector(0, 0, 1));
WHEN("xs <- intersect_world(w,r)")
{
Intersections xs = w.intersect_world(r);
THEN("xs.count = 4")
{
REQUIRE(xs.count() == 4);
}
AND_THEN("xs[0].t = 4")
{
REQUIRE(xs[0].distance_t() == 4);
}
AND_THEN("xs[1].t = 4.5")
{
REQUIRE(xs[1].distance_t() == 4.5);
}
AND_THEN("xs[2].t = 5.5")
{
REQUIRE(xs[2].distance_t() == 5.5);
}
AND_THEN("xs[3].t = 6")
{
REQUIRE(xs[3].distance_t() == 6);
}
}
}
}
}

View File

@@ -17,6 +17,7 @@ add_executable(raytracing_test
04_transformations.cpp 04_transformations.cpp
05_rays.cpp 05_rays.cpp
06_light_shading.cpp 06_light_shading.cpp
07_making_scene.cpp
) )
include_directories("${CMAKE_SOURCE_DIR}/tests") include_directories("${CMAKE_SOURCE_DIR}/tests")