Files
raytracer_challenge/raytracing/src/shape.cpp
2024-02-27 17:49:05 +01:00

169 lines
4.5 KiB
C++

/*!
* shape.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: 08/02/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 "intersections.h"
#include "shape.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
#define kNothing 0
uint32_t Shape::s_current_index = 0;
/* ------------------------------------------------------------------------- */
Shape::Shape(void) : m_id(kNothing), m_transform(Matrix::identity())
{
}
/* ------------------------------------------------------------------------- */
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(a_copy.m_transform), m_material(a_copy.m_material)
{
}
/* ------------------------------------------------------------------------- */
const Shape &Shape::operator=(const Shape &a_shape)
{
if (this == &a_shape)
{
return *this;
}
m_id = a_shape.m_id;
m_transform = a_shape.m_transform;
m_material = a_shape.m_material;
return *this;
}
/* ------------------------------------------------------------------------- */
bool Shape::operator==(const Shape &a_shape) const
{
// (m_id == a_shape.m_id) && (
return (m_transform == a_shape.m_transform) && (m_material == a_shape.m_material);
}
/* ------------------------------------------------------------------------- */
Matrix &Shape::transform(void)
{
return m_transform;
}
/* ------------------------------------------------------------------------- */
const Matrix &Shape::transform(void) const
{
return m_transform;
}
/* ------------------------------------------------------------------------- */
void Shape::set_transform(const Matrix &a_transform_matrix)
{
m_transform = a_transform_matrix;
}
/* ------------------------------------------------------------------------- */
Material &Shape::material(void)
{
return m_material;
}
/* ------------------------------------------------------------------------- */
const Material &Shape::material(void) const
{
return m_material;
}
/* ------------------------------------------------------------------------- */
void Shape::set_material(const Material &a_material)
{
m_material = a_material;
}
/* ------------------------------------------------------------------------- */
Tuple Shape::normal_at(const Tuple &a_world_point) const
{
Tuple the_local_point = m_transform.inverse() * a_world_point;
Tuple the_local_normal = local_normal_at(the_local_point);
Tuple the_world_normal = m_transform.inverse().transpose() * the_local_normal;
the_world_normal.set_w(0);
return the_world_normal.normalize();
}
/* ------------------------------------------------------------------------- */
Tuple Shape::local_normal_at(const Tuple &a_local_point) const
{
return a_local_point;
}
/* ------------------------------------------------------------------------- */
Intersections Shape::intersect(const Ray &a_ray)
{
Ray the_ray = a_ray.transform(transform().inverse());
return local_intersect(the_ray);
}
/* ------------------------------------------------------------------------- */
Intersections Shape::local_intersect(const Ray &a_ray)
{
Intersections the_ret;
return the_ret;
}
/* ------------------------------------------------------------------------- */
void Shape::inc_id(void)
{
m_id = s_current_index++;
}