Starting working on area lights.
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user