diff --git a/source/include/light.h b/source/include/light.h index a555fa2..d1c64d2 100644 --- a/source/include/light.h +++ b/source/include/light.h @@ -14,9 +14,13 @@ #include #include +class World; + enum LightType { POINT_LIGHT = 0, + AREA_LIGHT, + }; class Light @@ -26,15 +30,34 @@ public: Tuple position; LightType type; + /* For area light */ + Tuple corner; + Tuple uVec; + Tuple vVec; + uint32_t samples; + uint32_t uSteps; + uint32_t vSteps; + public: Light(LightType type = POINT_LIGHT, Tuple position=Point(0, 0, 0), Colour intensity=Colour(1, 1, 1)) : type(type), position(position), intensity(intensity) { stats.addLight(); }; + Light(LightType type, Tuple corner, Tuple fullUVec, uint32_t uSteps, Tuple fullVVec, uint32_t vSteps, + Colour intensity, bool jitter = false): type(type), corner(corner), uVec(fullUVec / uSteps), uSteps(uSteps), + vVec(fullVVec / vSteps), vSteps(vSteps), intensity(intensity) + { + this->samples = this->vSteps * this->uSteps; + this->position = this->corner + ((fullUVec + fullVVec) / 2); + stats.addLight(); + }; + double intensityAt(World &w, Tuple point); bool operator==(const Light &b) const { return this->intensity == b.intensity && this->position == b.position && this->type == b.type; }; + Tuple pointOnLight(uint32_t u, uint32_t v); + void dumpMe(FILE *fp); }; diff --git a/source/include/material.h b/source/include/material.h index ba53b98..616b808 100644 --- a/source/include/material.h +++ b/source/include/material.h @@ -36,7 +36,8 @@ public: Material() : colour(Colour(1, 1, 1)), ambient(0.1), diffuse(0.9), specular(0.9), shininess(200), reflective(0.0), transparency(0.0), emissive(0), refractiveIndex(1.0), pattern(nullptr) {}; - Colour lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector, Shape *hitObject, bool inShadow = false); + Colour lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector, Shape *hitObject, + double lightIntensity = 1.0); bool operator==(const Material &b) const { return double_equal(this->ambient, b.ambient) && double_equal(this->diffuse, b.diffuse) && diff --git a/source/include/world.h b/source/include/world.h index b203d35..6562092 100644 --- a/source/include/world.h +++ b/source/include/world.h @@ -45,7 +45,7 @@ public: Tuple shadeHit(Computation comps, uint32_t depthCount = 4); Tuple colourAt(Ray r, uint32_t depthCount = 4); - bool isShadowed(Tuple point, uint32_t light = 0); + bool isShadowed(Tuple point, Tuple lightPosition); Colour reflectColour(Computation comps, uint32_t depthCount = 4); Colour refractedColour(Computation comps, uint32_t depthCount = 4); diff --git a/source/shapes/light.cpp b/source/shapes/light.cpp index a9c079d..8069dc9 100644 --- a/source/shapes/light.cpp +++ b/source/shapes/light.cpp @@ -8,6 +8,7 @@ */ #include #include +#include void Light::dumpMe(FILE *fp) { @@ -16,4 +17,35 @@ void Light::dumpMe(FILE *fp) fprintf(fp, "\"Position\": {\"x\": %f, \"y\": %f, \"z\":%f},\n", this->position.x, this->position.y, this->position.z); fprintf(fp, "\"Type\": \"PointLight\",\n"); +} + +double Light::intensityAt(World &w, Tuple point) +{ + switch(this->type) + { + case POINT_LIGHT: + default: + return (w.isShadowed(point, this->position))?0.0:1.0; + + case AREA_LIGHT: + double total = 0.0; + uint32_t v, u; + for(v = 0; v < this->vSteps; v++) + { + for(u = 0; u < this->uSteps; u++) + { + if (!w.isShadowed(point, this->pointOnLight(u, v))) + { + total = total + 1.0; + } + } + } + return total / this->samples; + break; + } +} + +Tuple Light::pointOnLight(uint32_t u, uint32_t v) +{ + return this->corner + this->uVec * (u+0.5) + this->vVec * (v+0.5); } \ No newline at end of file diff --git a/source/shapes/material.cpp b/source/shapes/material.cpp index db274a8..8cb4df5 100644 --- a/source/shapes/material.cpp +++ b/source/shapes/material.cpp @@ -11,7 +11,8 @@ #include #include -Colour Material::lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector, Shape *hitObject, bool inShadow) +Colour Material::lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector, Shape *hitObject, + double lightIntensity) { Colour pointColor = this->colour; @@ -36,33 +37,34 @@ Colour Material::lighting(Light light, Tuple point, Tuple eyeVector, Tuple norma emissiveColour = pointColor * this->emissive; - if (!inShadow) - { - lightDotNormal = lightVector.dot(normalVector); + lightDotNormal = lightVector.dot(normalVector); - if (lightDotNormal < 0) + if (lightDotNormal < 0) + { + diffuseColour = Colour(0, 0, 0); + specularColour = Colour(0, 0, 0); + } + else + { + diffuseColour = effectiveColour * this->diffuse * lightDotNormal; + reflectVector = -lightVector.reflect(normalVector); + + reflectDotEye = reflectVector.dot(eyeVector); + + if (reflectDotEye < 0) { - diffuseColour = Colour(0, 0, 0); specularColour = Colour(0, 0, 0); } else { - diffuseColour = effectiveColour * this->diffuse * lightDotNormal; - reflectVector = -lightVector.reflect(normalVector); - - reflectDotEye = reflectVector.dot(eyeVector); - - if (reflectDotEye < 0) - { - specularColour = Colour(0, 0, 0); - } - else - { - double factor = pow(reflectDotEye, this->shininess); - specularColour = light.intensity * this->specular * factor; - } + double factor = pow(reflectDotEye, this->shininess); + specularColour = light.intensity * this->specular * factor; } } + + diffuseColour = diffuseColour * lightIntensity; + specularColour = specularColour * lightIntensity; + finalColour = emissiveColour + ambientColour + diffuseColour + specularColour; return Colour(finalColour.x, finalColour.y, finalColour.z); diff --git a/source/world.cpp b/source/world.cpp index ef62108..1b743f9 100644 --- a/source/world.cpp +++ b/source/world.cpp @@ -102,10 +102,10 @@ Tuple World::shadeHit(Computation comps, uint32_t depthCount) for(lightIndex = 0; lightIndex < this->lightCount; lightIndex++) { - bool isThereAnObstacle = this->isShadowed(comps.overHitPoint, lightIndex); + double lightLevel = this->lightList[lightIndex]->intensityAt(*this, comps.overHitPoint); surface = surface + comps.material->lighting(*this->lightList[lightIndex], comps.overHitPoint, comps.eyeVector, - comps.normalVector, comps.object, isThereAnObstacle); + comps.normalVector, comps.object, lightLevel); } Tuple reflected = this->reflectColour(comps, depthCount); Tuple refracted = this->refractedColour(comps, depthCount); @@ -137,9 +137,9 @@ Tuple World::colourAt(Ray r, uint32_t depthCount) } } -bool World::isShadowed(Tuple point, uint32_t light) +bool World::isShadowed(Tuple point, Tuple lightPosition) { - Tuple v = this->lightList[light]->position - point; + Tuple v = lightPosition - point; double distance = v.magnitude(); Tuple direction = v.normalise(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 72afea4..93d327e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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 $) add_test(NAME Chapter13_Test COMMAND $) add_test(NAME Chapter13_ConeBonus COMMAND $) add_test(NAME Chapter14_Test COMMAND $) +add_test(NAME AreaLight_Test COMMAND $) add_test(NAME Test_Rendering COMMAND $) +add_test(NAME Triangle_RenderTest COMMAND $) add_test(NAME ChristmasBall_Rendering COMMAND $) add_test(NAME Hw3Render COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/test.hw3scene) add_test(NAME Hw3RenderAllCmds COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/test_keys.hw3scene) \ No newline at end of file diff --git a/tests/arealight_test.cpp b/tests/arealight_test.cpp new file mode 100644 index 0000000..a0807fc --- /dev/null +++ b/tests/arealight_test.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +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; +} \ No newline at end of file diff --git a/tests/light_test.cpp b/tests/light_test.cpp index c374bbd..121ccb8 100644 --- a/tests/light_test.cpp +++ b/tests/light_test.cpp @@ -11,6 +11,8 @@ #include #include #include +#include +#include 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])); + } + } \ No newline at end of file diff --git a/tests/material_test.cpp b/tests/material_test.cpp index 23c3832..6cf7c24 100644 --- a/tests/material_test.cpp +++ b/tests/material_test.cpp @@ -11,6 +11,8 @@ #include #include #include +#include +#include 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]); + } + } \ No newline at end of file diff --git a/tests/triangle_rendertest.cpp b/tests/triangle_rendertest.cpp new file mode 100644 index 0000000..08f40df --- /dev/null +++ b/tests/triangle_rendertest.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +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; +} \ No newline at end of file diff --git a/tests/world_test.cpp b/tests/world_test.cpp index b1b635b..36ee043 100644 --- a/tests/world_test.cpp +++ b/tests/world_test.cpp @@ -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]); + } } \ No newline at end of file