[WIP] starting the implementation of color

This commit is contained in:
NADAL Jean-Baptiste
2024-01-30 18:43:50 +01:00
parent 3d3b8b7f02
commit c3534cd7a1
12 changed files with 452 additions and 21 deletions

73
.vscode/settings.json vendored
View File

@@ -3,5 +3,76 @@
"cSpell.words": [
"NADAL",
"Raytracer"
]
],
"files.associations": {
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
}
}

View File

@@ -5,8 +5,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(raytracing)
add_library(raytracing
add_library(raytracing
src/common.cpp
src/tuple.cpp
src/color.cpp
)
target_include_directories(${PROJECT_NAME}

View File

@@ -25,4 +25,6 @@
#pragma once
#include "common.h"
#include "tuple.h"
#include "color.h"

132
raytracing/src/color.cpp Normal file
View File

@@ -0,0 +1,132 @@
/*!
* color.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: 30/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 "common.h"
#include "color.h"
using namespace Raytracer;
#include <cstdio>
/* ------------------------------------------------------------------------- */
Color::Color(void) : m_red(0), m_green(0), m_blue(0)
{
//printf("%s red: %f green: %f, blue: %f\n", __PRETTY_FUNCTION__, m_red, m_green, m_blue);
}
/* ------------------------------------------------------------------------- */
Color::Color(const Color &a_copy) : m_red(a_copy.m_red), m_green(a_copy.m_green), m_blue(a_copy.m_blue)
{
}
/* ------------------------------------------------------------------------- */
Color::Color(double a_red, double a_green, double a_blue) : m_red(a_red), m_green(a_green), m_blue(a_blue)
{
}
/* ------------------------------------------------------------------------- */
bool Color::operator==(const Color &a_color) const
{
if (double_equal(m_red, a_color.m_red) && double_equal(m_green, a_color.m_green) && double_equal(m_blue, a_color.m_blue))
{
return true;
}
return false;
}
/* ------------------------------------------------------------------------- */
const Color &Color::operator=(const Color &a_color)
{
m_red = a_color.m_red;
m_green = a_color.m_green;
m_blue = a_color.m_blue;
return *this;
}
/* ------------------------------------------------------------------------- */
const Color &Color::operator+(const Color &a_color)
{
m_red += a_color.m_red;
m_green += a_color.m_green;
m_blue += a_color.m_blue;
return *this;
}
/* ------------------------------------------------------------------------- */
const Color &Color::operator-(const Color &a_color)
{
m_red -= a_color.m_red;
m_green -= a_color.m_green;
m_blue -= a_color.m_blue;
return *this;
}
/* ------------------------------------------------------------------------- */
const Color &Color::operator*(double a_scalar)
{
m_red *= a_scalar;
m_green *= a_scalar;
m_blue *= a_scalar;
return *this;
}
/* ------------------------------------------------------------------------- */
double Color::red(void) const
{
return m_red;
}
/* ------------------------------------------------------------------------- */
double Color::green(void) const
{
return m_green;
}
/* ------------------------------------------------------------------------- */
double Color::blue(void) const
{
return m_blue;
}

56
raytracing/src/color.h Normal file
View File

@@ -0,0 +1,56 @@
/*!
* color.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: 30/01/2024
*
*/
#ifndef _RAYTRACER_COLOR_H
#define _RAYTRACER_COLOR_H
namespace Raytracer
{
class Color
{
public:
Color(void);
Color(const Color &a_copy);
Color(double a_red, double a_green, double a_blue);
bool operator==(const Color &a_color) const;
const Color &operator=(const Color &an_other);
const Color &operator+(const Color &a_color);
const Color &operator-(const Color &a_color);
const Color &operator*(double a_scalar);
// Access
double red(void) const;
double green(void) const;
double blue(void) const;
private:
double m_red;
double m_green;
double m_blue;
};
};
#endif

47
raytracing/src/common.cpp Normal file
View File

@@ -0,0 +1,47 @@
/*!
* common.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: 30/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 <cmath>
#include "common.h"
#define kEpsilon 0.00001
/* ------------------------------------------------------------------------- */
bool Raytracer::double_equal(double a, double b)
{
if ((std::abs((a) - (b)) < kEpsilon))
{
return true;
}
return false;
}

34
raytracing/src/common.h Normal file
View File

@@ -0,0 +1,34 @@
/*!
* common.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: 30/01/2024
*
*/
#ifndef _RAYTRACER_COMMON_H
#define _RAYTRACER_COMMON_H
namespace Raytracer
{
bool double_equal(double a, double b);
};
#endif /* _RAYTRACER_COMMON_H */

View File

@@ -31,9 +31,9 @@
#include <cmath>
#include <cstdio>
#include "common.h"
#include "tuple.h"
#define kEpsilon 0.00001
#define square(a) (a) * (a)
using namespace Raytracer;
@@ -236,15 +236,3 @@ Tuple Tuple::cross(const Tuple &a_tuple)
m_x * a_tuple.m_y - m_y * a_tuple.m_x
);
}
/* ------------------------------------------------------------------------- */
bool Tuple::double_equal(double a, double b) const
{
if ((std::abs((a) - (b)) < kEpsilon))
{
return true;
}
return false;
}

View File

@@ -69,14 +69,11 @@ namespace Raytracer
Tuple cross(const Tuple &a_tuple);
private:
bool double_equal(double a, double b) const;
double m_x;
double m_y;
double m_z;
double m_w;
};
};
#endif /* _RAYTRACER_TUPLE_H */

View File

@@ -1,5 +1,5 @@
/*!
* chapitre-01_tests.cpp
* 01_tuples.cpp
*
* Copyright (c) 2015-2024, NADAL Jean-Baptiste. All rights reserved.
*
@@ -19,7 +19,7 @@
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 07/02/2021
* @Date: 29/01/2024
*
*/

100
tests/02_colors.cpp Normal file
View File

@@ -0,0 +1,100 @@
/*!
* 01_colors.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: 30/01/2024
*
*/
/*---------------------------------------------------------------------------*/
#include <catch.hpp>
#include "color.h"
using namespace Raytracer;
/* ------------------------------------------------------------------------- */
TEST_CASE("colors are (red,green,blue) tuples", "[Colors]")
{
Color c(-0.5, 0.4, 1.7);
REQUIRE(c.red() == -0.5);
REQUIRE(c.green() == 0.4);
REQUIRE(c.blue() == 1.7);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Colors could be copied", "[Colors]")
{
Color c1(-0.5, 0.4, 1.7);
Color c2;
c2 = c1;
REQUIRE(c2.red() == -0.5);
REQUIRE(c2.green() == 0.4);
REQUIRE(c2.blue() == 1.7);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Adding colors", "[Colors]")
{
Color c1(0.9, 0.6, 0.75);
Color c2(0.7, 0.1, 0.25);
//Color c3 = c1 + c2;
REQUIRE((c1 + c2) == Color(1.6, 0.7, 1.0));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Adding colors without modify c1", "[Colors]")
{
Color c1(0.9, 0.6, 0.75);
Color c2(0.7, 0.1, 0.25);
Color c3 = c1 + c2;
REQUIRE((c1 + c2) == Color(1.6, 0.7, 1.0));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Subtracting colors", "[Colors]")
{
Color c1(0.9, 0.6, 0.75);
Color c2(0.7, 0.1, 0.25);
REQUIRE((c1 - c2) == Color(0.2, 0.5, 0.5));
}
/* ------------------------------------------------------------------------- */
TEST_CASE("Multiplying a color by a scalar", "[Colors]")
{
Color c(0.2, 0.3, 0.4);
REQUIRE(c * 2 == Color(0.4, 0.6, 0.8));
}

View File

@@ -12,7 +12,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(main_test
main_test.cpp
chapitre01_tuples.cpp
01_tuples.cpp
02_colors.cpp
)
include_directories("${CMAKE_SOURCE_DIR}/tests")