[FEAT] ADD lighting method
This commit is contained in:
@@ -45,15 +45,13 @@ Color::Color(void) : m_red(0), m_green(0), m_blue(0)
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
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(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)
|
||||
Color::Color(double a_red, double a_green, double a_blue) : m_red(a_red), m_green(a_green), m_blue(a_blue)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -217,3 +215,38 @@ uint8_t Color::color_to_integer(double a_color) const
|
||||
|
||||
return the_value;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Color Color::Black(void)
|
||||
{
|
||||
return Color(0, 0, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Color Color::White(void)
|
||||
{
|
||||
return Color(1, 1, 1);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Color Color::Red(void)
|
||||
{
|
||||
return Color(1, 0, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Color Color::Green(void)
|
||||
{
|
||||
return Color(0, 1, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Color blue(void)
|
||||
{
|
||||
return Color(0, 0, 1);
|
||||
}
|
||||
|
||||
@@ -63,6 +63,12 @@ namespace Raytracer
|
||||
uint8_t green_to_integer(void) const;
|
||||
uint8_t blue_to_integer(void) const;
|
||||
|
||||
static Color Black(void);
|
||||
static Color White(void);
|
||||
static Color Red(void);
|
||||
static Color Green(void);
|
||||
static Color Blue(void);
|
||||
|
||||
private:
|
||||
uint8_t color_to_integer(double a_color) const;
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "material.h"
|
||||
@@ -111,3 +113,52 @@ void Material::set_shininess(double a_value)
|
||||
{
|
||||
m_shininess = a_value;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Color Material::lighting(const PointLight &a_light, const Tuple &a_point, const Tuple &an_eyev, const Tuple &a_normalv)
|
||||
{
|
||||
Color the_effective_color;
|
||||
Tuple the_light_v, the_reflect_v;
|
||||
Color the_ambient, the_diffuse, the_specular;
|
||||
|
||||
// Combine the surface color with the light's color
|
||||
the_effective_color = m_color * a_light.intensity();
|
||||
|
||||
// Find the direction to the light source
|
||||
the_light_v = (a_light.position() - a_point).normalize();
|
||||
|
||||
// Compute the ambient contribution
|
||||
the_ambient = the_effective_color * m_ambient;
|
||||
|
||||
// light_dot_normal represents the cosine of the angle between the light vector and the normal vector.
|
||||
// a negative number means the light is on the other side of the surface.
|
||||
double the_light_dot_normal = the_light_v.dot(a_normalv);
|
||||
|
||||
if (the_light_dot_normal < 0)
|
||||
{
|
||||
the_diffuse = Color::Black();
|
||||
the_specular = Color::Black();
|
||||
}
|
||||
else
|
||||
{
|
||||
the_diffuse = the_effective_color * m_diffuse * the_light_dot_normal;
|
||||
|
||||
// reflect_dot_eye represent the consine of the angle between reflection vector and the eye vector.
|
||||
// A negative number means the light reflects away from the eye.
|
||||
the_reflect_v = (-the_light_v).reflect(a_normalv);
|
||||
double the_reflect_dot_eye = the_reflect_v.dot(an_eyev);
|
||||
if (the_reflect_dot_eye <= 0)
|
||||
{
|
||||
the_specular = Color::Black();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Compute the specular contribution
|
||||
double the_factor = pow(the_reflect_dot_eye, m_shininess);
|
||||
the_specular = a_light.intensity() * m_specular * the_factor;
|
||||
}
|
||||
}
|
||||
|
||||
return the_ambient + the_diffuse + the_specular;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
#include "color.h"
|
||||
#include "point-light.h"
|
||||
#include "tuple.h"
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
@@ -55,6 +57,8 @@ namespace Raytracer
|
||||
const double &shininess(void) const;
|
||||
void set_shininess(double a_value);
|
||||
|
||||
Color lighting(const PointLight &a_light, const Tuple &a_point, const Tuple &an_eyev, const Tuple &a_normalv);
|
||||
|
||||
private:
|
||||
Color m_color;
|
||||
double m_ambient;
|
||||
|
||||
@@ -286,14 +286,14 @@ void Tuple::set_w(double a_w)
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
bool Tuple::is_point(void)
|
||||
bool Tuple::is_point(void) const
|
||||
{
|
||||
return double_equal(m_w, kRaytracerTuplePoint);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
bool Tuple::is_vector(void)
|
||||
bool Tuple::is_vector(void) const
|
||||
{
|
||||
return double_equal(m_w, kRaytracerTupleVector);
|
||||
}
|
||||
@@ -307,7 +307,7 @@ double Tuple::magnitude(void) const
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Tuple Tuple::normalize(void)
|
||||
Tuple Tuple::normalize(void) const
|
||||
{
|
||||
double the_magnitude = magnitude();
|
||||
|
||||
@@ -316,14 +316,14 @@ Tuple Tuple::normalize(void)
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
double Tuple::dot(const Tuple &a_tuple)
|
||||
double Tuple::dot(const Tuple &a_tuple) const
|
||||
{
|
||||
return m_x * a_tuple.m_x + m_y * a_tuple.m_y + m_z * a_tuple.m_z + m_w * a_tuple.m_w;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Tuple Tuple::cross(const Tuple &a_tuple)
|
||||
Tuple Tuple::cross(const Tuple &a_tuple) const
|
||||
{
|
||||
return Vector(m_y * a_tuple.m_z - m_z * a_tuple.m_y, m_z * a_tuple.m_x - m_x * a_tuple.m_z,
|
||||
m_x * a_tuple.m_y - m_y * a_tuple.m_x);
|
||||
@@ -331,7 +331,7 @@ Tuple Tuple::cross(const Tuple &a_tuple)
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
Tuple Tuple::reflect(const Tuple &a_normal)
|
||||
Tuple Tuple::reflect(const Tuple &a_normal) const
|
||||
{
|
||||
return *this - a_normal * 2 * dot(a_normal);
|
||||
}
|
||||
|
||||
@@ -78,16 +78,16 @@ namespace Raytracer
|
||||
void set_z(double a_z);
|
||||
void set_w(double a_w);
|
||||
|
||||
bool is_point(void);
|
||||
bool is_vector(void);
|
||||
bool is_point(void) const;
|
||||
bool is_vector(void) const;
|
||||
|
||||
double magnitude(void) const;
|
||||
Tuple normalize(void);
|
||||
Tuple normalize(void) const;
|
||||
|
||||
double dot(const Tuple &a_tuple);
|
||||
Tuple cross(const Tuple &a_tuple);
|
||||
double dot(const Tuple &a_tuple) const;
|
||||
Tuple cross(const Tuple &a_tuple) const;
|
||||
|
||||
Tuple reflect(const Tuple &a_normal);
|
||||
Tuple reflect(const Tuple &a_normal) const;
|
||||
|
||||
private:
|
||||
void set_at_index(uint8_t an_index, double a_value);
|
||||
|
||||
@@ -312,3 +312,148 @@ SCENARIO("A sphere may be assigned a material", "[features/spheres.feature]")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
SCENARIO("Lighting with the eye between the light and the surface", "[features/materials.feature]")
|
||||
{
|
||||
Tuple position = Tuple::Point(0, 0, 0);
|
||||
Material m;
|
||||
|
||||
GIVEN("eyev <- vector(0, 0, -1)")
|
||||
{
|
||||
Tuple eyev = Tuple::Vector(0, 0, -1);
|
||||
AND_GIVEN("normalv <- vector(0, 0, -1)")
|
||||
{
|
||||
Tuple normalv = Tuple::Vector(0, 0, -1);
|
||||
AND_GIVEN("light <- point_light(point(0, 0, -10),color(1, 1, 1))")
|
||||
{
|
||||
PointLight light = PointLight(Tuple::Point(0, 0, -10), Color(1, 1, 1));
|
||||
WHEN("result <- lighting(m, light, position, eyev, normalv)")
|
||||
{
|
||||
Color result = m.lighting(light, position, eyev, normalv);
|
||||
THEN("result = color(1.9, 1.9, 1.9)")
|
||||
{
|
||||
REQUIRE(result == Color(1.9, 1.9, 1.9));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
SCENARIO("Lighting with eye between the light & surface, eye offset 45°", "[features/materials.feature]")
|
||||
{
|
||||
Tuple position = Tuple::Point(0, 0, 0);
|
||||
Material m;
|
||||
|
||||
GIVEN("eyev <- vector(0, sqrt(2)/2, -sqrt(2)/2)")
|
||||
{
|
||||
Tuple eyev = Tuple::Vector(0, sqrt(2) / 2, -sqrt(2) / 2);
|
||||
AND_GIVEN("normalv <- vector(0, 0, -1)")
|
||||
{
|
||||
Tuple normalv = Tuple::Vector(0, 0, -1);
|
||||
AND_GIVEN("light <- point_light(point(0, 0, -10),color(1, 1, 1))")
|
||||
{
|
||||
PointLight light = PointLight(Tuple::Point(0, 0, -10), Color(1, 1, 1));
|
||||
WHEN("result <- lighting(m, light, position, eyev, normalv)")
|
||||
{
|
||||
Color result = m.lighting(light, position, eyev, normalv);
|
||||
THEN("result = color(1.0, 1.0, 1.0)")
|
||||
{
|
||||
REQUIRE(result == Color(1.0, 1.0, 1.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
SCENARIO("Lighting with the eye opposite surface, light offset 45°", "[features/materials.feature]")
|
||||
{
|
||||
Tuple position = Tuple::Point(0, 0, 0);
|
||||
Material m;
|
||||
|
||||
GIVEN("eyev <- vector(0, 0, -1")
|
||||
{
|
||||
Tuple eyev = Tuple::Vector(0, 0, -1);
|
||||
AND_GIVEN("normalv <- vector(0, 0, -1)")
|
||||
{
|
||||
Tuple normalv = Tuple::Vector(0, 0, -1);
|
||||
AND_GIVEN("light <- point_light(point(0, 10, -10),color(1, 1, 1))")
|
||||
{
|
||||
PointLight light = PointLight(Tuple::Point(0, 10, -10), Color(1, 1, 1));
|
||||
WHEN("result <- lighting(m, light, position, eyev, normalv)")
|
||||
{
|
||||
Color result = m.lighting(light, position, eyev, normalv);
|
||||
THEN("result = color(0.7364, 0.7364, 0.7364)")
|
||||
{
|
||||
REQUIRE(result == Color(0.7364, 0.7364, 0.7364));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
SCENARIO("Lighting with the eye in the path of the reflection vector", "[features/materials.feature]")
|
||||
{
|
||||
Tuple position = Tuple::Point(0, 0, 0);
|
||||
Material m;
|
||||
|
||||
GIVEN("eyev <- vector(0, -sqrt(2)/2, -sqrt(2)/2")
|
||||
{
|
||||
Tuple eyev = Tuple::Vector(0, -sqrt(2) / 2, -sqrt(2) / 2);
|
||||
AND_GIVEN("normalv <- vector(0, 0, -1)")
|
||||
{
|
||||
Tuple normalv = Tuple::Vector(0, 0, -1);
|
||||
AND_GIVEN("light <- point_light(point(0, 10, -10),color(1, 1, 1))")
|
||||
{
|
||||
PointLight light = PointLight(Tuple::Point(0, 10, -10), Color(1, 1, 1));
|
||||
WHEN("result <- lighting(m, light, position, eyev, normalv)")
|
||||
{
|
||||
Color result = m.lighting(light, position, eyev, normalv);
|
||||
THEN("result = color(1.6364, 1.6364, 1.6364)")
|
||||
{
|
||||
REQUIRE(result == Color(1.6364, 1.6364, 1.6364));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
|
||||
SCENARIO("Lighting with the light behind the surface", "[features/materials.feature]")
|
||||
{
|
||||
Tuple position = Tuple::Point(0, 0, 0);
|
||||
Material m;
|
||||
|
||||
GIVEN("eyev <- vector(0, 0, -1")
|
||||
{
|
||||
Tuple eyev = Tuple::Vector(0, 0, -1);
|
||||
AND_GIVEN("normalv <- vector(0, 0, -1)")
|
||||
{
|
||||
Tuple normalv = Tuple::Vector(0, 0, -1);
|
||||
AND_GIVEN("light <- point_light(point(0, 0, 10),color(1, 1, 1))")
|
||||
{
|
||||
PointLight light = PointLight(Tuple::Point(0, 0, 10), Color(1, 1, 1));
|
||||
WHEN("result <- lighting(m, light, position, eyev, normalv)")
|
||||
{
|
||||
Color result = m.lighting(light, position, eyev, normalv);
|
||||
THEN("result = color(0.1, 0.1, 0.1)")
|
||||
{
|
||||
REQUIRE(result == Color(0.1, 0.1, 0.1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user