Skip to content

Commit

Permalink
Starting working on area lights.
Browse files Browse the repository at this point in the history
  • Loading branch information
Godzil committed Feb 28, 2020
1 parent 53f66b5 commit c4b6807
Show file tree
Hide file tree
Showing 12 changed files with 508 additions and 31 deletions.
23 changes: 23 additions & 0 deletions source/include/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
#include <renderstat.h>
#include <stdio.h>

class World;

enum LightType
{
POINT_LIGHT = 0,
AREA_LIGHT,

};

class Light
Expand All @@ -26,15 +30,34 @@ class Light
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);
};

Expand Down
3 changes: 2 additions & 1 deletion source/include/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Material
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) &&
Expand Down
2 changes: 1 addition & 1 deletion source/include/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class World

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);
Expand Down
32 changes: 32 additions & 0 deletions source/shapes/light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
#include <stdio.h>
#include <light.h>
#include <world.h>

void Light::dumpMe(FILE *fp)
{
Expand All @@ -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);
}
40 changes: 21 additions & 19 deletions source/shapes/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#include <colour.h>
#include <shape.h>

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;

Expand All @@ -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);

if (lightDotNormal < 0)
{
diffuseColour = Colour(0, 0, 0);
specularColour = Colour(0, 0, 0);
}
else
{
lightDotNormal = lightVector.dot(normalVector);
diffuseColour = effectiveColour * this->diffuse * lightDotNormal;
reflectVector = -lightVector.reflect(normalVector);

reflectDotEye = reflectVector.dot(eyeVector);

if (lightDotNormal < 0)
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);
Expand Down
8 changes: 4 additions & 4 deletions source/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down
12 changes: 12 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 changes: 99 additions & 0 deletions tests/arealight_test.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit c4b6807

Please sign in to comment.