Starting working on area lights.
This commit is contained in:
@@ -14,9 +14,13 @@
|
||||
#include <renderstat.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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) &&
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <light.h>
|
||||
#include <world.h>
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user