171 lines
4.2 KiB
C++
171 lines
4.2 KiB
C++
/*!
|
|
* 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;
|
|
}
|