Starting working on area lights.

This commit is contained in:
Godzil
2020-02-28 18:35:45 +00:00
parent 53f66b554b
commit c4b680789e
12 changed files with 509 additions and 32 deletions

View File

@@ -90,6 +90,16 @@ target_include_directories(ch14_test PUBLIC ../source/include)
target_sources(ch14_test PRIVATE ch14_test.cpp)
target_link_libraries(ch14_test rayonnement)
add_executable(arealight_test)
target_include_directories(arealight_test PUBLIC ../source/include)
target_sources(arealight_test PRIVATE arealight_test.cpp)
target_link_libraries(arealight_test rayonnement)
add_executable(triangle_rendertest)
target_include_directories(triangle_rendertest PUBLIC ../source/include)
target_sources(triangle_rendertest PRIVATE triangle_rendertest.cpp)
target_link_libraries(triangle_rendertest rayonnement)
add_executable(christmasball_render)
target_include_directories(christmasball_render PUBLIC ../source/include)
target_sources(christmasball_render PRIVATE christmasball_render.cpp)
@@ -107,7 +117,9 @@ add_test(NAME Chapter12_Test COMMAND $<TARGET_FILE:ch12_test>)
add_test(NAME Chapter13_Test COMMAND $<TARGET_FILE:ch13_test>)
add_test(NAME Chapter13_ConeBonus COMMAND $<TARGET_FILE:ch13_cone>)
add_test(NAME Chapter14_Test COMMAND $<TARGET_FILE:ch14_test>)
add_test(NAME AreaLight_Test COMMAND $<TARGET_FILE:arealight_test>)
add_test(NAME Test_Rendering COMMAND $<TARGET_FILE:test_render>)
add_test(NAME Triangle_RenderTest COMMAND $<TARGET_FILE:triangle_rendertest>)
add_test(NAME ChristmasBall_Rendering COMMAND $<TARGET_FILE:christmasball_render>)
add_test(NAME Hw3Render COMMAND $<TARGET_FILE:hw3render> ${CMAKE_CURRENT_SOURCE_DIR}/test.hw3scene)
add_test(NAME Hw3RenderAllCmds COMMAND $<TARGET_FILE:hw3render> ${CMAKE_CURRENT_SOURCE_DIR}/test_keys.hw3scene)

99
tests/arealight_test.cpp Normal file
View File

@@ -0,0 +1,99 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Render test for reflection in chapter 13.
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <world.h>
#include <light.h>
#include <sphere.h>
#include <plane.h>
#include <cube.h>
#include <cylinder.h>
#include <material.h>
#include <colour.h>
#include <canvas.h>
#include <camera.h>
#include <group.h>
#include <cone.h>
#include <pattern.h>
#include <strippattern.h>
#include <gradientpattern.h>
#include <checkerspattern.h>
#include <ringpattern.h>
#include <transformation.h>
int main()
{
World w = World();
/* Add lights */
#if 0
Light light1 = Light(AREA_LIGHT, Point(-1, 2, 4),
Vector(2, 0, 0), 8,
Vector(0, 2, 0), 8,
//jitter,
Colour(1.5, 1.5, 1.5));
#else
Light light1 = Light(POINT_LIGHT, Point(-1, 2, 4), Colour(1.5,1.5,1.5));
#endif
w.addLight(&light1);
/* ----------------------------- */
/* Cube that pretend to be the light */
Cube c = Cube();
c.material.colour = Colour(1.5, 1.5, 1.5);
c.material.ambient = 1;
c.material.diffuse = 0;
c.material.specular = 0;
c.dropShadow = false;
c.setTransform(translation(0, 3, 4) * scaling(1, 1, 0.01));
w.addObject(&c);
Plane p = Plane();
p.material.colour = Colour(1, 1, 1);
p.material.ambient = 0.025;
p.material.diffuse = 0.67;
p.material.specular = 0;
w.addObject(&p);
Sphere s1 = Sphere();
s1.setTransform(translation(0.5, 0.5,0) * scaling(0.5, 0.5, 0.5));
s1.material.colour = Colour(1, 0, 0);
s1.material.ambient = 0.1;
s1.material.specular = 0;
s1.material.diffuse = 0.6;
s1.material.reflective = 0.3;
w.addObject(&s1);
Sphere s2 = Sphere();
s2.setTransform(translation(-0.25, 0.33,0) * scaling(0.33, 0.33, 0.33));
s2.material.colour = Colour(0.5, 0.5, 1);
s2.material.ambient = 0.1;
s2.material.specular = 0;
s2.material.diffuse = 0.6;
s2.material.reflective = 0.3;
w.addObject(&s2);
/* ----------------------------- */
/* Set the camera */
//Camera camera = Camera(400, 160, 0.7854);
Camera camera = Camera(800, 320, 0.7854);
camera.setTransform(viewTransform(Point(-3, 1, 2.5),
Point(0, 0.5, 0),
Vector(0, 1, 0)));
/* Now render it */
Canvas image = camera.render(w, 5);
image.SaveAsPNG("arealight_test.png");
return 0;
}

View File

@@ -11,6 +11,8 @@
#include <colour.h>
#include <tuple.h>
#include <gtest/gtest.h>
#include <world.h>
#include <worldbuilder.h>
TEST(LightTest, A_point_lighthas_a_position_and_intensity)
{
@@ -21,4 +23,127 @@ TEST(LightTest, A_point_lighthas_a_position_and_intensity)
ASSERT_EQ(light.position, position);
ASSERT_EQ(light.intensity, intensity);
}
TEST(LightTest, Point_lights_evaluate_the_lights_intensity_at_a_given_point)
{
World w = DefaultWorld();
Light *light = w.getLight(0);
Tuple testList[] = {
Point(0, 1.0001, 0),
Point(-1.0001, 0, 0),
Point(0, 0, -1.0001),
Point(0, 0, 1.0001),
Point(1.0001, 0, 0),
Point(0, -1.0001, 0),
Point(0, 0, 0),
};
double testResult[] = {
1.0,
1.0,
1.0,
0.0,
0.0,
0.0,
0.0,
};
int testCount = sizeof(testList)/sizeof((testList)[0]);
int i;
for(i = 0; i < testCount; i++)
{
ASSERT_TRUE(double_equal(light->intensityAt(w, testList[i]), testResult[i]));
}
}
TEST(LightTest, Creating_an_area_light)
{
Tuple corner = Point(0, 0, 0);
Tuple v1 = Vector(2, 0, 0);
Tuple v2 = Vector(0, 0, 1);
Light light = Light(AREA_LIGHT, corner, v1, 4, v2, 2, Colour(1, 1, 1));
/* Position is used to store the corner in area lights */
ASSERT_EQ(light.corner, corner);
ASSERT_EQ(light.uVec, Vector(0.5, 0, 0));
ASSERT_EQ(light.uSteps, 4);
ASSERT_EQ(light.vVec, Vector(0, 0, 0.5));
ASSERT_EQ(light.vSteps, 2);
ASSERT_EQ(light.samples, 8);
ASSERT_EQ(light.position, Point(1, 0, 0.5));
}
TEST(LightTest, Finding_a_single_point_on_an_area_light)
{
Tuple corner = Point(0, 0, 0);
Tuple v1 = Vector(2, 0, 0);
Tuple v2 = Vector(0, 0, 1);
Light light = Light(AREA_LIGHT, corner, v1, 4, v2, 2, Colour(1, 1, 1));
uint32_t testList[][2] = {
{0, 0},
{1, 0},
{0, 1},
{2, 0},
{3, 1},
};
Point testResults[] {
Point(0.25, 0, 0.25),
Point(0.75, 0, 0.25),
Point(0.25, 0, 0.75),
Point(1.25, 0, 0.25),
Point(1.75, 0, 0.75),
};
int testCount = sizeof(testList)/sizeof((testList)[0]);
int i;
for(i = 0; i < testCount; i++)
{
Tuple tp = light.pointOnLight(testList[i][0], testList[i][1]);
ASSERT_EQ(tp, testResults[i]);
}
}
TEST(LightTest, The_area_light_intensity_function)
{
World w = DefaultWorld();
Tuple corner = Point(-0.5, -0.5, -5);
Tuple v1 = Vector(1, 0, 0);
Tuple v2 = Vector(0, 1, 0);
Light light = Light(AREA_LIGHT, corner, v1, 2, v2, 2, Colour(1, 1, 1));
Point testList[] = {
Point(0, 0, 2),
Point(1, -1, 2),
Point(1.5, 0, 2),
Point(1.25, 1.25, 3),
Point(0, 0, -2),
};
double testResults[] {
0.0,
0.25,
0.5,
0.75,
1,
};
int testCount = sizeof(testList)/sizeof((testList)[0]);
int i;
for(i = 0; i < testCount; i++)
{
double intensity = light.intensityAt(w, testList[i]);
ASSERT_TRUE(double_equal(intensity, testResults[i]));
}
}

View File

@@ -11,6 +11,8 @@
#include <colour.h>
#include <testshape.h>
#include <gtest/gtest.h>
#include <world.h>
#include <worldbuilder.h>
TEST(MaterialTest, The_default_material)
{
@@ -101,9 +103,9 @@ TEST(MaterialTest, Lighting_with_the_surface_in_shadow)
Vector eyev = Vector(0, 0, -1);
Vector normalv = Vector(0, 0, -1);
Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
bool inShadow = true;
double lightLevel = 0.0;
Colour result = m.lighting(light, position, eyev, normalv, &t, inShadow);
Colour result = m.lighting(light, position, eyev, normalv, &t, lightLevel);
ASSERT_EQ(result, Colour(0.1, 0.1, 0.1));
}
@@ -164,4 +166,43 @@ TEST(MaterialTest, Equality_tests)
m.colour = Colour(32, 32, 32);
ASSERT_NE(m, m2);
}
TEST(MaterialTest, lighting_uses_light_intensity_to_attenuate_colour)
{
World w = DefaultWorld();
Light *light = w.getLight(0);
Shape *shape = w.getObject(0);
light->position = Point(0, 0, -10);
light->intensity = Colour(1, 1, 1);
shape->material.ambient = 0.1;
shape->material.diffuse = 0.9;
shape->material.specular = 0;
shape->material.colour = Colour(1, 1, 1);
Tuple pt = Point(0, 0, -1);
Tuple eyev = Vector(0, 0, -1);
Tuple normalv = Vector(0, 0, -1);
double testList[] = {
1.0,
0.5,
0.0,
};
Colour testResult[] = {
Colour(1, 1, 1),
Colour(0.55, 0.55, 0.55),
Colour(0.1, 0.1, 0.1),
};
int testCount = sizeof(testList)/sizeof((testList)[0]);
int i;
for(i = 0; i < testCount; i++)
{
ASSERT_EQ(shape->material.lighting(*light, pt, eyev, normalv, shape, testList[i]), testResult[i]);
}
}

View File

@@ -0,0 +1,116 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Render test for reflection in chapter 13.
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <world.h>
#include <light.h>
#include <plane.h>
#include <colour.h>
#include <canvas.h>
#include <camera.h>
#include <triangle.h>
#include <group.h>
#include <time.h>
#include <transformation.h>
double frand(double max)
{
return max * (rand() / ((double) RAND_MAX));
}
int main()
{
World w = World();
/* Add lights */
Light light1 = Light(POINT_LIGHT, Point(10000, 10000, -10000), Colour(0.25, 0.25, 0.25));
w.addLight(&light1);
Light light2 = Light(POINT_LIGHT, Point(-10000, 10000, -10000), Colour(0.25, 0.25, 0.25));
w.addLight(&light2);
Light light3 = Light(POINT_LIGHT, Point(10000, -10000, -10000), Colour(0.25, 0.25, 0.25));
w.addLight(&light3);
Light light4 = Light(POINT_LIGHT, Point(-10000, -10000, -10000), Colour(0.25, 0.25, 0.25));
w.addLight(&light4);
/* ----------------------------- */
/* White background */
Plane p = Plane();
p.setTransform(translation(0, 0, 100) * rotationX(1.5708));
p.material.colour = Colour(1, 1, 1);
p.material.ambient = 1;
p.material.diffuse = 0;
p.material.specular = 0;
w.addObject(&p);
/* ----------------------------- */
Group g = Group();
srand(10); /* Force the seed to a known value */
int i;
Point p1, p2, p3;
/* Let's add a bunche of "random" triangles */
for (i = 0; i < 10; i++)
{
p1.x = 7-frand(14);
p1.y = 7-frand(14);
p1.z = 7-frand(14);
p2.x = 7-frand(14);
p2.y = 7-frand(14);
p2.z = 7-frand(14);
p3.x = 7-frand(14);
p3.y = 7-frand(14);
p3.z = 7-frand(14);
Triangle *tri = new Triangle(p1, p2, p3);
tri->material.colour.x = frand(1); // red
tri->material.colour.y = frand(1); // green
tri->material.colour.z = frand(1); // blue
//tri->material.refractiveIndex = frand(2);
//tri->material.transparency = frand(1);
//tri->material.shininess = frand(300);
//tri->material.specular = frand(1);
//tri->material.ambient = frand(1);
tri->material.reflective = frand(1);
//tri->material.diffuse = frand(1);
g.addObject(tri);
}
g.setTransform(rotationX(3.14));
w.addObject(&g);
/* ----------------------------- */
FILE *fpOut = fopen("triangles_world.json", "wt");
if (fpOut)
{
w.dumpMe(fpOut);
fclose(fpOut);
}
/* ----------------------------- */
/* Set the camera */
Camera camera = Camera(500, 500, M_PI/1.5);
camera.setTransform(viewTransform(Point(0, 0, -9),
Point(0, 0, 0),
Vector(0, 1, 0)));
/* Now render it */
Canvas image = camera.render(w, 5);
image.SaveAsPNG("triangle_rendertest.png");
return 0;
}

View File

@@ -120,7 +120,7 @@ TEST(WorldTest, There_is_no_shadow_when_nothing_is_collinear_with_point_and_ligh
World w = DefaultWorld();
Tuple p = Point(0, 10, 0);
ASSERT_FALSE(w.isShadowed(p));
ASSERT_FALSE(w.isShadowed(p, w.getLight(0)->position));
}
TEST(WorldTest, The_shadow_when_an_object_is_between_the_point_and_the_light)
@@ -128,7 +128,7 @@ TEST(WorldTest, The_shadow_when_an_object_is_between_the_point_and_the_light)
World w = DefaultWorld();
Tuple p = Point(10, -10, 10);
ASSERT_TRUE(w.isShadowed(p));
ASSERT_TRUE(w.isShadowed(p, w.getLight(0)->position));
}
TEST(WorldTest, There_is_no_shadow_whne_an_object_is_behing_the_light)
@@ -136,7 +136,7 @@ TEST(WorldTest, There_is_no_shadow_whne_an_object_is_behing_the_light)
World w = DefaultWorld();
Tuple p = Point(-20, 20, -20);
ASSERT_FALSE(w.isShadowed(p));
ASSERT_FALSE(w.isShadowed(p, w.getLight(0)->position));
}
TEST(WorldTest, There_is_no_shadow_when_an_object_is_behing_the_point)
@@ -144,7 +144,7 @@ TEST(WorldTest, There_is_no_shadow_when_an_object_is_behing_the_point)
World w = DefaultWorld();
Tuple p = Point(-2, 2, -2);
ASSERT_FALSE(w.isShadowed(p));
ASSERT_FALSE(w.isShadowed(p, w.getLight(0)->position));
}
TEST(WorldTest, Shade_hit_is_given_an_intersection_in_shadow)
@@ -426,4 +426,30 @@ TEST(WorldTest, Shade_hit_with_a_reflective_transparent_material)
ASSERT_EQ(c, Colour(0.93391, 0.69643, 0.69243));
set_equal_precision(FLT_EPSILON);
}
TEST(WorldTest, Is_shadow_test_for_occlusion_between_two_points)
{
World w = DefaultWorld();
Tuple lightPosition = Point(-10, -10, -10);
Point testList[] = {
Point(-10, -10, 10),
Point(10, 10, 10),
Point(-20, -20, 20),
Point(-5, -5, 5),
};
bool testResult[] = {
false,
true,
false,
false,
};
int testCount = sizeof(testList)/sizeof((testList)[0]);
int i;
for(i = 0; i < testCount; i++)
{
ASSERT_EQ(w.isShadowed(lightPosition, testList[i]), testResult[i]);
}
}