49 lines
1.3 KiB
C++
49 lines
1.3 KiB
C++
#include "camera.h"
|
|
#include "color.h"
|
|
#include "light.h"
|
|
|
|
|
|
// PUBLIC FUNCTIONS
|
|
ColorF32 ComputeLight(
|
|
Point &position, Vector &normal, Material &material, Light &light, Camera &camera)
|
|
{
|
|
// Point light intensity is a function of the falloff factors and the distance
|
|
Vector direction = light.position - position;
|
|
VectorNormalize(direction);
|
|
float distance = VectorLength(direction);
|
|
|
|
ColorF32 intensity =
|
|
(light.intensity * light.color)
|
|
/ (light.falloffConstant + (light.falloffLinear * distance));
|
|
|
|
|
|
// Diffuse Light = Kr * I * (alpha + (1 - alpha) * n.d)
|
|
float dotNormalDirection = MAX(VectorDot(normal, direction), 0.0f);
|
|
float alpha = 0.2f;
|
|
|
|
ColorF32 diffuseLight =
|
|
material.diffuse
|
|
* intensity
|
|
* (alpha + (1.0f - alpha) * dotNormalDirection);
|
|
|
|
|
|
// Specular Light = Ks * I * (r.v)^sp
|
|
Vector view = camera.position - position;
|
|
VectorNormalize(view);
|
|
Vector reflection = (2.0f * dotNormalDirection * normal) - direction;
|
|
float dotReflectionView = MAX(VectorDot(reflection, view), 0.0f);
|
|
|
|
ColorF32 specularLight =
|
|
material.specular
|
|
* intensity
|
|
* powf(dotReflectionView, material.glossiness);
|
|
|
|
|
|
// Total light is sum of all lights
|
|
ColorF32 result = diffuseLight + specularLight;
|
|
|
|
|
|
return result;
|
|
}
|
|
|